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

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

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

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

	public GameOfferControl(final ConnectionControl connectionControl,
			final GameSettings settings, final IView view) {
		this.connectionControl = connectionControl;
		this.view = view;

		gameData = new GameData(UUID.randomUUID(), settings);

		view.getSettingsPanel().setSettingsMode(SettingsMode.NETWORK_OFFER);
		view.getSettingsPanel().enableAddPlayerButton(false);
		view.getSettingsPanel().setGameSettings(settings);

		connections.add(connectionControl.getGameJoinEvent().add(
				new IListener2<UUID, String>() {
					@Override
					public void handle(UUID uuid, String sender) {
						if (!uuid.equals(gameData.getGameID())) {
							return;
						}

						for (PlayerSettings player : settings.getPlayerList()) {
							if (player.getType() == Type.VACANT) {
								player.setName(sender);
								player.setType(Type.NETWORK);
								view.getSettingsPanel().setGameSettings(settings);
								connectionControl.ackJoinGame(uuid, sender, true);
								connectionControl.offerGame(gameData);
								return;
							}
						}

						connectionControl.ackJoinGame(uuid, sender, false);
					}
				}));
	}

	public void startGameOffer() {
		connectionControl.offerGame(gameData);

		view.showSettingsPanel(true);

	}

	public void abort() {
		connectionControl.withdrawGame(gameData.getGameID());
	}
}