Skip to content

Commit 25e3b78

Browse files
committed
next turn and turns for users completed
1 parent 60197fc commit 25e3b78

File tree

5 files changed

+151
-74
lines changed

5 files changed

+151
-74
lines changed

src/main/java/client/controller/ListenerController.java

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import client.model.GlobalThings;
44
import client.model.Request;
55
import client.model.Response;
6+
import client.view.GameMenu;
67
import client.view.GameSettingsMenu;
8+
import client.view.Menu;
79
import com.google.gson.Gson;
810
import javafx.application.Platform;
911
import javafx.event.ActionEvent;
@@ -67,7 +69,7 @@ public void run() {
6769
hBox.setStyle("-fx-background-color: purple");
6870
Label label = new Label(command + " do you accept?");
6971
label.setFont(new Font(40));
70-
label.setStyle("-fx-fill: yellow");
72+
label.setStyle("-fx-text-fill: yellow");
7173
Button accept = new Button("accept");
7274
accept.setOnAction(new EventHandler<ActionEvent>() {
7375
@Override
@@ -130,11 +132,46 @@ public void run() {
130132
}
131133

132134
});
133-
135+
return;
134136
}
135-
switch (command) {
136137

138+
if (command.equals("game started")) {
139+
Controller.send("change menu Game");
140+
Platform.runLater(() -> {
141+
Controller.setGameMenu(new GameMenu());
142+
Controller.getWindow().setScene(Controller.getGameMenu().getScene());
143+
});
144+
return;
145+
}
137146

147+
if (command.startsWith("not your turn. turn for : ")) {
148+
Platform.runLater(() -> {
149+
HBox hBox = new HBox();
150+
hBox.toBack();
151+
hBox.getChildren().clear();
152+
hBox.setStyle("-fx-background-color: purple");
153+
Label label = new Label(command);
154+
label.setFont(new Font(40));
155+
label.setStyle("-fx-text-fill: yellow");
156+
hBox.getChildren().add(label);
157+
hBox.setAlignment(Pos.CENTER);
158+
hBox.setPrefHeight(720);
159+
hBox.setPrefWidth(1280);
160+
((AnchorPane) Controller.getWindow().getScene().getRoot()).getChildren().add(hBox);
161+
});
162+
return;
138163
}
164+
if (command.equals("its your turn")) {
165+
Platform.runLater(() -> {
166+
if (((AnchorPane) Controller.getWindow().getScene().getRoot()).getChildren().
167+
get(((AnchorPane) Controller.getWindow().getScene().getRoot()).getChildren().size() - 1) instanceof HBox)
168+
((AnchorPane) Controller.getWindow().getScene().getRoot()).getChildren().remove(
169+
((AnchorPane) Controller.getWindow().getScene().getRoot()).getChildren().size() - 1);
170+
Controller.getGameMenu().updateAll();
171+
});
172+
return;
173+
}
174+
175+
139176
}
140177
}

src/main/java/client/view/GameMenu.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public void initialize(URL location, ResourceBundle resources) {
114114
});
115115
}
116116

