-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessageInterpreter.java
227 lines (203 loc) · 9.92 KB
/
MessageInterpreter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package backend.interpreter;
import backend.GameSingleton;
import backend.socketing.MessageQueueSingleton;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import model.exceptions.CannotAddPlayerException;
import model.exceptions.MoveNotAllowedException;
import model.exceptions.NoSuchPieceException;
import model.exceptions.NoSuchPlayerException;
import model.player.Piece;
import model.player.PiecePosition;
import model.player.Player;
/***
* Modifies state of game in the GameSingleton and adds proper messages to message Queue.
*/
public class MessageInterpreter {
/***
* Takes a message and interprets it.
* @param message Message to be interpreted.
*/
public static void interpret(String message) {
String type = new JsonParser().parse(message).getAsJsonObject().get("type").getAsString();
/*
* message is a JSON
* {
* "type": "new-client" | "move",
* "content": ... ,
* "to": "all" | id
* }
*/
switch (type) {
case "new-client": {
//content - String: nickname
String nick = new JsonParser().parse(message).getAsJsonObject().get("content").getAsString();
Player player = new Player(nick);
int id = -1;
try {
id = GameSingleton.getGame().addPlayer(player);
} catch (CannotAddPlayerException e) {
e.printStackTrace();
}
GameSingleton.getGame().createArmy(player);
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("type", "new-client");
JsonArray arr = new JsonArray();
arr.add(nick);
arr.add(id);
jsonObject.add("content", arr);
jsonObject.addProperty("to", "all");
MessageQueueSingleton.getMessages().add(jsonObject.toString());
break;
}
case "move": {
JsonArray arr = new JsonParser().parse(message).getAsJsonObject().get("content").getAsJsonArray();
int from = new JsonParser().parse(message).getAsJsonObject().get("from").getAsInt();
int[] moveOld = new int[2];
int[] moveNew = new int[2];
moveOld[0] = arr.get(0).getAsJsonArray().get(0).getAsInt();
moveOld[1] = arr.get(0).getAsJsonArray().get(1).getAsInt();
moveNew[0] = arr.get(1).getAsJsonArray().get(0).getAsInt();
moveNew[1] = arr.get(1).getAsJsonArray().get(1).getAsInt();
try {
Piece piece;
try {
System.out.println("Trying to move from " + moveOld[0] + ", " + moveOld[1] + " by player " + from);
piece = GameSingleton.getGame().getPlayerById(from).getArmy().getPieceByPosition(new PiecePosition(moveOld[0], moveOld[1]));
} catch (NoSuchPieceException e) {
e.printStackTrace();
return;
}
GameSingleton.getGame().getBoardMovementInterface().makeMove(piece, new PiecePosition(moveNew[0], moveNew[1]));
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("type", "make-move");
jsonObject.add("content", new JsonParser().parse(message).getAsJsonObject().get("content").getAsJsonArray());
jsonObject.addProperty("to", "all");
MessageQueueSingleton.getMessages().add(jsonObject.toString());
} catch (NoSuchPlayerException e) {
e.printStackTrace();
} catch (MoveNotAllowedException e) {
//todo add WRONG MOVE here
e.printStackTrace();
}
//done? i think so...
//CHECKING IF SOMEONE WON AFTER THIS MOVE and adding a msg to queue about it
for (Player player : GameSingleton.getGame().getPlayers()) {
if (GameSingleton.getGame().hasWon(player) && !GameSingleton.getWinners().contains(player)) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("type", "won");
jsonObject.addProperty("content", from);
jsonObject.addProperty("to", "all");
try {
GameSingleton.getWinners().add(GameSingleton.getGame().getPlayerById(from));
} catch (NoSuchPlayerException e) {
e.printStackTrace();
}
MessageQueueSingleton.getMessages().add(jsonObject.toString());
}
}
if (GameSingleton.getWinners().size() == GameSingleton.getGame().getLimit() - 1) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("type", "end");
jsonObject.addProperty("content", "");
jsonObject.addProperty("to", "all");
MessageQueueSingleton.getMessages().add(jsonObject.toString());
}
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("type", "next-turn");
switch (GameSingleton.getGame().getLimit()) {
case 2: {
switch (from) {
case 1: {
jsonObject.addProperty("content", 4);
GameSingleton.getGame().setTurn(4);
break;
}
case 4: {
jsonObject.addProperty("content", 1);
GameSingleton.getGame().setTurn(1);
break;
}
}
break;
}
case 4: {
switch (from) {
case 1: {
jsonObject.addProperty("content", 2);
GameSingleton.getGame().setTurn(2);
break;
}
case 2: {
jsonObject.addProperty("content", 4);
GameSingleton.getGame().setTurn(4);
break;
}
case 4: {
jsonObject.addProperty("content", 5);
GameSingleton.getGame().setTurn(5);
break;
}
case 5: {
jsonObject.addProperty("content", 1);
GameSingleton.getGame().setTurn(1);
break;
}
}
break;
}
case 6: {
jsonObject.addProperty("content", from % 6 + 1);
break;
}
}
jsonObject.addProperty("to", "all");
MessageQueueSingleton.getMessages().add(jsonObject.toString());
//when some players already won, the rest can continue
//we have to skip players that won
int turn = GameSingleton.getGame().getTurn();
try {
if (GameSingleton.getWinners().contains(GameSingleton.getGame().getPlayerById(turn))) {
//probably correct
while (GameSingleton.getWinners().contains(GameSingleton.getGame().getPlayerById(turn)) ||
!GameSingleton.getGame().getPlayers().contains(GameSingleton.getGame().getPlayerById(turn))) {
turn = turn % 6 + 1;
}
GameSingleton.getGame().setTurn(turn);
jsonObject = new JsonObject();
jsonObject.addProperty("type", "next-turn");
jsonObject.addProperty("content", turn);
jsonObject.addProperty("to", "all");
MessageQueueSingleton.getMessages().add(jsonObject.toString());
}
} catch (NoSuchPlayerException e) {
e.printStackTrace();
}
break;
}
case "ready": {
int from = new JsonParser().parse(message).getAsJsonObject().get("from").getAsInt();
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("type", "ready");
jsonObject.addProperty("content", from);
jsonObject.addProperty("to", "all");
MessageQueueSingleton.getMessages().add(jsonObject.toString());
if (GameSingleton.readyPlayer(from)) {
JsonObject start = new JsonObject();
start.addProperty("type", "start-game");
start.addProperty("content", "");
start.addProperty("to", "all");
GameSingleton.getGame().setTurn(1);
JsonObject turn = new JsonObject();
turn.addProperty("type", "next-turn");
turn.addProperty("content", 1);
turn.addProperty("to", "all");
MessageQueueSingleton.getMessages().add(start.toString());
MessageQueueSingleton.getMessages().add(turn.toString());
}
break;
}
}
}
}