@@ -65,37 +65,38 @@ public void run() {
65
65
while (true ) {
66
66
Gson gson = GlobalThings .gson ;
67
67
String s = dataInputStream .readUTF ();
68
- System .out .println ("<<REQUEST>> : \n " + s ); // TODO : delete this line
68
+ // System.out.println("<<REQUEST>> : \n" + s); // TODO : delete this line
69
69
Request request = gson .fromJson (s , Request .class );
70
- System .out .println ("New request from " + socket );
70
+ // System.out.println("New request from " + socket);
71
71
Response response = handleRequest (request );
72
- System .out .println ("<<RESPONSE>> : \n " + gson .toJson (response )); // TODO : delete this line
72
+ // System.out.println("<<RESPONSE>> : \n" + gson.toJson(response)); // TODO : delete this line
73
73
dataOutputStream .writeUTF (gson .toJson (response ));
74
74
dataOutputStream .flush ();
75
75
}
76
76
} catch (IOException | NoSuchMethodException | InvocationTargetException | IllegalAccessException exception ) {
77
77
if (user != null )
78
78
user .setLastOnlineTime (LocalDateTime .now ());
79
+ exception .printStackTrace ();
79
80
ServerController .getInstance ().removeSocket (this );
80
81
// TODO : send updated list of users to online users
81
82
}
82
83
}
83
84
84
85
private Response handleRequest (Request request ) throws NoSuchMethodException , InvocationTargetException , IllegalAccessException {
85
86
String methodName = request .getMethodName ();
86
- if (methodName .equals ("getPlayersInLobby" )){
87
+ if (methodName .equals ("getPlayersInLobby" )) {
87
88
Response response = new Response ();
88
89
ArrayList <String > usernames = new ArrayList <>();
89
90
for (SocketHandler socketHandler : waitingInLobbyWithYou ) {
90
91
usernames .add (socketHandler .user .getUsername ());
91
92
}
92
93
response .setAnswer (usernames );
93
- return response ;
94
+ return response ;
94
95
}
95
- if (methodName .startsWith ("accept invite from" )){
96
+ if (methodName .startsWith ("accept invite from" )) {
96
97
String username = methodName .substring (19 );
97
98
for (SocketHandler socketHandler : ServerController .getInstance ().getSocketHandlers ()) {
98
- if (socketHandler .user .getUsername ().equals (username )){
99
+ if (socketHandler .user .getUsername ().equals (username )) {
99
100
socketHandler .waitingInLobbyWithYou .add (this );
100
101
this .waitingInLobbyWithYou = socketHandler .waitingInLobbyWithYou ;
101
102
socketHandler .sendCommand (user .getUsername () + " has accepted your invite" );
@@ -106,10 +107,10 @@ private Response handleRequest(Request request) throws NoSuchMethodException, In
106
107
}
107
108
return new Response ();
108
109
}
109
- if (methodName .startsWith ("reject invite from" )){
110
+ if (methodName .startsWith ("reject invite from" )) {
110
111
String username = methodName .substring (19 );
111
112
for (SocketHandler socketHandler : ServerController .getInstance ().getSocketHandlers ()) {
112
- if (socketHandler .user .getUsername ().equals (username )){
113
+ if (socketHandler .user .getUsername ().equals (username )) {
113
114
socketHandler .sendCommand (user .getUsername () + " has rejected your invite" );
114
115
}
115
116
}
@@ -166,10 +167,30 @@ private Response handleRequest(Request request) throws NoSuchMethodException, In
166
167
case "Game" :
167
168
method = gameMenuController .getClass ().getMethod (methodName , types );
168
169
answer = method .invoke (gameMenuController , arguments );
170
+ if (answer instanceof String ) {
171
+ if (((String ) answer ).startsWith ("change turn done \n It's now your turn " ))
172
+ updateMapForClients ();
173
+ }
169
174
break ;
170
175
case "GameSetting" :
171
176
method = gameSettingMenuController .getClass ().getMethod (methodName , types );
172
177
answer = method .invoke (gameSettingMenuController , arguments );
178
+ if (answer instanceof String ) {
179
+ if (((String ) answer ).startsWith ("a new game started with " )) {
180
+ new Thread (()->{
181
+ try {
182
+ Thread .sleep (100 );
183
+ } catch (InterruptedException e ) {
184
+ throw new RuntimeException (e );
185
+ }
186
+ for (SocketHandler socketHandler : waitingInLobbyWithYou ) {
187
+ socketHandler .sendCommand ("game started" );
188
+ }
189
+ updateMapForClients ();
190
+ }).start ();
191
+
192
+ }
193
+ }
173
194
break ;
174
195
case "Login" :
175
196
method = loginMenuController .getClass ().getMethod (methodName , types );
@@ -182,6 +203,7 @@ private Response handleRequest(Request request) throws NoSuchMethodException, In
182
203
user .setAuthToken (user .getUsername () + user .getPassword () + user .getNickname () + String .valueOf (random ));
183
204
answer += user .getAuthToken ();
184
205
}
206
+
185
207
}
186
208
break ;
187
209
case "Main" :
@@ -207,6 +229,17 @@ private Response handleRequest(Request request) throws NoSuchMethodException, In
207
229
return response ;
208
230
}
209
231
232
+ private void updateMapForClients () {
233
+ for (SocketHandler socketHandler : waitingInLobbyWithYou ) {
234
+ if (socketHandler .user .getUsername ().equals (Game .getGame ().getSelectedCivilization ().getUsername ())) {
235
+ socketHandler .sendCommand ("its your turn" );
236
+ }
237
+ else {
238
+ socketHandler .sendCommand ("not your turn. turn for : " + Game .getGame ().getSelectedCivilization ().getUsername ());
239
+ }
240
+ }
241
+ }
242
+
210
243
private void changeMenu (String name ) {
211
244
menu = name ;
212
245
System .out .println ("you are now in " + name );
0 commit comments