summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control/network/NetworkSettingsControl.java
blob: 1f6cc3d1803f2caa7d018454d086ff9bbb725a1a (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package jrummikub.control.network;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import jrummikub.control.AbstractSettingsControl;
import jrummikub.model.GameSettings;
import jrummikub.model.PlayerSettings;
import jrummikub.model.PlayerSettings.Type;
import jrummikub.util.Event;
import jrummikub.util.Event1;
import jrummikub.util.IListener;
import jrummikub.view.ISettingsPanel;
import jrummikub.view.ISettingsPanel.SettingsMode;
import jrummikub.view.IView;

/**
 * Settings for network games, entered by host
 */
public class NetworkSettingsControl extends AbstractSettingsControl {
	private String nickname;
	private Event1<GameSettings> offerGameEvent = new Event1<GameSettings>();
	private Event backEvent = new Event();

	/**
	 * Creates a new network settings control
	 * 
	 * @param nickname
	 *          to be set as player name
	 * @param view
	 *          for events and handlers
	 * @param settings
	 *          for playerNumber, color...
	 */
	public NetworkSettingsControl(String nickname, IView view,
			GameSettings settings) {
		super(view, settings);
		this.nickname = nickname;

		view.getSettingsPanel().setSettingsMode(SettingsMode.NETWORK_SETUP);

		addFirstPlayer();
		addPlayer();

		addListeners();

		connections.add(view.getSettingsPanel().getOfferGameEvent()
				.add(new IListener() {
					@Override
					public void handle() {
						offerGame();
					}
				}));

		connections.add(view.getSettingsPanel().getBackEvent().add(new IListener() {
			@Override
			public void handle() {
				abort();
				backEvent.emit();
			}
		}));
	}

	/**
	 * The game offer event is emitted when a player wants to offer a new network
	 * game
	 * 
	 * @return the event
	 */
	public Event1<GameSettings> getOfferGameEvent() {
		return offerGameEvent;
	}

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

	private void offerGame() {
		if (!checkSettings()) {
			return;
		}
		abort();
		offerGameEvent.emit(settings);
	}

	@Override
	protected void update() {
		view.getSettingsPanel().enableAddPlayerButton(
				settings.getPlayerList().size() < ISettingsPanel.PLAYER_COLORS.length);

		checkSettings();

		List<List<Type>> choices = new ArrayList<List<Type>>();
		List<Boolean> enableRemoveButtons = new ArrayList<Boolean>();
		List<Boolean> playerNamesEditable = new ArrayList<Boolean>();

		for (int i = 0; i < settings.getPlayerList().size(); i++) {
			if (i == 0) {
				choices.add(Collections.singletonList(Type.HUMAN));
				enableRemoveButtons.add(false);
			} else {
				choices.add(Arrays.asList(Type.VACANT, Type.COMPUTER));
				enableRemoveButtons.add(settings.getPlayerList().size() > 2);
			}

			playerNamesEditable
					.add(settings.getPlayerList().get(i).getType() == Type.COMPUTER);
		}
		view.getSettingsPanel().enableRemovePlayerButtons(enableRemoveButtons);
		view.getSettingsPanel().setPlayerNamesEditable(playerNamesEditable);
		view.getSettingsPanel().setPlayerTypeChoices(choices);

		view.getSettingsPanel().setGameSettings(settings);
	}

	@Override
	protected void setPlayerType(int i, Type type) {
		Type oldType = settings.getPlayerList().get(i).getType();

		settings.getPlayerList().get(i).setType(type);

		if (type == Type.VACANT) {
			settings.getPlayerList().get(i).setName("Offen");
		} else if (oldType != Type.COMPUTER && type == Type.COMPUTER) {
			// Find unused player name
			int num = 0;

			nameLoop: while (true) {
				num++;
				String playerName = "Computer " + num;

				for (PlayerSettings player : settings.getPlayerList()) {
					if (player.getType() == Type.COMPUTER
							&& playerName.equals(player.getName())) {
						continue nameLoop;
					}
				}
				break;
			}

			settings.getPlayerList().get(i).setName("Computer " + num);
		}

		update();
	}

	private void addFirstPlayer() {
		PlayerSettings playerSettings = new PlayerSettings(nickname,
				findUnusedColor());
		settings.getPlayerList().add(playerSettings);
	}

	@Override
	protected void addPlayer() {
		if (settings.getPlayerList().size() >= ISettingsPanel.PLAYER_COLORS.length) {
			return;
		}

		PlayerSettings playerSettings = new PlayerSettings("Offen",
				findUnusedColor());
		playerSettings.setType(Type.VACANT);
		settings.getPlayerList().add(playerSettings);

		update();
	}
}