-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProxyController.java
More file actions
232 lines (210 loc) · 8.21 KB
/
ProxyController.java
File metadata and controls
232 lines (210 loc) · 8.21 KB
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
228
229
230
231
232
package cs3500.pa04;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import cs3500.pa04.JSON.FleetJson;
import cs3500.pa04.JSON.JoinJson;
import cs3500.pa04.JSON.JsonUtils;
import cs3500.pa04.JSON.MessageJson;
import cs3500.pa04.JSON.SetupJson;
import cs3500.pa04.JSON.ShipJson;
import cs3500.pa04.JSON.VolleyJson;
import cs3500.pa03.Model.Coord;
import cs3500.pa03.Model.GameBoard;
import cs3500.pa03.Model.GameResult;
import cs3500.pa03.Model.HitOrMiss;
import cs3500.pa03.Model.Ship;
import cs3500.pa03.Model.ShipType;
import cs3500.pa03.Model.Player;
import cs3500.pa03.View.PrintBoard;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Represents communication between the server and client
*/
public class ProxyController {
private final Socket server;
private final InputStream in;
private final PrintStream out;
private final Player player;
private final ObjectMapper mapper = new ObjectMapper();
private static final JsonNode VOID_RESPONSE =
new ObjectMapper().getNodeFactory().textNode("void");
HitOrMiss hm = new HitOrMiss();
int width;
int height;
List<Ship> fleet;
PrintBoard pb = new PrintBoard();
public ProxyController(Socket server, Player player) throws IOException {
this.server = server;
this.in = server.getInputStream();
this.out = new PrintStream(server.getOutputStream());
this.player = player;
this.fleet = fleet;
}
/**
* overall methods that runs everything
*/
public void run() {
try {
JsonParser parser = this.mapper.getFactory().createParser(this.in);
while (!this.server.isClosed()) {
MessageJson message = parser.readValueAs(MessageJson.class);
delegateMessage(message);
}
} catch (IOException e) {
}
}
/**
* @param methodName
* @throws JsonProcessingException //delegates to other methods based on the method name given
*/
private void delegateMessage(MessageJson methodName)
throws JsonProcessingException {
String name = methodName.method();
JsonNode args = methodName.args();
if (name.equals("join")) {
handleJoin();
} else if (name.equals("setup")) {
handleSetup(mapper.convertValue(args, SetupJson.class));
} else if (name.equals("take-shots")) {
handleTakingShots();
} else if (name.equals("report-damage")) {
handleReportingDamage(args);
} else if (name.equals("successful-hits")) {
handleSuccessfulHits(args);
} else if (name.equals("end-game")) {
handleEndingGame(args);
} else {
throw new IllegalArgumentException("name does not exist");
}
}
/**
* @throws JsonProcessingException //represents the Join method communication
*/
private void handleJoin() throws JsonProcessingException {
JoinJson joinGame = new JoinJson(player.name(), "SINGLE");
JsonNode joinServer = JsonUtils.serializeRecord(joinGame);
MessageJson join = new MessageJson("join", joinServer);
JsonNode joins = JsonUtils.serializeRecord(join);
System.out.println(mapper.writeValueAsString(joins)); // delete all the exceptions
this.out.println(joins);
}
/**
* @param args -> arguments server asks for
* @throws JsonProcessingException //represents SetUp method communcation
*/
private void handleSetup(SetupJson args) throws JsonProcessingException {
this.width = args.width();
this.height = args.height();
System.out.println(width);
System.out.println(height);
Map<ShipType, Integer> fleetSpec = args.fleetSpec();
System.out.println(fleetSpec.toString());
this.fleet = player.setup(this.height, this.width, fleetSpec);
GameBoard aiBoard = new GameBoard(this.height, this.width);
for (Ship ship : this.fleet) {
aiBoard.placeShip(ship.getShipType(), ship.getDirection());
}
pb.printBoard(aiBoard, "Board:");
List<Ship> aiShipList = aiBoard.getShipsPlace();
List<ShipJson> fleetJson = new ArrayList<>();
for (Ship ship : aiBoard.getShipsPlace()) {
fleetJson.add(new ShipJson(ship.getFirstCoord(), ship.getShipLength(), ship.getDirection()));
}
this.fleet = aiShipList;
FleetJson fleetJsonObj = new FleetJson(fleetJson);
JsonNode fleetServer = JsonUtils.serializeRecord(fleetJsonObj);
MessageJson setup = new MessageJson("setup", fleetServer);
JsonNode setups = JsonUtils.serializeRecord(setup);
System.out.println(mapper.writeValueAsString(setups));
String finalSetup = mapper.writeValueAsString(setups);
this.out.println(finalSetup);
}
/**
* @throws JsonProcessingException //represents TakeShots method communication
*/
private void handleTakingShots() throws JsonProcessingException {
VolleyJson shots = new VolleyJson(player.takeShots());
JsonNode shotsServer = JsonUtils.serializeRecord(shots);
MessageJson shotsMessage = new MessageJson("take-shots", shotsServer);
JsonNode shotsMessages = JsonUtils.serializeRecord(shotsMessage);
// System.out.println(mapper.writeValueAsString(shotsMessages));
String finalShots = mapper.writeValueAsString(shotsMessages);
this.out.println(finalShots);
}
/**
* @param args arguments server asks for
* @throws JsonProcessingException // represents report damage method communication
*/
private void handleReportingDamage(JsonNode args) throws JsonProcessingException {
System.out.println("SLAYYYY");
JsonNode coords = args.get("coordinates");
List<Coord> targetCoords = new ArrayList<>();
if (coords.isArray()) {
for (JsonNode coord : coords) {
int x = coord.get("x").asInt();
int y = coord.get("y").asInt();
Coord targetCoord = new Coord(x, y);
targetCoords.add(targetCoord);
}
}
List<Coord> shotsTaken = player.reportDamage(hm.AiServerBoardHits(targetCoords, this.fleet));
ObjectNode damage = mapper.createObjectNode();
ArrayNode damageArray = damage.putArray("coordinates");
for (Coord coord : shotsTaken) {
damageArray.add(JsonUtils.serializeRecord(coord));
}
MessageJson damageMessage = new MessageJson("report-damage", damage);
String finalDamage = mapper.writeValueAsString(damageMessage);
System.out.println(finalDamage.toCharArray());
this.out.println(finalDamage);
}
/**
* @param args arguments server asks for
* @throws JsonProcessingException // represents successfulHits method communication
*/
private void handleSuccessfulHits(JsonNode args) throws JsonProcessingException {
JsonNode sHits = args.get("coordinates");
List<Coord> successfulHits = new ArrayList<>();
if (sHits.isArray()) {
for (JsonNode coord : sHits) {
int x = coord.get("x").asInt();
int y = coord.get("y").asInt();
Coord targetCoord = new Coord(x, y);
successfulHits.add(targetCoord);
}
}
System.out.println("HERE");
player.successfulHits(successfulHits);
MessageJson successfulHitsMessage = new MessageJson("successful-hits", VOID_RESPONSE);
String finalSuccessfulHits = mapper.writeValueAsString(successfulHitsMessage);
System.out.println("RIGHT" + finalSuccessfulHits);
this.out.println(finalSuccessfulHits);
}
/**
* @param args arguments server asks for
* @throws JsonProcessingException // represents endgame method communication
*/
private void handleEndingGame(JsonNode args) {
String JsonResult = args.get("result").asText();
String reason = args.get("reason").asText();
GameResult result = null;
if (JsonResult.equals("WON")) {
result = GameResult.WIN;
} else if (JsonResult.equals("LOST")) {
result = GameResult.LOSE;
} else if (JsonResult.equals("TIE")) {
result = GameResult.TIE;
}
player.endGame(result, reason);
}
}