質問編集履歴

2

内容の充実化

2021/06/29 06:07

投稿

akaevaka
akaevaka

スコア8

test CHANGED
File without changes
test CHANGED
@@ -32,33 +32,55 @@
32
32
 
33
33
  public class Requester extends Worker {
34
34
 
35
-
35
+ //send score to server, show ranking
36
-
36
+
37
- public void requestGameData(GameData gameData){
37
+ public void sendScore(Player player){
38
-
38
+
39
- try {
39
+ try {
40
+
40
-
41
+ String name = player.getName();
42
+
43
+ String score = String.valueOf(player.getScore());
44
+
45
+ String mode = String.valueOf(player.getMode());
46
+
47
+
48
+
41
- InetAddress addr = InetAddress.getByName("localhost"); // change for IPAddress
49
+ InetAddress addr = InetAddress.getByName("localhost"); // change for IP address
42
-
50
+
43
- Socket socket = new Socket(addr, 8080); // set port No.
51
+ Socket socket = new Socket(addr, 8080); //set port No.
52
+
44
-
53
+ PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
54
+
55
+ out.println(name); //send name
56
+
57
+ out.println(score); //send score
58
+
59
+ out.println(mode); //send mode
60
+
61
+
62
+
45
- ObjectInputStream objectInput = new ObjectInputStream(socket.getInputStream());
63
+ BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
46
-
64
+
65
+
66
+
47
- //set questions
67
+ //show ranking
48
-
49
- Object object = objectInput.readObject();
68
+
50
-
51
- gameData.setQstrings((ArrayList<String>) object);
52
-
53
- //set waiting time
69
+ for(int i=0; i<6; i++){
54
-
70
+
55
- Object object2 = objectInput.readObject();
71
+ String str = in.readLine();
56
-
72
+
57
- gameData.setTimeList((ArrayList<Long>) object2);
73
+ System.out.println(str);
74
+
75
+ Thread.sleep(1000);
76
+
77
+ }
78
+
79
+
58
80
 
59
81
  socket.close();
60
82
 
61
- }catch (Exception e){
83
+ } catch (Exception e) {
62
84
 
63
85
  e.printStackTrace();
64
86
 
@@ -68,412 +90,402 @@
68
90
 
69
91
 
70
92
 
93
+ }
94
+
95
+ ```
96
+
97
+
98
+
99
+ ###例外本文
100
+
101
+ ```ここに言語を入力
102
+
103
+ java.net.SocketException: Connection reset
104
+
105
+ at java.base/sun.nio.ch.NioSocketImpl.implRead(NioSocketImpl.java:323)
106
+
107
+ at java.base/sun.nio.ch.NioSocketImpl.read(NioSocketImpl.java:350)
108
+
109
+ at java.base/sun.nio.ch.NioSocketImpl$1.read(NioSocketImpl.java:803)
110
+
111
+ at java.base/java.net.Socket$SocketInputStream.read(Socket.java:976)
112
+
113
+ at java.base/sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:297)
114
+
115
+ at java.base/sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:339)
116
+
117
+ at java.base/sun.nio.cs.StreamDecoder.read(StreamDecoder.java:188)
118
+
119
+ at java.base/java.io.InputStreamReader.read(InputStreamReader.java:178)
120
+
121
+ at java.base/java.io.BufferedReader.fill(BufferedReader.java:161)
122
+
123
+ at java.base/java.io.BufferedReader.readLine(BufferedReader.java:329)
124
+
125
+ at java.base/java.io.BufferedReader.readLine(BufferedReader.java:396)
126
+
127
+ at Requester.sendScore(Requester.java:55)
128
+
129
+ at ClientMain.main(ClientMain.java:57)
130
+
131
+ ```
132
+
133
+
134
+
135
+
136
+
137
+ ###追記
138
+
139
+ Server側
140
+
141
+ updateRankingでスコアを受け取っています.
142
+
143
+
144
+
145
+ ```Java
146
+
147
+ import java.net.*;
148
+
149
+ import java.util.*;
150
+
151
+ import java.util.Map.Entry;
152
+
153
+ import java.io.*;
154
+
155
+
156
+
157
+
158
+
159
+ public class Responser{
160
+
161
+ private static ArrayList<String> Qstring = new ArrayList<>(); //question
162
+
163
+ private static ArrayList<Long> timeList = new ArrayList<>(); //waiting list
164
+
165
+
166
+
167
+ //accept score and register it to ranking
168
+
71
- public void sendDifficulty(Player player){
169
+ public void updateRanking() throws IOException {
170
+
171
+ ServerSocket s = new ServerSocket(8080); // make socket
172
+
173
+ Socket socket = s.accept(); // wait setting demand of connection
174
+
175
+ BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
176
+
177
+ String p_name = in.readLine(); //accept name
178
+
179
+ String p_score = in.readLine(); //accept score
180
+
181
+ int p_mode = Integer.parseInt(in.readLine()); //accept mode
182
+
183
+
184
+
185
+ String rankingFilePath;
186
+
187
+ if(p_mode == 1) rankingFilePath = "data/ranking/easyranking.txt";
188
+
189
+ else rankingFilePath = "data/ranking/hardranking.txt";
190
+
191
+
192
+
193
+ //key=ID, value=score
194
+
195
+ Map<Integer,Integer> scores = new HashMap<Integer,Integer>();
196
+
197
+ ArrayList<String> playerNameList = new ArrayList<String>();
198
+
199
+
200
+
201
+ Scanner s2 = new Scanner(new File(rankingFilePath));
202
+
203
+ int count = 1;
204
+
205
+
206
+
207
+ int id = -1, score = -1;
208
+
209
+ String name = "";
210
+
211
+ while (s2.hasNext()){
212
+
213
+ if(count % 3 == 1){
214
+
215
+ id = Integer.parseInt(s2.next());
216
+
217
+ }else if(count % 3 == 2){
218
+
219
+ score = Integer.parseInt(s2.next());
220
+
221
+ }else{
222
+
223
+ name = s2.next();
224
+
225
+ scores.put(id,score);
226
+
227
+ playerNameList.add(name);
228
+
229
+ }
230
+
231
+ count++;
232
+
233
+ }
234
+
235
+
72
236
 
73
237
  try{
74
238
 
239
+ int nextId = playerNameList.size()+1;
240
+
75
- String mode = String.valueOf(player.getMode());
241
+ File file = new File(rankingFilePath);
242
+
76
-
243
+ FileWriter filewriter = new FileWriter(file, true);
244
+
77
- InetAddress addr = InetAddress.getByName("localhost"); // change for IP address
245
+ filewriter.write(nextId + " " + p_score + " " + p_name + "\n");
78
-
79
- Socket socket = new Socket(addr, 8080); //set port No.
246
+
80
-
81
- PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
82
-
83
- out.println(mode); //send mode
84
-
85
- socket.close();
247
+ filewriter.close();
248
+
86
-
249
+ scores.put(nextId, Integer.parseInt(p_score));
250
+
251
+ playerNameList.add(p_name);
252
+
87
- } catch (Exception e) {
253
+ }catch(IOException e){
88
-
254
+
89
- e.printStackTrace();
255
+ System.out.println(e);
90
256
 
91
257
  }
92
258
 
259
+
260
+
261
+ // make entry list of scorelist
262
+
263
+ List<Entry<Integer, Integer>> list_entries = new ArrayList<Entry<Integer, Integer>>(scores.entrySet());
264
+
265
+
266
+
267
+ //sort scorelist
268
+
269
+ Collections.sort(list_entries, new Comparator<Entry<Integer, Integer>>() {
270
+
271
+ public int compare(Entry<Integer, Integer> obj1, Entry<Integer, Integer> obj2) {
272
+
273
+ return obj2.getValue().compareTo(obj1.getValue());
274
+
275
+ }
276
+
277
+ });
278
+
279
+
280
+
281
+
282
+
283
+ PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); //setting of sending buffer
284
+
285
+
286
+
287
+ //send ranking data to client
288
+
289
+ String[] ordinalNumber = {"1st", "2nd", "3rd", "4th", "5th"};
290
+
291
+ out.println("--- Rank | Player | Score ---");
292
+
293
+ for(int i=0; i<5; i++){
294
+
295
+ out.println(ordinalNumber[i] + ", " + playerNameList.get(list_entries.get(i).getKey()-1) + ", " + list_entries.get(i).getValue());
296
+
297
+ }
298
+
299
+ s.close();
300
+
93
301
  }
94
302
 
95
303
 
96
304
 
97
- //send score to server, show ranking
98
-
99
- public void sendScore(Player player){
100
-
101
- try {
102
-
103
- String name = player.getName();
104
-
105
- String score = String.valueOf(player.getScore());
106
-
107
- String mode = String.valueOf(player.getMode());
108
-
109
-
110
-
111
- InetAddress addr = InetAddress.getByName("localhost"); // change for IP address
112
-
113
- Socket socket = new Socket(addr, 8080); //set port No.
114
-
115
- PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
116
-
117
- out.println(name); //send name
118
-
119
- out.println(score); //send score
120
-
121
- out.println(mode); //send mode
122
-
123
-
124
-
125
- BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
126
-
127
-
128
-
129
- //show ranking
130
-
131
- for(int i=0; i<6; i++){
132
-
133
- String str = in.readLine();
134
-
135
- System.out.println(str);
136
-
137
- Thread.sleep(1000);
138
-
139
- }
140
-
141
-
142
-
143
- socket.close();
144
-
145
- } catch (Exception e) {
146
-
147
- e.printStackTrace();
148
-
149
- }
305
+ }
306
+
307
+ ```
308
+
309
+
310
+
311
+
312
+
313
+ ###ClientMain
314
+
315
+ ```Java
316
+
317
+ import java.io.*;
318
+
319
+ import java.util.*;
320
+
321
+
322
+
323
+ public class ClientMain {
324
+
325
+ public static void main(String[] args) throws InterruptedException{
326
+
327
+ Player player = new Player("", 0, 0); //(name, score, mdoe)
328
+
329
+ GameData gameData = new GameData(player);
330
+
331
+ ClientSystem clientsystem = new ClientSystem();
332
+
333
+ Requester requester = new Requester();
334
+
335
+
336
+
337
+ clientsystem.opening(); //op
338
+
339
+
340
+
341
+ clientsystem.say("Choose difficulty from easy:1 or hard:2.");
342
+
343
+ clientsystem.say("Input the difficulty number.");
344
+
345
+ Scanner scan = new Scanner(System.in);
346
+
347
+ int difficulty = scan.nextInt();
348
+
349
+
350
+
351
+ player.setMode(difficulty);
352
+
353
+ requester.sendDifficulty(player);
354
+
355
+
356
+
357
+ clientsystem.say("send difficulty");
358
+
359
+ clientsystem.say("gamedata download...");
360
+
361
+ Thread.sleep(1000);
362
+
363
+
364
+
365
+ requester.requestGameData(gameData); //download gamedata from server
366
+
367
+ clientsystem.say("Load Successes!");
368
+
369
+ Thread.sleep(2000);
370
+
371
+
372
+
373
+ clientsystem.startGame(gameData); //gamestart
374
+
375
+ System.out.println();
376
+
377
+ clientsystem.say("Finish");
378
+
379
+
380
+
381
+ clientsystem.showScore(player); //show score
382
+
383
+
384
+
385
+
386
+
387
+ clientsystem.say("Please input your name.");
388
+
389
+
390
+
391
+ String playerName = scan.nextLine();
392
+
393
+ scan.close();
394
+
395
+ player.setName(playerName);
396
+
397
+
398
+
399
+ requester.sendScore(player);
400
+
401
+
402
+
403
+ clientsystem.say("See you!");
150
404
 
151
405
  }
152
406
 
153
-
154
-
155
407
  }
156
408
 
157
409
  ```
158
410
 
159
411
 
160
412
 
413
+
414
+
415
+
416
+
161
- ###例外本文
417
+ ###実行本文
162
418
 
163
419
  ```ここに言語を入力
164
420
 
421
+ [Welcome to this TYPING GAME.]
422
+
423
+ [When this system says something, uses [] like this message]
424
+
425
+ [--rule--]
426
+
427
+ [Type the message enclosed in {}.]
428
+
429
+ [The next is a example.]
430
+
431
+ Q.{ もんだいぶん }
432
+
433
+ [if you misstype and enter without modifying, subtract your score.]
434
+
435
+ [There are two difficulty levels]
436
+
437
+ [Choose difficulty from easy:1 or hard:2.]
438
+
439
+ [Input the difficulty number.]
440
+
441
+ 1
442
+
443
+ [send difficulty]
444
+
445
+ [gamedata download...]
446
+
447
+ [Load Successes!]
448
+
449
+ [Are you ready?]
450
+
451
+ [START!]
452
+
453
+ 0
454
+
455
+
456
+
457
+ [Finish]
458
+
459
+ [Score: 0]
460
+
461
+ [Please input your name.]
462
+
463
+ --- Rank | Player | Score ---
464
+
165
465
  java.net.SocketException: Connection reset
166
466
 
167
- at java.base/sun.nio.ch.NioSocketImpl.implRead(NioSocketImpl.java:323)
168
-
169
- at java.base/sun.nio.ch.NioSocketImpl.read(NioSocketImpl.java:350)
170
-
171
- at java.base/sun.nio.ch.NioSocketImpl$1.read(NioSocketImpl.java:803)
172
-
173
- at java.base/java.net.Socket$SocketInputStream.read(Socket.java:976)
174
-
175
- at java.base/sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:297)
176
-
177
- at java.base/sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:339)
178
-
179
- at java.base/sun.nio.cs.StreamDecoder.read(StreamDecoder.java:188)
180
-
181
- at java.base/java.io.InputStreamReader.read(InputStreamReader.java:178)
182
-
183
- at java.base/java.io.BufferedReader.fill(BufferedReader.java:161)
184
-
185
- at java.base/java.io.BufferedReader.readLine(BufferedReader.java:329)
186
-
187
- at java.base/java.io.BufferedReader.readLine(BufferedReader.java:396)
188
-
189
- at Requester.sendScore(Requester.java:55)
467
+ すでに載せたので文字数の都合上割愛
468
+
190
-
469
+ [See you!]
470
+
191
- at ClientMain.main(ClientMain.java:57)
471
+ PS C:\Users\Client>
192
472
 
193
473
  ```
194
474
 
195
475
 
196
476
 
197
-
198
-
199
- ###追記
200
-
201
- Server側
202
-
203
- updateRankingでスコアを受け取っています.
204
-
205
-
206
-
207
- ```Java
208
-
209
- import java.net.*;
210
-
211
- import java.util.*;
212
-
213
- import java.util.Map.Entry;
214
-
215
- import java.io.*;
216
-
217
-
218
-
219
- //EASY = 1;
220
-
221
- //HARD = 2;
222
-
223
-
224
-
225
- public class Responser{
226
-
227
- private static ArrayList<String> Qstring = new ArrayList<>(); //question
228
-
229
- private static ArrayList<Long> timeList = new ArrayList<>(); //waiting list
230
-
231
-
232
-
233
- public int loadDifficulty() throws IOException{
234
-
235
- ServerSocket s = new ServerSocket(8080); // make socket
236
-
237
- Socket socket = s.accept(); // wait setting demand of connection
238
-
239
- BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
240
-
241
- s.close();
242
-
243
- return Integer.parseInt(in.readLine()); //accept difficulty
244
-
245
- }
246
-
247
-
248
-
249
- //load Qstringsdata from arraylist to file
250
-
251
- public void loadQstrings(String QuestionFilePath) throws FileNotFoundException {
252
-
253
- Responser.Qstring.add("");
254
-
255
- Scanner s = new Scanner(new File(QuestionFilePath));
256
-
257
- Responser.Qstring.clear();
258
-
259
- while (s.hasNext()){
260
-
261
- Responser.Qstring.add(s.nextLine());
262
-
263
- }
264
-
265
- s.close();
266
-
267
- }
268
-
269
-
270
-
271
- //load timelist from arraylist to file
272
-
273
- public void loadTimeList(String timeFilePath) throws FileNotFoundException {
274
-
275
- Responser.timeList.add(0L);
276
-
277
- Scanner s = new Scanner(new File(timeFilePath));
278
-
279
- Responser.timeList.clear();
280
-
281
- while (s.hasNext()){
282
-
283
- Responser.timeList.add(s.nextLong());
284
-
285
- }
286
-
287
- s.close();
288
-
289
- }
290
-
291
-
292
-
293
- //send gamedata in response to client requests
294
-
295
- public void sendGameData() throws IOException {
296
-
297
- try {
298
-
299
- ServerSocket s = new ServerSocket(8080); // make socket
300
-
301
- Socket socket = s.accept(); //wait setting demand of connection
302
-
303
-
304
-
305
- try {
306
-
307
- ObjectOutputStream objectOutput = new ObjectOutputStream(socket.getOutputStream());
308
-
309
- //send question
310
-
311
- objectOutput.writeObject(Responser.Qstring);
312
-
313
- //send timelist
314
-
315
- objectOutput.writeObject(Responser.timeList);
316
-
317
- } catch (IOException e) {
318
-
319
- e.printStackTrace();
320
-
321
- }
322
-
323
- s.close();
324
-
325
- } catch (Exception e) {
326
-
327
- e.printStackTrace();
328
-
329
- }
330
-
331
-
332
-
333
- }
334
-
335
-
336
-
337
-
338
-
339
- //accept score and register it to ranking
340
-
341
- public void updateRanking() throws IOException {
342
-
343
- ServerSocket s = new ServerSocket(8080); // make socket
344
-
345
- Socket socket = s.accept(); // wait setting demand of connection
346
-
347
- BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
348
-
349
- String p_name = in.readLine(); //accept name
350
-
351
- String p_score = in.readLine(); //accept score
352
-
353
- int p_mode = Integer.parseInt(in.readLine()); //accept mode
354
-
355
-
356
-
357
- String rankingFilePath;
358
-
359
- if(p_mode == 1) rankingFilePath = "data/ranking/easyranking.txt";
360
-
361
- else rankingFilePath = "data/ranking/hardranking.txt";
362
-
363
-
364
-
365
- //key=ID, value=score
366
-
367
- Map<Integer,Integer> scores = new HashMap<Integer,Integer>();
368
-
369
- ArrayList<String> playerNameList = new ArrayList<String>();
370
-
371
-
372
-
373
- Scanner s2 = new Scanner(new File(rankingFilePath));
374
-
375
- int count = 1;
376
-
377
-
378
-
379
- int id = -1, score = -1;
380
-
381
- String name = "";
382
-
383
- while (s2.hasNext()){
384
-
385
- if(count % 3 == 1){
386
-
387
- id = Integer.parseInt(s2.next());
388
-
389
- }else if(count % 3 == 2){
390
-
391
- score = Integer.parseInt(s2.next());
392
-
393
- }else{
394
-
395
- name = s2.next();
396
-
397
- scores.put(id,score);
398
-
399
- playerNameList.add(name);
400
-
401
- }
402
-
403
- count++;
404
-
405
- }
406
-
407
-
408
-
409
- try{
410
-
411
- int nextId = playerNameList.size()+1;
412
-
413
- File file = new File(rankingFilePath);
414
-
415
- FileWriter filewriter = new FileWriter(file, true);
416
-
417
- filewriter.write(nextId + " " + p_score + " " + p_name + "\n");
418
-
419
- filewriter.close();
420
-
421
- scores.put(nextId, Integer.parseInt(p_score));
422
-
423
- playerNameList.add(p_name);
424
-
425
- }catch(IOException e){
426
-
427
- System.out.println(e);
428
-
429
- }
430
-
431
-
432
-
433
- // make entry list of scorelist
434
-
435
- List<Entry<Integer, Integer>> list_entries = new ArrayList<Entry<Integer, Integer>>(scores.entrySet());
436
-
437
-
438
-
439
- //sort scorelist
440
-
441
- Collections.sort(list_entries, new Comparator<Entry<Integer, Integer>>() {
442
-
443
- public int compare(Entry<Integer, Integer> obj1, Entry<Integer, Integer> obj2) {
444
-
445
- return obj2.getValue().compareTo(obj1.getValue());
446
-
447
- }
448
-
449
- });
450
-
451
-
452
-
453
-
454
-
455
- PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); //setting of sending buffer
456
-
457
-
458
-
459
- //send ranking data to client
460
-
461
- String[] ordinalNumber = {"1st", "2nd", "3rd", "4th", "5th"};
462
-
463
- out.println("--- Rank | Player | Score ---");
464
-
465
- for(int i=0; i<5; i++){
466
-
467
- out.println(ordinalNumber[i] + ", " + playerNameList.get(list_entries.get(i).getKey()-1) + ", " + list_entries.get(i).getValue());
468
-
469
- }
470
-
471
- s.close();
472
-
473
- }
474
-
475
-
476
-
477
- }
478
-
479
- ```
477
+ ###原因の推定
478
+
479
+ 名前の入力をまたずに
480
+
481
+ --- Rank |
482
+
483
+ というものが出力されてしまっているので,
484
+
485
+ 名前の入力をしようとしたときにupdateRankingがもう始まってしまっているのが原因だと考えられます.
486
+
487
+ 本来ならこのupdateはsendScoreを待たなければいけないのに先に終わらせてしまっているからこのような例がでたと考えております.
488
+
489
+ updateRankinggがしっかりと待機するようにするにはどうすればよいですか
490
+
491
+ 文字数の都合上,必要ないと判断した関数は省略しました

1

内容の充実化

2021/06/29 06:07

投稿

akaevaka
akaevaka

スコア8

test CHANGED
File without changes
test CHANGED
@@ -191,3 +191,289 @@
191
191
  at ClientMain.main(ClientMain.java:57)
192
192
 
193
193
  ```
