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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
package jrummikub.control.network;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import jrummikub.control.SaveControl;
import jrummikub.model.GameSettings;
import jrummikub.util.Connection;
import jrummikub.util.Event;
import jrummikub.util.GameData;
import jrummikub.util.IEvent;
import jrummikub.util.IListener;
import jrummikub.util.IListener1;
import jrummikub.util.LoginData;
import jrummikub.view.IView;
import jrummikub.view.IView.BottomPanelType;
import jrummikub.view.LoginError;
/**
* Class dealing with network connection, offering and choice of network games
*/
public class NetworkControl {
private IConnectionControl connectionControl;
private IView view;
private List<Connection> connections = new ArrayList<Connection>();
private Event stopNetworkEvent = new Event();
private Event backToLoginEvent = new Event();
private NetworkSettingsControl settingsControl;
private GameOfferControl gameOfferControl;
private GameJoinControl gameJoinControl;
private SaveControl saveControl;
private NetworkGameControl gameControl;
private List<UUID> games = new ArrayList<UUID>();
private Map<UUID, GameData> gameMap = new HashMap<UUID, GameData>();
/**
* Creates a new network control
*
* @param loginData
* user's login data
* @param connectionControl
* current connection for events and messages
* @param saveControl
* save control if saving will ever be allowed
* @param view
* for events and handlers
*/
public NetworkControl(final LoginData loginData,
IConnectionControl connectionControl, SaveControl saveControl,
final IView view) {
this.view = view;
this.connectionControl = connectionControl;
this.saveControl = saveControl;
addConnectionSetupListeners(loginData);
addConnectionControlListeners();
addViewEventListeners();
}
private void addViewEventListeners() {
connections.add(view.getGameListPanel().getJoinEvent()
.add(new IListener1<GameData>() {
@Override
public void handle(GameData gameData) {
join(gameData);
}
}));
connections.add(view.getGameListPanel().getOpenNewGameEvent()
.add(new IListener() {
@Override
public void handle() {
createSettingsControl();
}
}));
connections.add(view.getGameListPanel().getCancelEvent()
.add(new IListener() {
@Override
public void handle() {
abort();
stopNetworkEvent.emit();
}
}));
}
private void join(GameData gameData) {
view.showGameListPanel(false);
connectionControl.joinGame(gameData);
}
/**
* Adds the listeners for connection control events
*/
private void addConnectionControlListeners() {
addOfferUpdateListener();
connections.add(connectionControl.getGameWithdrawalEvent().add(
new IListener1<UUID>() {
@Override
public void handle(UUID uuid) {
games.remove(uuid);
gameMap.remove(uuid);
updateGameList();
}
}));
connections.add(connectionControl.getGameJoinAckEvent().add(
new IListener1<Boolean>() {
@Override
public void handle(Boolean ack) {
if (ack) {
createGameJoinControl();
} else {
view.showGameListPanel(true);
}
}
}));
addConnectionLostListeners();
}
/**
* Adds the listeners for lost connection events
*/
private void addConnectionLostListeners() {
connections.add(connectionControl.getParticipantLeftEvent().add(
new IListener1<String>() {
@Override
public void handle(String nickname) {
for (Map.Entry<UUID, GameData> entry : gameMap.entrySet()) {
if (entry.getValue().getHost().equals(nickname)) {
games.remove(entry.getKey());
gameMap.remove(entry.getKey());
break;
}
}
updateGameList();
}
}));
connections.add(connectionControl.getConnectionLostEvent().add(
new IListener() {
@Override
public void handle() {
abort();
view.setBottomPanel(BottomPanelType.NETWORK_SERVER_CONNECTION_LOST_PANEL);
connections.add(view.getAcknowledgeConnectionLostEvent().add(
new IListener() {
@Override
public void handle() {
abort();
view.setBottomPanel(BottomPanelType.START_GAME_PANEL);
backToLoginEvent.emit();
}
}));
}
}));
}
private void addOfferUpdateListener() {
connections.add(connectionControl.getGameOfferEvent().add(
new IListener1<GameData>() {
@Override
public void handle(GameData gameData) {
UUID uuid = gameData.getGameID();
GameData game = gameMap.get(uuid);
if (game == null) {
game = gameData;
gameMap.put(uuid, gameData);
games.add(uuid);
} else {
game.setGameSettings(gameData.getGameSettings());
}
updateGameList();
}
}));
}
private void addConnectionSetupListeners(final LoginData loginData) {
connections.add(connectionControl.getConnectedEvent().add(new IListener() {
@Override
public void handle() {
view.getGameListPanel().setChannelName(loginData.getChannelName());
view.showConnectPanel(false);
view.showGameListPanel(true);
}
}));
connections.add(connectionControl.getConnectionFailedEvent().add(
new IListener1<LoginError>() {
@Override
public void handle(LoginError error) {
view.getConnectPanel().setMode(error);
view.showConnectPanel(true);
}
}));
connections.add(view.getConnectPanel().getCancelEvent()
.add(new IListener() {
@Override
public void handle() {
abort();
backToLoginEvent.emit();
}
}));
}
private void updateGameList() {
List<GameData> gameList = new ArrayList<GameData>();
for (UUID uuid : games) {
gameList.add(gameMap.get(uuid));
}
view.getGameListPanel().setGameList(gameList);
}
/**
* Starts a new network connection with the specified data
*/
public void startNetwork() {
view.showConnectPanel(true);
view.getConnectPanel().setMode(null);
connectionControl.connect();
}
/**
* Ends the network connection if canceled
*/
public void abort() {
for (Connection c : connections) {
c.remove();
}
abortControls();
connectionControl.disconnect();
}
private void abortControls() {
if (settingsControl != null) {
settingsControl.abort();
settingsControl = null;
}
if (gameOfferControl != null) {
gameOfferControl.abort();
gameOfferControl = null;
}
if (gameJoinControl != null) {
gameJoinControl.abort();
gameJoinControl = null;
}
if (gameControl != null) {
gameControl.abortGame();
gameControl = null;
}
view.showGameListPanel(false);
view.showConnectPanel(false);
view.showSidePanel(false);
view.setBottomPanel(BottomPanelType.START_GAME_PANEL);
}
/**
* Getter for stopNetworkEvent
*
* @return stopNetworkEvent
*/
public IEvent getStopNetworkEvent() {
return stopNetworkEvent;
}
/**
* The back to login event is emitted when the player aborted the connecting
* process or when a connection error has occured and been acknowledged
*
* @return the event
*/
public IEvent getBackToLoginEvent() {
return backToLoginEvent;
}
private void createSettingsControl() {
if (settingsControl != null) {
return;
}
view.showGameListPanel(false);
settingsControl = new NetworkSettingsControl(
connectionControl.getNickname(), view, new GameSettings());
settingsControl.getOfferGameEvent().add(new IListener1<GameSettings>() {
@Override
public void handle(GameSettings settings) {
settingsControl = null;
createGameOfferControl(settings);
}
});
settingsControl.getBackEvent().add(new IListener() {
@Override
public void handle() {
settingsControl = null;
view.showGameListPanel(true);
}
});
settingsControl.startSettings();
}
private void createGameJoinControl() {
if (gameJoinControl != null) {
return;
}
GameData gameData = connectionControl.getCurrentGame();
gameJoinControl = new GameJoinControl(connectionControl, gameData, view);
gameJoinControl.getBackEvent().add(new IListener() {
@Override
public void handle() {
gameJoinControl = null;
view.showGameListPanel(true);
}
});
gameJoinControl.getStartGameEvent().add(new IListener() {
@Override
public void handle() {
createGameControl(null, false);
}
});
gameJoinControl.startGameJoin();
}
private void createGameOfferControl(GameSettings settings) {
if (gameOfferControl != null) {
return;
}
gameOfferControl = new GameOfferControl(connectionControl, settings, view);
gameOfferControl.getBackEvent().add(new IListener() {
@Override
public void handle() {
gameOfferControl = null;
view.showGameListPanel(true);
}
});
gameOfferControl.getStartGameEvent().add(new IListener1<GameSettings>() {
@Override
public void handle(GameSettings settings) {
createGameControl(settings, true);
}
});
gameOfferControl.startGameOffer();
}
private void createGameControl(GameSettings settings, boolean host) {
gameControl = new NetworkGameControl(settings, saveControl, view,
connectionControl, host);
gameControl.getBackEvent().add(new IListener() {
@Override
public void handle() {
view.setBottomPanel(BottomPanelType.START_GAME_PANEL);
abortControls();
view.showGameListPanel(true);
}
});
gameControl.getEndOfGameEvent().add(new IListener() {
@Override
public void handle() {
view.setBottomPanel(BottomPanelType.START_GAME_PANEL);
abortControls();
view.showGameListPanel(true);
}
});
gameControl.startGame();
}
/**
* Returns if there is a running game
*
* @return is there a game running
*/
public boolean isGameRunning() {
return gameControl != null;
}
}
|