5
5
import Client .controllers .HexController ;
6
6
import Client .enums .QueryRequests ;
7
7
import Client .models .User ;
8
+ import Client .models .network .Response ;
8
9
import com .google .gson .Gson ;
9
10
import com .google .gson .reflect .TypeToken ;
11
+ import javafx .animation .KeyFrame ;
12
+ import javafx .animation .Timeline ;
10
13
import javafx .event .EventHandler ;
11
14
import javafx .fxml .FXML ;
12
15
import javafx .scene .control .*;
17
20
import javafx .scene .layout .VBox ;
18
21
import javafx .scene .paint .Color ;
19
22
import javafx .scene .text .Text ;
23
+ import javafx .util .Duration ;
20
24
21
25
import java .io .IOException ;
22
26
import java .util .ArrayList ;
@@ -47,10 +51,22 @@ public class StartGameMenuController {
47
51
private Button saveGameButton ;
48
52
@ FXML
49
53
private TextField saveNameTextField ;
54
+ @ FXML
55
+ private VBox lobbyPeopleVBox ;
56
+ public static boolean updateInvites = false ;
57
+ public Timeline timeline ;
50
58
51
59
public void initialize () {
52
60
initPanes ();
53
61
initInvitations ();
62
+ timeline = new Timeline (new KeyFrame (Duration .millis (500 ), actionEvent -> {
63
+ if (updateInvites ) {
64
+ updateInvites = false ;
65
+ initInvitations ();
66
+ updateLobbyPeopleVBox ();
67
+ }
68
+ }));
69
+ timeline .setCycleCount (-1 );
54
70
// continueButton.setVisible(WorldController.getWorld() != null);
55
71
// saveGameButton.setVisible(WorldController.getWorld() != null);
56
72
// saveNameTextField.setVisible(WorldController.getWorld() != null);
@@ -65,6 +81,15 @@ public void initialize() {
65
81
cheatCodeText .setVisible (!cheatCodeText .isVisible ());
66
82
}
67
83
});
84
+ updateLobbyPeopleVBox ();
85
+ timeline .play ();
86
+ }
87
+
88
+ public void updateLobbyPeopleVBox () {
89
+ lobbyPeopleVBox .getChildren ().clear ();
90
+ for (String s : MainMenuController .loggedInUser .getPeopleInLobby ()) {
91
+ lobbyPeopleVBox .getChildren ().add (new Text (s ));
92
+ }
68
93
}
69
94
70
95
public void initPanes () {
@@ -82,38 +107,32 @@ public void initPanes() {
82
107
83
108
public void initInvitations () {
84
109
invitationsVBox .getChildren ().clear ();
85
- // for (String invitation : UserController.getLoggedInUser() .getInvitations()) {
86
- // invitationsVBox.getChildren().add(makeInvitationBox(invitation));
87
- // }
110
+ for (String invitation : MainMenuController . loggedInUser .getInvitations ()) {
111
+ invitationsVBox .getChildren ().add (makeInvitationBox (invitation ));
112
+ }
88
113
}
89
114
90
115
public HBox makeInvitationBox (String invitation ) {
91
116
Button accept = new Button ("✔" );
92
117
Button decline = new Button ("❌" );
93
- accept .setOnMouseClicked (new EventHandler <MouseEvent >() {
94
- @ Override
95
- public void handle (MouseEvent mouseEvent ) {
96
- // User host = UserController.getUserByUsername(invitation.substring(16));
97
- // assert host != null;
98
- // UserController.getLoggedInUser().resetPeopleInLobby();
99
- // for (String s : host.getPeopleInLobby()) {
100
- // UserController.getLoggedInUser().addPersonToLobby(s);
101
- // }
102
- // host.addPersonToLobby(UserController.getLoggedInUser().getUsername());
103
- // for (String s : host.getPeopleInLobby()) {
104
- // if (!s.equals(host.getUsername()) && !s.equals(UserController.getLoggedInUser().getUsername()))
105
- // Objects.requireNonNull(UserController.getUserByUsername(s)).addPersonToLobby(UserController.getLoggedInUser().getUsername());
106
- // }
107
- // UserController.getLoggedInUser().removeInvitation(invitation);
108
- // initInvitations();
109
- }
118
+ accept .setOnMouseClicked (mouseEvent -> {
119
+ Response response = ClientSocketController .sendRequestAndGetResponse (QueryRequests .ACCEPT_INVITATION , new HashMap <>() {{
120
+ put ("loggedInUser" , MainMenuController .loggedInUser .getUsername ());
121
+ put ("host" , invitation .substring (16 ));
122
+ put ("invitation" , invitation );
123
+ }});
124
+ assert response != null ;
125
+ MainMenuController .loggedInUser = new Gson ().fromJson (response .getParams ().get ("user" ), User .class );
126
+ initInvitations ();
110
127
});
111
- decline .setOnMouseClicked (new EventHandler <MouseEvent >() {
112
- @ Override
113
- public void handle (MouseEvent mouseEvent ) {
114
- // UserController.getLoggedInUser().removeInvitation(invitation);
115
- // initInvitations();
116
- }
128
+ decline .setOnMouseClicked (mouseEvent -> {
129
+ Response response = ClientSocketController .sendRequestAndGetResponse (QueryRequests .DECLINE_INVITATION , new HashMap <>() {{
130
+ put ("loggedInUser" , MainMenuController .loggedInUser .getUsername ());
131
+ put ("invitation" , invitation );
132
+ }});
133
+ assert response != null ;
134
+ MainMenuController .loggedInUser = new Gson ().fromJson (response .getParams ().get ("user" ), User .class );
135
+ initInvitations ();
117
136
});
118
137
accept .setStyle ("-fx-pref-width: 50" );
119
138
decline .setStyle ("-fx-pref-width: 50" );
@@ -131,43 +150,55 @@ public void cheatCodeAreaTyped(KeyEvent keyEvent) {
131
150
if (command .equals ("clear" )) {
132
151
cheatCodeArea .clear ();
133
152
} else {
134
- ClientSocketController .sendRequestAndGetResponse (QueryRequests .CHEAT_COMMAND , new HashMap <>(){{
153
+ ClientSocketController .sendRequestAndGetResponse (QueryRequests .CHEAT_COMMAND , new HashMap <>() {{
135
154
put ("command" , command );
136
155
}});
137
156
}
138
157
}
139
158
}
140
159
141
160
public void backButtonClicked (MouseEvent mouseEvent ) {
161
+ timeline .stop ();
162
+ ClientSocketController .sendRequestAndGetResponse (QueryRequests .LEAVE_LOBBY , new HashMap <>(){{
163
+ put ("username" , MainMenuController .loggedInUser .getUsername ());
164
+ }});
142
165
App .changeScene ("mainMenuPage" );
143
166
}
144
167
145
168
public void sendInvitations (MouseEvent mouseEvent ) {
146
- // for (MenuItem item : usernamesMenuButton.getItems()) {
147
- // if (((CheckMenuItem) item).isSelected())
148
- // Objects.requireNonNull(UserController.getUserByUsername(item.getText())).addInvitations(UserController.getLoggedInUser().getUsername());
149
- // }
169
+ ArrayList <String > receivers = new ArrayList <>();
170
+ for (MenuItem item : usernamesMenuButton .getItems ()) {
171
+ if (((CheckMenuItem ) item ).isSelected ())
172
+ receivers .add (item .getText ());
173
+ }
174
+ if (receivers .size () >= 1 )
175
+ ClientSocketController .sendRequestAndGetResponse (QueryRequests .SEND_INVITATION , new HashMap <>() {{
176
+ put ("sender" , MainMenuController .loggedInUser .getUsername ());
177
+ put ("receivers" , new Gson ().toJson (receivers ));
178
+ }});
150
179
}
151
180
152
181
public void startGameButtonClicked (MouseEvent mouseEvent ) {
153
- // if (UserController.getLoggedInUser() .getPeopleInLobby().size() < 2) {
154
- // System.out.println("can't start");
155
- // return;
156
- // } else if (UserController.getLoggedInUser() .getPeopleInLobby().size() > numberOfPlayersSpinner.getValue()) {
157
- // System.out.println("number of players is less the actual players");
158
- // return;
159
- // }
182
+ if (MainMenuController . loggedInUser .getPeopleInLobby ().size () < 2 ) {
183
+ System .out .println ("can't start" );
184
+ return ;
185
+ } else if (MainMenuController . loggedInUser .getPeopleInLobby ().size () > numberOfPlayersSpinner .getValue ()) {
186
+ System .out .println ("number of players is less the actual players" );
187
+ return ;
188
+ }
160
189
System .out .println (MainMenuController .loggedInUser .getPeopleInLobby () + " * " + mapWidthSpinner .getValue () + " " + mapHeightSpinner .getValue ());
161
190
HexController .generateHexes (mapWidthSpinner .getValue (), mapHeightSpinner .getValue ());
162
- ClientSocketController .sendRequestAndGetResponse (QueryRequests .NEW_WORLD , new HashMap <>(){{
191
+ ClientSocketController .sendRequestAndGetResponse (QueryRequests .NEW_WORLD , new HashMap <>() {{
163
192
put ("people" , new Gson ().toJson (MainMenuController .loggedInUser .getPeopleInLobby ()));
164
193
put ("width" , String .valueOf (mapWidthSpinner .getValue ()));
165
194
put ("height" , String .valueOf (mapHeightSpinner .getValue ()));
166
195
}});
196
+ timeline .stop ();
167
197
App .changeScene ("gamePage" );
168
198
}
169
199
170
200
public void continueButtonClicked (MouseEvent mouseEvent ) {
201
+ timeline .stop ();
171
202
// App.changeScene("gamePage");
172
203
}
173
204
0 commit comments