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

import java.util.ArrayList;
import java.util.List;

import jrummikub.model.GameSettings;
import jrummikub.model.PlayerSettings;
import jrummikub.model.PlayerSettings.Type;
import jrummikub.util.Connection;
import jrummikub.util.GameData;
import jrummikub.util.IListener1;
import jrummikub.view.ISettingsPanel.SettingsMode;
import jrummikub.view.IView;

public class GameJoinControl {
	private List<Connection> connections = new ArrayList<Connection>();
	private GameData gameData;
	private ConnectionControl connectionControl;
	private IView view;

	public GameJoinControl(final ConnectionControl connectionControl,
			final GameData gameData, final IView view) {
		this.connectionControl = connectionControl;
		this.gameData = gameData;
		this.view = view;

		view.getSettingsPanel().setSettingsMode(SettingsMode.NETWORK_JOIN);
		view.getSettingsPanel().enableAddPlayerButton(false);
		view.getSettingsPanel().setGameSettings(gameData.getGameSettings());

		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);
							view.getSettingsPanel().setGameSettings(settings);
						}
					}
				}));
	}

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

	public void startGameJoin() {
		view.showSettingsPanel(true);
	}

	public void abort() {
		// TODO Implement abort

		for (Connection c : connections) {
			c.remove();
		}
	}
}