Repository navigation
Expand file tree
/
Copy pathServerHandler.java
More file actions
208 lines (190 loc) · 7.31 KB
/
Copy pathServerHandler.java
File metadata and controls
208 lines (190 loc) · 7.31 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
package networking.client;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Arrays;
import static networking.server.ServerGame.PORT;
import logic.utils.players.ClientPlayer;
import networking.protocol.Command;
import networking.protocol.Error;
import networking.protocol.Handler;
public class ServerHandler extends Handler {
@Override
public void run() {
try {
InetAddress address = null;
while (address == null) {
try {
String input = ClientGame.getUserInput("Enter a valid ip for the server or \".\" for localhost");
if (input.equals(".")) {
input = "127.0.0.1";
ClientGame.print("127.0.0.1");
}
address = InetAddress.getByName(input);
} catch (UnknownHostException e) {
System.out.println("Unknown host, please retry.");
}
}
init(address);
while (ClientGame.getPlayer() == null) {
sendCommand(Command.HELLO, ClientGame.getUserInput("Enter your name:"));
String text;
while (true) {
text = inputReader.readLine();
handleCommand(text);
if (text.startsWith(Command.WELCOME.toString()) || Error.fromString(text) != null) break;
}
}
ClientGame.doneInit();
String text;
while ((text = inputReader.readLine()) != null) {
try {
handleCommand(text);
} catch (IllegalArgumentException e) {
ClientGame.print(e.getMessage());
ClientGame.print("message was: " + text);
sendError(Error.E7);
}
}
} catch (IOException e) {
System.out.println("The connection was closed: " + e.getCause());
System.exit(0);
}
}
/**
* Initializes a socket connection to the given address.
*
* @param address The InetAddress of the server to connect to.
*/
protected void init(InetAddress address) {
Socket socket = null;
while (socket == null) {
try {
System.out.println("Attempting to connect to " + address + ":" + PORT + "...");
socket = new Socket(address, PORT);
inputReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
outputWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
System.out.println("Connection established!");
} catch (IOException e) {
System.out.println("Could not create a socket on " + address + ":" + PORT);
}
}
}
/**
* Handles the "Welcome" command received from the server.
* Sets the name of the client game.
*
* @param handler The handler for the command.
* @param args The authorized player name.
*/
public static void handleWelcome(Handler handler, String... args) {
ClientGame.setName(args[0]);
System.out.println("Welcome: " + args[0]);
}
/**
* Handles a "notify" command received from the server.
*
* @param handler The handler for the command.
* @param args The message sent
*/
public static void handleNotify(Handler handler, String... args) {
ClientGame.print(args[0]);
}
/**
* Handles the next turn in the game. If it's the player's turn, it takes the turn and notifies the player
* of their options. Otherwise, it prints a message indicating whose turn it is.
*
* @param handler The handler for the command.
* @param args Firstly the last player, secondly the current player.
*/
public static void handleNext(Handler handler, String... args) {
ClientPlayer player = ClientGame.getPlayer();
ClientGame.displayGame();
if (player.getName().equals(args[1])) {
player
.takeTurn()
.thenAcceptAsync(card -> {
if (card == null) handler.sendCommand(Command.CARD);
else handler.sendCommand(Command.MOVE, card.name());
});
} else {
player.endTurn();
ClientGame.print("It is now " + args[1] + "'s turn");
}
}
/**
* Handles the "player's" information received from the server.
*
* @param handler The handler for the command.
* @param args Firstly the cards, secondly the top card, thirdly pile size.
*/
public static void handlePlayer(Handler handler, String... args) {
ClientGame.getPlayer().setCards(args[0]);
ClientGame.setLastCardPlayed(args[1]);
ClientGame.setPileSize(args[2]);
}
/**
* Handles a "hand" command received from the server.
*
* @param handler The handler for the command.
* @param args The list of targets.
*/
public static void handleHand(Handler handler, String... args) {
ClientGame.getPlayer()
.choosePlayer(Arrays.asList(args[0].split(", ")))
.thenAcceptAsync( player -> handler.sendCommand(Command.TARGET, player) );
}
/**
* Handles "demand" command received from the server. It prompts the user to choose a card to give to another player.
*
* @param handler The Handler instance for the command.
* @param args The name of the player to give the card to.
*/
public static void handleDemand(Handler handler, String... args) {
ClientGame.displayGame();
ClientGame.getPlayer()
.chooseCard()
.thenAcceptAsync( card -> handler.sendCommand(Command.GIVE, card.name()) );
}
/**
* Handles the execution of a move. Prints a success message.
*
* @param handler The handler for the move.
* @param args None.
*/
public static void handleExecuted(Handler handler, String... args) {
ClientGame.print("Your move has been executed");
ClientPlayer player = ClientGame.getPlayer();
if (player.canPlay()) {
ClientGame.displayGame();
player
.takeTurn()
.thenAcceptAsync(card -> {
if (card == null) handler.sendCommand(Command.CARD);
else handler.sendCommand(Command.MOVE, card.name());
});
} else player.confirmMove();
}
/**
* Handles the game over event. This method is called when the game is over and the player has lost.
*
* @param handler The handler for the command. It is not used in this method.
* @param args None.
*/
public static void handleGameOver(Handler handler, String... args) {
ClientGame.getPlayer().stop();
}
/**
* Handles an "explode" command received from the server.
*
* @param handler The handler for the command.
* @param args None.
*/
public static void handleExplode(Handler handler, String... args) {
ClientGame.displayGame();
ClientGame.getPlayer()
.choosePosition()
.thenAcceptAsync(position -> handler.sendCommand(Command.PLACE, position + ""));
}
}