Browse the code
| Revision log Information on the revision | |
|---|---|
| Revision: | 180 (differences) |
| Author: | inouire |
| Log message: | affichage du numéro de version des clients dans l'interface admin |
| Change revision: | |
0
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/* Copyright 2009-2010 Edouard Garnier de Labareyre * * This file is part of B@ggle. * * B@ggle is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * B@ggle is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with B@ggle. If not, see <http://www.gnu.org/licenses/>. */ package threads; import server.Key; import server.Main; import server.Semaphore; import server.Utils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.util.ArrayList; /** * * @author edouard */ public class ClientConnection extends Thread{ public String PLAYER_NAME = ""; public String PLAYER_AVATAR = "0"; public int total_score = 0; public int score = 0; public int state=0; //0:idle, 1: ready, 2:pause, 3:reset private final ArrayList<String> words_found=new ArrayList<String>(); private final Semaphore wfm=new Semaphore();//semaphore for words_found private PrintWriter out; private BufferedReader in; private Socket socket; public final static int max_watchdog=180;//seconds public double time_of_last_action=0; public boolean IS_PLAYER=true; public ClientConnection(Socket s) throws IOException{ out = new PrintWriter(s.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(s.getInputStream())); socket = s; if(Main.clients.isFull()){ send(Key.SERVER_FULL+":"+Main.MAX_NUMBER_OF_PLAYERS); closeStreams(); } this.IS_PLAYER=true; } @Override public void run(){ Utils.printLogIfVerbose("Connection initiated..."); //réception du premier mot //si commence par PING-> répondre au ping //si commence par NEW_PLAYER -> fonctionnement normal //si commence par ADMIN:password -> créer une connection de type administration try { String firstWord = in.readLine(); if(firstWord!=null){ if(firstWord.toUpperCase().startsWith("PING")){ pingRoutine(); } else if(firstWord.toUpperCase().startsWith(Key.NEW_PLAYER.toString())){ playerRoutine(firstWord.substring(firstWord.indexOf(":")+1)); } else if(firstWord.toUpperCase().startsWith("ADMIN:")){ if(firstWord.substring(firstWord.indexOf(":")+1).equals(Main.ADMIN_PASSWORD)){ adminRoutine(); }else{ Utils.printLogIfVerbose("Admin connection failed: wrong password"); } } } }catch(IOException ex){ //do nothing } catch (Exception ex) { ex.printStackTrace(); } finally{ send(Key.DISCONNECTED+":"); closeStreams(); Utils.printLogIfVerbose("Connection closed"); } } private void playerRoutine(String first_word){ Utils.printLogIfVerbose("New player connection"); this.IS_PLAYER=true; //password verification if any if(Main.PASSWORD.length()>0){ try { String password=in.readLine(); if(password != null){ if(!password.equals(Main.PASSWORD)){ send(Key.WRONG_PASSWORD+":"); closeStreams(); return; }else{ send(Key.GOOD_PASSWORD+":"); } } } catch (IOException ex) { closeStreams(); } } //sending informations to this new client Main.clients.sendInitInformationToClient(this,first_word); this.time_of_last_action=System.currentTimeMillis(); String packet; Key key; String message; try { while ((packet = in.readLine()) != null && !packet.equals(Key.DISCONNECTED+":")){ if(packet.contains(":") && packet.length() < Main.MAX_PAQUET_LENGTH){ try{ key = Key.valueOf(packet.substring(0, packet.indexOf(':'))); }catch(Exception ex){ key=null; } if(key!=null){ this.time_of_last_action=System.currentTimeMillis(); message = packet.substring(packet.indexOf(':')+1).trim(); switch (key){ case READY: readyAction(); break; case CHAT: chatAction(message); break; case RESET: resetAction(); break; case WORD: if(!Main.is_playing){ chatAction(message); } wordAction(message.toUpperCase()); break; case STATE: statusChangeAction(message); break; case VERSION: versionActionPlayer(message); break; default: Utils.printLogIfVerbose("Unrecognized message from "+PLAYER_NAME+": "+message); break; } } } } } catch (IOException ex) { Utils.printLogIfVerbose("Brutal deconnection of client "+this.PLAYER_NAME); }finally{ Main.clients.disconnectClient(this); } } /** * Server routine for admin connection */ private void adminRoutine(){ Utils.printLogIfVerbose("New admin connection"); this.IS_PLAYER=false; //sending informations to this new client Main.clients.sendInitInformationToClient(this,""); String packet; Key key; String message; try { while ((packet = in.readLine()) != null && !packet.equals(Key.DISCONNECTED+":")){ if(packet.contains(":") && packet.length() < Main.MAX_PAQUET_LENGTH){ try{ key = Key.valueOf(packet.substring(0, packet.indexOf(':'))); }catch(Exception ex){ key=null; } if(key!=null){ message = packet.substring(packet.indexOf(':')+1).trim(); switch (key){ case CHAT: chatAction(message); break; case VERSION: versionActionAdmin(); break; case GET_PROPERTY: getPropertyActionAdmin(message); break; case SET_PROPERTY: setPropertyActionAdmin(message); break; case KICK: kickActionAdmin(message); break; default: Utils.printLogIfVerbose("Unrecognized message from admin: "+message); break; } } } } }catch (IOException ex) { Utils.printLogIfVerbose("Brutal deconnection of client "+this.PLAYER_NAME); }finally{ Main.clients.disconnectClient(this); } } public void new_player(String message){ String name = message.substring(0, message.indexOf(":")); if(name.length()==0){ name="sans_nom"; } this.PLAYER_NAME = Main.clients.testName(this,name); this.PLAYER_AVATAR = message.substring(message.indexOf(":")+1); try{ int tmp = Integer.parseInt(PLAYER_AVATAR); if(tmp<0 || tmp > 8){ PLAYER_AVATAR="0"; } }catch(Exception e){ PLAYER_AVATAR="0"; } broadcast(Key.NEW_PLAYER+":"+PLAYER_NAME+":"+PLAYER_AVATAR); broadcast(Key.TOTAL_SCORE+":"+PLAYER_NAME+":0"); broadcast(Key.INFO+":"+name+" a rejoint la partie."); Utils.printLog("New player: " + PLAYER_NAME ); } public void closeStreams(){ try { in.close(); out.close(); socket.close(); } catch (IOException ex) { ex.printStackTrace(); } } private void pingRoutine(){ Utils.printLogIfVerbose("PING connection: answering and closing"); send("PING"); RegisterThread.keepRegistering(); } public void addResultsTo(ArrayList<String> all){ try{ wfm.P(false); for(String a:words_found){ if(!all.contains(a)){ all.add(a); } } }catch(Exception ex){ ex.printStackTrace(); }finally{ wfm.V(); } } public String[] getAgregatedResults(ArrayList<String> all){ String s=""; String t=""; int points=0; try{ wfm.P(false); if(!Main.COUNT_ALL_WORDS){//normal mode for(String a: words_found){ if(!all.contains(a)){ s+=":"+a; points+=Utils.getPoints(a); } else t+=":"+a; } }else{//all words count mode String[] sorted= {"","","","","","",""}; for(String a: words_found){ sorted[9-a.length()]+=":"+a; points+=Utils.getPoints(a); } for(String a: sorted){ s+=a; } } }catch(Exception e){ e.printStackTrace(); }finally{ wfm.V(); } score = points; total_score+=score; String[] r = {s,t}; return r; } public int getNumberOfWordsFound(){ int result=0; try{ wfm.P(false); result=words_found.size(); }catch(Exception ex){ ex.printStackTrace(); }finally{ wfm.V(); } return result; } public boolean isReady(){ if(state==1) return true; else return false; } public boolean isActive(){ if(state!=2) return true; else return false; } public boolean wantsToReset(){ if(state==3) return true; else return false; } public void send(String message){ out.println(message); } public void broadcast(String message){ Main.clients.broadcast(message); } public void resetStatus() { if(state!=2){ state=0; } broadcast(Key.STATE+":"+this.PLAYER_NAME+":IDLE"); } private void readyAction() { if(!Main.is_playing){ if(!this.isActive()){ broadcast(Key.STATE+":"+PLAYER_NAME+":PLAY"); } this.state=1; broadcast(Key.READY+":"+PLAYER_NAME); Utils.printLogIfVerbose(PLAYER_NAME + " is ready."); } } private void statusChangeAction(String message) { String status = message.substring(0, message.indexOf(":")); if(status.equals("PAUSE")){ state=2; broadcast(Key.STATE+":"+PLAYER_NAME+":PAUSE"); Utils.printLogIfVerbose(PLAYER_NAME+" is having a break."); } else if(status.equals("PLAY")){ state=0; broadcast(Key.STATE+":"+PLAYER_NAME+":PLAY"); Utils.printLogIfVerbose(PLAYER_NAME+" is back in the game !"); } } private void chatAction(String message) { message = message.trim().replaceAll("/me", PLAYER_NAME); if(message.trim().length()>0){ if(IS_PLAYER){ broadcast(Key.CHAT+":"+PLAYER_NAME+":"+message); Utils.printLogIfVerbose(PLAYER_NAME + " says: "+message); }else{ broadcast(Key.INFO+":"+message); Utils.printLogIfVerbose("Admin says: "+message); } } } private void resetAction() { if(this.isActive() && Main.is_playing){ state=3; broadcast(Key.RESET+":"+PLAYER_NAME); Utils.printLogIfVerbose(PLAYER_NAME + " wants to reset the grid."); } } private void wordAction(String word) { if(Main.is_playing){ if(Utils.isValid(word)){ try{ wfm.P(true); if(!words_found.contains(word)){ send(Key.GOOD+":"+word); Utils.printLogIfVerbose(PLAYER_NAME + " found the word \""+word+"\"."); words_found.add(word); Main.total_number_of_words_found++; if(!this.isActive()){ state=0; broadcast(Key.STATE+":"+PLAYER_NAME+":PLAY"); send(Key.STATE+":GO_TO_PLAY"); Utils.printLogIfVerbose(PLAYER_NAME+" is back in the game !"); } } }catch(Exception e){ e.printStackTrace(); }finally{ wfm.V(); } Main.clients.broadcastGauge(); }else{ send(Key.BAD+":"+word); Utils.printLogIfVerbose(PLAYER_NAME + " found \""+word+"\" but it's false."); } } } /** * Reaction when receiving a VERSION keyword from a player * @param message the version */ private void versionActionPlayer(String message) { Main.clients.broadcastToAdmins(Key.VERSION+":"+this.PLAYER_NAME+":"+message); Utils.printLogIfVerbose(PLAYER_NAME+" version is "+message); } /** * Reaction when receiving a VERSION keyword from an admin */ private void versionActionAdmin() { Main.clients.broadcast(Key.VERSION+":"); Utils.printLogIfVerbose("Admin is asking for client versions"); } /** * Reaction when receiving a KICK keyword from an admin */ private void kickActionAdmin(String message) { Utils.printLog("Kick received from admin about "+message); Main.clients.kickClient(message); } private void getPropertyActionAdmin(String message) { } private void setPropertyActionAdmin(String message) { } public void resetWordsFound() { try{ wfm.P(true); this.words_found.clear(); }catch(Exception ex){ ex.printStackTrace(); }finally{ wfm.V(); } } }

B@ggle