Skip to content

Commit

Permalink
Rework map pools & map vote picker (#1010)
Browse files Browse the repository at this point in the history
Signed-off-by: Pablete1234 <[email protected]>
  • Loading branch information
Pablete1234 authored Jul 15, 2022
1 parent f90dd7a commit a5b7178
Show file tree
Hide file tree
Showing 20 changed files with 400 additions and 220 deletions.
5 changes: 1 addition & 4 deletions core/src/main/java/tc/oc/pgm/api/map/MapOrder.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,10 @@ public interface MapOrder {
* Forces a specific map to be played next. The underlying {@link MapOrder} may ignore this, but
* it is recommended not to.
*
* @param map The map to set next
* @param map The map to set next, null to reset
*/
void setNextMap(MapInfo map);

/** Removes any map that was set manually, returning the server to what was previously chosen. */
void resetNextMap();

/**
* Returns the duration used for cycles in {@link CycleMatchModule}.
*
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/tc/oc/pgm/command/MapCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
import tc.oc.pgm.api.map.MapLibrary;
import tc.oc.pgm.api.map.MapTag;
import tc.oc.pgm.api.map.Phase;
import tc.oc.pgm.rotation.MapPool;
import tc.oc.pgm.rotation.MapPoolManager;
import tc.oc.pgm.rotation.pools.MapPool;
import tc.oc.pgm.util.Audience;
import tc.oc.pgm.util.PrettyPaginatedComponentResults;
import tc.oc.pgm.util.named.MapNameStyle;
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/tc/oc/pgm/command/MapOrderCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void setNext(
if (reset) {
if (mapOrder.getNextMap() != null) {
Component mapName = mapOrder.getNextMap().getStyledName(MapNameStyle.COLOR);
mapOrder.resetNextMap();
mapOrder.setNextMap(null);
ChatDispatcher.broadcastAdminChatMessage(
translatable(
"map.setNext.revert",
Expand Down
16 changes: 8 additions & 8 deletions core/src/main/java/tc/oc/pgm/command/MapPoolCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
import tc.oc.pgm.api.match.Match;
import tc.oc.pgm.api.player.MatchPlayer;
import tc.oc.pgm.cycle.CycleCountdown;
import tc.oc.pgm.rotation.MapPoll;
import tc.oc.pgm.rotation.MapPool;
import tc.oc.pgm.rotation.MapPoolManager;
import tc.oc.pgm.rotation.Rotation;
import tc.oc.pgm.rotation.VotingPool;
import tc.oc.pgm.rotation.pools.MapPool;
import tc.oc.pgm.rotation.pools.Rotation;
import tc.oc.pgm.rotation.pools.VotingPool;
import tc.oc.pgm.rotation.vote.MapPoll;
import tc.oc.pgm.util.Audience;
import tc.oc.pgm.util.PrettyPaginatedComponentResults;
import tc.oc.pgm.util.named.MapNameStyle;
Expand All @@ -43,7 +43,7 @@ public final class MapPoolCommand {
private static final DecimalFormat SCORE_FORMAT = new DecimalFormat("00.00%");

@Command(
aliases = {"pool", "rotation", "rot"},
aliases = {"pool"},
desc = "List the maps in the map pool",
usage = "[page] [-p pool] [-s scores] [-c chance of vote]")
public static void pool(
Expand Down Expand Up @@ -96,7 +96,7 @@ public static void pool(
if (chance && votes != null) {
double maxWeight = 0, currWeight;
for (MapInfo map : votes.getMaps()) {
chances.put(map, currWeight = MapPoll.getWeight(votes.getMapScore(map)));
chances.put(map, currWeight = votes.mapPicker.getWeight(null, map, votes.getMapScore(map)));
maxWeight += currWeight;
}
double finalMaxWeight = maxWeight;
Expand Down Expand Up @@ -127,7 +127,7 @@ public Component format(MapInfo map, int index) {
}

@Command(
aliases = {"pools", "rotations", "rots"},
aliases = {"pools"},
desc = "List all the map pools",
flags = "d")
public static void pools(
Expand Down Expand Up @@ -204,7 +204,7 @@ public Component format(MapPool mapPool, int index) {
}

@Command(
aliases = {"setpool", "setrot"},
aliases = {"setpool"},
desc = "Change the map pool",
usage = "[pool name] -r (revert to dynamic) -t (time limit for map pool) -m (match # limit)",
flags = "rtm",
Expand Down
42 changes: 22 additions & 20 deletions core/src/main/java/tc/oc/pgm/command/VotingCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import tc.oc.pgm.api.match.Match;
import tc.oc.pgm.listeners.ChatDispatcher;
import tc.oc.pgm.rotation.MapPoolManager;
import tc.oc.pgm.rotation.VotingPool;
import tc.oc.pgm.rotation.pools.VotingPool;
import tc.oc.pgm.rotation.vote.MapVotePicker;
import tc.oc.pgm.rotation.vote.VotePoolOptions;
import tc.oc.pgm.util.Audience;
import tc.oc.pgm.util.UsernameFormatUtils;
import tc.oc.pgm.util.named.MapNameStyle;
Expand All @@ -44,7 +46,7 @@ public void addMap(
MapOrder mapOrder,
Match match)
throws CommandException {
VotingPool vote = getVotingPool(sender, mapOrder);
VotePoolOptions vote = getVoteOptions(sender, mapOrder);

Component addMessage =
translatable(
Expand All @@ -53,12 +55,12 @@ public void addMap(
UsernameFormatUtils.formatStaffName(sender, match),
map.getStyledName(MapNameStyle.COLOR));

if (vote.getOptions().isAdded(map)) {
if (vote.isAdded(map)) {
viewer.sendWarning(addMessage);
return;
}

if (vote.getOptions().addVote(map)) {
if (vote.addVote(map)) {
ChatDispatcher.broadcastAdminChatMessage(addMessage, match);
} else {
viewer.sendWarning(translatable("vote.limit", NamedTextColor.RED));
Expand All @@ -77,8 +79,8 @@ public void removeMap(
MapOrder mapOrder,
Match match)
throws CommandException {
VotingPool vote = getVotingPool(sender, mapOrder);
if (vote.getOptions().removeMap(map)) {
VotePoolOptions vote = getVoteOptions(sender, mapOrder);
if (vote.removeMap(map)) {
ChatDispatcher.broadcastAdminChatMessage(
translatable(
"vote.remove",
Expand All @@ -97,10 +99,10 @@ public void removeMap(
perms = Permissions.SETNEXT)
public void mode(Audience viewer, CommandSender sender, MapOrder mapOrder, Match match)
throws CommandException {
VotingPool vote = getVotingPool(sender, mapOrder);
VotePoolOptions vote = getVoteOptions(sender, mapOrder);
Component voteModeName =
translatable(
vote.getOptions().toggleMode() ? "vote.mode.replace" : "vote.mode.create",
vote.toggleMode() ? "vote.mode.replace" : "vote.mode.create",
NamedTextColor.LIGHT_PURPLE);
ChatDispatcher.broadcastAdminChatMessage(
translatable(
Expand All @@ -117,10 +119,10 @@ public void mode(Audience viewer, CommandSender sender, MapOrder mapOrder, Match
perms = Permissions.SETNEXT)
public void clearMaps(Audience viewer, CommandSender sender, Match match, MapOrder mapOrder)
throws CommandException {
VotingPool vote = getVotingPool(sender, mapOrder);
VotePoolOptions vote = getVoteOptions(sender, mapOrder);

List<Component> maps =
vote.getOptions().getCustomVoteMaps().stream()
vote.getCustomVoteMaps().stream()
.map(mi -> mi.getStyledName(MapNameStyle.COLOR))
.collect(Collectors.toList());
Component clearedMsg =
Expand All @@ -130,7 +132,7 @@ public void clearMaps(Audience viewer, CommandSender sender, Match match, MapOrd
UsernameFormatUtils.formatStaffName(sender, match),
TextFormatter.list(maps, NamedTextColor.GRAY));

vote.getOptions().clear();
vote.clear();

if (maps.isEmpty()) {
viewer.sendWarning(translatable("vote.noMapsFound"));
Expand All @@ -144,17 +146,17 @@ public void clearMaps(Audience viewer, CommandSender sender, Match match, MapOrd
desc = "View a list of maps that have been selected for the next vote")
public void listMaps(CommandSender sender, Audience viewer, MapOrder mapOrder)
throws CommandException {
VotingPool vote = getVotingPool(sender, mapOrder);
VotePoolOptions vote = getVoteOptions(sender, mapOrder);

int currentMaps = vote.getOptions().getCustomVoteMaps().size();
int currentMaps = vote.getCustomVoteMaps().size();
TextColor listNumColor =
currentMaps >= VotingPool.MIN_CUSTOM_VOTE_OPTIONS
? currentMaps < VotingPool.MAX_VOTE_OPTIONS
currentMaps >= MapVotePicker.MIN_CUSTOM_VOTE_OPTIONS
? currentMaps < MapVotePicker.MAX_VOTE_OPTIONS
? NamedTextColor.GREEN
: NamedTextColor.YELLOW
: NamedTextColor.RED;

String modeKey = vote.getOptions().isReplace() ? "replace" : "create";
String modeKey = vote.isReplace() ? "replace" : "create";
Component mode =
translatable(String.format("vote.mode.%s", modeKey), NamedTextColor.LIGHT_PURPLE)
.hoverEvent(showText(translatable("vote.mode.hover", NamedTextColor.AQUA)))
Expand All @@ -166,7 +168,7 @@ public void listMaps(CommandSender sender, Audience viewer, MapOrder mapOrder)
.append(text(": ("))
.append(text(currentMaps, listNumColor))
.append(text("/"))
.append(text(VotingPool.MAX_VOTE_OPTIONS, NamedTextColor.RED))
.append(text(MapVotePicker.MAX_VOTE_OPTIONS, NamedTextColor.RED))
.append(text(") "))
.append(text("\u00BB", NamedTextColor.GOLD))
.append(text(" ["))
Expand All @@ -177,7 +179,7 @@ public void listMaps(CommandSender sender, Audience viewer, MapOrder mapOrder)
viewer.sendMessage(listMsg);

int index = 1;
for (MapInfo mi : vote.getOptions().getCustomVoteMaps()) {
for (MapInfo mi : vote.getCustomVoteMaps()) {
Component indexedName =
text()
.append(text(index, NamedTextColor.YELLOW))
Expand All @@ -189,7 +191,7 @@ public void listMaps(CommandSender sender, Audience viewer, MapOrder mapOrder)
}
}

public static VotingPool getVotingPool(CommandSender sender, MapOrder mapOrder)
public static VotePoolOptions getVoteOptions(CommandSender sender, MapOrder mapOrder)
throws CommandException {
if (mapOrder instanceof MapPoolManager) {
MapPoolManager manager = (MapPoolManager) mapOrder;
Expand All @@ -199,7 +201,7 @@ public static VotingPool getVotingPool(CommandSender sender, MapOrder mapOrder)
throw new CommandException(
ChatColor.RED + TextTranslations.translate("vote.modify.disallow", sender));
}
return votePool;
return manager.getVoteOptions();
}
throw new CommandException(
ChatColor.RED + TextTranslations.translate("vote.disabled", sender));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import tc.oc.pgm.api.match.Match;
import tc.oc.pgm.rotation.MapPool;
import tc.oc.pgm.rotation.pools.MapPool;

/** MapPoolAdjustEvent is called when the active {@link MapPool} is set to another * */
public class MapPoolAdjustEvent extends Event {
Expand Down
46 changes: 26 additions & 20 deletions core/src/main/java/tc/oc/pgm/rotation/MapPoolManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -14,6 +15,7 @@
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.apache.commons.io.FileUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
Expand All @@ -27,6 +29,10 @@
import tc.oc.pgm.api.match.Match;
import tc.oc.pgm.blitz.BlitzMatchModule;
import tc.oc.pgm.events.MapPoolAdjustEvent;
import tc.oc.pgm.rotation.pools.MapPool;
import tc.oc.pgm.rotation.pools.Rotation;
import tc.oc.pgm.rotation.pools.VotingPool;
import tc.oc.pgm.rotation.vote.VotePoolOptions;
import tc.oc.pgm.util.TimeUtils;

/**
Expand Down Expand Up @@ -55,15 +61,15 @@ public class MapPoolManager implements MapOrder {
private MapInfo overriderMap;

/** Options related to voting pools, allows for custom voting @see {@link VotingPool} * */
private CustomVotingPoolOptions options;
private final VotePoolOptions options;

private Datastore database;
private final Datastore database;

public MapPoolManager(Logger logger, File mapPoolsFile, Datastore database) {
this.logger = logger;
this.mapPoolsFile = mapPoolsFile;
this.database = database;
this.options = new CustomVotingPoolOptions();
this.options = new VotePoolOptions();

if (!mapPoolsFile.exists()) {
try {
Expand Down Expand Up @@ -204,11 +210,11 @@ public MapPool getMapPoolByName(String name) {
.orElse(null);
}

protected MapInfo getOverriderMap() {
public MapInfo getOverriderMap() {
return overriderMap;
}

public CustomVotingPoolOptions getCustomVoteOptions() {
public VotePoolOptions getVoteOptions() {
return options;
}

Expand Down Expand Up @@ -236,33 +242,29 @@ public MapInfo popNextMap() {
@Override
public MapInfo getNextMap() {
if (overriderMap != null) return overriderMap;
if (activeMapPool != null) return activeMapPool.getNextMap();
if (activeMapPool == null) return getFallback().getNextMap();
return null;
return getOrder().getNextMap();
}

@Override
public void setNextMap(MapInfo map) {
overriderMap = map;

// Notify pool/fallback a next map has been set
if (activeMapPool != null) {
activeMapPool.setNextMap(map);
} else {
getFallback().setNextMap(map);
}
getOrder().setNextMap(map);
}

@Override
public void resetNextMap() {
if (overriderMap != null) {
overriderMap = null;
public double getActivePlayers(Match match) {
if (match == null) {
Iterator<Match> matches = PGM.get().getMatchManager().getMatches();
// Fallback to just raw online playercount
if (!matches.hasNext()) return Bukkit.getOnlinePlayers().size();
match = matches.next();
}
double obsBias = match.getModule(BlitzMatchModule.class) != null ? 0.85 : 0.5;
return match.getParticipants().size() + match.getObservers().size() * obsBias;
}

public Optional<MapPool> getAppropriateDynamicPool(Match match) {
double obsBias = match.getModule(BlitzMatchModule.class) != null ? 0.85 : 0.5;
double activePlayers = match.getParticipants().size() + match.getObservers().size() * obsBias;
double activePlayers = getActivePlayers(match);
return mapPools.keySet().stream()
.filter(MapPool::isDynamic)
.filter(pool -> activePlayers >= pool.getPlayers())
Expand Down Expand Up @@ -308,4 +310,8 @@ private boolean shouldRevert(Match match) {
private boolean hasMatchCountLimit() {
return !activeMapPool.isDynamic() && (matchCountLimit > 0);
}

private MapOrder getOrder() {
return activeMapPool != null ? activeMapPool : getFallback();
}
}
15 changes: 6 additions & 9 deletions core/src/main/java/tc/oc/pgm/rotation/RandomMapOrder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class RandomMapOrder implements MapOrder {

private final Random random;
private final Deque<WeakReference<MapInfo>> deque;
private List<MapInfo> origMaps;
private final List<MapInfo> origMaps;

public RandomMapOrder(List<MapInfo> maps) {
this.random = new Random();
Expand Down Expand Up @@ -69,14 +69,11 @@ public MapInfo getNextMap() {

@Override
public void setNextMap(MapInfo map) {
// Set next maps are sent to the front of the deque
deque.addFirst(new WeakReference<>(map));
}

@Override
public void resetNextMap() {
if (deque.pollFirst() != null) {
deque.removeFirst();
if (map == null) {
if (deque.pollFirst() != null) deque.removeFirst();
} else {
// Set next maps are sent to the front of the deque
deque.addFirst(new WeakReference<>(map));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package tc.oc.pgm.rotation;
package tc.oc.pgm.rotation.pools;

import org.bukkit.configuration.ConfigurationSection;
import tc.oc.pgm.api.map.MapInfo;
import tc.oc.pgm.rotation.MapPoolManager;

public class DisabledMapPool extends MapPool {
DisabledMapPool(MapPoolManager manager, ConfigurationSection section, String name) {
Expand All @@ -22,7 +23,4 @@ public MapInfo popNextMap() {
public MapInfo getNextMap() {
return null;
}

@Override
public void resetNextMap() {}
}
Loading

0 comments on commit a5b7178

Please sign in to comment.