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

import java.util.UUID;

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);

		updateSettingsPanel();

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

							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.getParticipantLeftEvent().add(
				new IListener1<String>() {
					@Override
					public void handle(String nickname) {
						if (nickname.equals(gameData.getHost())) {
							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();
	}
}