This commit is contained in:
Cedric Beck
2024-12-02 22:58:58 +01:00
7 changed files with 51 additions and 76 deletions

View File

@@ -100,6 +100,7 @@ private void mainMenu() {
private void tryHost() { private void tryHost() {
int port = 0; int port = 0;
String text = hostDialog.getPort(); String text = hostDialog.getPort();
app.getGameLogic().selectHost("");
try { try {
port = Integer.parseInt(text); port = Integer.parseInt(text);
@@ -113,7 +114,6 @@ private void tryHost() {
} }
hostDialog.connectServerAsClient(); hostDialog.connectServerAsClient();
app.getModelSynchronize().setHost(port); app.getModelSynchronize().setHost(port);
app.getGameLogic().selectHost("");
//app.getAcousticHandler().playSound(MdgaSound.WRONG_INPUT); //app.getAcousticHandler().playSound(MdgaSound.WRONG_INPUT);
return; return;
} }
@@ -128,6 +128,7 @@ private void tryJoin() {
int port = 0; int port = 0;
String ip = joinDialog.getIpt(); String ip = joinDialog.getIpt();
String portText = joinDialog.getPort(); String portText = joinDialog.getPort();
app.getGameLogic().selectJoin("");
try { try {
// Validate the port // Validate the port
@@ -141,7 +142,6 @@ private void tryJoin() {
app.getModelSynchronize().setName(startDialog.getName()); app.getModelSynchronize().setName(startDialog.getName());
joinDialog.setHostname(ip); joinDialog.setHostname(ip);
joinDialog.connectToServer(); joinDialog.connectToServer();
app.getGameLogic().selectJoin("");
return; return;
} }
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {

View File

@@ -25,6 +25,8 @@ public class ClientGameLogic implements ServerInterpreter {
private ClientState state; private ClientState state;
private final ArrayList<Notification> notifications = new ArrayList<>(); private final ArrayList<Notification> notifications = new ArrayList<>();
private boolean isHost; private boolean isHost;
private int ownPlayerID;
private String ownPlayerName;
private final DialogsState dialogsState = new DialogsState(null, this); private final DialogsState dialogsState = new DialogsState(null, this);
private final GameState gameState = new GameState(null, this); private final GameState gameState = new GameState(null, this);
@@ -79,6 +81,42 @@ public ClientSender getClientSender(){
return clientSender; return clientSender;
} }
/**
* This method is used to get the ownPlayerId
*
* @return the ownPlayerId
*/
public int getOwnPlayerId() {
return ownPlayerID;
}
/**
* This method is used to get the ownPlayerName
*
* @return the ownPlayerName
*/
public String getOwnPlayerName() {
return ownPlayerName;
}
/**
* This method is used to set the ownPlayerName
*
* @param ownPlayerName the ownPlayerName to be set
*/
public void setOwnPlayerName(String ownPlayerName) {
this.ownPlayerName = ownPlayerName;
}
/**
* This method is used to set the ownPlayerId
*
* @param ownPlayerId the ownPlayerId to be set
*/
public void setOwnPlayerId(int ownPlayerId) {
this.ownPlayerID = ownPlayerId;
}
/** /**
* This method returns the game * This method returns the game
* *

View File

@@ -11,9 +11,6 @@ public class DialogsState extends ClientState {
private DialogStates currentState; private DialogStates currentState;
private int ownPlayerID;
private String ownPlayerName;
private final LobbyState lobbyState = new LobbyState(this, logic); private final LobbyState lobbyState = new LobbyState(this, logic);
private final NetworkDialogState networkDialogState = new NetworkDialogState(this, logic); private final NetworkDialogState networkDialogState = new NetworkDialogState(this, logic);
private final StartDialogState startDialogState = new StartDialogState(this, logic); private final StartDialogState startDialogState = new StartDialogState(this, logic);
@@ -42,8 +39,6 @@ public void exit(){
@Override @Override
public void enter(){ public void enter(){
setState(startDialogState); setState(startDialogState);
ownPlayerID = 0;
ownPlayerName = null;
} }
/** /**
@@ -59,42 +54,6 @@ public void setState(DialogStates newState){
currentState = newState; currentState = newState;
} }
/**
* This method is used to get the ownPlayerId
*
* @return the ownPlayerId
*/
public int getOwnPlayerId() {
return ownPlayerID;
}
/**
* This method is used to get the ownPlayerName
*
* @return the ownPlayerName
*/
public String getOwnPlayerName() {
return ownPlayerName;
}
/**
* This method is used to set the ownPlayerName
*
* @param ownPlayerName the ownPlayerName to be set
*/
public void setOwnPlayerName(String ownPlayerName) {
this.ownPlayerName = ownPlayerName;
}
/**
* This method is used to set the ownPlayerId
*
* @param ownPlayerId the ownPlayerId to be set
*/
public void setOwnPlayerId(int ownPlayerId) {
this.ownPlayerID = ownPlayerId;
}
/** /**
* This method is used to get the lobbyState * This method is used to get the lobbyState
* *

View File

@@ -27,7 +27,7 @@ public LobbyState(ClientState parent, ClientGameLogic logic) {
@Override @Override
public void enter() { public void enter() {
logic.send(new JoinedLobbyMessage(parent.getOwnPlayerName())); logic.send(new JoinedLobbyMessage(logic.getOwnPlayerName()));
} }
@Override @Override
@@ -74,7 +74,7 @@ public void selectStart(){
@Override @Override
public void received(ServerStartGameMessage msg){ public void received(ServerStartGameMessage msg){
logic.addNotification(new GameNotification(logic.getGame().getPlayerById(parent.getOwnPlayerId()).getColor())); logic.addNotification(new GameNotification(logic.getGame().getPlayerById(logic.getOwnPlayerId()).getColor()));
for (Map.Entry<Color, PlayerData> entry : logic.getGame().getBoard().getPlayerData().entrySet()) { for (Map.Entry<Color, PlayerData> entry : logic.getGame().getBoard().getPlayerData().entrySet()) {
List<UUID> pieceList = new ArrayList<>(); List<UUID> pieceList = new ArrayList<>();
for(Piece piece : entry.getValue().getPieces()){ for(Piece piece : entry.getValue().getPieces()){
@@ -88,21 +88,21 @@ public void received(ServerStartGameMessage msg){
@Override @Override
public void received(LobbyPlayerJoinedMessage msg){ public void received(LobbyPlayerJoinedMessage msg){
if(msg.getPlayer().getName().equals(parent.getOwnPlayerName())){ if(msg.getPlayer().getName().equals(logic.getOwnPlayerName())){
parent.setOwnPlayerId(msg.getId()); logic.setOwnPlayerId(msg.getId());
} }
if (msg.isHost() && msg.getId() == parent.getOwnPlayerId()){ if (msg.isHost() && msg.getId() == logic.getOwnPlayerId()){
logic.setHost(true); logic.setHost(true);
} }
logic.addNotification(new TskSelectNotification(msg.getPlayer().getColor(), msg.getPlayer().getName(), msg.getPlayer().getName().equals(parent.getOwnPlayerName()))); logic.addNotification(new TskSelectNotification(msg.getPlayer().getColor(), msg.getPlayer().getName(), msg.getPlayer().getName().equals(logic.getOwnPlayerName())));
logic.getGame().getPlayers().put(msg.getId(), msg.getPlayer()); logic.getGame().getPlayers().put(msg.getId(), msg.getPlayer());
} }
@Override @Override
public void received(UpdateTSKMessage msg){ public void received(UpdateTSKMessage msg){
if(msg.isTaken()) { if(msg.isTaken()) {
logic.addNotification(new TskSelectNotification(msg.getColor(), logic.getGame().getPlayers().get(msg.getId()).getName(), parent.getOwnPlayerId()== msg.getId())); logic.addNotification(new TskSelectNotification(msg.getColor(), logic.getGame().getPlayers().get(msg.getId()).getName(), logic.getOwnPlayerId()== msg.getId()));
} else { } else {
logic.addNotification(new TskUnselectNotification(logic.getGame().getPlayers().get(msg.getId()).getColor())); logic.addNotification(new TskUnselectNotification(logic.getGame().getPlayers().get(msg.getId()).getColor()));
} }

View File

@@ -53,6 +53,8 @@ public void selectLeave() {
*/ */
@Override @Override
public void received(LobbyAcceptMessage msg) { public void received(LobbyAcceptMessage msg) {
logic.getGame().setHost(msg.getHost());
logic.setHost(logic.getGame().isHost());
parent.setState(parent.getLobby()); parent.setState(parent.getLobby());
logic.addNotification(new LobbyDialogNotification()); logic.addNotification(new LobbyDialogNotification());
} }

View File

@@ -32,30 +32,6 @@ public void enter() {
public void exit() { public void exit() {
} }
/**
* Select the join option
*
* @param name the name
*/
@Override
public void selectJoin(String name) {
parent.setOwnPlayerName(name);
parent.setState(parent.getNetworkDialog());
logic.setHost(false);
}
/**
* Select the host option
*
* @param name the name
*/
@Override
public void selectHost(String name) {
parent.setOwnPlayerName(name);
parent.setState(parent.getLobby());
logic.setHost(true);
}
/** /**
* Set the name * Set the name
* *
@@ -63,8 +39,8 @@ public void selectHost(String name) {
*/ */
@Override @Override
public void setName(String name) { public void setName(String name) {
logic.setOwnPlayerName(name);
parent.setState(parent.getNetworkDialog()); parent.setState(parent.getNetworkDialog());
parent.setOwnPlayerName(name);
} }
/** /**

View File

@@ -34,7 +34,7 @@ public void selectDice(){
@Override @Override
public void received(DieMessage msg){ public void received(DieMessage msg){
logic.addNotification(new RollDiceNotification(logic.getGame().getPlayerById(logic.getDialogs().getOwnPlayerId()).getColor(), msg.getDiceEye(),true)); logic.addNotification(new RollDiceNotification(logic.getGame().getPlayerById(logic.getOwnPlayerId()).getColor(), msg.getDiceEye(),true));
parent.setState(parent.getWaitRanking()); parent.setState(parent.getWaitRanking());
} }
} }