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 connections = new ArrayList(); 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() { @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()); } }