Extended Doc in all classes I did smt in to hopefully make it understandable

This commit is contained in:
Seraina 2022-04-09 23:39:38 +02:00
parent 362b965742
commit 0710703f77
14 changed files with 84 additions and 43 deletions

View File

@ -12,6 +12,7 @@ public class ClientGameInfoHandler {
/**
* All messages that are used in VoteHandler
* TODO(Seraina&Alex): Adjust strings to be more meaningful
*/
//relevant:
public static final String ghostVoteRequest = "Vote on who to ghostify!";
@ -28,11 +29,5 @@ public class ClientGameInfoHandler {
public static final String isAHuman = "but they're a human!";
public static final String gotKickedOff = "is a Ghost and got kicked off";
/**
* sends a msg "" to Server stating who voted for who, this being the Client that votes
* @param position the position of the passenger that is voted for
*/
public void vote(int position) {
}
}

View File

@ -2,6 +2,11 @@ package ch.unibas.dmi.dbis.cs108.gamelogic;
import java.util.Arrays;
/**
* Data structure that is used to store clientVotes in an array, where the index correponds to the
* position in the train of the client.
*/
public class ClientVoteData {
private int[] vote; //saves vote of clientHandler for later transmission to passenger, by default MAX_VALUE, index corresponds to Passenger position
@ -9,7 +14,7 @@ public class ClientVoteData {
public ClientVoteData() {
int[] h = new int[6];
Arrays.fill(h,0);
Arrays.fill(h,Integer.MAX_VALUE);
this.vote = h;
this.hasVoted = new boolean[6];
}
@ -22,10 +27,20 @@ public class ClientVoteData {
return hasVoted;
}
/**
* Sets a vote value at the right position in the vote array
* @param position the index of the array
* @param vote the vote value
*/
public void setVote(int position, int vote) {
this.vote[position] = vote;
}
/**
* Sets true or false at the right position in the hasVoted array
* @param position the index of the array
* @param hasVoted the vote state value
*/
public void setHasVoted(int position, boolean hasVoted) {
this.hasVoted[position] = hasVoted;
}

View File

@ -64,6 +64,13 @@ public class Game implements Runnable {
isDay = day;
}
/**
* Starts a new game, creates a passenger array and saves it in gameState, sets the OG
* currently at gameState.train[3] fills the passengerTrain moving from left to rigth in the
* gameState.train array it connects clientHandlers witch the passengers in those positions
* (Players) and fills the rest with NPC's
* TODO: set ghost in a random position(i), gameState.train[i] so that a lone player can also start as a Ghost maybe use Train class
*/
@Override
public void run() {
LOGGER.info("the run-method has been called");
@ -113,8 +120,8 @@ public class Game implements Runnable {
gameOverCheck = voteHandler.humanVote(gameState.getPassengerTrain(), this);
setDay(false);
}
if (gameOverCheck.equals("Game over: ghosts win!") || gameOverCheck.equals(
"Game over: humans win!")) {
if (gameOverCheck.equals(ClientGameInfoHandler.gameOverGhostsWin) || gameOverCheck.equals(
ClientGameInfoHandler.gameOverHumansWin)) {
clientHandler.broadcastAnnouncement(gameOverCheck);
return;
}

View File

@ -23,6 +23,9 @@ public class GameState {
* contains all Passengers on train, needs to be updated
*/
private Passenger[] passengerTrain;
/**
* Saves ClientVoteData, might not be used
*/
private ClientVoteData clientVoteData;

View File

@ -40,6 +40,12 @@ public class ServerGameInfoHandler {
return msg;
}
/**
* decides which action an GhostNpc needs to take, based on a message
* @param npc the GhostNpc needing to do smt
* @param msg the msg containing the information on what to do
* @param game the game the GhostNpc lives in (in gameState.passengerTrain)
*/
public static void ghostNpcParser(GhostNPC npc, String msg, Game game) {
switch (msg) {
case ClientGameInfoHandler.noiseNotification:
@ -53,6 +59,12 @@ public class ServerGameInfoHandler {
}
/**
* decides which action an HumanNpc needs to take, based on a message
* @param npc the HumanNpc needing to do smt
* @param msg the msg containing the information on what to do
* @param game the game the HumanNpc lives in (in gameState.passengerTrain)
*/
public static void humanNpcParser(HumanNPC npc, String msg, Game game) {
switch (msg) {
case ClientGameInfoHandler.noiseNotification:

View File

@ -9,7 +9,7 @@ public class Train {
public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
int[] orderOfTrain; //gives the random order in which the passengers enter the train
int positionOfGhost;
int positionOfGhost; // useful for randomization of og ghost position
/**
* Constructs a Train with orderOfTrain of the size nrOfPlayers, filled with a Random order of the
@ -67,12 +67,4 @@ public class Train {
return false;
}
public static void main(String[] args) {
try {
Train t = new Train(6, 1);
} catch (TrainOverflow e) {
LOGGER.error(e.getMessage());
}
}
}

View File

@ -1,5 +1,8 @@
package ch.unibas.dmi.dbis.cs108.gamelogic;
/**
* An exception that is thrown, if for some reason to many clients want to start a game
*/
public class TrainOverflow extends Exception {
private static final String message = "Too many users are logged on";

View File

@ -41,6 +41,7 @@ public class GhostNPC extends Ghost {
/**
* Sets vote of this Ghost position on a number between 0 and 5,
* but only for positions where there aren't any ghosts and sets hasVoted to true
* TODO: Make NPC smarter
*/
public void vote(Game game){
int ghostCounter = 0;
@ -64,21 +65,13 @@ public class GhostNPC extends Ghost {
LOGGER.info("GhostNPC at Position: " + this.getPosition() + " has voted for: " + vote);
}
/**
* Decides what to do when a noise ist heard, currently just always broadcasts it
* TODO: Make NPC smarter
*/
public void noise() {
clientHandler.broadcastChatMessage("I heard some noise tonight");
}
/*public static void main(String[] args) {
try {
Game game = new Game(6,1,1);
Passenger p = new Passenger();
GhostNPC ghostNPC = new GhostNPC(2,"peter", false, game);
p = ghostNPC;
ghostNPC.vote(game);
} catch (TrainOverflow e) {
LOGGER.warn(e.getMessage());
}
}*/
}

View File

@ -33,7 +33,12 @@ public class GhostPlayer extends Ghost {
}
}
/**
* Sends a message to the client handled bye this client handler
* TODO: does this also work with 2 clients?
* @param msg the message that is sent to this player.
* @param game the game the GhostPlayer lives on (in game.gameState.passengerTrain)
*/
@Override
public void send(String msg, Game game) {
String formattedMsg = ServerGameInfoHandler.format(msg, this, game);

View File

@ -29,6 +29,11 @@ public class HumanNPC extends Human {
}
}
/**
* Sends a msg to the ServerGameInfoHandler.humanNpcParser to decide what has to happen now
* @param msg the message that is sent to this player.
* @param game the game the HumanNPC lives on (in game.gameState.passengerTrain)
*/
@Override
public void send(String msg, Game game) {
ServerGameInfoHandler.humanNpcParser(this, msg, game);

View File

@ -32,6 +32,12 @@ public class HumanPlayer extends Human {
}
}
/**
* Sends a message to the client handled bye this client handler
* TODO: does this also work with 2 clients?
* @param msg the message that is sent to this player.
* @param game the game the HumanPlayer lives on (in game.gameState.passengerTrain)
*/
@Override
public void send(String msg, Game game) {
String formattedMsg = ServerGameInfoHandler.format(msg,this, game);

View File

@ -22,17 +22,6 @@ public class Passenger {
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)
/**
* 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, Game game) {
if (isPlayer) {
String formattedMsg = ServerGameInfoHandler.format(msg,this,game);
clientHandler.sendMsgToClient(formattedMsg);
}
LOGGER.warn("This object should not just be a passenger. Position:" + position);
}
/**
* sets the Position of this passenger
@ -116,4 +105,16 @@ public class Passenger {
LOGGER.debug("a NPC called this method hopefully: " + position);
}
/**
* 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, Game game) {
if (isPlayer) {
String formattedMsg = ServerGameInfoHandler.format(msg,this,game);
clientHandler.sendMsgToClient(formattedMsg);
}
LOGGER.warn("This object should not just be a passenger. Position:" + position);
}
}

View File

@ -56,7 +56,7 @@ public class ServerPinger implements Runnable {
System.out.println(
"Lost connection to user " + c.getClientUserName() + ". Waiting to reconnect...");
} else {
//c.disconnectClient();
//c.disconnectClient(); TODO: is that ever necessary?
//LOGGER.debug("gotPingBack has not been set to true and isConnected has been set to false before");
}
}

View File

@ -75,6 +75,10 @@ public class ClientHandler implements Runnable {
return socket;
}
/**
* Needed to fill a train with client TODO: how do lobbies fit here?
* @return the HashSet of Connected Clients
*/
public static HashSet<ClientHandler> getConnectedClients() {
return connectedClients;
}