changed the logic so that the isHost is not decided by the client and instead by the server

This commit is contained in:
Fleischer Hanno
2024-12-02 22:45:44 +01:00
parent e163b87cc4
commit e9ba888651
7 changed files with 51 additions and 76 deletions

View File

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

View File

@@ -25,6 +25,8 @@ public class ClientGameLogic implements ServerInterpreter {
private ClientState state;
private final ArrayList<Notification> notifications = new ArrayList<>();
private boolean isHost;
private int ownPlayerID;
private String ownPlayerName;
private final DialogsState dialogsState = new DialogsState(null, this);
private final GameState gameState = new GameState(null, this);
@@ -79,6 +81,42 @@ public ClientSender getClientSender(){
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
*

View File

@@ -11,9 +11,6 @@ public class DialogsState extends ClientState {
private DialogStates currentState;
private int ownPlayerID;
private String ownPlayerName;
private final LobbyState lobbyState = new LobbyState(this, logic);
private final NetworkDialogState networkDialogState = new NetworkDialogState(this, logic);
private final StartDialogState startDialogState = new StartDialogState(this, logic);
@@ -42,8 +39,6 @@ public void exit(){
@Override
public void enter(){
setState(startDialogState);
ownPlayerID = 0;
ownPlayerName = null;
}
/**
@@ -59,42 +54,6 @@ public void setState(DialogStates 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
*

View File

@@ -27,7 +27,7 @@ public LobbyState(ClientState parent, ClientGameLogic logic) {
@Override
public void enter() {
logic.send(new JoinedLobbyMessage(parent.getOwnPlayerName()));
logic.send(new JoinedLobbyMessage(logic.getOwnPlayerName()));
}
@Override
@@ -74,7 +74,7 @@ public void selectStart(){
@Override
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()) {
List<UUID> pieceList = new ArrayList<>();
for(Piece piece : entry.getValue().getPieces()){
@@ -88,21 +88,21 @@ public void received(ServerStartGameMessage msg){
@Override
public void received(LobbyPlayerJoinedMessage msg){
if(msg.getPlayer().getName().equals(parent.getOwnPlayerName())){
parent.setOwnPlayerId(msg.getId());
if(msg.getPlayer().getName().equals(logic.getOwnPlayerName())){
logic.setOwnPlayerId(msg.getId());
}
if (msg.isHost() && msg.getId() == parent.getOwnPlayerId()){
if (msg.isHost() && msg.getId() == logic.getOwnPlayerId()){
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());
}
@Override
public void received(UpdateTSKMessage msg){
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 {
logic.addNotification(new TskUnselectNotification(logic.getGame().getPlayers().get(msg.getId()).getColor()));
}

View File

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

View File

@@ -32,30 +32,6 @@ public void enter() {
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
*
@@ -63,8 +39,8 @@ public void selectHost(String name) {
*/
@Override
public void setName(String name) {
logic.setOwnPlayerName(name);
parent.setState(parent.getNetworkDialog());
parent.setOwnPlayerName(name);
}
/**

View File

@@ -34,7 +34,7 @@ public void selectDice(){
@Override
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());
}
}