summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control/network/GameJoinControl.java
blob: b9690b6a9f4e0db2f827d927efbc89d4a6507a66 (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.model.PlayerSettings;
import jrummikub.model.PlayerSettings.Type;
import jrummikub.util.GameData;
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 {

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

		fixGameSettings(gameData.getGameSettings());
		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();

							fixGameSettings(settings);

							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() {
				startGame();
			}
		}));
	}

	private void fixGameSettings(GameSettings settings) {
		for (PlayerSettings player : settings.getPlayerList()) {
			if (player.getType() == Type.HUMAN) {
				player.setType(Type.NETWORK);
			} else if (player.getType() == Type.NETWORK
					&& player.getName().equals(connectionControl.getNickname())) {
				player.setType(Type.HUMAN);
			}
		}
	}

	/**
	 * 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();
	}
}