Extended Doc in all classes I did smt in to hopefully make it understandable
This commit is contained in:
parent
362b965742
commit
0710703f77
@ -12,6 +12,7 @@ public class ClientGameInfoHandler {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* All messages that are used in VoteHandler
|
* All messages that are used in VoteHandler
|
||||||
|
* TODO(Seraina&Alex): Adjust strings to be more meaningful
|
||||||
*/
|
*/
|
||||||
//relevant:
|
//relevant:
|
||||||
public static final String ghostVoteRequest = "Vote on who to ghostify!";
|
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 isAHuman = "but they're a human!";
|
||||||
public static final String gotKickedOff = "is a Ghost and got kicked off";
|
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) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,6 +2,11 @@ package ch.unibas.dmi.dbis.cs108.gamelogic;
|
|||||||
|
|
||||||
import java.util.Arrays;
|
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 {
|
public class ClientVoteData {
|
||||||
|
|
||||||
private int[] vote; //saves vote of clientHandler for later transmission to passenger, by default MAX_VALUE, index corresponds to Passenger position
|
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() {
|
public ClientVoteData() {
|
||||||
int[] h = new int[6];
|
int[] h = new int[6];
|
||||||
Arrays.fill(h,0);
|
Arrays.fill(h,Integer.MAX_VALUE);
|
||||||
this.vote = h;
|
this.vote = h;
|
||||||
this.hasVoted = new boolean[6];
|
this.hasVoted = new boolean[6];
|
||||||
}
|
}
|
||||||
@ -22,10 +27,20 @@ public class ClientVoteData {
|
|||||||
return hasVoted;
|
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) {
|
public void setVote(int position, int vote) {
|
||||||
this.vote[position] = 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) {
|
public void setHasVoted(int position, boolean hasVoted) {
|
||||||
this.hasVoted[position] = hasVoted;
|
this.hasVoted[position] = hasVoted;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -64,6 +64,13 @@ public class Game implements Runnable {
|
|||||||
isDay = day;
|
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
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
LOGGER.info("the run-method has been called");
|
LOGGER.info("the run-method has been called");
|
||||||
@ -113,8 +120,8 @@ public class Game implements Runnable {
|
|||||||
gameOverCheck = voteHandler.humanVote(gameState.getPassengerTrain(), this);
|
gameOverCheck = voteHandler.humanVote(gameState.getPassengerTrain(), this);
|
||||||
setDay(false);
|
setDay(false);
|
||||||
}
|
}
|
||||||
if (gameOverCheck.equals("Game over: ghosts win!") || gameOverCheck.equals(
|
if (gameOverCheck.equals(ClientGameInfoHandler.gameOverGhostsWin) || gameOverCheck.equals(
|
||||||
"Game over: humans win!")) {
|
ClientGameInfoHandler.gameOverHumansWin)) {
|
||||||
clientHandler.broadcastAnnouncement(gameOverCheck);
|
clientHandler.broadcastAnnouncement(gameOverCheck);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -23,6 +23,9 @@ public class GameState {
|
|||||||
* contains all Passengers on train, needs to be updated
|
* contains all Passengers on train, needs to be updated
|
||||||
*/
|
*/
|
||||||
private Passenger[] passengerTrain;
|
private Passenger[] passengerTrain;
|
||||||
|
/**
|
||||||
|
* Saves ClientVoteData, might not be used
|
||||||
|
*/
|
||||||
private ClientVoteData clientVoteData;
|
private ClientVoteData clientVoteData;
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -40,6 +40,12 @@ public class ServerGameInfoHandler {
|
|||||||
return msg;
|
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) {
|
public static void ghostNpcParser(GhostNPC npc, String msg, Game game) {
|
||||||
switch (msg) {
|
switch (msg) {
|
||||||
case ClientGameInfoHandler.noiseNotification:
|
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) {
|
public static void humanNpcParser(HumanNPC npc, String msg, Game game) {
|
||||||
switch (msg) {
|
switch (msg) {
|
||||||
case ClientGameInfoHandler.noiseNotification:
|
case ClientGameInfoHandler.noiseNotification:
|
||||||
|
|||||||
@ -9,7 +9,7 @@ public class Train {
|
|||||||
public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
|
public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
|
||||||
|
|
||||||
int[] orderOfTrain; //gives the random order in which the passengers enter the train
|
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
|
* 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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
try {
|
|
||||||
Train t = new Train(6, 1);
|
|
||||||
} catch (TrainOverflow e) {
|
|
||||||
LOGGER.error(e.getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,8 @@
|
|||||||
package ch.unibas.dmi.dbis.cs108.gamelogic;
|
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 {
|
public class TrainOverflow extends Exception {
|
||||||
|
|
||||||
private static final String message = "Too many users are logged on";
|
private static final String message = "Too many users are logged on";
|
||||||
|
|||||||
@ -41,6 +41,7 @@ public class GhostNPC extends Ghost {
|
|||||||
/**
|
/**
|
||||||
* Sets vote of this Ghost position on a number between 0 and 5,
|
* 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
|
* but only for positions where there aren't any ghosts and sets hasVoted to true
|
||||||
|
* TODO: Make NPC smarter
|
||||||
*/
|
*/
|
||||||
public void vote(Game game){
|
public void vote(Game game){
|
||||||
int ghostCounter = 0;
|
int ghostCounter = 0;
|
||||||
@ -64,21 +65,13 @@ public class GhostNPC extends Ghost {
|
|||||||
LOGGER.info("GhostNPC at Position: " + this.getPosition() + " has voted for: " + vote);
|
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() {
|
public void noise() {
|
||||||
clientHandler.broadcastChatMessage("I heard some noise tonight");
|
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());
|
|
||||||
}
|
|
||||||
|
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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
|
@Override
|
||||||
public void send(String msg, Game game) {
|
public void send(String msg, Game game) {
|
||||||
String formattedMsg = ServerGameInfoHandler.format(msg, this, game);
|
String formattedMsg = ServerGameInfoHandler.format(msg, this, game);
|
||||||
|
|||||||
@ -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
|
@Override
|
||||||
public void send(String msg, Game game) {
|
public void send(String msg, Game game) {
|
||||||
ServerGameInfoHandler.humanNpcParser(this, msg, game);
|
ServerGameInfoHandler.humanNpcParser(this, msg, game);
|
||||||
|
|||||||
@ -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
|
@Override
|
||||||
public void send(String msg, Game game) {
|
public void send(String msg, Game game) {
|
||||||
String formattedMsg = ServerGameInfoHandler.format(msg,this, game);
|
String formattedMsg = ServerGameInfoHandler.format(msg,this, game);
|
||||||
|
|||||||
@ -22,17 +22,6 @@ public class Passenger {
|
|||||||
protected boolean hasVoted; //true if the player gave his vote during voting time
|
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)
|
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
|
* sets the Position of this passenger
|
||||||
@ -116,4 +105,16 @@ public class Passenger {
|
|||||||
LOGGER.debug("a NPC called this method hopefully: " + position);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -56,7 +56,7 @@ public class ServerPinger implements Runnable {
|
|||||||
System.out.println(
|
System.out.println(
|
||||||
"Lost connection to user " + c.getClientUserName() + ". Waiting to reconnect...");
|
"Lost connection to user " + c.getClientUserName() + ". Waiting to reconnect...");
|
||||||
} else {
|
} 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");
|
//LOGGER.debug("gotPingBack has not been set to true and isConnected has been set to false before");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -75,6 +75,10 @@ public class ClientHandler implements Runnable {
|
|||||||
return socket;
|
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() {
|
public static HashSet<ClientHandler> getConnectedClients() {
|
||||||
return connectedClients;
|
return connectedClients;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user