summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control/network/GameJoinControl.java
blob: d846ebc67feffe711fbb0e2c33b52328913c9f4d (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
package jrummikub.control.network;

import java.util.UUID;

import jrummikub.model.GameSettings;
import jrummikub.util.Event;
import jrummikub.util.GameData;
import jrummikub.util.IEvent;
import jrummikub.util.IListener;
import jrummikub.util.IListener1;
import jrummikub.view.ISettingsPanel.SettingsMode;
import jrummikub.view.IView;

/**
 * Control for joining a network game
 */
public class GameJoinControl extends AbstractGameBeginControl {
	private Event gameStartEvent = new Event();

	/**
	 * Creates new game join control
	 * 
	 * @param connectionControl
	 *          the current connection control for events and messages
	 * @param gameData
	 *          the game data for settings, game id
	 * @param view
	 *          the view
	 */
	public GameJoinControl(final IConnectionControl connectionControl,
			final GameData gameData, final IView view) {
		super(connectionControl, view, gameData, SettingsMode.NETWORK_JOIN);

		NetworkControl.fixGameSettings(gameData.getGameSettings(), connectionControl.getNickname());
		updateSettingsPanel();

		connections.add(connectionControl.getGameOfferEvent().add(
				new IListener1<GameData>() {
					@Override
					public void handle(GameData data) {
						if (data.getGameID().equals(gameData.getGameID())) {
							GameSettings settings = data.getGameSettings();

							NetworkControl.fixGameSettings(settings, connectionControl.getNickname());

							gameData.setGameSettings(settings);

							updateSettingsPanel();
						}
					}
				}));

		connections.add(connectionControl.getGameWithdrawalEvent().add(
				new IListener1<UUID>() {
					@Override
					public void handle(UUID uuid) {
						if (uuid.equals(gameData.getGameID())) {
							abort();
							backEvent.emit();
						}
					}
				}));

		connections.add(connectionControl.getGameStartEvent().add(new IListener() {
			@Override
			public void handle() {
				abort();
				gameStartEvent.emit();
			}
		}));
	}

	/**
	 * The event that is emitted when the game is started
	 * 
	 * @return the event
	 */
	public IEvent getStartGameEvent() {
		return gameStartEvent;
	}

	/**
	 * Starts the join control and sets the settings panel in game join mode
	 */
	public void startGameJoin() {
		view.showSettingsPanel(true);
	}

	/**
	 * Aborts joining and goes back to game list
	 */
	@Override
	protected void goBack() {
		abort();
		connectionControl.leaveGame();
		backEvent.emit();
	}
}