mirror of
https://github.com/SunNetservers/MiniGames.git
synced 2024-12-05 00:43:15 +01:00
Implements group record placeholders
This commit is contained in:
parent
8e9b274fc0
commit
f6a272b0c0
@ -1,12 +1,12 @@
|
||||
package net.knarcraft.dropper;
|
||||
|
||||
import net.knarcraft.dropper.arena.ArenaRecord;
|
||||
import net.knarcraft.dropper.arena.DropperArenaData;
|
||||
import net.knarcraft.dropper.arena.DropperArenaGroup;
|
||||
import net.knarcraft.dropper.arena.DropperArenaHandler;
|
||||
import net.knarcraft.dropper.arena.DropperArenaPlayerRegistry;
|
||||
import net.knarcraft.dropper.arena.DropperArenaRecordsRegistry;
|
||||
import net.knarcraft.dropper.arena.DropperArenaSession;
|
||||
import net.knarcraft.dropper.arena.record.ArenaRecord;
|
||||
import net.knarcraft.dropper.command.CreateArenaCommand;
|
||||
import net.knarcraft.dropper.command.EditArenaCommand;
|
||||
import net.knarcraft.dropper.command.EditArenaTabCompleter;
|
||||
|
@ -1,94 +0,0 @@
|
||||
package net.knarcraft.dropper.arena;
|
||||
|
||||
import net.knarcraft.dropper.Dropper;
|
||||
import net.knarcraft.dropper.property.ArenaGameMode;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
public class DropperArenaGroupRecordsRegistry {
|
||||
|
||||
private final Map<UUID, Map<UUID, Map<ArenaGameMode, Integer>>> deathRecords = new HashMap<>();
|
||||
private final Map<UUID, Map<UUID, Map<ArenaGameMode, Long>>> timeRecords = new HashMap<>();
|
||||
private int numberOfArenas;
|
||||
|
||||
public void initialize(DropperArenaGroup group) {
|
||||
DropperArenaHandler arenaHandler = Dropper.getInstance().getArenaHandler();
|
||||
// The number of arenas is required to know if a player has finished all the arenas
|
||||
numberOfArenas = group.getArenas().size();
|
||||
for (UUID arenaId : group.getArenas()) {
|
||||
DropperArena arena = arenaHandler.getArena(arenaId);
|
||||
if (arena == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Load all existing records
|
||||
@NotNull Map<ArenaGameMode, DropperArenaRecordsRegistry> registries = arena.getData().recordRegistries();
|
||||
for (Map.Entry<ArenaGameMode, DropperArenaRecordsRegistry> entry : registries.entrySet()) {
|
||||
for (ArenaRecord<Integer> record : entry.getValue().getLeastDeathsRecords()) {
|
||||
loadRecords(deathRecords, record, arenaId, entry.getKey());
|
||||
}
|
||||
for (ArenaRecord<Long> record : entry.getValue().getShortestTimeMilliSecondsRecords()) {
|
||||
loadRecords(timeRecords, record, arenaId, entry.getKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Set<ArenaRecord<Integer>> getCombinedDeathRecords(ArenaGameMode gameMode) {
|
||||
return getCombinedRecords(gameMode, deathRecords);
|
||||
}
|
||||
|
||||
public Set<ArenaRecord<Long>> getCombinedTimeRecords(ArenaGameMode gameMode) {
|
||||
return getCombinedRecords(gameMode, timeRecords);
|
||||
}
|
||||
|
||||
private <K extends Comparable<K>> Set<ArenaRecord<K>> getCombinedRecords(ArenaGameMode gameMode,
|
||||
Map<UUID, Map<UUID, Map<ArenaGameMode, K>>> rawRecords) {
|
||||
Map<UUID, ArenaRecord<K>> combinedRecords = new HashMap<>();
|
||||
for (Map.Entry<UUID, Map<UUID, Map<ArenaGameMode, K>>> entry : rawRecords.entrySet()) {
|
||||
// Only get records for players that have played all arenas
|
||||
if (entry.getValue().size() != numberOfArenas) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Combine all records to get a "best of all" value
|
||||
for (Map<ArenaGameMode, K> records : entry.getValue().values()) {
|
||||
K value = records.get(gameMode);
|
||||
if (!combinedRecords.containsKey(entry.getKey())) {
|
||||
combinedRecords.put(entry.getKey(), new ArenaRecord<>(entry.getKey(), value));
|
||||
} else {
|
||||
//TODO: Find the best way to combine objects of type K
|
||||
//combinedRecords.put(entry.getKey(), combinedRecords.get(entry.getKey()).combine(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
return new HashSet<>(combinedRecords.values());
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a record
|
||||
*
|
||||
* @param combinedRecords <p>The map of combined records to update</p>
|
||||
* @param record <p>The record to add</p>
|
||||
* @param arenaId <p>The arena the record belongs to</p>
|
||||
* @param arenaGameMode <p>The game-mode the record was achieved for</p>
|
||||
* @param <K> <p>The type of the record's value</p>
|
||||
*/
|
||||
private <K extends Comparable<K>> void loadRecords(
|
||||
Map<UUID, Map<UUID, Map<ArenaGameMode, K>>> combinedRecords,
|
||||
ArenaRecord<K> record, UUID arenaId, ArenaGameMode arenaGameMode) {
|
||||
if (!combinedRecords.containsKey(record.userId())) {
|
||||
combinedRecords.put(record.userId(), new HashMap<>());
|
||||
}
|
||||
if (!combinedRecords.get(record.userId()).containsKey(arenaId)) {
|
||||
combinedRecords.get(record.userId()).put(arenaId, new HashMap<>());
|
||||
}
|
||||
combinedRecords.get(record.userId()).get(arenaId).put(arenaGameMode, record.record());
|
||||
}
|
||||
|
||||
}
|
@ -1,10 +1,15 @@
|
||||
package net.knarcraft.dropper.arena;
|
||||
|
||||
import net.knarcraft.dropper.Dropper;
|
||||
import net.knarcraft.dropper.arena.record.ArenaRecord;
|
||||
import net.knarcraft.dropper.arena.record.IntegerRecord;
|
||||
import net.knarcraft.dropper.arena.record.LongRecord;
|
||||
import net.knarcraft.dropper.arena.record.SummableArenaRecord;
|
||||
import net.knarcraft.dropper.container.SerializableUUID;
|
||||
import net.knarcraft.dropper.property.RecordResult;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
@ -19,8 +24,8 @@ import java.util.concurrent.atomic.AtomicReference;
|
||||
public class DropperArenaRecordsRegistry implements ConfigurationSerializable {
|
||||
|
||||
private final UUID arenaId;
|
||||
private final @NotNull Set<ArenaRecord<Integer>> leastDeaths;
|
||||
private final @NotNull Set<ArenaRecord<Long>> shortestTimeMilliSeconds;
|
||||
private final @NotNull Set<IntegerRecord> leastDeaths;
|
||||
private final @NotNull Set<LongRecord> shortestTimeMilliSeconds;
|
||||
|
||||
/**
|
||||
* Instantiates a new empty records registry
|
||||
@ -37,8 +42,8 @@ public class DropperArenaRecordsRegistry implements ConfigurationSerializable {
|
||||
* @param leastDeaths <p>The existing least death records to use</p>
|
||||
* @param shortestTimeMilliSeconds <p>The existing leash time records to use</p>
|
||||
*/
|
||||
private DropperArenaRecordsRegistry(@NotNull UUID arenaId, @NotNull Set<ArenaRecord<Integer>> leastDeaths,
|
||||
@NotNull Set<ArenaRecord<Long>> shortestTimeMilliSeconds) {
|
||||
private DropperArenaRecordsRegistry(@NotNull UUID arenaId, @NotNull Set<IntegerRecord> leastDeaths,
|
||||
@NotNull Set<LongRecord> shortestTimeMilliSeconds) {
|
||||
this.arenaId = arenaId;
|
||||
this.leastDeaths = new HashSet<>(leastDeaths);
|
||||
this.shortestTimeMilliSeconds = new HashSet<>(shortestTimeMilliSeconds);
|
||||
@ -49,7 +54,7 @@ public class DropperArenaRecordsRegistry implements ConfigurationSerializable {
|
||||
*
|
||||
* @return <p>Existing death records</p>
|
||||
*/
|
||||
public Set<ArenaRecord<Integer>> getLeastDeathsRecords() {
|
||||
public Set<SummableArenaRecord<Integer>> getLeastDeathsRecords() {
|
||||
return new HashSet<>(this.leastDeaths);
|
||||
}
|
||||
|
||||
@ -58,7 +63,7 @@ public class DropperArenaRecordsRegistry implements ConfigurationSerializable {
|
||||
*
|
||||
* @return <p>Existing time records</p>
|
||||
*/
|
||||
public Set<ArenaRecord<Long>> getShortestTimeMilliSecondsRecords() {
|
||||
public Set<SummableArenaRecord<Long>> getShortestTimeMilliSecondsRecords() {
|
||||
return new HashSet<>(this.shortestTimeMilliSeconds);
|
||||
}
|
||||
|
||||
@ -70,7 +75,8 @@ public class DropperArenaRecordsRegistry implements ConfigurationSerializable {
|
||||
* @return <p>The result explaining what type of record was achieved</p>
|
||||
*/
|
||||
public @NotNull RecordResult registerDeathRecord(@NotNull UUID playerId, int deaths) {
|
||||
return registerRecord(leastDeaths, playerId, deaths);
|
||||
Set<ArenaRecord<Integer>> asInt = new HashSet<>(leastDeaths);
|
||||
return registerRecord(asInt, playerId, deaths);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -81,7 +87,8 @@ public class DropperArenaRecordsRegistry implements ConfigurationSerializable {
|
||||
* @return <p>The result explaining what type of record was achieved</p>
|
||||
*/
|
||||
public @NotNull RecordResult registerTimeRecord(@NotNull UUID playerId, long milliseconds) {
|
||||
return registerRecord(shortestTimeMilliSeconds, playerId, milliseconds);
|
||||
Set<ArenaRecord<Long>> asLong = new HashSet<>(shortestTimeMilliSeconds);
|
||||
return registerRecord(asLong, playerId, milliseconds);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -102,7 +109,7 @@ public class DropperArenaRecordsRegistry implements ConfigurationSerializable {
|
||||
private <T extends Comparable<T>> @NotNull RecordResult registerRecord(@NotNull Set<ArenaRecord<T>> existingRecords,
|
||||
@NotNull UUID playerId, T amount) {
|
||||
RecordResult result;
|
||||
if (existingRecords.stream().allMatch((entry) -> amount.compareTo(entry.record()) < 0)) {
|
||||
if (existingRecords.stream().allMatch((entry) -> amount.compareTo(entry.getRecord()) < 0)) {
|
||||
// If the given value is less than all other values, that's a world record!
|
||||
result = RecordResult.WORLD_RECORD;
|
||||
existingRecords.add(new ArenaRecord<>(playerId, amount));
|
||||
@ -111,7 +118,7 @@ public class DropperArenaRecordsRegistry implements ConfigurationSerializable {
|
||||
}
|
||||
|
||||
ArenaRecord<T> playerRecord = getRecord(existingRecords, playerId);
|
||||
if (playerRecord != null && amount.compareTo(playerRecord.record()) < 0) {
|
||||
if (playerRecord != null && amount.compareTo(playerRecord.getRecord()) < 0) {
|
||||
// If the given value is less than the player's previous value, that's a personal best!
|
||||
result = RecordResult.PERSONAL_BEST;
|
||||
existingRecords.add(new ArenaRecord<>(playerId, amount));
|
||||
@ -127,11 +134,19 @@ public class DropperArenaRecordsRegistry implements ConfigurationSerializable {
|
||||
return result;
|
||||
}
|
||||
|
||||
private <T extends Comparable<T>> ArenaRecord<T> getRecord(@NotNull Set<ArenaRecord<T>> existingRecords,
|
||||
@NotNull UUID playerId) {
|
||||
/**
|
||||
* Gets the record stored for the given player
|
||||
*
|
||||
* @param existingRecords <p>The existing records to look through</p>
|
||||
* @param playerId <p>The id of the player to look for</p>
|
||||
* @param <T> <p>The type of the stored record</p>
|
||||
* @return <p>The record, or null if not found</p>
|
||||
*/
|
||||
private <T extends Comparable<T>> @Nullable ArenaRecord<T> getRecord(@NotNull Set<ArenaRecord<T>> existingRecords,
|
||||
@NotNull UUID playerId) {
|
||||
AtomicReference<ArenaRecord<T>> record = new AtomicReference<>();
|
||||
existingRecords.forEach((item) -> {
|
||||
if (item.userId().equals(playerId)) {
|
||||
if (item.getUserId().equals(playerId)) {
|
||||
record.set(item);
|
||||
}
|
||||
});
|
||||
@ -157,10 +172,10 @@ public class DropperArenaRecordsRegistry implements ConfigurationSerializable {
|
||||
@SuppressWarnings({"unused", "unchecked"})
|
||||
public static DropperArenaRecordsRegistry deserialize(Map<String, Object> data) {
|
||||
UUID arenaId = ((SerializableUUID) data.get("arenaId")).uuid();
|
||||
Set<ArenaRecord<Integer>> leastDeaths =
|
||||
(Set<ArenaRecord<Integer>>) data.getOrDefault("leastDeaths", new HashMap<>());
|
||||
Set<ArenaRecord<Long>> shortestTimeMilliseconds =
|
||||
(Set<ArenaRecord<Long>>) data.getOrDefault("shortestTime", new HashMap<>());
|
||||
Set<IntegerRecord> leastDeaths =
|
||||
(Set<IntegerRecord>) data.getOrDefault("leastDeaths", new HashMap<>());
|
||||
Set<LongRecord> shortestTimeMilliseconds =
|
||||
(Set<LongRecord>) data.getOrDefault("shortestTime", new HashMap<>());
|
||||
return new DropperArenaRecordsRegistry(arenaId, leastDeaths, shortestTimeMilliseconds);
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
package net.knarcraft.dropper.arena;
|
||||
package net.knarcraft.dropper.arena.record;
|
||||
|
||||
import net.knarcraft.dropper.container.SerializableUUID;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
@ -6,17 +6,43 @@ import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* A record stored for an arena
|
||||
*
|
||||
* @param userId <p>The id of the player that achieved the record</p>
|
||||
* @param record <p>The record achieved</p>
|
||||
* @param <K> <p>The comparable type of the record</p>
|
||||
*/
|
||||
public record ArenaRecord<K extends Comparable<K>>(UUID userId, K record) implements Comparable<ArenaRecord<K>>,
|
||||
ConfigurationSerializable {
|
||||
public class ArenaRecord<K extends Comparable<K>> implements Comparable<ArenaRecord<K>>, ConfigurationSerializable {
|
||||
|
||||
private final UUID userId;
|
||||
private final K record;
|
||||
|
||||
/**
|
||||
* @param userId <p>The id of the player that achieved the record</p>
|
||||
* @param record <p>The record achieved</p>
|
||||
*/
|
||||
public ArenaRecord(UUID userId, K record) {
|
||||
this.userId = userId;
|
||||
this.record = record;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the id of the user this record belongs to
|
||||
*
|
||||
* @return <p>The record's achiever</p>
|
||||
*/
|
||||
public UUID getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of the stored record
|
||||
*
|
||||
* @return <p>The record value</p>
|
||||
*/
|
||||
public K getRecord() {
|
||||
return record;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
@ -32,7 +58,7 @@ public record ArenaRecord<K extends Comparable<K>>(UUID userId, K record) implem
|
||||
@Override
|
||||
public Map<String, Object> serialize() {
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
data.put("userId", new SerializableUUID(userId()));
|
||||
data.put("userId", new SerializableUUID(getUserId()));
|
||||
data.put("record", record);
|
||||
return data;
|
||||
}
|
||||
@ -49,4 +75,14 @@ public record ArenaRecord<K extends Comparable<K>>(UUID userId, K record) implem
|
||||
return new ArenaRecord<>(((SerializableUUID) data.get("userId")).uuid(), (K) data.get("record"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(userId, record);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return userId + ": " + record;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package net.knarcraft.dropper.arena.record;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* A record storing an integer
|
||||
*/
|
||||
public class IntegerRecord extends SummableArenaRecord<Integer> {
|
||||
|
||||
/**
|
||||
* @param userId <p>The id of the player that achieved the record</p>
|
||||
* @param record <p>The record achieved</p>
|
||||
*/
|
||||
public IntegerRecord(UUID userId, Integer record) {
|
||||
super(userId, record);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SummableArenaRecord<Integer> sum(Integer value) {
|
||||
return new IntegerRecord(this.getUserId(), this.getRecord() + value);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package net.knarcraft.dropper.arena.record;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* A record storing a Long
|
||||
*/
|
||||
public class LongRecord extends SummableArenaRecord<Long> {
|
||||
|
||||
/**
|
||||
* @param userId <p>The id of the player that achieved the record</p>
|
||||
* @param record <p>The record achieved</p>
|
||||
*/
|
||||
public LongRecord(UUID userId, Long record) {
|
||||
super(userId, record);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SummableArenaRecord<Long> sum(Long value) {
|
||||
return new LongRecord(this.getUserId(), this.getRecord() + value);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package net.knarcraft.dropper.arena.record;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* A type of arena record which can be summed together
|
||||
*
|
||||
* @param <K> <p>The type of the stored value</p>
|
||||
*/
|
||||
public abstract class SummableArenaRecord<K extends Comparable<K>> extends ArenaRecord<K> {
|
||||
|
||||
/**
|
||||
* @param userId <p>The id of the player that achieved the record</p>
|
||||
* @param record <p>The record achieved</p>
|
||||
*/
|
||||
public SummableArenaRecord(UUID userId, K record) {
|
||||
super(userId, record);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a summable record with the resulting sum
|
||||
*
|
||||
* @param value <p>The value to add to the existing value</p>
|
||||
* @return <p>A record with the sum of this record and the given value</p>
|
||||
*/
|
||||
public abstract SummableArenaRecord<K> sum(K value);
|
||||
|
||||
}
|
@ -2,15 +2,16 @@ package net.knarcraft.dropper.placeholder;
|
||||
|
||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
import net.knarcraft.dropper.Dropper;
|
||||
import net.knarcraft.dropper.arena.ArenaRecord;
|
||||
import net.knarcraft.dropper.arena.DropperArena;
|
||||
import net.knarcraft.dropper.arena.DropperArenaGroup;
|
||||
import net.knarcraft.dropper.arena.DropperArenaHandler;
|
||||
import net.knarcraft.dropper.arena.DropperArenaRecordsRegistry;
|
||||
import net.knarcraft.dropper.arena.record.ArenaRecord;
|
||||
import net.knarcraft.dropper.placeholder.parsing.InfoType;
|
||||
import net.knarcraft.dropper.placeholder.parsing.RecordType;
|
||||
import net.knarcraft.dropper.placeholder.parsing.SelectionType;
|
||||
import net.knarcraft.dropper.property.ArenaGameMode;
|
||||
import net.knarcraft.dropper.util.DropperGroupRecordHelper;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
@ -110,18 +111,20 @@ public class DropperRecordExpansion extends PlaceholderExpansion {
|
||||
if (group == null) {
|
||||
return null;
|
||||
}
|
||||
// TODO: Basically, find all UUIDs that exist for all arenas (for selected game mode), and sum them together
|
||||
List<DropperArena> arenas = new ArrayList<>();
|
||||
for (UUID arenaId : group.getArenas()) {
|
||||
arenas.add(arenaHandler.getArena(arenaId));
|
||||
|
||||
ArenaRecord<?> record;
|
||||
if (recordType == RecordType.DEATHS) {
|
||||
record = getRecord(DropperGroupRecordHelper.getCombinedDeaths(group, gameMode), recordNumber);
|
||||
} else {
|
||||
record = getRecord(DropperGroupRecordHelper.getCombinedTime(group, gameMode), recordNumber);
|
||||
}
|
||||
|
||||
Set<DropperArenaRecordsRegistry> registries = new HashSet<>();
|
||||
for (DropperArena arena : arenas) {
|
||||
registries.add(arena.getData().recordRegistries().get(gameMode));
|
||||
// If a record number is not found, leave it blank, so it looks neat
|
||||
if (record == null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return "";
|
||||
return getRecordData(infoType, record);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -172,8 +175,8 @@ public class DropperRecordExpansion extends PlaceholderExpansion {
|
||||
private @Nullable ArenaRecord<?> getRecord(@NotNull DropperArenaRecordsRegistry recordsRegistry,
|
||||
@NotNull RecordType recordType, int recordNumber) {
|
||||
return switch (recordType) {
|
||||
case TIME -> getRecord(recordsRegistry.getShortestTimeMilliSecondsRecords(), recordNumber);
|
||||
case DEATHS -> getRecord(recordsRegistry.getLeastDeathsRecords(), recordNumber);
|
||||
case TIME -> getRecord(new HashSet<>(recordsRegistry.getShortestTimeMilliSecondsRecords()), recordNumber);
|
||||
case DEATHS -> getRecord(new HashSet<>(recordsRegistry.getLeastDeathsRecords()), recordNumber);
|
||||
};
|
||||
}
|
||||
|
||||
@ -203,9 +206,9 @@ public class DropperRecordExpansion extends PlaceholderExpansion {
|
||||
*/
|
||||
private String getRecordData(@NotNull InfoType infoType, @NotNull ArenaRecord<?> arenaRecord) {
|
||||
return switch (infoType) {
|
||||
case PLAYER -> getPlayerName(arenaRecord.userId());
|
||||
case VALUE -> arenaRecord.record().toString();
|
||||
case COMBINED -> getPlayerName(arenaRecord.userId()) + ": " + arenaRecord.record().toString();
|
||||
case PLAYER -> getPlayerName(arenaRecord.getUserId());
|
||||
case VALUE -> arenaRecord.getRecord().toString();
|
||||
case COMBINED -> getPlayerName(arenaRecord.getUserId()) + ": " + arenaRecord.getRecord().toString();
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,168 @@
|
||||
package net.knarcraft.dropper.util;
|
||||
|
||||
import net.knarcraft.dropper.Dropper;
|
||||
import net.knarcraft.dropper.arena.DropperArena;
|
||||
import net.knarcraft.dropper.arena.DropperArenaGroup;
|
||||
import net.knarcraft.dropper.arena.DropperArenaHandler;
|
||||
import net.knarcraft.dropper.arena.record.ArenaRecord;
|
||||
import net.knarcraft.dropper.arena.record.SummableArenaRecord;
|
||||
import net.knarcraft.dropper.property.ArenaGameMode;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
/**
|
||||
* A helper class for getting combined record data for a dropper group
|
||||
*/
|
||||
public final class DropperGroupRecordHelper {
|
||||
|
||||
private DropperGroupRecordHelper() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the combined least-death records for the given group and game-mode
|
||||
*
|
||||
* @param group <p>The group to get records from</p>
|
||||
* @param gameMode <p>The game-mode to get records for</p>
|
||||
* @return <p>The combined death records</p>
|
||||
*/
|
||||
public static @NotNull Set<ArenaRecord<Integer>> getCombinedDeaths(@NotNull DropperArenaGroup group,
|
||||
@NotNull ArenaGameMode gameMode) {
|
||||
Map<UUID, SummableArenaRecord<Integer>> records = new HashMap<>();
|
||||
@NotNull BiFunction<DropperArena, ArenaGameMode, Set<SummableArenaRecord<Integer>>> recordSupplier =
|
||||
(arena, aGameMode) -> arena.getData().recordRegistries().get(gameMode).getLeastDeathsRecords();
|
||||
|
||||
return getCombined(group, gameMode, records, recordSupplier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the combined least-time records for the given group and game-mode
|
||||
*
|
||||
* @param group <p>The group to get records from</p>
|
||||
* @param gameMode <p>The game-mode to get records for</p>
|
||||
* @return <p>The combined least-time records</p>
|
||||
*/
|
||||
public static @NotNull Set<ArenaRecord<Long>> getCombinedTime(@NotNull DropperArenaGroup group,
|
||||
@NotNull ArenaGameMode gameMode) {
|
||||
Map<UUID, SummableArenaRecord<Long>> records = new HashMap<>();
|
||||
@NotNull BiFunction<DropperArena, ArenaGameMode, Set<SummableArenaRecord<Long>>> recordSupplier =
|
||||
(arena, aGameMode) -> arena.getData().recordRegistries().get(gameMode).getShortestTimeMilliSecondsRecords();
|
||||
|
||||
return getCombined(group, gameMode, records, recordSupplier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the combined records for a group and game-mode
|
||||
*
|
||||
* @param group <p>The group to get combined records for</p>
|
||||
* @param gameMode <p>The game-mode to get records for</p>
|
||||
* @param records <p>The map to store the combined records to</p>
|
||||
* @param recordSupplier <p>The function that supplies records of this type</p>
|
||||
* @param <K> <p>The type of the records to combine</p>
|
||||
* @return <p>The combined records</p>
|
||||
*/
|
||||
private static <K extends Comparable<K>> @NotNull Set<ArenaRecord<K>> getCombined(@NotNull DropperArenaGroup group,
|
||||
@NotNull ArenaGameMode gameMode,
|
||||
@NotNull Map<UUID,
|
||||
SummableArenaRecord<K>> records,
|
||||
@NotNull BiFunction<DropperArena,
|
||||
ArenaGameMode,
|
||||
Set<SummableArenaRecord<K>>> recordSupplier) {
|
||||
DropperArenaHandler arenaHandler = Dropper.getInstance().getArenaHandler();
|
||||
|
||||
// Get all arenas in the group
|
||||
Set<DropperArena> arenas = getArenas(arenaHandler, group);
|
||||
|
||||
// Calculate the combined records
|
||||
Map<UUID, Integer> recordsFound = new HashMap<>();
|
||||
combineRecords(arenas, gameMode, records, recordsFound, recordSupplier);
|
||||
|
||||
// Filter out any players that haven't played through all arenas
|
||||
filterRecords(records, recordsFound, arenas.size());
|
||||
|
||||
return new HashSet<>(records.values());
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters away any records that belong to users who haven't set records for all arenas in the group
|
||||
*
|
||||
* @param records <p>The records to filter</p>
|
||||
* @param recordsFound <p>The map of how many records have been registered for each user</p>
|
||||
* @param arenas <p>The number of arenas in the group</p>
|
||||
* @param <K> <p>The type of the given records</p>
|
||||
*/
|
||||
private static <K extends Comparable<K>> void filterRecords(@NotNull Map<UUID, SummableArenaRecord<K>> records,
|
||||
@NotNull Map<UUID, Integer> recordsFound, int arenas) {
|
||||
for (UUID userId : recordsFound.keySet()) {
|
||||
if (recordsFound.get(userId) != arenas) {
|
||||
records.remove(userId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all arenas in the given group
|
||||
*
|
||||
* @param arenaHandler <p>The arena handler to get arenas from</p>
|
||||
* @param group <p>The group to get arenas for</p>
|
||||
* @return <p>The arenas found in the group</p>
|
||||
*/
|
||||
private static @NotNull Set<DropperArena> getArenas(@NotNull DropperArenaHandler arenaHandler,
|
||||
@NotNull DropperArenaGroup group) {
|
||||
// Get all arenas in the group
|
||||
Set<DropperArena> arenas = new HashSet<>();
|
||||
for (UUID arenaId : group.getArenas()) {
|
||||
DropperArena arena = arenaHandler.getArena(arenaId);
|
||||
if (arena != null) {
|
||||
arenas.add(arena);
|
||||
}
|
||||
}
|
||||
return arenas;
|
||||
}
|
||||
|
||||
/**
|
||||
* Combines arena records
|
||||
*
|
||||
* @param arenas <p>The arenas whose records should be combined</p>
|
||||
* @param gameMode <p>The game-mode to combine records for</p>
|
||||
* @param combinedRecords <p>The map to store the combined records to</p>
|
||||
* @param recordsFound <p>The map used to store the number of records registered for each player</p>
|
||||
* @param recordSupplier <p>The function that supplies record data of this type</p>
|
||||
* @param <K> <p>The type of record to combine</p>
|
||||
*/
|
||||
private static <K extends Comparable<K>> void combineRecords(@NotNull Set<DropperArena> arenas,
|
||||
@NotNull ArenaGameMode gameMode,
|
||||
@NotNull Map<UUID,
|
||||
SummableArenaRecord<K>> combinedRecords,
|
||||
@NotNull Map<UUID, Integer> recordsFound,
|
||||
@NotNull BiFunction<DropperArena, ArenaGameMode,
|
||||
Set<SummableArenaRecord<K>>> recordSupplier) {
|
||||
for (DropperArena arena : arenas) {
|
||||
Set<SummableArenaRecord<K>> existingRecords = recordSupplier.apply(arena, gameMode);
|
||||
// For each arena's record registry, calculate the combined records
|
||||
for (SummableArenaRecord<K> value : existingRecords) {
|
||||
UUID userId = value.getUserId();
|
||||
|
||||
// Bump the number of records found for the user
|
||||
if (!recordsFound.containsKey(userId)) {
|
||||
recordsFound.put(userId, 0);
|
||||
}
|
||||
recordsFound.put(userId, recordsFound.get(userId) + 1);
|
||||
|
||||
// Put the value, or the sum with the existing value, into combined records
|
||||
if (!combinedRecords.containsKey(userId)) {
|
||||
combinedRecords.put(value.getUserId(), value);
|
||||
} else {
|
||||
combinedRecords.put(userId, combinedRecords.get(userId).sum(value.getRecord()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user