194
+
195
+
196
+
197
+
198
+
199
+ ###追記
200
+
201
+ Server側
202
+
203
+ updateRankingでスコアを受け取っています.
204
+
205
+
206
+
207
+ ```Java
208
+
209
+ import java.net.*;
210
+
211
+ import java.util.*;
212
+
213
+ import java.util.Map.Entry;
214
+
215
+ import java.io.*;
216
+
217
+
218
+
219
+ //EASY = 1;
220
+
221
+ //HARD = 2;
222
+
223
+
224
+
225
+ public class Responser{
226
+
227
+ private static ArrayList<String> Qstring = new ArrayList<>(); //question
228
+
229
+ private static ArrayList<Long> timeList = new ArrayList<>(); //waiting list
230
+
231
+
232
+
233
+ public int loadDifficulty() throws IOException{
234
+
235
+ ServerSocket s = new ServerSocket(8080); // make socket
236
+
237
+ Socket socket = s.accept(); // wait setting demand of connection
238
+
239
+ BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
240
+
241
+ s.close();
242
+
243
+ return Integer.parseInt(in.readLine()); //accept difficulty
244
+
245
+ }
246
+
247
+
248
+
249
+ //load Qstringsdata from arraylist to file
250
+
251
+ public void loadQstrings(String QuestionFilePath) throws FileNotFoundException {
252
+
253
+ Responser.Qstring.add("");
254
+
255
+ Scanner s = new Scanner(new File(QuestionFilePath));
256
+
257
+ Responser.Qstring.clear();
258
+
259
+ while (s.hasNext()){
260
+
261
+ Responser.Qstring.add(s.nextLine());
262
+
263
+ }
264
+
265
+ s.close();
266
+
267
+ }
268
+
269
+
270
+
271
+ //load timelist from arraylist to file
272
+
273
+ public void loadTimeList(String timeFilePath) throws FileNotFoundException {
274
+
275
+ Responser.timeList.add(0L);
276
+
277
+ Scanner s = new Scanner(new File(timeFilePath));
278
+
279
+ Responser.timeList.clear();
280
+
281
+ while (s.hasNext()){
282
+
283
+ Responser.timeList.add(s.nextLong());
284
+
285
+ }
286
+
287
+ s.close();
288
+
289
+ }
290
+
291
+
292
+
293
+ //send gamedata in response to client requests
294
+
295
+ public void sendGameData() throws IOException {
296
+
297
+ try {
298
+
299
+ ServerSocket s = new ServerSocket(8080); // make socket
300
+
301
+ Socket socket = s.accept(); //wait setting demand of connection
302
+
303
+
304
+
305
+ try {
306
+
307
+ ObjectOutputStream objectOutput = new ObjectOutputStream(socket.getOutputStream());
308
+
309
+ //send question
310
+
311
+ objectOutput.writeObject(Responser.Qstring);
312
+
313
+ //send timelist
314
+
315
+ objectOutput.writeObject(Responser.timeList);
316
+
317
+ } catch (IOException e) {
318
+
319
+ e.printStackTrace();
320
+
321
+ }
322
+
323
+ s.close();
324
+
325
+ } catch (Exception e) {
326
+
327
+ e.printStackTrace();
328
+
329
+ }
330
+
331
+
332
+
333
+ }
334
+
335
+
336
+
337
+
338
+
339
+ //accept score and register it to ranking
340
+
341
+ public void updateRanking() throws IOException {
342
+
343
+ ServerSocket s = new ServerSocket(8080); // make socket
344
+
345
+ Socket socket = s.accept(); // wait setting demand of connection
346
+
347
+ BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
348
+
349
+ String p_name = in.readLine(); //accept name
350
+
351
+ String p_score = in.readLine(); //accept score
352
+
353
+ int p_mode = Integer.parseInt(in.readLine()); //accept mode
354
+
355
+
356
+
357
+ String rankingFilePath;
358
+
359
+ if(p_mode == 1) rankingFilePath = "data/ranking/easyranking.txt";
360
+
361
+ else rankingFilePath = "data/ranking/hardranking.txt";
362
+
363
+
364
+
365
+ //key=ID, value=score
366
+
367
+ Map<Integer,Integer> scores = new HashMap<Integer,Integer>();
368
+
369
+ ArrayList<String> playerNameList = new ArrayList<String>();
370
+
371
+
372
+
373
+ Scanner s2 = new Scanner(new File(rankingFilePath));
374
+
375
+ int count = 1;
376
+
377
+
378
+
379
+ int id = -1, score = -1;
380
+
381
+ String name = "";
382
+
383
+ while (s2.hasNext()){
384
+
385
+ if(count % 3 == 1){
386
+
387
+ id = Integer.parseInt(s2.next());
388
+
389
+ }else if(count % 3 == 2){
390
+
391
+ score = Integer.parseInt(s2.next());
392
+
393
+ }else{
394
+
395
+ name = s2.next();
396
+
397
+ scores.put(id,score);
398
+
399
+ playerNameList.add(name);
400
+
401
+ }
402
+
403
+ count++;
404
+
405
+ }
406
+
407
+
408
+
409
+ try{
410
+
411
+ int nextId = playerNameList.size()+1;
412
+
413
+ File file = new File(rankingFilePath);
414
+
415
+ FileWriter filewriter = new FileWriter(file, true);
416
+
417
+ filewriter.write(nextId + " " + p_score + " " + p_name + "\n");
418
+
419
+ filewriter.close();
420
+
421
+ scores.put(nextId, Integer.parseInt(p_score));
422
+
423
+ playerNameList.add(p_name);
424
+
425
+ }catch(IOException e){
426
+
427
+ System.out.println(e);
428
+
429
+ }
430
+
431
+
432
+
433
+ // make entry list of scorelist
434
+
435
+ List<Entry<Integer, Integer>> list_entries = new ArrayList<Entry<Integer, Integer>>(scores.entrySet());
436
+
437
+
438
+
439
+ //sort scorelist
440
+
441
+ Collections.sort(list_entries, new Comparator<Entry<Integer, Integer>>() {
442
+
443
+ public int compare(Entry<Integer, Integer> obj1, Entry<Integer, Integer> obj2) {
444
+
445
+ return obj2.getValue().compareTo(obj1.getValue());
446
+
447
+ }
448
+
449
+ });
450
+
451
+
452
+
453
+
454
+
455
+ PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); //setting of sending buffer
456
+
457
+
458
+
459
+ //send ranking data to client
460
+
461
+ String[] ordinalNumber = {"1st", "2nd", "3rd", "4th", "5th"};
462
+
463
+ out.println("--- Rank | Player | Score ---");
464
+
465
+ for(int i=0; i<5; i++){
466
+
467
+ out.println(ordinalNumber[i] + ", " + playerNameList.get(list_entries.get(i).getKey()-1) + ", " + list_entries.get(i).getValue());
468
+
469
+ }
470
+
471
+ s.close();
472
+
473
+ }
474
+
475
+
476
+
477
+ }
478
+
479
+ ```