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

import java.awt.Color;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import jrummikub.model.PlayerSettings;
import jrummikub.model.PlayerSettings.Type;
import jrummikub.util.Connection;
import jrummikub.util.Event;
import jrummikub.util.GameData;
import jrummikub.util.IListener;
import jrummikub.util.IListener2;
import jrummikub.view.ISettingsPanel;
import jrummikub.view.ISettingsPanel.SettingsMode;
import jrummikub.view.IView;

/**
 * Abstract class for network game controls inbetween choosing and starting a
 * game
 */
public abstract class AbstractGameBeginControl {
	protected List<Connection> connections = new ArrayList<Connection>();
	protected GameData gameData;
	protected IConnectionControl connectionControl;
	protected IView view;
	protected Event backEvent = new Event();

	/**
	 * Create a new game begin control
	 * 
	 * @param connection
	 *            connection control for mesages and events
	 * @param view
	 *            the view
	 * @param gameData
	 *            game data of chosen game
	 * @param settingsMode
	 *            mode of settings panel
	 */
	public AbstractGameBeginControl(IConnectionControl connection, IView view,
			final GameData gameData, SettingsMode settingsMode) {
		this.connectionControl = connection;
		this.view = view;
		this.gameData = gameData;

		view.getSettingsPanel().setSettingsMode(settingsMode);
		view.getSettingsPanel().enableAddPlayerButton(false);
		updateSettingsPanel();

		addViewListeners(view, gameData);

		connections.add(connectionControl.getChangeColorEvent().add(
				new IListener2<String, Color>() {
					@Override
					public void handle(String sender, Color color) {
						List<PlayerSettings> players = gameData
								.getGameSettings().getPlayerList();
						for (PlayerSettings s : players) {
							if (s.getName().equals(sender)
									&& s.getType() == Type.NETWORK) {
								s.setColor(color);
								break;
							}
						}
						updateSettingsPanel();
					}
				}));
	}

	private void addViewListeners(IView view, final GameData gameData) {
		connections.add(view.getSettingsPanel().getBackEvent().add(
				new IListener() {
					@Override
					public void handle() {
						goBack();
					}
				}));
		connections.add(view.getSettingsPanel().getChangePlayerColorEvent()
				.add(new IListener2<Integer, Color>() {
					@Override
					public void handle(Integer i, Color color) {
						for (PlayerSettings player : gameData.getGameSettings()
								.getPlayerList()) {
							if (player.getColor() == color) {
								return;
							}
						}
						PlayerSettings player = gameData.getGameSettings()
								.getPlayerList().get(i);
						if (player.getType() != Type.HUMAN) {
							return;
						}
						player.setColor(color);
						connectionControl.changeColor(color);
						updateSettingsPanel();
					}
				}));
	}

	protected abstract void goBack();

	/**
	 * The back event is emitted when the player wants to go back to the
	 * previous control and panel
	 * 
	 * @return the event
	 */
	public Event getBackEvent() {
		return backEvent;
	}

	protected void abort() {
		view.showSettingsPanel(false);
		for (Connection c : connections) {
			c.remove();
		}
	}

	protected void updateSettingsPanel() {
		view.getSettingsPanel().setGameSettings(gameData.getGameSettings());

		Set<Color> colors = new HashSet<Color>(Arrays
				.asList(ISettingsPanel.PLAYER_COLORS));

		for (PlayerSettings player : gameData.getGameSettings().getPlayerList()) {
			colors.remove(player.getColor());
		}

		view.getSettingsPanel().setPlayerColors(colors);
	}
}