summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control/ApplicationControl.java
blob: 04f18ac0c809d8be5dc5bed43c545629f7dc1072 (plain)
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
package jrummikub.control;

import jrummikub.control.network.ConnectionControl;
import jrummikub.control.network.NetworkControl;
import jrummikub.model.GameSettings;
import jrummikub.model.GameState;
import jrummikub.model.IRoundState;
import jrummikub.server.DedicatedServer;
import jrummikub.server.DedicatedServer.ServerStatus;
import jrummikub.util.Connection;
import jrummikub.util.IListener;
import jrummikub.util.IListener1;
import jrummikub.util.IListener3;
import jrummikub.util.LoginData;
import jrummikub.view.IQuitWarningPanel.QuitMode;
import jrummikub.view.IView;
import jrummikub.view.IView.BottomPanelType;

/**
 * The application control controls the settings for a new games and create the
 * game control
 */
public class ApplicationControl {
	private SettingsControl settingsControl;
	private LoginControl loginControl;
	private NetworkControl networkControl;
	private SaveControl saveControl;
	private GameControl gameControl;
	private Connection tempConnection;
	private DedicatedServer server;

	private IView view;

	/**
	 * Creates a new application control
	 * 
	 * @param view
	 *            the view to use
	 */
	public ApplicationControl(final IView view) {
		this.view = view;
		saveControl = new SaveControl(view);

		addQuitGameHandlers();
		addSaveLoadHandlers();
		addNewNetworkGameHandler();

		saveControl.getLoadEvent().add(
				new IListener3<GameSettings, GameState, IRoundState>() {
					@Override
					public void handle(GameSettings settings,
							GameState gameState, IRoundState roundState) {
						abortControls();
						gameControl = new GameControl(settings, saveControl,
								view);
						addGameControlListeners(gameControl);
						gameControl.continueGame(gameState, roundState);
					}
				});
		saveControl.getLoadErrorEvent().add(new IListener() {
			@Override
			public void handle() {
				view.showLoadingError();
			}
		});
	}

	private void addSaveLoadHandlers() {
		view.getLoadEvent().add(new IListener() {
			@Override
			public void handle() {
				if (!isGameRunning()) {
					abortControls();
					startApplication();
					view.load();
					return;
				}
				view.getQuitWarningPanel().setMode(QuitMode.QUIT_GAME);
				view.showQuitWarningPanel(true);

				tempConnection = view.getQuitWarningPanel().getQuitEvent()
						.add(new IListener() {
							@Override
							public void handle() {
								abortControls();
								startApplication();
								view.showQuitWarningPanel(false);
								view.load();
								tempConnection.remove();
							}
						});
			}
		});
		
	}

	private void addNewNetworkGameHandler() {
		view.getNetworkGameEvent().add(new IListener() {
			@Override
			public void handle() {
				if (!isGameRunning()) {
					abortControls();
					createLoginControl(true);
				} else {
					view.getQuitWarningPanel().setMode(QuitMode.QUIT_GAME);
					view.showQuitWarningPanel(true);
					tempConnection = view.getQuitWarningPanel().getQuitEvent()
							.add(new IListener() {
								@Override
								public void handle() {
									view.showQuitWarningPanel(false);
									abortControls();
									createLoginControl(true);
									tempConnection.remove();
									tempConnection = null;
								}
							});
				}
			}
		});
	}

	private void addQuitGameHandlers() {
		view.getMenuNewGameEvent().add(new IListener() {
			@Override
			public void handle() {
				showRestartWarning();
			}
		});
		view.getQuitEvent().add(new IListener() {
			@Override
			public void handle() {
				if (!isGameRunning()) {
					System.exit(0);
				} else {
					showQuitWarning();
				}
			}
		});
		view.getQuitWarningPanel().getCancelEvent().add(new IListener() {
			@Override
			public void handle() {
				view.showQuitWarningPanel(false);
				if (tempConnection != null) {
					tempConnection.remove();
					tempConnection = null;
				}
			}
		});
	}

	private void showRestartWarning() {
		view.getQuitWarningPanel().setMode(QuitMode.QUIT_GAME);
		view.showQuitWarningPanel(true);
		tempConnection = view.getQuitWarningPanel().getQuitEvent()
				.add(new IListener() {
					@Override
					public void handle() {
						abortControls();
						startApplication();
						view.showQuitWarningPanel(false);
						tempConnection.remove();
						tempConnection = null;
					}
				});
	}

