Skip to content

Commit

Permalink
Assign default for player-based leaderboards too
Browse files Browse the repository at this point in the history
  • Loading branch information
Gegy committed Nov 15, 2024
1 parent 8bb60b7 commit ada9cbe
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private Component[] renderSidebar(final IGamePhase game) {
if (teams == null) {
placement = Placement.fromPlayerScore(order, game, statistic, false);
} else {
placement = Placement.fromTeamScore(order, game, statistic, 0);
placement = Placement.fromTeamScore(order, game, statistic);
}
placement.addToSidebar(sidebar, count);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void register(IGamePhase game, EventRegistrar events) {
if (teams == null) {
Placement.fromPlayerScore(order, game, statistic).sendTo(players, length);
} else {
Placement.fromTeamScore(order, game, statistic, null).sendTo(players, length);
Placement.fromTeamScore(order, game, statistic).sendTo(players, length);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private GameWinner runPlacement(IGamePhase game) {
Placement.Score<PlayerKey, Integer> playerPlacement = Placement.fromPlayerScore(order, game, statistic);
playerPlacement.placeInto(StatisticKey.PLACEMENT);

Placement.Score<GameTeamKey, Integer> teamPlacement = Placement.fromTeamScore(order, game, statistic, 0);
Placement.Score<GameTeamKey, Integer> teamPlacement = Placement.fromTeamScore(order, game, statistic);
teamPlacement.placeInto(StatisticKey.PLACEMENT);

PlayerKey winningPlayerKey = playerPlacement.getWinner();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,25 @@ static <T extends Comparable<T>> Score<PlayerKey, T> fromPlayerScore(PlacementOr
return fromPlayerScore(order, game, statistic, false);
}

static <T extends Comparable<T>> Score<GameTeamKey, T> fromTeamScore(PlacementOrder order, IGamePhase game, StatisticKey<T> statistic, @Nullable T defaultValue) {
static <T extends Comparable<T>> Score<GameTeamKey, T> fromTeamScore(PlacementOrder order, IGamePhase game, StatisticKey<T> statistic) {
TeamState teams = game.instanceState().getOrNull(TeamState.KEY);
return fromScore(game, teams != null ? teams.getTeamKeys() : List.of(), statistic, order.asComparator(), defaultValue);
return fromScore(game, teams != null ? teams.getTeamKeys() : List.of(), statistic, order.asComparator());
}

static <T extends Comparable<T>> Score<PlayerKey, T> fromPlayerScore(PlacementOrder order, IGamePhase game, StatisticKey<T> statistic, boolean onlyOnline) {
List<PlayerKey> players = new ArrayList<>(game.statistics().getPlayers());
if (onlyOnline) {
players.removeIf(key -> !game.allPlayers().contains(key.id()));
}
return fromScore(game, players, statistic, order.asComparator(), null);
return fromScore(game, players, statistic, order.asComparator());
}

static <H extends StatisticHolder, T> Score<H, T> fromScore(IGamePhase game, Collection<H> scoreHolders, StatisticKey<T> scoreKey, Comparator<T> comparator, @Nullable T defaultValue) {
static <H extends StatisticHolder, T> Score<H, T> fromScore(IGamePhase game, Collection<H> scoreHolders, StatisticKey<T> scoreKey, Comparator<T> comparator) {
GameStatistics statistics = game.statistics();

List<H> sortedHolders = new ArrayList<>(scoreHolders);
sortedHolders.sort(Comparator.comparing(
holder -> holder.getOwnStatistics(statistics).get(scoreKey),
holder -> holder.getOwnStatistics(statistics).getOr(scoreKey, scoreKey.defaultValue()),
Comparator.nullsLast(comparator)
));

Expand All @@ -71,7 +71,7 @@ static <H extends StatisticHolder, T> Score<H, T> fromScore(IGamePhase game, Col

for (H holder : sortedHolders) {
StatisticsMap ownStatistics = holder.getOwnStatistics(statistics);
T score = ownStatistics.getOr(scoreKey, defaultValue);
T score = ownStatistics.getOr(scoreKey, scoreKey.defaultValue());
if (score == null) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public static <T> Codec<StatisticKey<T>> typedCodec(final Class<T> type) {
private final String key;
private final Function<T, JsonElement> serializer;
private Function<T, String> display = simple();
@Nullable
private T defaultValue;

private StatisticKey(Class<T> type, String key, Function<T, JsonElement> serializer) {
this.type = type;
Expand All @@ -93,15 +95,15 @@ public static <T> StatisticKey<T> register(Class<T> type, String key, Function<T
}

public static StatisticKey<Integer> ofInt(String key) {
return StatisticKey.register(Integer.class, key, JsonPrimitive::new);
return StatisticKey.register(Integer.class, key, JsonPrimitive::new).defaultValue(0);
}

public static StatisticKey<Float> ofFloat(String key) {
return StatisticKey.register(Float.class, key, JsonPrimitive::new);
return StatisticKey.register(Float.class, key, JsonPrimitive::new).defaultValue(0.0f);
}

public static StatisticKey<Boolean> ofBool(String key) {
return StatisticKey.register(Boolean.class, key, JsonPrimitive::new);
return StatisticKey.register(Boolean.class, key, JsonPrimitive::new).defaultValue(false);
}

public static StatisticKey<String> ofString(String key) {
Expand Down Expand Up @@ -151,10 +153,20 @@ public StatisticKey<T> displays(Function<T, String> display) {
return this;
}

public StatisticKey<T> defaultValue(T defaultValue) {
this.defaultValue = defaultValue;
return this;
}

public String getKey() {
return key;
}

@Nullable
public T defaultValue() {
return defaultValue;
}

public JsonElement serialize(T value) {
return serializer.apply(value);
}
Expand Down

0 comments on commit ada9cbe

Please sign in to comment.