From 913d0781de9ea5bef7fe390806e677d20a327f68 Mon Sep 17 00:00:00 2001 From: Seraina Date: Fri, 8 Apr 2022 11:10:05 +0200 Subject: [PATCH 1/5] Added adjustments for the testing of voteHandler - main methode - a print methode, that prints out a Passenger array --- .../dbis/cs108/gamelogic/GameFunctions.java | 1 + .../dmi/dbis/cs108/gamelogic/VoteHandler.java | 52 +++++++++++++++++-- .../gamelogic/klassenstruktur/Passenger.java | 8 ++- 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/GameFunctions.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/GameFunctions.java index 079e2e4..6d521ef 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/GameFunctions.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/GameFunctions.java @@ -40,6 +40,7 @@ public class GameFunctions { h.setPosition(train.orderOfTrain[i]); passengerTrain[i] = h; } + this.passengerTrain = passengerTrain; } public int getNrOfGhosts() { diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/VoteHandler.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/VoteHandler.java index 7d14119..4ff7364 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/VoteHandler.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/VoteHandler.java @@ -31,7 +31,7 @@ public class VoteHandler { * * @param passengers: passengers on the train */ - public void ghostVote(Passenger[] passengers, Game game) { + public static void ghostVote(Passenger[] passengers, Game game) { // array to collect votes for all players during voting, i.e. votes for player 1 (passengers[0]) // are saved in @@ -42,13 +42,13 @@ public class VoteHandler { // TODO(Seraina): Messages in for-loop should probably be handled by ServerGameInfoHandler for (Passenger passenger : passengers) { if (passenger.getIsGhost()) { - LOGGER.info("Send msg to Ghost in Position: " + passenger); + passenger.send("Vote on who to ghostify!"); } else { passenger.send( "Please wait, ghosts are active"); // TODO(Seraina): make sure whatever clients send in // this time, except chat is ignored - LOGGER.info("Send msg to Human in Position: " + passenger); + } } @@ -100,7 +100,7 @@ public class VoteHandler { * * @param passengers: train passengers */ - public void humanVote(Passenger[] passengers) { + public static void humanVote(Passenger[] passengers) { // array to collect votes for all players during voting, i.e. votes for player 1 are saved in // votesForPlayers[0] @@ -177,4 +177,48 @@ public class VoteHandler { } } } + + static void print(Passenger[] array) { + System.out.println(); + String[] print = new String[6]; + for (int i = 0; i < array.length; i++) { + if(array[i].getIsGhost()) { + print[i] = "| ghost |"; + } else { + print[i] = "| human |"; + } + } + + for (int i = 0; i < array.length; i++) { + System.out.print(print[i]); + } + System.out.println(); + + } + + public static void main(String[] args) { + try { + Game game = new Game(6,1, 6); + + Passenger[] testArray = game.gameFunctions.passengerTrain; + Passenger ghost = new Ghost(); + testArray[3] = ghost; + testArray[3].setGhost(); + testArray[3].setIsOg(); + print(testArray); + LOGGER.info("NIGHT"); + ghostVote(testArray,game); + print(testArray); + + LOGGER.info("Day"); + humanVote(testArray); + print(testArray); + + } catch (TrainOverflow e) { + LOGGER.warn(e.getMessage()); + } + + + + } } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/Passenger.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/Passenger.java index c968db9..526a8c7 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/Passenger.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/Passenger.java @@ -26,9 +26,9 @@ public class Passenger { **/ public void send(String msg) { if (msg.equals("Vote on who to ghostify!") || msg.equals("Vote for a ghost to kick off!")) { - vote = (int) (Math.random() * 6); + vote = 1; hasVoted = true; // for testing, when is it set to false again? - LOGGER.info("Voted for Position" + vote); + LOGGER.info("Voted for Position " + vote); } else { LOGGER.debug(msg); } @@ -76,6 +76,10 @@ public class Passenger { hasVoted = true; } + public void setIsOg() { + isOG = true; + } + public int getPosition() { return position; } From 5cfa6809a2806ab376fcc15bbf39785cf3eb7e44 Mon Sep 17 00:00:00 2001 From: Alexander Sazonov Date: Fri, 8 Apr 2022 11:27:52 +0200 Subject: [PATCH 2/5] Modified GhostifyHandler, added Logger statement --- .../dmi/dbis/cs108/gamelogic/GhostifyHandler.java | 14 +++++++------- .../dmi/dbis/cs108/gamelogic/VoteHandler.java | 12 ++++++------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/GhostifyHandler.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/GhostifyHandler.java index 553ad19..f95a887 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/GhostifyHandler.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/GhostifyHandler.java @@ -1,27 +1,27 @@ package ch.unibas.dmi.dbis.cs108.gamelogic; +import ch.unibas.dmi.dbis.cs108.BudaLogConfig; import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.GhostPlayer; import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Passenger; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; public class GhostifyHandler { + public static final Logger LOGGER = LogManager.getLogger(); + public static final BudaLogConfig l = new BudaLogConfig(LOGGER); /** * Changes passenger at position x to ghost and returns this ghost. Monitors the times the ghost method is being * called. If it's being called for the first time, the ghostified player is being set as the original ghost. * * @param p Passenger to be ghostified */ - private static int ghostifyCallCounter = -1; public GhostPlayer ghost(Passenger p, Game game) { p.setGhost(); GhostPlayer g; - ghostifyCallCounter++; - if (ghostifyCallCounter == 0) { - g = new GhostPlayer(p.getPosition(), p.getName(), p.getClientHandler(), true); - } else { - g = new GhostPlayer(p.getPosition(), p.getName(), p.getClientHandler(), false); - } + g = new GhostPlayer(p.getPosition(), p.getName(), p.getClientHandler(), false); game.gameFunctions.passengerTrain[g.getPosition()] = g; + LOGGER.info("Passenger at position " + p.getPosition() + "has been ghostified"); return g; } } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/VoteHandler.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/VoteHandler.java index a0befff..2264ba7 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/VoteHandler.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/VoteHandler.java @@ -75,14 +75,14 @@ public class VoteHandler { currentMax = votesForPlayer; } } - LOGGER.info("Most votes" + currentMax); + LOGGER.info("Most votes: " + currentMax + " vote"); // ghostify the player with most votes int ghostPosition = 0; for (int i = 0; i < votesForPlayers.length; i++) { if (votesForPlayers[i] == currentMax) { // if player at position i has most votes ghostPosition = i; - LOGGER.info("Most votes for Passenger" + i); + LOGGER.info("Most votes for Passenger " + i); } } GhostifyHandler gh = new GhostifyHandler(); @@ -148,7 +148,7 @@ public class VoteHandler { for (int i = 0; i < votesForPlayers.length; i++) { if (votesForPlayers[i] == currentMax) { // if player has most votes voteIndex = i; - LOGGER.info("Player " + voteIndex + "has the most votes"); + LOGGER.info("Player " + voteIndex + " has the most votes"); } } if (!passengers[voteIndex] @@ -178,11 +178,11 @@ public class VoteHandler { // kick this ghost off passengers[voteIndex].setKickedOff(true); for (Passenger passenger : passengers) { - passenger.send("Player " + voteIndex + "has been kicked off!"); + passenger.send("Player " + voteIndex + " has been kicked off!"); } } } - // set hasVoted to false for all passengers for future votings + // set hasVoted to false for all passengers for future voting for (Passenger passenger : passengers) { passenger.setHasVoted(false); } @@ -221,7 +221,7 @@ public class VoteHandler { print(testArray); LOGGER.info("Day"); - humanVote(testArray); + humanVote(testArray, game); print(testArray); } catch (TrainOverflow e) { From ce5723c3e9aef918c85b7b58d621fa80327b7387 Mon Sep 17 00:00:00 2001 From: sebaschi <74497638+sebaschi@users.noreply.github.com> Date: Fri, 8 Apr 2022 12:08:38 +0200 Subject: [PATCH 3/5] Correcions in CentralServerData: wherever a Client object wasa expected has been changed to expect a ClientHandler object. This is because the server sees clients via ClientHandlers but has no access to Client objects. Lobby: Field "admin" added which is a ClientHandler type object. This represents who started the game and, for the phase before the gameplay starts, controls relevant functions. --- .../dmi/dbis/cs108/sebaschi/CentralServerData.java | 11 ++++++----- .../java/ch/unibas/dmi/dbis/cs108/sebaschi/Lobby.java | 11 +++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/CentralServerData.java b/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/CentralServerData.java index 2c8d7b1..ed92d4c 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/CentralServerData.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/CentralServerData.java @@ -3,6 +3,7 @@ package ch.unibas.dmi.dbis.cs108.sebaschi; import ch.unibas.dmi.dbis.cs108.BudaLogConfig; import ch.unibas.dmi.dbis.cs108.gamelogic.Game; import ch.unibas.dmi.dbis.cs108.multiplayer.client.Client; +import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler; import java.net.Socket; import java.util.Map; import java.util.Set; @@ -11,7 +12,7 @@ import org.apache.logging.log4j.Logger; /** * This Class Represents an Object containing different Maps, Lists and Sets wherein a server object - * can find all needed data. An instance of this object can also be passed to other class-objects uf + * can find all needed data. An instance of this object can also be passed to other class-objects if * they need the same data. This Class is used to query for information in collections. */ public class CentralServerData { @@ -19,11 +20,11 @@ public class CentralServerData { public static final Logger LOGGER = LogManager.getLogger(); public static final BudaLogConfig l = new BudaLogConfig(LOGGER); - private Set clientsOnServer; + private Set clientsOnServer; private Set activeGames; private Set gamesOpenToJoin; - private Map clientSocketMap; - private Map socketClientMap; - private Map gameClientMap; + private Map clientSocketMap; + private Map socketClientMap; + private Map gameClientMap; } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/Lobby.java b/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/Lobby.java index 4427452..76cb7d9 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/Lobby.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/Lobby.java @@ -1,5 +1,16 @@ package ch.unibas.dmi.dbis.cs108.sebaschi; +import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler; + +/** + * The Lobby one is in after a client sends the CRTGM command. THe Server + */ public class Lobby { + /** + * The Person who created the game and can configure it and decide to start once enough players + * have entered the lobby. + */ + ClientHandler admin; + } From 90f844ce6b096c08ee490fb277e84ba6e0517f82 Mon Sep 17 00:00:00 2001 From: sebaschi <74497638+sebaschi@users.noreply.github.com> Date: Fri, 8 Apr 2022 12:10:49 +0200 Subject: [PATCH 4/5] Added List of ClientHandlers to represent who is in the lobby, aka the players. --- .../java/ch/unibas/dmi/dbis/cs108/sebaschi/Lobby.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/Lobby.java b/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/Lobby.java index 76cb7d9..3b104b0 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/Lobby.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/Lobby.java @@ -1,6 +1,7 @@ package ch.unibas.dmi.dbis.cs108.sebaschi; import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler; +import java.util.List; /** * The Lobby one is in after a client sends the CRTGM command. THe Server @@ -11,6 +12,11 @@ public class Lobby { * The Person who created the game and can configure it and decide to start once enough players * have entered the lobby. */ - ClientHandler admin; + private ClientHandler admin; + + /** + * Everyone who's in the lobby. + */ + private List players; } From 052a2078221a32f083c06a0139c4519341e088b4 Mon Sep 17 00:00:00 2001 From: sebaschi <74497638+sebaschi@users.noreply.github.com> Date: Fri, 8 Apr 2022 12:23:51 +0200 Subject: [PATCH 5/5] Added final field MAX_NO_OF_CLIENTS used to not overfill the lobby. --- .../unibas/dmi/dbis/cs108/sebaschi/Lobby.java | 39 ++++++++++++++++++- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/Lobby.java b/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/Lobby.java index 3b104b0..419b18d 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/Lobby.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/Lobby.java @@ -1,6 +1,7 @@ package ch.unibas.dmi.dbis.cs108.sebaschi; import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler; +import java.util.ArrayList; import java.util.List; /** @@ -12,11 +13,45 @@ public class Lobby { * The Person who created the game and can configure it and decide to start once enough players * have entered the lobby. */ - private ClientHandler admin; + private final ClientHandler admin; /** * Everyone who's in the lobby. */ - private List players; + private List players = new ArrayList<>(); + + private static final int MAX_NO_OF_CLIENTS = 6; + + + + + + + + /** + * The admin has to be set in the constructor. The admin is final. + * Every Lobby needs and admin, so no other constructors are needed. + * @param admin the Client who called CRTGM + */ + public Lobby(ClientHandler admin) { + this.admin = admin; + } + + /** + * Getter + * + * @return the admin of the lobby. + */ + public ClientHandler getAdmin() { + return this.admin; + } + + /** + * Adds a player to the lobby. + * @param player who wants to join the lobby. + */ + public void addPlayer(ClientHandler player) { + players.add(player); + } }