halbfertige netzwerkdinge
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@450 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
5a4d1ccb12
commit
4860867fe8
4 changed files with 94 additions and 25 deletions
|
@ -136,6 +136,10 @@ class ConnectionControl {
|
||||||
new Thread(new SendGameOfferRunner(offeredGame)).start();
|
new Thread(new SendGameOfferRunner(offeredGame)).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void sendChangeColor(Color color) {
|
||||||
|
new Thread(new SendChangeColorRunner(color)).start();
|
||||||
|
}
|
||||||
|
|
||||||
private static void emitLater(final Event event) {
|
private static void emitLater(final Event event) {
|
||||||
SwingUtilities.invokeLater(new Runnable() {
|
SwingUtilities.invokeLater(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -160,8 +164,8 @@ class ConnectionControl {
|
||||||
.getExtension(ELEMENT_NAME, NAMESPACE);
|
.getExtension(ELEMENT_NAME, NAMESPACE);
|
||||||
|
|
||||||
if (((Message) packet).getType() == Message.Type.error) {
|
if (((Message) packet).getType() == Message.Type.error) {
|
||||||
System.err.println("Received error message from '" + packet.getFrom()
|
System.err.println("Received error message from '"
|
||||||
+ "'");
|
+ packet.getFrom() + "'");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -172,27 +176,31 @@ class ConnectionControl {
|
||||||
|
|
||||||
if (messageType.equals("game_offer")) {
|
if (messageType.equals("game_offer")) {
|
||||||
UUID uuid = UUID.fromString(extension.getValue("uuid"));
|
UUID uuid = UUID.fromString(extension.getValue("uuid"));
|
||||||
GameSettings settings = (GameSettings) Base64.decodeToObject(extension
|
GameSettings settings = (GameSettings) Base64
|
||||||
.getValue("gameSettings"));
|
.decodeToObject(extension.getValue("gameSettings"));
|
||||||
|
|
||||||
GameData gameData = new GameData(uuid, settings, sender);
|
GameData gameData = new GameData(uuid, settings, sender);
|
||||||
gameOfferEvent.emit(gameData);
|
gameOfferEvent.emit(gameData);
|
||||||
} else if (messageType.equals("game_withdrawal")) {
|
} else if (messageType.equals("game_withdrawal")) {
|
||||||
gameWithdrawalEvent.emit(UUID.fromString(extension.getValue("uuid")));
|
gameWithdrawalEvent.emit(UUID
|
||||||
|
.fromString(extension.getValue("uuid")));
|
||||||
} else if (messageType.equals("game_request")) {
|
} else if (messageType.equals("game_request")) {
|
||||||
if (offeredGame != null) {
|
if (offeredGame != null) {
|
||||||
sendGameOffer();
|
sendGameOffer();
|
||||||
}
|
}
|
||||||
} else if (messageType.equals("game_join")) {
|
} else if (messageType.equals("game_join")) {
|
||||||
gameJoinEvent.emit(UUID.fromString(extension.getValue("uuid")), sender);
|
gameJoinEvent.emit(UUID.fromString(extension.getValue("uuid")),
|
||||||
|
sender);
|
||||||
} else if (messageType.equals("game_leave")) {
|
} else if (messageType.equals("game_leave")) {
|
||||||
gameLeaveEvent.emit(UUID.fromString(extension.getValue("uuid")), sender);
|
gameLeaveEvent.emit(UUID.fromString(extension.getValue("uuid")),
|
||||||
|
sender);
|
||||||
} else if (messageType.equals("game_join_ack")) {
|
} else if (messageType.equals("game_join_ack")) {
|
||||||
gameJoinAckEvent.emit(UUID.fromString(extension.getValue("uuid")),
|
gameJoinAckEvent.emit(UUID.fromString(extension.getValue("uuid")),
|
||||||
Boolean.valueOf(extension.getValue("ack")));
|
Boolean.valueOf(extension.getValue("ack")));
|
||||||
} else if (messageType.equals("changeColor")) {
|
} else if (messageType.equals("changeColor")) {
|
||||||
changeColorEvent.emit(UUID.fromString(extension.getValue("uuid")),
|
changeColorEvent.emit(UUID.fromString(extension.getValue("uuid")),
|
||||||
sender, (Color) Base64.decodeToObject(extension.getValue("color")));
|
sender, (Color) Base64.decodeToObject(extension
|
||||||
|
.getValue("color")));
|
||||||
} else {
|
} else {
|
||||||
System.err.println("Received unrecognized message of type '"
|
System.err.println("Received unrecognized message of type '"
|
||||||
+ messageType + "'");
|
+ messageType + "'");
|
||||||
|
@ -204,8 +212,8 @@ class ConnectionControl {
|
||||||
|
|
||||||
extension.setValue("messageType", "game_offer");
|
extension.setValue("messageType", "game_offer");
|
||||||
extension.setValue("uuid", data.getGameID().toString());
|
extension.setValue("uuid", data.getGameID().toString());
|
||||||
extension.setValue("gameSettings",
|
extension.setValue("gameSettings", Base64.encodeObject(data
|
||||||
Base64.encodeObject(data.getGameSettings(), Base64.GZIP));
|
.getGameSettings(), Base64.GZIP));
|
||||||
|
|
||||||
return createMessage(extension);
|
return createMessage(extension);
|
||||||
}
|
}
|
||||||
|
@ -246,7 +254,8 @@ class ConnectionControl {
|
||||||
return createMessage(extension);
|
return createMessage(extension);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Message createGameAckMessage(UUID uuid, String recipient, boolean ack) {
|
private Message createGameAckMessage(UUID uuid, String recipient,
|
||||||
|
boolean ack) {
|
||||||
DefaultPacketExtension extension = createJRummikubExtension();
|
DefaultPacketExtension extension = createJRummikubExtension();
|
||||||
|
|
||||||
extension.setValue("messageType", "game_join_ack");
|
extension.setValue("messageType", "game_join_ack");
|
||||||
|
@ -317,7 +326,8 @@ class ConnectionControl {
|
||||||
break; // Join was successful, break the loop
|
break; // Join was successful, break the loop
|
||||||
} catch (XMPPException e) {
|
} catch (XMPPException e) {
|
||||||
XMPPError error = e.getXMPPError();
|
XMPPError error = e.getXMPPError();
|
||||||
if (error.getType() == Type.CANCEL && error.getCode() == 409) {
|
if (error.getType() == Type.CANCEL
|
||||||
|
&& error.getCode() == 409) {
|
||||||
// There was a conflict, try again with another
|
// There was a conflict, try again with another
|
||||||
// nickname
|
// nickname
|
||||||
nickname += "_";
|
nickname += "_";
|
||||||
|
@ -446,7 +456,8 @@ class ConnectionControl {
|
||||||
public void run() {
|
public void run() {
|
||||||
synchronized (ConnectionControl.this) {
|
synchronized (ConnectionControl.this) {
|
||||||
if (connection != null) {
|
if (connection != null) {
|
||||||
connection.sendPacket(createGameAckMessage(uuid, recipient, ack));
|
connection.sendPacket(createGameAckMessage(uuid, recipient,
|
||||||
|
ack));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,28 +6,35 @@ import java.util.Arrays;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
import jrummikub.model.GameSettings;
|
import jrummikub.model.GameSettings;
|
||||||
import jrummikub.model.PlayerSettings;
|
import jrummikub.model.PlayerSettings;
|
||||||
import jrummikub.model.PlayerSettings.Type;
|
import jrummikub.model.PlayerSettings.Type;
|
||||||
import jrummikub.util.Connection;
|
import jrummikub.util.Connection;
|
||||||
|
import jrummikub.util.Event;
|
||||||
import jrummikub.util.GameData;
|
import jrummikub.util.GameData;
|
||||||
|
import jrummikub.util.IListener;
|
||||||
import jrummikub.util.IListener1;
|
import jrummikub.util.IListener1;
|
||||||
|
import jrummikub.util.IListener2;
|
||||||
|
import jrummikub.util.IListener3;
|
||||||
import jrummikub.view.ISettingsPanel;
|
import jrummikub.view.ISettingsPanel;
|
||||||
import jrummikub.view.ISettingsPanel.SettingsMode;
|
|
||||||
import jrummikub.view.IView;
|
import jrummikub.view.IView;
|
||||||
|
import jrummikub.view.ISettingsPanel.SettingsMode;
|
||||||
|
|
||||||
public class GameJoinControl {
|
public class GameJoinControl {
|
||||||
private List<Connection> connections = new ArrayList<Connection>();
|
private List<Connection> connections = new ArrayList<Connection>();
|
||||||
private GameData gameData;
|
private GameData gameData;
|
||||||
private ConnectionControl connectionControl;
|
private ConnectionControl connectionControl;
|
||||||
private IView view;
|
private IView view;
|
||||||
|
private Event backEvent = new Event();
|
||||||
|
|
||||||
public GameJoinControl(final ConnectionControl connectionControl,
|
public GameJoinControl(final ConnectionControl connectionControl,
|
||||||
final GameData gameData, final IView view) {
|
final GameData gameData, final IView view) {
|
||||||
this.connectionControl = connectionControl;
|
this.connectionControl = connectionControl;
|
||||||
this.gameData = gameData;
|
this.gameData = gameData;
|
||||||
this.view = view;
|
this.view = view;
|
||||||
|
|
||||||
|
|
||||||
view.getSettingsPanel().setSettingsMode(SettingsMode.NETWORK_JOIN);
|
view.getSettingsPanel().setSettingsMode(SettingsMode.NETWORK_JOIN);
|
||||||
view.getSettingsPanel().enableAddPlayerButton(false);
|
view.getSettingsPanel().enableAddPlayerButton(false);
|
||||||
|
@ -48,13 +55,29 @@ public class GameJoinControl {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
connections.add(view.getSettingsPanel().getBackEvent().add(new IListener() {
|
||||||
|
@Override
|
||||||
|
public void handle() {
|
||||||
|
//TODO mit game offer control und game data reden (spieler werden nich wieder entfernt)
|
||||||
|
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
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) {
|
private void updateSettingsPanel(GameSettings settings) {
|
||||||
view.getSettingsPanel().setGameSettings(settings);
|
view.getSettingsPanel().setGameSettings(settings);
|
||||||
|
|
||||||
Set<Color> colors = new HashSet<Color>(
|
Set<Color> colors = new HashSet<Color>(Arrays
|
||||||
Arrays.asList(ISettingsPanel.PLAYER_COLORS));
|
.asList(ISettingsPanel.PLAYER_COLORS));
|
||||||
|
|
||||||
for (PlayerSettings player : settings.getPlayerList()) {
|
for (PlayerSettings player : settings.getPlayerList()) {
|
||||||
colors.remove(player.getColor());
|
colors.remove(player.getColor());
|
||||||
|
@ -78,11 +101,15 @@ public class GameJoinControl {
|
||||||
view.showSettingsPanel(true);
|
view.showSettingsPanel(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Aborts joining and goes back to game list
|
||||||
|
*/
|
||||||
public void abort() {
|
public void abort() {
|
||||||
// TODO Implement abort
|
view.showSettingsPanel(false);
|
||||||
|
view.showGameListPanel(true);
|
||||||
for (Connection c : connections) {
|
for (Connection c : connections) {
|
||||||
c.remove();
|
c.remove();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,16 +48,16 @@ public class NetworkSettingsControl extends AbstractSettingsControl {
|
||||||
|
|
||||||
addListeners();
|
addListeners();
|
||||||
|
|
||||||
connections.add(view.getSettingsPanel().getOfferGameEvent()
|
connections.add(view.getSettingsPanel().getOfferGameEvent().add(
|
||||||
.add(new IListener() {
|
new IListener() {
|
||||||
@Override
|
@Override
|
||||||
public void handle() {
|
public void handle() {
|
||||||
offerGame();
|
offerGame();
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
connections.add(view.getSettingsPanel().getBackEvent()
|
connections.add(view.getSettingsPanel().getBackEvent().add(
|
||||||
.add(new IListener() {
|
new IListener() {
|
||||||
@Override
|
@Override
|
||||||
public void handle() {
|
public void handle() {
|
||||||
abort();
|
abort();
|
||||||
|
@ -66,15 +66,27 @@ public class NetworkSettingsControl extends AbstractSettingsControl {
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The game offer event is emitted when a player wants to offer a new
|
||||||
|
* network game
|
||||||
|
*
|
||||||
|
* @return the event
|
||||||
|
*/
|
||||||
public Event1<GameSettings> getOfferGameEvent() {
|
public Event1<GameSettings> getOfferGameEvent() {
|
||||||
return offerGameEvent;
|
return offerGameEvent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The get back event is emitted when the player wants to go back to the
|
||||||
|
* previous settings panel
|
||||||
|
*
|
||||||
|
* @return the event
|
||||||
|
*/
|
||||||
public Event getBackEvent() {
|
public Event getBackEvent() {
|
||||||
return backEvent;
|
return backEvent;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void offerGame(){
|
private void offerGame() {
|
||||||
if (!checkSettings()) {
|
if (!checkSettings()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -84,7 +96,8 @@ public class NetworkSettingsControl extends AbstractSettingsControl {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void update() {
|
protected void update() {
|
||||||
view.getSettingsPanel()
|
view
|
||||||
|
.getSettingsPanel()
|
||||||
.enableAddPlayerButton(
|
.enableAddPlayerButton(
|
||||||
settings.getPlayerList().size() < ISettingsPanel.PLAYER_COLORS.length);
|
settings.getPlayerList().size() < ISettingsPanel.PLAYER_COLORS.length);
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,8 @@ import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import jrummikub.model.PlayerSettings.Type;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The overall game settings
|
* The overall game settings
|
||||||
*/
|
*/
|
||||||
|
@ -229,4 +231,20 @@ public class GameSettings implements Serializable {
|
||||||
public void setNumberOfStonesDealt(int number) {
|
public void setNumberOfStonesDealt(int number) {
|
||||||
numberOfStonesDealt = number;
|
numberOfStonesDealt = number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if there is only one human player
|
||||||
|
*
|
||||||
|
* @return one human player
|
||||||
|
*/
|
||||||
|
public boolean oneHuman() {
|
||||||
|
int humans = 0;
|
||||||
|
for (PlayerSettings s : players) {
|
||||||
|
if (s.getType() == Type.HUMAN) {
|
||||||
|
humans++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (humans == 1);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue