Skip to content

Commit

Permalink
Minor fixes to voted pools
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Herrera <[email protected]>
  • Loading branch information
Pablete1234 committed Jan 4, 2025
1 parent a6ecd99 commit 860f86a
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 11 deletions.
3 changes: 1 addition & 2 deletions core/src/main/java/tc/oc/pgm/api/map/MapData.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.time.Duration;
import java.time.Instant;
import tc.oc.pgm.api.match.Match;

public interface MapData {

Expand All @@ -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);
}
6 changes: 2 additions & 4 deletions core/src/main/java/tc/oc/pgm/db/SQLDatastore.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/tc/oc/pgm/rotation/pools/VotingPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ private void updateScores(Map<MapInfo, Set<UUID>> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ protected List<MapInfo> getMaps(@Nullable List<MapInfo> selected, Map<MapInfo, V
return selected;
}

protected MapInfo getMap(List<MapInfo> selected, Map<MapInfo, VoteData> mapScores) {
public MapInfo getMap(List<MapInfo> selected, Map<MapInfo, VoteData> mapScores) {
NavigableMap<Double, MapInfo> cumulativeScores = new TreeMap<>();
double maxWeight = 0;
for (Map.Entry<MapInfo, VoteData> map : mapScores.entrySet()) {
Expand Down
12 changes: 10 additions & 2 deletions core/src/main/java/tc/oc/pgm/rotation/vote/VoteData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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() {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}
}
}

0 comments on commit 860f86a

Please sign in to comment.