Skip to content

Commit

Permalink
Fix up small issues with vote picking
Browse files Browse the repository at this point in the history
Signed-off-by: Pablete1234 <[email protected]>
  • Loading branch information
Pablete1234 committed Jun 15, 2022
1 parent 17ef3f1 commit 689f637
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions core/src/main/java/tc/oc/pgm/rotation/vote/MapVotePicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public class MapVotePicker {
public static final int MAX_VOTE_OPTIONS = 5;
public static final int MIN_CUSTOM_VOTE_OPTIONS = 2;

// A 0 that prevents arbitrarily low values with tons of precision, which cause issues when mixed
// with larger numbers.
private static final double MINIMUM_WEIGHT = 0.00000001;

private static final Formula DEFAULT_MODIFIER = Formula.pow(c -> c.score, Formula.constant(2));

private final MapPoolManager manager;
Expand Down Expand Up @@ -86,7 +90,7 @@ protected MapInfo getMap(List<MapInfo> selected, Map<MapInfo, Double> mapScores)
double maxWeight = 0;
for (Map.Entry<MapInfo, Double> map : mapScores.entrySet()) {
double weight = getWeight(selected, map.getKey(), map.getValue());
if (weight > 0) cumulativeScores.put(maxWeight += weight, map.getKey());
if (weight > MINIMUM_WEIGHT) cumulativeScores.put(maxWeight += weight, map.getKey());
}
Map.Entry<Double, MapInfo> selectedMap =
cumulativeScores.higherEntry(Math.random() * maxWeight);
Expand All @@ -111,9 +115,7 @@ public double getWeight(@Nullable List<MapInfo> selected, @NotNull MapInfo map,
map.getMaxPlayers().stream().mapToInt(i -> i).sum(),
manager.getActivePlayers(null));

// Use MIN_VALUE so that weight isn't exactly 0.
// That allows for the map to be used if nothing else exists.
return Math.max(modifier.applyAsDouble(context), Double.MIN_VALUE);
return Math.max(modifier.applyAsDouble(context), 0);
}

private double getRepeatedGamemodes(List<MapInfo> selected, MapInfo map) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public VotingBookListener(MapPoll poll, Match match) {
public void openVote(PlayerInteractEvent event) {
MatchPlayer player = match.getPlayer(event.getPlayer());
if (player != null
&& !poll.isRunning()
&& poll.isRunning()
&& isRightClick(event.getAction())
&& event.getMaterial() == Material.ENCHANTED_BOOK) {
String validator = VOTE_BOOK_TAG.get(event.getItem());
Expand Down

0 comments on commit 689f637

Please sign in to comment.