summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control/network/NetworkControl.java
blob: b6dc27e8427c476668c14e4a7e8cbbdbd1caa593 (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.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import jrummikub.model.GameSettings;
import jrummikub.util.Connection;
import jrummikub.util.Event;
import jrummikub.util.IEvent;
import jrummikub.util.IListener;
import jrummikub.util.IListener1;
import jrummikub.util.LoginData;
import jrummikub.view.IGameListPanel;
import jrummikub.view.IGameListPanel.GameData;
import jrummikub.view.IView;

public class NetworkControl {
	private ConnectionControl connectionControl;
	private IView view;
	private List<Connection> connections = new ArrayList<Connection>();
	private Event stopNetworkEvent = new Event();

	private NetworkSettingsControl settingsControl;

	private Map<UUID, GameData> gameMap = new HashMap<UUID, GameData>();

	public NetworkControl(final LoginData loginData, final IView view) {
		this.view = view;
		connectionControl = new ConnectionControl(loginData);

		connections.add(connectionControl.getConnectedEvent().add(new IListener() {
			@Override
			public void handle() {
				view.getGameListPanel().setChannelName(loginData.getChannelName());
				view.showGameListPanel(true);

				GameData testData = new GameData(UUID.randomUUID(), "NeoRaider");
				testData.setCurrentPlayerCount(2);
				testData.setMaxPlayerCount(4);

			}
		}));

		connections.add(connectionControl.getConnectionFailedEvent().add(
				new IListener() {
					@Override
					public void handle() {
						// TODO Auto-generated method stub

					}
				}));

		connections.add(connectionControl.getGameOfferEvent().add(
				new IListener1<IGameListPanel.GameData>() {
					@Override
					public void handle(GameData value) {
						GameData game = gameMap.get(value.getGameID());

						if (game == null) {
							game = value;
							gameMap.put(value.getGameID(), value);
						} else {
							game.setCurrentPlayerCount(value.getCurrentPlayerCount());
							game.setMaxPlayerCount(value.getMaxPlayerCount());
						}

						view.getGameListPanel().addGame(game);
					}
				}));
		connections.add(connectionControl.getGameWithdrawalEvent().add(
				new IListener1<UUID>() {
					@Override
					public void handle(UUID value) {
						GameData game = gameMap.get(value);

						if (game != null) {
							view.getGameListPanel().removeGame(game);
							gameMap.remove(value);
						}
					}
				}));

		connections.add(view.getGameListPanel().getJoinEvent()
				.add(new IListener1<IGameListPanel.GameData>() {
					@Override
					public void handle(GameData value) {
						// TODO Auto-generated method stub

					}
				}));

		connections.add(view.getGameListPanel().getOpenNewGameEvent()
				.add(new IListener() {
					@Override
					public void handle() {
						if (settingsControl == null) {
							view.showGameListPanel(false);

							settingsControl = new NetworkSettingsControl(connectionControl,
									view, new GameSettings());
							settingsControl.startSettings();
						}
					}
				}));

		connections.add(view.getGameListPanel().getCancelEvent()
				.add(new IListener() {
					@Override
					public void handle() {
						abort();
						stopNetworkEvent.emit();
					}
				}));
	}

	public void startNetwork() {
		connectionControl.connect();
	}

	public void abort() {
		for (Connection c : connections) {
			c.remove();
		}
		connectionControl.disconnect();
		view.showGameListPanel(false);
	}

	public IEvent getStopNetworkEvent() {
		return stopNetworkEvent;
	}

}