Coverage Summary for Class: CommandConverter (it.polimi.ingsw.controller)
Class | Method, % | Line, % |
---|---|---|
CommandConverter | 100% (5/ 5) | 100% (43/ 43) |
CommandConverter$1 | 100% (1/ 1) | 100% (1/ 1) |
total | 100% (6/ 6) | 100% (44/ 44) |
1 package it.polimi.ingsw.controller;
2
3 import com.google.gson.Gson;
4 import it.polimi.ingsw.model.*;
5 import it.polimi.ingsw.utils.model.Command;
6
7 import java.util.ArrayList;
8 import java.util.stream.Collectors;
9
10 /**
11 * Class with static functions to Convert Data into Command ArrayList
12 */
13 class CommandConverter {
14 /**
15 * Create an Arraylist of Command with Players Info
16 *
17 * @param phase GamePhase
18 * @param playerList list of Players to be converted
19 * @return Player converted into Command
20 */
21 public ArrayList<Command> reportPlayer(GamePhase phase, ArrayList<Player> playerList) {
22 switch (phase) {
23 case START_PLAYER:
24 return (ArrayList<Command>) playerList
25 .stream().map(e -> new Command(TypeCommand.PLAYER.getValue(),
26 FuncCommand.SET_START_PLAYER.getValue(), new Gson().toJson(e), e.getUsername()))
27 .collect(Collectors.toList());
28 default:
29 return (ArrayList<Command>) playerList.stream()
30 .map(e -> new Command(TypeCommand.PLAYER.getValue(), new Gson().toJson(e)))
31 .collect(Collectors.toList());
32 }
33 }
34
35 /**
36 * Create an Arraylist of Command with Board Info
37 *
38 * @param phase GamePhase
39 * @param board Game Board to be converted
40 * @param currentPlayer Current Player's username
41 * @return Board State as ArrayList<Command>
42 */
43 public ArrayList<Command> reportBoard(GamePhase phase, Cell[][] board, String currentPlayer) {
44 ArrayList<Command> report = new ArrayList<>();
45
46 switch (phase) {
47 case PENDING:
48 case CHOOSE_WORKER: {
49 for (int i = 0; i < board.length; i++)
50 for (int j = 0; j < board[i].length; j++) {
51 String funcName = null;
52 if (board[i][j].getBlock().getTypeBlock() == TypeBlock.WORKER
53 && board[i][j].getBlock().getOwner().equals(currentPlayer))
54 funcName = FuncCommand.CHOOSE_WORKER.getValue();
55 report.add(new Command(TypeCommand.BOARD.getValue(), funcName, new Gson().toJson(board[i][j]),
56 Integer.toString(i * 5 + j)));
57 }
58 }
59 break;
60 case SET_WORKERS: {
61 for (int i = 0; i < board.length; i++)
62 for (int j = 0; j < board[i].length; j++) {
63 String funcName = null;
64 if (board[i][j].getBlock().getTypeBlock() == TypeBlock.LEVEL0)
65 funcName = FuncCommand.SET_WORKERS.getValue();
66 report.add(new Command(TypeCommand.BOARD.getValue(), funcName, new Gson().toJson(board[i][j]),
67 Integer.toString(i * 5 + j)));
68 }
69 }
70 break;
71 default: {
72 for (int i = 0; i < board.length; i++)
73 for (int j = 0; j < board[i].length; j++) {
74 report.add(new Command(TypeCommand.BOARD.getValue(), null, new Gson().toJson(board[i][j]),
75 Integer.toString(i * 5 + j)));
76 }
77 }
78 }
79 return report;
80 }
81
82 /**
83 * Create an Arraylist of Command with Player Action Info
84 *
85 * @param phase GamePhase
86 * @param actions Action State to be converted
87 * @return Action State as ArrayList<Command>
88 */
89 public ArrayList<Command> reportAction(GamePhase phase, Action[][][] actions) {
90 ArrayList<Command> report = new ArrayList<>();
91
92 if (phase != GamePhase.CHOOSE_ACTION && phase != GamePhase.PENDING)
93 return report;
94 for (int i = 0; i < actions.length; i++)
95 for (int j = 0; j < actions[i].length; j++)
96 for (int k = 0; k < actions[i][j].length; k++)
97 if (actions[i][j][k].getStatus())
98 report.add(new Command(TypeCommand.ACTION.getValue(), FuncCommand.CHOOSE_ACTION.getValue(),
99 new Gson().toJson(actions[i][j][k]), new Gson().toJson(new int[] { i * 5 + j, k })));
100 return report;
101 }
102 }