summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control/network/GameJoinControl.java
blob: afb78d4b77cbcc6a109690a63bf48bff9c021123 (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
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 java.util.UUID;

import jrummikub.model.GameSettings;
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.IListener1;
import jrummikub.util.IListener2;
import jrummikub.util.IListener3;
import jrummikub.view.ISettingsPanel;
import jrummikub.view.IView;
import jrummikub.view.ISettingsPanel.SettingsMode;

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

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

							updateSettingsPanel(settings);
						}
					}
				}));
		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 playerNumber, Color color) {
						// TODO Auto-generated method stub

					}
				}));
	}

	private void updateSettingsPanel(GameSettings settings) {
		view.getSettingsPanel().setGameSettings(settings);

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

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

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

	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 Event getBackEvent(){
		return backEvent;
	}

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

	/**
	 * Aborts joining and goes back to game list
	 */
	private void goBack() {
		abort();
		connectionControl.leaveGame();
		view.showSettingsPanel(false);
		backEvent.emit();
	}

	/**
	 * Aborts joining
	 */
	public void abort() {
		for (Connection c : connections) {
			c.remove();
		}

	}
}