summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control/network/GameOfferControl.java
blob: f0dc526b63738a49c9c55d63fb8a5182a2352bc4 (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
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.GameData;
import jrummikub.util.IListener;
import jrummikub.util.IListener1;
import jrummikub.util.IListener2;
import jrummikub.view.ISettingsPanel;
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);
		updateSettingsPanel(settings);

		connections.add(connectionControl.getGameJoinEvent().add(
				new IListener1<String>() {
					@Override
					public void handle(String sender) {
						for (PlayerSettings player : settings.getPlayerList()) {
							if (player.getType() == Type.VACANT) {
								player.setName(sender);
								player.setType(Type.NETWORK);
								updateSettingsPanel(settings);
								connectionControl.ackJoinGame(sender, true);
								connectionControl.offerGame(gameData);
								return;
							}
						}

						connectionControl.ackJoinGame(sender, false);
					}
				}));
		connections.add(view.getSettingsPanel().getChangePlayerColorEvent()
				.add(new IListener2<Integer, Color>() {
					@Override
					public void handle(Integer i, Color color) {
						for (PlayerSettings player : settings.getPlayerList()) {
							if (player.getColor() == color) {
								return;
							}
						}
						settings.getPlayerList().get(i).setColor(color);
						updateSettingsPanel(settings);
					}
				}));
		connections.add(connectionControl.getGameLeaveEvent().add(new IListener1<String>() {
			@Override
			public void handle(String sender) {
				List<PlayerSettings> players = gameData.getGameSettings().getPlayerList();
				int index=0;
				for(PlayerSettings s:players){
					if (s.getName().equals(sender)){
						break;
					}
					index++;
				}
				//Only remove network players
				if(gameData.getGameSettings().getPlayerList().get(index).getType() == Type.NETWORK){
					gameData.getGameSettings().getPlayerList().get(index).setType(Type.VACANT);
					gameData.getGameSettings().getPlayerList().get(index).setName("Offen");
				}
				updateSettingsPanel(gameData.getGameSettings());
				connectionControl.offerGame(gameData);
				
			}
		}));
		connections.add(view.getSettingsPanel().getBackEvent().add(new IListener() {
			@Override
			public void handle() {
				// 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);
	}

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

		view.showSettingsPanel(true);

	}

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