|
| 1 | +package com.github.elic0de.hungergames.game; |
| 2 | + |
| 3 | +import com.github.elic0de.hungergames.user.GameUser; |
| 4 | +import de.themoep.minedown.MineDown; |
| 5 | + |
| 6 | +import java.util.*; |
| 7 | + |
| 8 | +public class GameRecords { |
| 9 | + |
| 10 | + //全記録 |
| 11 | + private final Map<UUID, Long> records = new HashMap<>(); |
| 12 | + |
| 13 | + private final HungerGame game; |
| 14 | + |
| 15 | + private Map<UUID, Integer> rank = new HashMap<>(); |
| 16 | + |
| 17 | + private UUID killLeader; |
| 18 | + |
| 19 | + public GameRecords(HungerGame game) { |
| 20 | + this.game = game; |
| 21 | + sortAsync(); |
| 22 | + } |
| 23 | + |
| 24 | + public int getRank(GameUser user) { |
| 25 | + final UUID uuid = user.getUniqueId(); |
| 26 | + if(rank.containsKey(uuid)) return rank.get(uuid); |
| 27 | + return -1; |
| 28 | + } |
| 29 | + |
| 30 | + public void addKill(GameUser user){ |
| 31 | + final UUID uuid = user.getUniqueId(); |
| 32 | + if (containsRecord(user)) { |
| 33 | + final long kills = records.get(uuid); |
| 34 | + |
| 35 | + if (kills >= 3) { |
| 36 | + if (killLeader != null) { |
| 37 | + if (getRank(user) == 1) { |
| 38 | + if (killLeader != uuid) { |
| 39 | + game.broadcast(new MineDown(String.format("&c%s&rが&c%s&rキルで新しいキルリーダになりました", user.getUsername(), Math.toIntExact(kills)))); |
| 40 | + killLeader = uuid; |
| 41 | + } |
| 42 | + } |
| 43 | + } else { |
| 44 | + game.broadcast(new MineDown(String.format("&c%s&rが&c3キルで新しいキルリーダになりました", user.getUsername()))); |
| 45 | + killLeader = uuid; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + records.put(uuid, kills + 1); |
| 50 | + return; |
| 51 | + } |
| 52 | + records.put(uuid, 1L); |
| 53 | + sortAsync(); |
| 54 | + } |
| 55 | + |
| 56 | + private boolean containsRecord(GameUser user){ |
| 57 | + return records.containsKey(user.getUniqueId()); |
| 58 | + } |
| 59 | + |
| 60 | + public long personalBest(GameUser user){ |
| 61 | + return records.getOrDefault(user.getUniqueId(), 0L); |
| 62 | + } |
| 63 | + |
| 64 | + public void removeAllRecord() { |
| 65 | + //すべてのレコードを削除する |
| 66 | + records.clear(); |
| 67 | + sortAsync(); |
| 68 | + } |
| 69 | + |
| 70 | + public void sortAsync(){ |
| 71 | + final List<Map.Entry<UUID, Long>> list = new ArrayList<>(records.entrySet()); |
| 72 | + |
| 73 | + //記録を昇順にソートする |
| 74 | + list.sort(Map.Entry.comparingByValue()); |
| 75 | + |
| 76 | + //最大で上位10件の記録をリストに追加する |
| 77 | + for(int index = 0; index < records.size(); index++){ |
| 78 | + //ソート済みリストから記録を取得する |
| 79 | + Map.Entry<UUID, Long> record = list.get(index); |
| 80 | + |
| 81 | + UUID uuid = record.getKey(); |
| 82 | + rank.put(uuid,index + 1); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments