From 27f8af70f57323411d3d3595bfcfaa6053eb912a Mon Sep 17 00:00:00 2001 From: Daniel Grigencha Date: Mon, 2 Dec 2024 22:34:45 +0100 Subject: [PATCH 1/4] Updated 'Game' class. Updated the 'Game' class by setting the default value of 'host' attribute. In Addition, the 'isHost' method was added. --- .../mdga/model/src/main/java/pp/mdga/game/Game.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/game/Game.java b/Projekte/mdga/model/src/main/java/pp/mdga/game/Game.java index 9d7d83d5..515aa1da 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/game/Game.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/game/Game.java @@ -43,7 +43,7 @@ public class Game { private Die die; // The host of this game - private int host; + private int host = -1; // The color of the active player. private Color activeColor; @@ -200,6 +200,15 @@ public Piece getPieceThroughUUID(UUID pieceId) { return null; } + /** + * This method will be used to check if this client is the host for the game. + * + * @return true or false. + */ + public boolean isHost() { + return this.host != -1; + } + /** * This method returns the dice modifier. * From 492f7422f5d591c1adc9fa0bb1e42919c4a19eeb Mon Sep 17 00:00:00 2001 From: Daniel Grigencha Date: Mon, 2 Dec 2024 22:36:48 +0100 Subject: [PATCH 2/4] Updated 'LobbyAcceptMessage' class. Updated the 'LobbyAcceptMessage' class by adding the 'host' attribute and its getter method to it. --- .../message/server/LobbyAcceptMessage.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/message/server/LobbyAcceptMessage.java b/Projekte/mdga/model/src/main/java/pp/mdga/message/server/LobbyAcceptMessage.java index 90b55126..fbaf7c56 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/message/server/LobbyAcceptMessage.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/message/server/LobbyAcceptMessage.java @@ -7,13 +7,39 @@ */ @Serializable public class LobbyAcceptMessage extends ServerMessage { + /** + * Create LobbyAcceptMessage attributes. + */ + private final int host; + /** * Constructs a new LobbyAccept instance. */ public LobbyAcceptMessage() { super(); + this.host = -1; } + /** + * Constructor. + * + * @param host as the id of the host as an Integer. + */ + public LobbyAcceptMessage(int host) { + super(); + this.host = host; + } + + /** + * This method will be used return host attribute of LobbyAcceptMessage class. + * + * @return host as an Integer. + */ + public int getHost() { + return this.host; + } + + /** * Accepts a visitor to process this message. * From 1eb24b7a668f53699cf50278509de55a0fc12e70 Mon Sep 17 00:00:00 2001 From: Daniel Grigencha Date: Mon, 2 Dec 2024 22:37:46 +0100 Subject: [PATCH 3/4] Updated 'MdgaServer' class. Updated the 'MdgaServer' class by updating the logic inside the 'connectionAdded' method in it. --- .../client/src/main/java/pp/mdga/client/server/MdgaServer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Projekte/mdga/client/src/main/java/pp/mdga/client/server/MdgaServer.java b/Projekte/mdga/client/src/main/java/pp/mdga/client/server/MdgaServer.java index bbcfe2c4..f3301618 100644 --- a/Projekte/mdga/client/src/main/java/pp/mdga/client/server/MdgaServer.java +++ b/Projekte/mdga/client/src/main/java/pp/mdga/client/server/MdgaServer.java @@ -216,7 +216,7 @@ public void connectionAdded(Server server, HostedConnection hostedConnection) { this.logic.getServerSender().send(hostedConnection.getId(), new LobbyDenyMessage()); hostedConnection.close(""); } else { - if (this.myServer.getConnections().size() == 1) { + if (hostedConnection.getAddress().contains("127.0.0.1") && this.logic.getGame().getHost() == -1) { this.logic.getGame().setHost(hostedConnection.getId()); } this.logic.getServerSender().send(hostedConnection.getId(), new LobbyAcceptMessage()); From e163b87cc471b059060172aa5c5d3aef299203e8 Mon Sep 17 00:00:00 2001 From: Daniel Grigencha Date: Mon, 2 Dec 2024 22:41:17 +0100 Subject: [PATCH 4/4] Updated 'MdgaServer' class. Updated the 'MdgaServer' class by updating the logic inside the 'connectionAdded' method in it. --- .../src/main/java/pp/mdga/client/server/MdgaServer.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Projekte/mdga/client/src/main/java/pp/mdga/client/server/MdgaServer.java b/Projekte/mdga/client/src/main/java/pp/mdga/client/server/MdgaServer.java index f3301618..97295da2 100644 --- a/Projekte/mdga/client/src/main/java/pp/mdga/client/server/MdgaServer.java +++ b/Projekte/mdga/client/src/main/java/pp/mdga/client/server/MdgaServer.java @@ -218,8 +218,10 @@ public void connectionAdded(Server server, HostedConnection hostedConnection) { } else { if (hostedConnection.getAddress().contains("127.0.0.1") && this.logic.getGame().getHost() == -1) { this.logic.getGame().setHost(hostedConnection.getId()); + this.logic.getServerSender().send(hostedConnection.getId(), new LobbyAcceptMessage(hostedConnection.getId())); + } else { + this.logic.getServerSender().send(hostedConnection.getId(), new LobbyAcceptMessage()); } - this.logic.getServerSender().send(hostedConnection.getId(), new LobbyAcceptMessage()); } }