aboutsummaryrefslogtreecommitdiff
path: root/src/ch/epfl/xblast/server/PlayerPriorityManager.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/ch/epfl/xblast/server/PlayerPriorityManager.java')
-rw-r--r--src/ch/epfl/xblast/server/PlayerPriorityManager.java82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/ch/epfl/xblast/server/PlayerPriorityManager.java b/src/ch/epfl/xblast/server/PlayerPriorityManager.java
new file mode 100644
index 0000000..ce6e12a
--- /dev/null
+++ b/src/ch/epfl/xblast/server/PlayerPriorityManager.java
@@ -0,0 +1,82 @@
1package ch.epfl.xblast.server;
2
3import ch.epfl.xblast.Lists;
4import ch.epfl.xblast.PlayerID;
5
6import java.util.Arrays;
7import java.util.List;
8import java.util.Map;
9import java.util.function.Function;
10import java.util.stream.Collectors;
11
12/**
13 * The player priority manager providing a sequential priority conflict resolution method.
14 *
15 * @author Pacien TRAN-GIRARD (261948)
16 */
17public final class PlayerPriorityManager {
18
19 /**
20 * The list of player priority order permutations.
21 */
22 private static final List<List<PlayerID>> PLAYER_PRIORITY_ORDERS = buildPlayerPriorityOrderList();
23
24 private PlayerPriorityManager() {
25 // Static class
26 }
27
28 /**
29 * Builds and returns the player priority order permutations.
30 *
31 * @return the list of player priority orders
32 */
33 private static List<List<PlayerID>> buildPlayerPriorityOrderList() {
34 return Lists.permutations(Arrays.asList(PlayerID.values()));
35 }
36
37 /**
38 * Returns the current player priority order permutation.
39 *
40 * @param ticks the current GameState ticks
41 * @return the player priority order
42 */
43 private static List<PlayerID> getCurrentPlayerPriorityOrder(int ticks) {
44 int priorityIndex = ticks % PLAYER_PRIORITY_ORDERS.size();
45 return PLAYER_PRIORITY_ORDERS.get(priorityIndex);
46 }
47
48 /**
49 * Resolves a conflict according to the current priority order.
50 *
51 * @param claimants the list of claimants
52 * @param ticks the current GameState ticks
53 * @return the highest priority player
54 */
55 private static PlayerID resolveConflict(List<PlayerID> claimants, int ticks) {
56 if (claimants == null || claimants.isEmpty()) return null;
57
58 return getCurrentPlayerPriorityOrder(ticks).stream()
59 .filter(claimants::contains)
60 .findFirst().orElse(null);
61 }
62
63 /**
64 * Resolves a conflict according to the current priority order.
65 *
66 * @param claimants the list of claimants
67 * @param ticks the current GameState ticks
68 * @return the highest priority player
69 */
70 public static Player resolvePlayerConflict(List<Player> claimants, int ticks) {
71 if (claimants == null || claimants.isEmpty()) return null;
72
73 Map<PlayerID, Player> claimantsMap = claimants.stream()
74 .collect(Collectors.toMap(Player::id, Function.identity()));
75
76 List<PlayerID> claimantsIDs = claimants.stream()
77 .map(Player::id).collect(Collectors.toList());
78
79 return claimantsMap.get(resolveConflict(claimantsIDs, ticks));
80 }
81
82}