From a7fc20719258be36b99fc0f3e41c048e1aeec844 Mon Sep 17 00:00:00 2001 From: Pablo Herrera Date: Sun, 5 Jan 2025 05:19:49 +0100 Subject: [PATCH] Minor fixes to voted pools (#1474) Signed-off-by: Pablo Herrera --- core/src/main/java/tc/oc/pgm/api/map/MapData.java | 3 +-- core/src/main/java/tc/oc/pgm/db/SQLDatastore.java | 6 ++---- .../java/tc/oc/pgm/rotation/pools/VotingPool.java | 3 +-- .../java/tc/oc/pgm/rotation/vote/MapVotePicker.java | 2 +- .../main/java/tc/oc/pgm/rotation/vote/VoteData.java | 12 ++++++++++-- 5 files changed, 15 insertions(+), 11 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/api/map/MapData.java b/core/src/main/java/tc/oc/pgm/api/map/MapData.java index 5801d0387a..5afb55b382 100644 --- a/core/src/main/java/tc/oc/pgm/api/map/MapData.java +++ b/core/src/main/java/tc/oc/pgm/api/map/MapData.java @@ -2,7 +2,6 @@ import java.time.Duration; import java.time.Instant; -import tc.oc.pgm.api.match.Match; public interface MapData { @@ -16,5 +15,5 @@ public interface MapData { void setScore(double score, boolean update); - void saveMatch(Match match, double score); + void saveMatch(Duration duration, double score); } diff --git a/core/src/main/java/tc/oc/pgm/db/SQLDatastore.java b/core/src/main/java/tc/oc/pgm/db/SQLDatastore.java index 59596ec994..a84a3fe1d1 100644 --- a/core/src/main/java/tc/oc/pgm/db/SQLDatastore.java +++ b/core/src/main/java/tc/oc/pgm/db/SQLDatastore.java @@ -22,7 +22,6 @@ import tc.oc.pgm.api.PGM; import tc.oc.pgm.api.map.MapActivity; import tc.oc.pgm.api.map.MapData; -import tc.oc.pgm.api.match.Match; import tc.oc.pgm.api.player.Username; import tc.oc.pgm.api.setting.SettingKey; import tc.oc.pgm.api.setting.SettingValue; @@ -332,10 +331,9 @@ public void setScore(double score, boolean update) { } @Override - public void saveMatch(Match match, double score) { - this.score = score; + public void saveMatch(Duration duration, double score) { this.lastPlayed = Instant.now(); - this.lastDuration = match.getDuration(); + this.lastDuration = duration; this.score = score; submitQuery(new UpdateQuery()); } diff --git a/core/src/main/java/tc/oc/pgm/rotation/pools/VotingPool.java b/core/src/main/java/tc/oc/pgm/rotation/pools/VotingPool.java index dd409c1f3c..915745dab0 100644 --- a/core/src/main/java/tc/oc/pgm/rotation/pools/VotingPool.java +++ b/core/src/main/java/tc/oc/pgm/rotation/pools/VotingPool.java @@ -114,8 +114,7 @@ private void updateScores(Map> votes) { @Override public MapInfo popNextMap() { - if (currentPoll == null) return getRandom(); - + if (currentPoll == null) return mapPicker.getMap(List.of(), mapScores); MapInfo map = currentPoll.finishVote(); updateScores(currentPoll.getVotes()); manager.getVoteOptions().clearMaps(); diff --git a/core/src/main/java/tc/oc/pgm/rotation/vote/MapVotePicker.java b/core/src/main/java/tc/oc/pgm/rotation/vote/MapVotePicker.java index 1cf050e2b1..a5099008ca 100644 --- a/core/src/main/java/tc/oc/pgm/rotation/vote/MapVotePicker.java +++ b/core/src/main/java/tc/oc/pgm/rotation/vote/MapVotePicker.java @@ -98,7 +98,7 @@ protected List getMaps(@Nullable List selected, Map selected, Map mapScores) { + public MapInfo getMap(List selected, Map mapScores) { NavigableMap cumulativeScores = new TreeMap<>(); double maxWeight = 0; for (Map.Entry map : mapScores.entrySet()) { diff --git a/core/src/main/java/tc/oc/pgm/rotation/vote/VoteData.java b/core/src/main/java/tc/oc/pgm/rotation/vote/VoteData.java index d34ea49991..87b85faa8a 100644 --- a/core/src/main/java/tc/oc/pgm/rotation/vote/VoteData.java +++ b/core/src/main/java/tc/oc/pgm/rotation/vote/VoteData.java @@ -8,6 +8,7 @@ import tc.oc.pgm.api.map.MapInfo; import tc.oc.pgm.api.match.Match; import tc.oc.pgm.rotation.pools.VotingPool; +import tc.oc.pgm.util.TimeUtils; public class VoteData { private static final long SECONDS_PER_DAY = Duration.of(1, ChronoUnit.DAYS).toSeconds(); @@ -33,7 +34,7 @@ public void setScore(double score) { } public void onMatchEnd(Match match, VotingPool.VoteConstants constants) { - mapData.saveMatch(match, constants.scoreAfterPlay().apply(match)); + mapData.saveMatch(getDuration(match, constants), constants.scoreAfterPlay().apply(match)); } public double getWeight() { @@ -61,6 +62,12 @@ public void tickScore(VotingPool.VoteConstants constants) { mapData.setScore(constants.tickScore(getScore()), false); } + protected Duration getDuration(Match match, VotingPool.VoteConstants c) { + var duration = match.getDuration(); + // Prevent saving with lower duration if it somehow plays during cooldown + return isOnCooldown(c) ? TimeUtils.max(mapData.lastDuration(), duration) : duration; + } + static class Local extends VoteData { private double score; @@ -92,7 +99,8 @@ public void onMatchEnd(Match match, VotingPool.VoteConstants c) { // If this data starts being used later, we'll run into no maps available! // For this reason, store (non-negative) scores as a low value instead mapData.saveMatch( - match, score < 0 ? score : Math.max(score, c.scoreMinToVote()) + c.scoreRise()); + getDuration(match, c), + score < 0 ? score : Math.max(score, c.scoreMinToVote()) + c.scoreRise()); } } }