117-
private void updateAll() {
117+
public void updateAll() {
118118
updateStatusBar();
119119
updateCurrentResearchStatus();
120120
while (true) {

src/main/java/client/view/GameSettingsMenu.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,8 @@ public void startGameWithFriend(MouseEvent mouseEvent) {
144144
String text = (String) Controller.send("gameWithFriends", createArraylistOfUsers(), mapLength.getValue(), mapWidth.getValue(),
145145
autoSave.getValue(), numberOfKeptSavedFiles.getValue());
146146
information.setText(text);
147-
if (text.startsWith("a new game started with ")) {
148-
Controller.send("change menu Game");
149-
setup(cancelButton);
150-
Controller.setGameMenu(new GameMenu());
151-
window.setScene(Controller.getGameMenu().getScene());
152147

153-
}
148+
154149
} else {
155150
String text = (String) Controller.send("loadSavedGame", loadGame.getValue());
156151
if (text.endsWith("successfully")) {

src/main/java/server/controller/SocketHandler.java

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,37 +65,38 @@ public void run() {
6565
while (true) {
6666
Gson gson = GlobalThings.gson;
6767
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
6969
Request request = gson.fromJson(s, Request.class);
70-
System.out.println("New request from " + socket);
70+
//System.out.println("New request from " + socket);
7171
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
7373
dataOutputStream.writeUTF(gson.toJson(response));
7474
dataOutputStream.flush();
7575
}
7676
} catch (IOException | NoSuchMethodException | InvocationTargetException | IllegalAccessException exception) {
7777
if (user != null)
7878
user.setLastOnlineTime(LocalDateTime.now());
79+
exception.printStackTrace();
7980
ServerController.getInstance().removeSocket(this);
8081
// TODO : send updated list of users to online users
8182
}
8283
}
8384

8485
private Response handleRequest(Request request) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
8586
String methodName = request.getMethodName();
86-
if (methodName.equals("getPlayersInLobby")){
87+
if (methodName.equals("getPlayersInLobby")) {
8788
Response response = new Response();
8889
ArrayList<String> usernames = new ArrayList<>();
8990
for (SocketHandler socketHandler : waitingInLobbyWithYou) {
9091
usernames.add(socketHandler.user.getUsername());
9192
}
9293
response.setAnswer(usernames);
93-
return response ;
94+
return response;
9495
}
95-
if (methodName.startsWith("accept invite from")){
96+
if (methodName.startsWith("accept invite from")) {
9697
String username = methodName.substring(19);
9798
for (SocketHandler socketHandler : ServerController.getInstance().getSocketHandlers()) {
98-
if (socketHandler.user.getUsername().equals(username)){
99+
if (socketHandler.user.getUsername().equals(username)) {
99100
socketHandler.waitingInLobbyWithYou.add(this);
100101
this.waitingInLobbyWithYou = socketHandler.waitingInLobbyWithYou;
101102
socketHandler.sendCommand(user.getUsername() + " has accepted your invite");
@@ -106,10 +107,10 @@ private Response handleRequest(Request request) throws NoSuchMethodException, In
106107
}
107108
return new Response();
108109
}
109-
if (methodName.startsWith("reject invite from")){
110+
if (methodName.startsWith("reject invite from")) {
110111
String username = methodName.substring(19);
111112
for (SocketHandler socketHandler : ServerController.getInstance().getSocketHandlers()) {
112-
if (socketHandler.user.getUsername().equals(username)){
113+
if (socketHandler.user.getUsername().equals(username)) {
113114
socketHandler.sendCommand(user.getUsername() + " has rejected your invite");
114115
}
115116
}
@@ -166,10 +167,30 @@ private Response handleRequest(Request request) throws NoSuchMethodException, In
166167
case "Game":
167168
method = gameMenuController.getClass().getMethod(methodName, types);
168169
answer = method.invoke(gameMenuController, arguments);
170+
if (answer instanceof String) {
171+
if (((String) answer).startsWith("change turn done \nIt's now your turn "))
172+
updateMapForClients();
173+
}
169174
break;
170175
case "GameSetting":
171176
method = gameSettingMenuController.getClass().getMethod(methodName, types);
172177
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+
}
173194
break;
174195
case "Login":
175196
method = loginMenuController.getClass().getMethod(methodName, types);
@@ -182,6 +203,7 @@ private Response handleRequest(Request request) throws NoSuchMethodException, In
182203
user.setAuthToken(user.getUsername() + user.getPassword() + user.getNickname() + String.valueOf(random));
183204
answer += user.getAuthToken();
184205
}
206+
185207
}
186208
break;
187209
case "Main":
@@ -207,6 +229,17 @@ private Response handleRequest(Request request) throws NoSuchMethodException, In
207229
return response;
208230
}
209231

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+
210243
private void changeMenu(String name) {
211244
menu = name;
212245
System.out.println("you are now in " + name);

0 commit comments

Comments
 (0)