
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@479 72836036-5685-4462-b002-a69064685172
151 lines
3.9 KiB
Java
151 lines
3.9 KiB
Java
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 jrummikub.model.PlayerSettings;
|
|
import jrummikub.model.PlayerSettings.Type;
|
|
import jrummikub.util.Connection;
|
|
import jrummikub.util.Event;
|
|
import jrummikub.util.Event1;
|
|
import jrummikub.util.GameData;
|
|
import jrummikub.util.IEvent;
|
|
import jrummikub.util.IEvent1;
|
|
import jrummikub.util.IListener;
|
|
import jrummikub.util.IListener2;
|
|
import jrummikub.view.ISettingsPanel;
|
|
import jrummikub.view.ISettingsPanel.SettingsMode;
|
|
import jrummikub.view.IView;
|
|
|
|
/**
|
|
* Abstract class for network game controls in between choosing and starting a
|
|
* game
|
|
*/
|
|
public abstract class AbstractGameBeginControl {
|
|
protected List<Connection> connections = new ArrayList<Connection>();
|
|
protected GameData gameData;
|
|
protected IConnectionControl connectionControl;
|
|
protected IView view;
|
|
protected Event backEvent = new Event();
|
|
private Event1<GameData> gameStartEvent = new Event1<GameData>();
|
|
|
|
/**
|
|
* Create a new game begin control
|
|
*
|
|
* @param connection
|
|
* connection control for messages and events
|
|
* @param view
|
|
* the view
|
|
* @param gameData
|
|
* game data of chosen game
|
|
* @param settingsMode
|
|
* mode of settings panel
|
|
*/
|
|
public AbstractGameBeginControl(IConnectionControl connection, IView view,
|
|
final GameData gameData, SettingsMode settingsMode) {
|
|
this.connectionControl = connection;
|
|
this.view = view;
|
|
this.gameData = gameData;
|
|
|
|
view.getSettingsPanel().setSettingsMode(settingsMode);
|
|
view.getSettingsPanel().enableAddPlayerButton(false);
|
|
updateSettingsPanel();
|
|
|
|
addViewListeners(view, gameData);
|
|
|
|
connections.add(connectionControl.getChangeColorEvent().add(
|
|
new IListener2<String, Color>() {
|
|
@Override
|
|
public void handle(String sender, Color color) {
|
|
List<PlayerSettings> players = gameData.getGameSettings()
|
|
.getPlayerList();
|
|
for (PlayerSettings s : players) {
|
|
if (s.getName().equals(sender) && s.getType() == Type.NETWORK) {
|
|
s.setColor(color);
|
|
break;
|
|
}
|
|
}
|
|
updateSettingsPanel();
|
|
}
|
|
}));
|
|
}
|
|
|
|
private void addViewListeners(IView view, final GameData gameData) {
|
|
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 i, Color color) {
|
|
for (PlayerSettings player : gameData.getGameSettings()
|
|
.getPlayerList()) {
|
|
if (player.getColor() == color) {
|
|
return;
|
|
}
|
|
}
|
|
PlayerSettings player = gameData.getGameSettings().getPlayerList()
|
|
.get(i);
|
|
if (player.getType() != Type.HUMAN) {
|
|
return;
|
|
}
|
|
player.setColor(color);
|
|
connectionControl.changeColor(color);
|
|
updateSettingsPanel();
|
|
}
|
|
}));
|
|
}
|
|
|
|
protected abstract void goBack();
|
|
|
|
/**
|
|
* The back event is emitted when the player wants to go back to the previous
|
|
* control and panel
|
|
*
|
|
* @return the event
|
|
*/
|
|
public IEvent getBackEvent() {
|
|
return backEvent;
|
|
}
|
|
|
|
/**
|
|
* The event that is emitted when the game is started
|
|
*
|
|
* @return the event
|
|
*/
|
|
public IEvent1<GameData> getStartTurnEvent() {
|
|
return gameStartEvent;
|
|
}
|
|
|
|
protected void startGame() {
|
|
abort();
|
|
gameStartEvent.emit(gameData);
|
|
}
|
|
|
|
protected void abort() {
|
|
view.showSettingsPanel(false);
|
|
for (Connection c : connections) {
|
|
c.remove();
|
|
}
|
|
}
|
|
|
|
protected void updateSettingsPanel() {
|
|
view.getSettingsPanel().setGameSettings(gameData.getGameSettings());
|
|
|
|
Set<Color> colors = new HashSet<Color>(
|
|
Arrays.asList(ISettingsPanel.PLAYER_COLORS));
|
|
|
|
for (PlayerSettings player : gameData.getGameSettings().getPlayerList()) {
|
|
colors.remove(player.getColor());
|
|
}
|
|
|
|
view.getSettingsPanel().setPlayerColors(colors);
|
|
}
|
|
}
|