From 16e6364cc9ab100f658e9d9dfccfd3e243afe0b4 Mon Sep 17 00:00:00 2001 From: sebaschi <74497638+sebaschi@users.noreply.github.com> Date: Fri, 8 Apr 2022 12:40:11 +0200 Subject: [PATCH 1/7] Lobby now have static field to count how many lobbies there are in total which is used to assign a particular lobby and ID, so players can ask to join the lobby. --- .../unibas/dmi/dbis/cs108/sebaschi/Lobby.java | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 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 419b18d..f98e664 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 @@ -9,6 +9,9 @@ import java.util.List; */ public class Lobby { + private static final int MAX_NO_OF_CLIENTS = 6; + private static int lobbies; + /** * The Person who created the game and can configure it and decide to start once enough players * have entered the lobby. @@ -18,23 +21,29 @@ public class Lobby { /** * Everyone who's in the lobby. */ - private List players = new ArrayList<>(); - - private static final int MAX_NO_OF_CLIENTS = 6; - + private List players = new ArrayList<>(6); + private int numberOfPlayersInLobby; + private final int lobbyID = lobbies++; + static { + lobbies = 0; + } /** - * The admin has to be set in the constructor. The admin is final. - * Every Lobby needs and admin, so no other constructors are needed. + * Constructor. Sets the admin to who created the lobby. Adds the admin to the list of players. + * Increases the number of players from 0 to 1. + * * @param admin the Client who called CRTGM */ public Lobby(ClientHandler admin) { this.admin = admin; + this.players.add(admin); + this.numberOfPlayersInLobby = 1; + lobbies++; } /** @@ -48,6 +57,7 @@ public class Lobby { /** * Adds a player to the lobby. + * * @param player who wants to join the lobby. */ public void addPlayer(ClientHandler player) { From 4337583de6156a2035fd621a982fa85743709370 Mon Sep 17 00:00:00 2001 From: Seraina Date: Fri, 8 Apr 2022 12:42:02 +0200 Subject: [PATCH 2/7] Fixed some bugs now the VoteHanlder does what it is supposed to --- .../dbis/cs108/gamelogic/GameFunctions.java | 2 +- .../dbis/cs108/gamelogic/GhostifyHandler.java | 10 ++++--- .../dmi/dbis/cs108/gamelogic/VoteHandler.java | 26 ++++++++++++++----- .../gamelogic/klassenstruktur/Passenger.java | 12 ++++++--- 4 files changed, 37 insertions(+), 13 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 6d521ef..0ca27de 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 @@ -38,7 +38,7 @@ public class GameFunctions { for (int i = 0; i < nrOfPlayers; i++) { Human h = new Human(); h.setPosition(train.orderOfTrain[i]); - passengerTrain[i] = h; + passengerTrain[train.orderOfTrain[i]] = h; } this.passengerTrain = passengerTrain; } 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 f95a887..f3ea707 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,6 +1,7 @@ package ch.unibas.dmi.dbis.cs108.gamelogic; import ch.unibas.dmi.dbis.cs108.BudaLogConfig; +import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Ghost; import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.GhostPlayer; import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Passenger; import org.apache.logging.log4j.LogManager; @@ -16,10 +17,13 @@ public class GhostifyHandler { * @param p Passenger to be ghostified */ - public GhostPlayer ghost(Passenger p, Game game) { + public Ghost ghost(Passenger p, Game game) { //TODO: Adjust for not only players but also npcs + LOGGER.debug("Passenger Position " + p.getPosition()); p.setGhost(); - GhostPlayer g; - g = new GhostPlayer(p.getPosition(), p.getName(), p.getClientHandler(), false); + Ghost g; + g = new Ghost(); + g.setGhost(); + g.setPosition(p.getPosition()); 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 2264ba7..f1b5c74 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 @@ -83,8 +83,10 @@ public class VoteHandler { if (votesForPlayers[i] == currentMax) { // if player at position i has most votes ghostPosition = i; LOGGER.info("Most votes for Passenger " + i); + } } + LOGGER.debug("ghostPosition: " + ghostPosition); GhostifyHandler gh = new GhostifyHandler(); Ghost g = gh.ghost(passengers[ghostPosition], game); passengers[ghostPosition] = g; @@ -170,9 +172,9 @@ public class VoteHandler { if (!passenger.getIsGhost()) { humans++; } - if (humans == 1) { - System.out.println("Game over: ghosts win!"); - } + } + if (humans == 1) { + System.out.println("Game over: ghosts win!"); } // Usual case: there is more than one human left and a normal ghost has been voted for --> // kick this ghost off @@ -192,10 +194,14 @@ public class VoteHandler { System.out.println(); String[] print = new String[6]; for (int i = 0; i < array.length; i++) { - if(array[i].getIsGhost()) { - print[i] = "| ghost |"; + if (array[i].getKickedOff()) { + print[i] = "| kicked off " + array[i].getPosition() + "|"; } else { - print[i] = "| human |"; + if (array[i].getIsGhost()) { + print[i] = "| ghost " + array[i].getPosition() + "|"; + } else { + print[i] = "| human " + array[i].getPosition() + "|"; + } } } @@ -215,6 +221,7 @@ public class VoteHandler { testArray[3] = ghost; testArray[3].setGhost(); testArray[3].setIsOg(); + testArray[3].setPosition(3); print(testArray); LOGGER.info("NIGHT"); ghostVote(testArray,game); @@ -224,6 +231,13 @@ public class VoteHandler { humanVote(testArray, game); print(testArray); + LOGGER.info("NIGHT"); + ghostVote(testArray,game); + print(testArray); + + LOGGER.info("Day"); + humanVote(testArray, game); + 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 7e19bc2..aa52c83 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 @@ -19,17 +19,23 @@ public class Passenger { protected ClientHandler clientHandler;//the socket for the client associated with this Passenger, for NPCs, this can be null. protected boolean hasVoted; //true if the player gave his vote during voting time protected int vote; //saves the number of the player this passenger voted for during voting (0-5) - + int sendcounter = 0; /** * Sends a protocol message to the respective player or NPC. * @param msg the message that is sent to this player. **/ public void send(String msg) { - if (msg.equals("Vote on who to ghostify!") || msg.equals("Vote for a ghost to kick off!")) { - vote = 1; + sendcounter++; + if (msg.equals("Vote on who to ghostify!")) { + vote = 1*sendcounter; + hasVoted = true; // for testing, when is it set to false again? + LOGGER.info("Voted for Position " + vote); + } else if(msg.equals("Vote for a ghost to kick off!")) { + vote = (int) (0.5*sendcounter); hasVoted = true; // for testing, when is it set to false again? LOGGER.info("Voted for Position " + vote); } else { + LOGGER.debug(msg); } /*if (isPlayer) { From 9cc7a37eb90507d4b1fcef4ee2115c4f928dc6e9 Mon Sep 17 00:00:00 2001 From: sebaschi <74497638+sebaschi@users.noreply.github.com> Date: Fri, 8 Apr 2022 12:43:36 +0200 Subject: [PATCH 3/7] ServerLobby now has a list of open lobbies and a list of all clients on the server. --- .../java/ch/unibas/dmi/dbis/cs108/sebaschi/ServerLobby.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/ServerLobby.java b/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/ServerLobby.java index f0bbedf..9063170 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/ServerLobby.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/ServerLobby.java @@ -1,5 +1,8 @@ package ch.unibas.dmi.dbis.cs108.sebaschi; +import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler; +import java.util.List; + /** * This class is just everyone on the server, which games are open to join, who is in session. I.E. * the context of the game just after joining the server, before starting a game and entering into a @@ -7,4 +10,7 @@ package ch.unibas.dmi.dbis.cs108.sebaschi; */ public class ServerLobby { + List openLobbies; + List allClients; + } From c134de8bba7346c4e596793363904c9575aea8f8 Mon Sep 17 00:00:00 2001 From: Seraina Date: Fri, 8 Apr 2022 12:45:57 +0200 Subject: [PATCH 4/7] Removed static from Methods --- .../dmi/dbis/cs108/gamelogic/VoteHandler.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) 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 f1b5c74..80b887e 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 static void ghostVote(Passenger[] passengers, Game game) { + public 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 @@ -107,7 +107,7 @@ public class VoteHandler { * * @param passengers: train passengers */ - public static void humanVote(Passenger[] passengers, Game game) { + public void humanVote(Passenger[] passengers, Game game) { // array to collect votes for all players during voting, i.e. votes for player 1 are saved in @@ -215,6 +215,7 @@ public class VoteHandler { public static void main(String[] args) { try { Game game = new Game(6,1, 6); + VoteHandler voteHandler = new VoteHandler(); Passenger[] testArray = game.gameFunctions.passengerTrain; Passenger ghost = new Ghost(); @@ -224,19 +225,19 @@ public class VoteHandler { testArray[3].setPosition(3); print(testArray); LOGGER.info("NIGHT"); - ghostVote(testArray,game); + voteHandler.ghostVote(testArray,game); print(testArray); LOGGER.info("Day"); - humanVote(testArray, game); + voteHandler.humanVote(testArray, game); print(testArray); LOGGER.info("NIGHT"); - ghostVote(testArray,game); + voteHandler.ghostVote(testArray,game); print(testArray); LOGGER.info("Day"); - humanVote(testArray, game); + voteHandler.humanVote(testArray, game); print(testArray); } catch (TrainOverflow e) { LOGGER.warn(e.getMessage()); From ac022e1f7cf4999b2af3037ed0771cb5773090ae Mon Sep 17 00:00:00 2001 From: sebaschi <74497638+sebaschi@users.noreply.github.com> Date: Fri, 8 Apr 2022 12:47:19 +0200 Subject: [PATCH 5/7] Changed the list in ServerLobby to static, since they are global per running server. --- .../unibas/dmi/dbis/cs108/sebaschi/ServerLobby.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/ServerLobby.java b/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/ServerLobby.java index 9063170..b4c48f7 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/ServerLobby.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/ServerLobby.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; /** @@ -10,7 +11,15 @@ import java.util.List; */ public class ServerLobby { - List openLobbies; - List allClients; + private static List openLobbies; + private static List allClients; + static { + openLobbies = new ArrayList<>(); + allClients = new ArrayList<>(); + } + + public static List getOpenLobbies() { + return openLobbies; + } } From f858c734f73be138bed0c78c4dde2cd90bb5fce6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Seraina=20Sch=C3=B6b?= Date: Fri, 8 Apr 2022 10:58:17 +0000 Subject: [PATCH 6/7] Update Diary.txt --- Meilenstein III/Diary.txt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Meilenstein III/Diary.txt b/Meilenstein III/Diary.txt index 9b7084e..503348c 100644 --- a/Meilenstein III/Diary.txt +++ b/Meilenstein III/Diary.txt @@ -241,5 +241,11 @@ ToDo: Stand: Server - Client: Connection loss handling funktioniert nun. Lobby: ? - Spiellogik: + Spiellogik: VoteHandler wurde getestet und tut weitestgehenst was er soll + +ToDo: + Spiellogik: - Send() methode von Passenger mit Client-Server verknüpfen(Seraina) + - NoiseHandler (Alex) + - Game Zyklus implementieren (Seraina) + From dffd1731e213b0f77fa19478d6bf97268475eebc Mon Sep 17 00:00:00 2001 From: Seraina Date: Fri, 8 Apr 2022 13:00:47 +0200 Subject: [PATCH 7/7] Added Doc for print Method --- .../java/ch/unibas/dmi/dbis/cs108/gamelogic/VoteHandler.java | 4 ++++ 1 file changed, 4 insertions(+) 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 80b887e..f2ed804 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 @@ -190,6 +190,10 @@ public class VoteHandler { } } + /** + * Just a print Method for testing the VoteHandler + * @param array the Passenger array to be visualized + */ static void print(Passenger[] array) { System.out.println(); String[] print = new String[6];