-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatServer.java
More file actions
405 lines (338 loc) · 11.6 KB
/
ChatServer.java
File metadata and controls
405 lines (338 loc) · 11.6 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class ChatServer extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* The port that the server listens on.
*/
private static final int PORT = 8000;
/**
* The set of all names of clients in the chat room. Maintained so that we
* can check that new clients are not registering name already in use.
*/
private static HashSet<String> names = new HashSet<String>();
/**
* The set of all the print writers for all the clients. This set is kept so
* we can easily broadcast messages.
*/
private static HashSet<PrintWriter> writers = new HashSet<PrintWriter>();
private static HashMap<String, Player> userMap = new HashMap<>();
private static ArrayList<Player> playerList = new ArrayList<>();
private static int count, playerCount, waitingCount;
private static JTextArea outputArea;
private static Player[] players = new Player[2];
private static Player[] waitingPlayers = new Player[2];
private static ExecutorService runGame;
private static Lock gameLock;
private static Condition otherPlayerConnected;
public ChatServer() {
} // no arg default constructor
/**
* A handler thread class. Handlers are spawned from the listening loop and
* are responsible for a dealing with a single client and broadcasting its
* messages.
*/
static class Handler extends Thread {
private String name;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
/**
* Constructs a handler thread
*
*/
public Handler(Socket socket) {
this.socket = socket;
}
/**
* Waits for a unique screen name, registers the client and handles
* messages
*/
public void run() {
try {
// Create character streams for the socket.
in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
playerCount = 0;
waitingCount = 0;
// Continually request a user name until a unique name is received
while (true) {
// out.println("SUBMITNAME");
name = in.readLine();
if (name.equals("null") || name.isEmpty()) {
name = "anonymous" + count++;
}
synchronized (names) {
if (!names.contains(name)) {
names.add(name);
for (PrintWriter writer : writers) {
if (writer != out) {
writer.println("MESSAGE " + name
+ " has joined the server");
}
}
break;
}
}
}
// Now that a unique name has been registered, add the socket's print writer to the set of all writers so this client can receive broadcast messages.
out.println("NAMEACCEPTED");
Player player = new Player(name, out);
player.setPort(socket.getPort());
player.setAddress(socket.getInetAddress());
userMap.put(player.getPlayerName(), player);
playerList.add(player);
writers.add(out);
for (PrintWriter writer : writers) {
for (String s : names) {
writer.println("UPDATE " + s);
}
}
// Accept messages from this client and broadcast them. Ignore other clients that cannot be broadcasted to.
String input;
while (true) {
while ((input = in.readLine()) == null) {
}
;
// process a request to challenge another player head 2 head
// TODO add more games than Tetris
if (input.startsWith("CHALLENGE")) {
String challenger = input.substring(10);
players[playerCount] = userMap.get(challenger);
playerCount++;
if (playerCount == players.length) {
for (int i = 0; i < players.length; i++) {
out = players[i].getWriter();
out.println("TETRIS");
}
playerCount = 0;
players = new Player[2];
}
/*
* wait for both players to finish their games and
* report to each the other's score and the winner
*/
} else if (input.startsWith("GETSCORE")) {
String getPlayer = input.substring(8);
Player curP = userMap.get(getPlayer);
waitingPlayers[waitingCount] = curP;
waitingCount++;
if (waitingCount >= waitingPlayers.length) {
PrintWriter pzero = waitingPlayers[0].getWriter();
int scorezero = waitingPlayers[0]
.getScore("TETRIS");
String namezero = waitingPlayers[0].getPlayerName();
PrintWriter pone = waitingPlayers[1].getWriter();
int scoreone = waitingPlayers[1].getScore("TETRIS");
String nameone = waitingPlayers[1].getPlayerName();
String game = "TETRIS";
int oldscorezero = userMap.get(namezero)
.getScore(game);
int oldscoreone = userMap.get(nameone)
.getScore(game);
if (scorezero > oldscorezero) {
userMap.get(namezero).addScore(scorezero,
"TETRIS");
}
if (scoreone > oldscoreone) {
userMap.get(nameone).addScore(scoreone,
"TETRIS");
}
writeScores(userMap);
System.out.println("FROM USERMAP\n"
+ userMap.get(nameone).getPlayerName()
+ " high score for tetris "
+ userMap.get(nameone).getScore("TETRIS"));
System.out.println("FROM USERMAP\n"
+ userMap.get(namezero).getPlayerName()
+ " high score for tetris "
+ userMap.get(namezero).getScore("TETRIS"));
if (userMap.get(namezero)
.getScore("TETRIS") < scorezero)
System.out.println("Writing " + namezero
+ " scores to file");
if (userMap.get(nameone)
.getScore("TETRIS") < scoreone)
System.out.println("Writing " + nameone
+ " scores to file");
writeScores(userMap);
if (scorezero > scoreone) {
pzero.println("\nYOU WIN! Your score: "
+ scorezero + "\n" + nameone + ": "
+ scoreone);
pone.println("\nYOU LOSE! Your score: "
+ scoreone + "\n" + namezero + ": "
+ scorezero);
} else if (scorezero < scoreone) {
pzero.println("\nYOU LOSE! Your score: "
+ scorezero + "\n" + nameone + ": "
+ scoreone);
pone.println("\nYOU WIN! Your score: "
+ scoreone + "\n" + namezero + ": "
+ scorezero);
} else {
pzero.println("\nYOU TIE!\n Your score: "
+ scorezero + "\n" + nameone + ": "
+ scoreone);
pone.println("\nYOU TIE!\n Your score: "
+ scoreone + "\n" + namezero + ": "
+ scorezero);
}
waitingCount = 0;
waitingPlayers = new Player[2];
}
} else if (input.startsWith("REPORT")) {
int delim = input.indexOf(":");
int delim2 = input.lastIndexOf(":");
String getPlayer = input.substring(delim + 1, delim2);
String scoreString = input.substring(delim2 + 1);
int score = Integer.valueOf(scoreString);
if (userMap.containsKey(getPlayer)) {
PrintWriter pw = userMap.get(getPlayer).getWriter();
pw.println("REPORT:" + score + ":" + getPlayer);
}
} else if (input.startsWith("DM")) {
int index = input.indexOf(":");
String name = input.substring(2, index);
System.err.println("name: " + name);
int index2 = input.lastIndexOf(":");
String message = input.substring(index + 1, index2);
String user = input.substring(index2 + 1);
System.err.println("User: " + user);
System.err.println(userMap);
System.out.println(
"SERVER: DM to " + name + "from user " + user);
if(userMap.get(user)!= null) {
PrintWriter writer = userMap.get(user).getWriter();
writer.println("DM from " + user + ": " + message);
continue;
}else {
System.err.println(user + " does not exist in map");
}
} else if (input.startsWith("EXIT")) {
String exiting = input.substring(4);
names.remove(exiting);
for (PrintWriter writer : writers) {
writer.println("REMOVE " + exiting);
writer.println(exiting + " left the server");
userMap.remove(exiting);
}
} else if (input.startsWith("TETRIS")) {
int index = input.indexOf(":") + 1;
int index2 = input.lastIndexOf(":");
String game = "TETRIS";
String name = input.substring(index, index2);
String score = input.substring(index2 + 1);
Player curPlayer = userMap.get(name);
curPlayer.addScore(Integer.valueOf(score), game);
} else if (input.startsWith("MESSAGE")) {
input = input.substring(7);
for (PrintWriter writer : writers) {
writer.println("MESSAGE " + name + ": " + input);
}
} else {
for (String s : names) {
out.println("UPDATE " + s);
}
}
}
} catch (
IOException e) {
System.out.println(e);
} finally {
// This client is going down! Remove its name and its print
// writer from the sets, and close its socket.
if (name != null) {
names.remove(name);
}
if (out != null) {
writers.remove(out);
}
try {
socket.close();
} catch (IOException e) {
}
}
}
}
protected static void writeScores(HashMap<String, Player> map) {
File file = new File("HighScores.txt");
try {
PrintWriter pw = new PrintWriter(file);
map.forEach((key, value) -> pw
.println(key + ":" + "TETRIS: " + value.getScore("TETRIS")));
map.forEach((key, value) -> System.out
.println(key + ":" + "TETRIS: " + value.getScore("TETRIS")));
pw.close();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
}
}
protected static void readScores() {
File file = new File("HighScores.txt");
String line = "";
String paragraph = "";
String name, game, score;
if (file.exists()) {
try {
Scanner in = new Scanner(file);
while (in.hasNextLine()) {
line = in.nextLine();
paragraph += line + "\n";
int markone = line.indexOf(":");
int marktwo = line.lastIndexOf(":");
name = line.substring(0, markone);
game = line.substring(markone + 1, marktwo);
score = line.substring(marktwo + 1);
int scoreint = Integer.valueOf(score);
System.out.println("name: " + name);
System.out.println("game: " + game);
System.out.println("score: " + scoreint);
}
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception {
System.out.println("Game Hub server running.");
count = 1;
/* will start Tetris game server here for multi-player */
runGame = Executors.newFixedThreadPool(2);
gameLock = new ReentrantLock();
otherPlayerConnected = gameLock.newCondition();
players = new Player[2];
ChatServer chat = new ChatServer();
ServerSocket listener = new ServerSocket(PORT);
try {
while (true) {
new Handler(listener.accept()).start();
}
} finally {
listener.close();
}
}
}