Coverage Summary for Class: Parser (it.polimi.ingsw.view.socket)
Class | Method, % | Line, % |
---|---|---|
Parser | 80% (12/ 15) | 73,3% (33/ 45) |
Parser$1 | 100% (1/ 1) | 100% (1/ 1) |
total | 81,2% (13/ 16) | 73,9% (34/ 46) |
1 package it.polimi.ingsw.view.socket;
2
3 import com.google.gson.Gson;
4 import com.google.gson.JsonSyntaxException;
5 import com.google.gson.reflect.TypeToken;
6 import it.polimi.ingsw.utils.Observable;
7 import it.polimi.ingsw.utils.Observer;
8 import it.polimi.ingsw.utils.model.Command;
9 import it.polimi.ingsw.view.model.Cell;
10 import it.polimi.ingsw.view.model.Player;
11
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.stream.Collectors;
15
16 /**
17 * Client parser of Game Info
18 */
19 public class Parser extends Observable<ArrayList<Command>> implements Observer<String> {
20 /**
21 * Client Game State
22 */
23 private ArrayList<String> commandList = new ArrayList<>();
24
25 /**
26 * Get a copy of Client Game State
27 *
28 * @return game state
29 */
30 private ArrayList<Command> duplicateCommandList() {
31 return (ArrayList<Command>) commandList.stream().map(e -> new Gson().fromJson(e, Command.class))
32 .collect(Collectors.toList());
33 }
34
35 /**
36 * Update Client Game State
37 *
38 * @param commandList new data to add or delete
39 */
40 private synchronized void setCommandList(ArrayList<Command> commandList) {
41
42 // Discard all Actions
43 this.commandList = (ArrayList<String>) this.commandList.stream()
44 .filter(e -> !(new Gson().fromJson(e, Command.class)).getType().equals("action"))
45 .collect(Collectors.toList());
46
47 commandList.forEach(e -> {
48 if (e.getStatus() == true) {
49 e.setStatus(null);
50 this.commandList.add(new Gson().toJson(e));
51 } else {
52 e.setStatus(null);
53 this.commandList.remove(new Gson().toJson(e));
54 }
55 });
56 }
57
58 @Override
59 public void update(String commandList) {
60 try {
61 ArrayList<Command> parsed = new Gson().fromJson(commandList, new TypeToken<ArrayList<Command>>() {
62 }.getType());
63 if (parsed == null || parsed.isEmpty())
64 return;
65 setCommandList(parsed);
66 notify(duplicateCommandList());
67 } catch (JsonSyntaxException e) {
68 // Fail Json Convert
69 }
70 }
71
72 /**
73 * Get Current available Type Command
74 *
75 * @return an array list of string of actual filter available (type of command)
76 */
77 public List<String> getFilters() {
78 return duplicateCommandList().stream().map(e -> e.getType()).distinct().collect(Collectors.toList());
79 }
80
81 /**
82 * Get an arraylist of command of a certain type
83 *
84 * @param filter a string to filter commands (type field)
85 * @return array list with filtered command (type field)
86 */
87 public List<Command> getCommandList(String filter) {
88 if (getFilters().contains(filter))
89 return duplicateCommandList().stream().filter(e -> e.getType().equals(filter)).collect(Collectors.toList());
90 return new ArrayList<>();
91 }
92
93 /**
94 * Get all player available command
95 *
96 * @return array list of all commands usable from users
97 */
98 public List<Command> getUsableCommandList() {
99 return duplicateCommandList().stream().filter(e -> {
100 return e.getFuncName() != null;
101 }).collect(Collectors.toList());
102 }
103
104 /**
105 * Convert a Command into a String to send to the server
106 *
107 * @param command to parse into string
108 * @return command parsed into string
109 */
110 public static String toString(Command command) {
111 return new Gson().toJson(command);
112 }
113
114 /**
115 * Get Game Board
116 *
117 * @return Game Board
118 */
119 public Cell[][] getBoard() {
120 List<Command> boardInfo = getCommandList("board");
121 Cell[][] boardParsed = new Cell[5][5];
122 boardInfo.forEach(e -> {
123 try {
124 boardParsed[Integer.parseInt(e.getFuncData()) / 5][Integer.parseInt(e.getFuncData()) % 5] = new Gson()
125 .fromJson(e.getInfo(), Cell.class);
126 } catch (Exception err) {
127 // Fail String to Int
128 }
129 });
130 return boardParsed;
131 }
132
133 /**
134 * Get Player Info
135 *
136 * @return Arraylist of Player Info
137 */
138 public List<Player> getPlayers() {
139 return getCommandList("player").stream().map(e -> new Gson().fromJson(e.getInfo(), Player.class))
140 .collect(Collectors.toList());
141 }
142
143 /**
144 * Get current player username
145 *
146 * @return current player username
147 */
148 public String getCurrentPlayer() {
149 return getCommandList("currentPlayer").stream().map(e -> e.getInfo()).reduce("", (p, e) -> p + e);
150 }
151
152 /**
153 * Get game phase
154 *
155 * @return game phase
156 */
157 public String getGamePhase() {
158 return getCommandList("gamePhase").stream().map(e -> e.getInfo()).reduce("", (p, e) -> p + e);
159 }
160 }