Repository navigation
Expand file tree
/
Copy pathClientHandler.java
More file actions
195 lines (165 loc) · 5.45 KB
/
Copy pathClientHandler.java
File metadata and controls
195 lines (165 loc) · 5.45 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
package networking.server;
import logic.utils.Card;
import logic.utils.players.Player;
import networking.protocol.Command;
import networking.protocol.Error;
import networking.protocol.Handler;
import java.io.*;
import java.net.Socket;
import java.util.regex.Pattern;
public class ClientHandler extends Handler {
private final Socket socket;
private Player player;
public ClientHandler(Socket socket) {
this.socket = socket;
init(socket);
}
@Override
public void run() {
try {
String message;
while ((message = inputReader.readLine()) != null) {
handleCommand(message);
}
} catch (IOException ignored) {}
shutdown();
}
private void init(Socket socket) {
try {
inputReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
outputWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
} catch (IOException e) {
shutdown();
}
}
private void shutdown() {
try {
inputReader.close();
outputWriter.close();
socket.close();
} catch (IOException e){
e.printStackTrace();
}
ServerGame.removeClient(this);
}
public Player getPlayer() {
return player;
}
/**
* Handles the "Hello" command received from the client.
*
* @param handler The handler object.
* @param args The name the client wishes to have.
*/
public static void handleHello(Handler handler, String... args) {
if (!(handler instanceof ClientHandler)) return;
ClientHandler client = (ClientHandler) handler;
if (client.player != null) {
client.sendError(Error.E1);
return;
}
String name = args[0];
Pattern pattern = Pattern.compile("^[a-zA-Z0-9]{1,12}$"); // check length and validity of characters
if (!pattern.asMatchPredicate().test(name) || !ServerGame.isNameValid(name)) {
client.sendError(Error.E2);
return;
}
client.player = new Player(name);
client.sendCommand(Command.WELCOME, name);
System.out.println("A new player has been created: " + name);
ServerGame.addPlayer();
}
/**
* Handles the "Exit" command by shutting down the client connection.
*
* @param handler The handler object.
* @param args None.
*/
public static void handleExit(Handler handler, String... args) {
if (!(handler instanceof ClientHandler)) return;
ClientHandler client = (ClientHandler) handler;
client.shutdown();
}
/**
* Handles the "Move" command received from the client.
*
* @param handler The handler object.
* @param args The card to play;
*/
public static void handleMove(Handler handler, String... args) {
if (!(handler instanceof ClientHandler)) return;
ClientHandler client = (ClientHandler) handler;
if (ServerGame.hasEnded()) {
client.sendError(Error.E8);
return;
}
Card card = Card.valueOf(args[0]);
ServerGame.getGame().doMove(client, card);
}
/**
* Handles the "Target" command received from the client.
*
* @param handler The handler object.
* @param args The target name;
*/
public static void handleTarget(Handler handler, String... args) {
if (!(handler instanceof ClientHandler)) return;
ClientHandler client = (ClientHandler) handler;
if (ServerGame.hasEnded()) {
client.sendError(Error.E8);
return;
}
ServerGame.getGame().chooseTarget(client, args[0]);
}
/**
* Handles the "Give" command received from the client.
*
* @param handler The handler object.
* @param args The card to give.
*/
public static void handleGive(Handler handler, String... args) {
if (!(handler instanceof ClientHandler)) return;
ClientHandler client = (ClientHandler) handler;
if (ServerGame.hasEnded()) {
client.sendError(Error.E8);
return;
}
Card card = Card.valueOf(args[0]);
ServerGame.getGame().giveCard(client, card);
}
/**
*
* Handles the "Place" command received from the client.
*
* @param handler The handler object.
* @param args The index to put the card at
*/
public static void handlePlace(Handler handler, String... args) {
if (!(handler instanceof ClientHandler)) return;
ClientHandler client = (ClientHandler) handler;
if (ServerGame.hasEnded()) {
client.sendError(Error.E8);
return;
}
try {
ServerGame.getGame().place(client, Card.EXPLODING_KITTEN, Integer.parseInt(args[0]));
} catch (NumberFormatException ignore) {
client.sendError(Error.E3);
}
}
/**
* Handles the "Draw" command received from the client.
*
* @param handler The handler object.
* @param args None.
*/
public static void handleDraw(Handler handler, String... args) {
if (!(handler instanceof ClientHandler)) return;
ClientHandler client = (ClientHandler) handler;
if (ServerGame.hasEnded()) {
client.sendError(Error.E8);
return;
}
ServerGame.getGame().drawCard(client);
}
}