Coverage Summary for Class: TeamMapper (it.polimi.ingsw.Model)
Class |
Class, %
|
Method, %
|
Branch, %
|
Line, %
|
TeamMapper |
100%
(1/1)
|
80%
(4/5)
|
100%
(10/10)
|
75%
(12/16)
|
1 package it.polimi.ingsw.Model;
2
3 import it.polimi.ingsw.Model.Enums.TeamID;
4 import it.polimi.ingsw.Model.Enums.TowerColour;
5
6 import java.io.Serial;
7 import java.io.Serializable;
8 import java.util.HashMap;
9 import java.util.List;
10 import java.util.Map;
11
12 /**
13 * Maps the players in the model to their respective teams and {@link TowerStorage}
14 */
15 public class TeamMapper implements Serializable {
16 @Serial
17 private static final long serialVersionUID = 136L; // convention: 1 for model, (01 -> 99) for objects
18 final Map<PlayerBoard, TeamID> playerTeamMap;
19 final Map<TeamID, TowerStorage> towerStorageMap;
20
21 /**
22 * Creates a new mapper. If the players are not 4, every player gets put into its own special team. If 4 players
23 * are inputted, the first pair of players will be put in the first team, and the second pair into the second team.
24 *
25 * @param players a {@link List} of {@link PlayerBoard}s to put into teams.
26 */
27 public TeamMapper(List<PlayerBoard> players) {
28 this.playerTeamMap = new HashMap<>();
29 int nop = players.size();
30 for (int i = 0; i < nop; i++) {
31 this.playerTeamMap.put(players.get(i), TeamID.fromInteger(i % (nop == 4 ? 2 : nop)));
32 } // note: for 4 players the first team is always made up by the even nicknames
33 this.towerStorageMap = new HashMap<>(); // creates tower storage associations based on number of players
34 for (int i = 0; i < (nop == 4 ? 2 : nop); i++) {
35 TeamID tID = TeamID.fromInteger(i);
36 this.towerStorageMap.put(tID, new TowerStorage(TowerColour.fromTeamId(tID), nop == 3 ? 6 : 8));
37 }
38 }
39
40 /**
41 * Get a team's players
42 *
43 * @param tID the ID of the Team to search players of
44 * @return an Unmutable {@link List} containing references to the team's {@link PlayerBoard}s
45 */
46 public List<PlayerBoard> getMutablePlayers(TeamID tID) {
47 return playerTeamMap.entrySet().stream()
48 .filter(e -> e.getValue().equals(tID))
49 .map(Map.Entry::getKey)
50 .toList();
51 }
52
53 /**
54 * Get a team's tower storage
55 *
56 * @param pb the player to find the {@link TowerStorage} of
57 * @return a reference to {@link TowerStorage}, or null if the {@link PlayerBoard} matches no team in the game
58 */
59 public TowerStorage getMutableTowerStorage(PlayerBoard pb) {
60 return this.getMutableTowerStorage(this.getTeamID(pb));
61 }
62
63 /**
64 * Get a team's tower storage
65 *
66 * @param tID the ID of the Team to search the tower storage of
67 * @return a reference to {@link TowerStorage}, or null if the TeamID is invalid
68 */
69 public TowerStorage getMutableTowerStorage(TeamID tID) {
70 return towerStorageMap.get(tID);
71 }
72
73 /**
74 * Get the team of a player
75 *
76 * @param pb the player to find the {@link TeamID} of
77 * @return the {@link TeamID} of the player in input or null if the {@link PlayerBoard} matches no team in the game
78 */
79 public TeamID getTeamID(PlayerBoard pb) {
80 return playerTeamMap.get(pb);
81 }
82 }