	private void showQuitWarning() {
		view.getQuitWarningPanel().setMode(QuitMode.QUIT_PROCESS);
		view.showQuitWarningPanel(true);
		tempConnection = view.getQuitWarningPanel().getQuitEvent()
				.add(new IListener() {
					@Override
					public void handle() {
						System.exit(0);
					}
				});
	}

	/**
	 * Checks if a game is currently running
	 * @return true when a game is running
	 */
	public boolean isGameRunning() {
		return gameControl != null || networkControl != null
				&& networkControl.isGameRunning();
	}

	/**
	 * Create a new network login control
	 */
	private void createLoginControl(boolean reset) {
		loginControl = new LoginControl(view, this);
		loginControl.getLoginEvent().add(new IListener1<LoginData>() {
			@Override
			public void handle(LoginData loginData) {
				createNetworkControl(loginData);
			}
		});
		loginControl.getCancelEvent().add(new IListener() {
			@Override
			public void handle() {
				startApplication();
			}
		});
		loginControl.startLogin(reset);
	}

	/**
	 * End all controls in case of e.g. quit event
	 */
	private void abortControls() {
		if (settingsControl != null) {
			settingsControl.abort();
			settingsControl = null;
		}

		if (gameControl != null) {
			gameControl.abortGame();
			gameControl = null;
		}

		if (loginControl != null) {
			loginControl.abort();
			loginControl = null;
		}

		if (networkControl != null) {
			networkControl.abort();
			networkControl = null;
		}

		view.showSidePanel(false);
		view.showScorePanel(false);
		view.setBottomPanel(BottomPanelType.START_GAME_PANEL);
	}

	/**
	 * Starts the application by showing the game settings dialog panel
	 */
	public void startApplication() {
		view.showSidePanel(false);
		view.showScorePanel(false);
		view.setBottomPanel(BottomPanelType.START_GAME_PANEL);
		saveControl.setGameSettings(null);
		saveControl.setGameState(null);

		settingsControl = new SettingsControl(view, new GameSettings());
		view.enableSave(false);

		settingsControl.getStartGameEvent().add(new IListener1<GameSettings>() {
			@Override
			public void handle(GameSettings settings) {
				view.enableSave(true);
				settingsControl = null;

				saveControl.setGameSettings(settings);

				gameControl = new GameControl(settings, saveControl, view);
				addGameControlListeners(gameControl);

				gameControl.startGame();
			}
		});
		settingsControl.startSettings();
	}

	/**
	 * Adds events listeners to game control events
	 * 
	 * @param gameControl
	 *            of current game
	 */
	private void addGameControlListeners(GameControl gameControl) {
		gameControl.getEndOfGameEvent().add(new IListener() {
			@Override
			public void handle() {
				startApplication();
			}
		});
	}

	/**
	 * Create a new network game control
	 * 
	 * @param loginData
	 *            users login data for channel
	 */
	private void createNetworkControl(LoginData loginData) {
		ConnectionControl connectionControl = new ConnectionControl(loginData);
		networkControl = new NetworkControl(loginData, connectionControl,
				saveControl, view);

		networkControl.getStopNetworkEvent().add(new IListener() {
			@Override
			public void handle() {
				startApplication();
			}
		});

		networkControl.getBackToLoginEvent().add(new IListener() {
			@Override
			public void handle() {
				networkControl = null;
				createLoginControl(false);
			}
		});

		networkControl.startNetwork();
	}

	/**
	 * Ensure the dedicated server is running
	 * 
	 * @param password
	 *            password to use, if empty "jrummikub" is used
	 * @return whether the server could be started
	 */
	public boolean startDedicatedServer(String password) {
		if (server == null) {
			DedicatedServer newServer = new DedicatedServer(password);
			ServerStatus status = newServer.start();
			switch (status) {
			case STARTED:
				server = newServer;
				break;
			case ALREADY_RUNNING:
				view.showServerStartupError(true);
				return false;
			case ERROR:
				view.showServerStartupError(false);
				return false;
			}
		}
		server.setServerPassword(password);
		view.getLoginPanel().setServer(server.getHostName());
		view.getLoginPanel().setChannel(
				"jrummikub@play." + server.getHostName());
		view.getLoginPanel().setDedicatedServerRunning(true);
		return true;
	}

	/**
	 * If the login given is to our own dedicated server, update it's password
	 * to match
	 * 
	 * @param loginData
	 *            login data of user trying to connect
	 */
	public void updateDedicatedServerPassword(LoginData loginData) {
		if (server != null && server.getHostName() == loginData.getServerName()) {
			server.setServerPassword(loginData.getPassword());
		}
	}
}