-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerThread.java
355 lines (304 loc) · 10.6 KB
/
ServerThread.java
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
import java.io.*;
import java.net.Socket;
import java.util.*;
public class ServerThread implements Runnable {
private String WORDS_FILE;
private Socket socket;
private Sharer sharer;
private String clientUsername;
private String secretWord;
private int attemptsLeft;
private boolean isAuthenticated = false;
private boolean hasAskedForGameInit = false;
private boolean hasWin = false;
private boolean sharedRes = false;
private Database DB;
//ArrayList in cui vengono inseriti i tentativi fatti codificati in stringhe che compongono una notifica inviabile
private List<String> attempts_list_for_notification = new ArrayList<>();
ServerThread (Socket socket, Sharer sharer, Database database, String wordsFile) {
this.socket = socket;
this.sharer = sharer;
this.secretWord = WordleServerMain.getSecretWord();
this.DB = database;
this.WORDS_FILE = wordsFile;
}
public void run () {
Scanner in;
PrintWriter out;
printl("Client connected");
try {
in = new Scanner(socket.getInputStream());
out = new PrintWriter(socket.getOutputStream(), true);
} catch (IOException e) { printl("Unable to create comunication with client. Please, try again later."); return; }
while (in.hasNextLine()) {
String line = in.nextLine();
String[] content = line.trim().split(";");
String cmd = content[0];
if (cmd.equals("REGISTER")) {
serveRegister(out, content[1], content[2]);
}
else if (cmd.equals("LOGIN")) {
serveLogin(out, content[1], content[2]);
}
else if (cmd.equals("LOGOUT")) {
serveLogout(out);
}
else if (cmd.equals("PLAYWORDLE")) {
startWordle(out);
}
else if (cmd.equals("GUESSWORD")) {
guessWord(out, content[1]);
}
else if (cmd.equals("SENDMESTATISTICS")) {
sendMeStatistics(out);
}
else if (cmd.equals("SHARE")) {
share(out);
}
else if (cmd.equals("EXIT")) {
in.close();
out.close();
return;
}
else printl("Error Server");
}
}
private void printl(String s) { System.out.println(s); }
private void serveRegister (PrintWriter out, String username, String password) {
User user = this.DB.findOne(username);
if (isAuthenticated) {
out.println(403);
return;
}
if (user == null) {
// Utente non presente nel database
this.DB.insertOne(new User(username, password));
clientUsername = username;
out.println(200);
isAuthenticated = true;
}
else {
// Un utente già registrato non può registrarsi di nuovo!
out.println(409);
}
}
private void serveLogin (PrintWriter out, String username, String password) {
User user = this.DB.findOne(username);
if (isAuthenticated) {
out.println(403);
return;
}
if (user == null) {
// Non esiste
out.println(409);
return;
}
if (password.equals(user.getPassword())) {
clientUsername = username;
out.println(200);
isAuthenticated = true;
}
else {
out.println(401);
}
}
private void serveLogout (PrintWriter out) {
isAuthenticated = false;
hasAskedForGameInit = false;
hasWin = false;
User user = this.DB.findOne(clientUsername);
user.setHasPlayed(true); //"ogni tentativo di indovinare la parola si ritiene concluso se l'utente chiede il logout"
this.DB.updateOne(user);
out.println(200);
}
private void startWordle (PrintWriter out) {
if (!isAuthenticated) {
out.println(403);
return;
}
if (hasAskedForGameInit) {
out.println(202);
return;
}
User user = this.DB.findOne(clientUsername);
if (user.getHasPlayed()) {
out.println(409);
return;
}
//Non ha già giocato e quindi può giocare.
hasAskedForGameInit = true;
user.setHasPlayed(true);
attemptsLeft = 12;
user.incrementPlayed();
this.DB.updateOne(user);
out.println(200);
}
private void guessWord (PrintWriter out, String providedWord) {
if (!hasAskedForGameInit) {
out.println(400);
return;
}
if (!isAuthenticated) {
out.println(403);
return;
}
checkSecretWordChanges(); //nel caso la parola cambiasse quando l'utente sta giocando
if (attemptsLeft == 0) {
out.println(405);
return;
}
if (hasWin) {
out.println(406);
return;
}
User user = this.DB.findOne(clientUsername);
if (providedWord.equals(secretWord)) {
out.println(200);
user.incrementWon();
if (user.getLastResult() == 1 || user.getLastResult() == -1) {
user.incrementLastStreak();
} else if (user.getLastResult() == 0) {
user.resetLastStreak();
}
if (user.getLastStreak() >= user.getBestStreak()) {
user.setBestStreak(user.getLastStreak());
}
user.setLastResult(1);
attemptsLeft--;
user.guessDistributionAdd(12-attemptsLeft);
this.DB.updateOne(user);
hasWin = true;
}
else {
if (isWordPresent(providedWord)) {
out.println(203);
String tipsWord = generateTipsWord(providedWord);
out.println(tipsWord);
attemptsLeft--;
if (attemptsLeft == 0) {
user.setLastResult(0);
}
attempts_list_for_notification.add(tipsWord);
this.DB.updateOne(user);
}
else {
out.println(202);
}
}
}
private boolean binarySearch (long l, long r, RandomAccessFile wf, String gw) throws IOException {
if (l > r) return false;
long mid = (l + r) / 2 ;
wf.seek(mid * 12);
String readWord = wf.readLine();
if (gw.equals(readWord)) {
return true;
}
if (gw.compareTo(readWord) < 0) {
return binarySearch(l, mid-1, wf, gw);
}
return binarySearch(mid+1, r, wf, gw);
}
private boolean isWordPresent (String word) {
RandomAccessFile wfile;
long numWords;
try {
wfile = new RandomAccessFile(WORDS_FILE, "r");
numWords = wfile.length()/12;
} catch (IOException e) {
printl("Error with words file");
return false;
}
boolean res;
try {
res = binarySearch(0,numWords-1, wfile, word);
} catch (IOException e) {
printl("Error! during binarysearch on words file");
return false;
}
try {
wfile.close();
} catch (IOException e) {
printl("Unable to close words file!");
return false;
}
return res;
}
private String generateTipsWord (String providedWord) {
HashMap<Character,Integer> charMap = new HashMap<>();
for (int i=0; i<secretWord.length(); i++) {
Character chr = secretWord.charAt(i);
charMap.put(chr, charMap.getOrDefault(chr,0)+1);
}
Character[] tipsWord = new Character[10];
for (int i=0; i<10; i++) {
// Inizializzazione -> La parola è tutta grigia
tipsWord[i] = 'x';
}
for (int i=0; i<10; i++) {
// Metto innanzitutto i caratteri che matchano la posizione
int index = secretWord.indexOf(providedWord.charAt(i));
if (index == i) {
tipsWord[i] = '+';
int val = charMap.get(secretWord.charAt(i));
charMap.put(secretWord.charAt(i), --val);
}
}
for (int i=0; i<10; i++) {
Character prChr = providedWord.charAt(i);
int val = charMap.getOrDefault(prChr, -1);
if (val > 0) { // è presente nella parola segreta e c'è bisogno di marcarlo.
tipsWord[i] = '?';
charMap.put(prChr, --val);
}
}
String toRet = "";
for (int i=0; i<10; i++) { toRet = toRet + tipsWord[i]; }
return toRet;
}
private void sendMeStatistics (PrintWriter out) {
if (!isAuthenticated) {
out.println(403);
return;
}
User user = this.DB.findOne(clientUsername);
out.println(200);
out.println(user.getTotPlayed());
out.println(user.getTotWon());
out.println(user.getLastResult());
out.println(user.getLastStreak());
out.println(user.getBestStreak());
for (int val : user.getGuessDistribution()) {
out.println(val);
}
out.println(-1);
}
private void share (PrintWriter out) {
if (!isAuthenticated) {
out.println(403);
return;
}
checkSecretWordChanges();
if (sharedRes) {
out.println(406); //already shared result for this word()
return;
}
if (!hasWin) {
out.println(405);
return;
}
sharer.notify(attempts_list_for_notification, clientUsername);
sharedRes = true;
out.println(200);
}
private void checkSecretWordChanges () {
String currentSecretWord = WordleServerMain.getSecretWord();
if (!currentSecretWord.equals(this.secretWord)) {
attempts_list_for_notification.clear();
this.secretWord = currentSecretWord;
attemptsLeft = 12;
hasWin = false;
sharedRes = false;
}
}
}