Documented everything I could and tried unifying format

This commit is contained in:
Seraina 2022-03-27 22:43:35 +02:00
parent 6cf98643d1
commit c87d894e10
12 changed files with 80 additions and 145 deletions

View File

@ -0,0 +1,31 @@
Client commands:
Implemented:
* CHATA$message Send chat message to all
* QUITS quit server/ leave server
* CPING Ping from client to server.
* PINGB Pingback from client to server.
* NAMEC$name Change name to whatever is specified
Future / planned:
* CRTGM Create a new game
* CHATW whisper chat
* CHATG ghost chat
* LEAVG leave a game
* JOING join a game
* VOTEG ghost voting who to infect
* VOTEH humans voting who is the ghost
* LISTP list players/clients in session with the Server
Server Commands:
Implemented:
* SPING Ping from server to client
* PINGB Pingback from client to server.
Future / planned:
* MSGRS "Message received": Paramaters: a string detailing to the client that and what the server received as command.
* SEROR Server had an error. (used for debugging)
* NOCMD No command found.

View File

@ -1,52 +0,0 @@
PRE GAME:
----------------------------------------------------
int spielerzahl = 6; //Default setting
Fill train -> Randomize
/**
Creates an array of random numbers 1 - spielerzahl
**/
int[] fillTrain(int spielerzahl) //... arbitrary amount of users, gives Array I think^^
if spielerzahl < #users
throw some sort of exception
int[] userTrain = new int[spielerzahl];
for (int i = 0; i < spielerzahl; i++) //
Random nr. 1-6
if randomnr hasnt been used before
userTrain[i] = randomnr.
else ? //find a way either increase number or whatever to handle this case
return userTrain
Create Passengers -> use fillTrain array to position one after another
Start with clients till we run out, then create npc'
Save Passengers in an array (Positions corresponding to index+1)
Gostyfy first time, create first ghost
IN GAME
----------------------------------------
Night:
Vote Ghosts (can only vote for non-Ghosts os needs to be verified)
-> Timer!
-> last Human? -> End of the Game (break out of a loop?) "Ghosts win"
Ghostyfycation
Send Messages ("You have been ghostyfied", "You heard something suspicious")
Day:
Vote Humans (Ghost votes don't count)
->Timer!
-> OG Ghost? -> End of the Game "Humans Win"
Evtl. kickoff (non OG-Ghosts)
Send Messages ("X Has been kicked off", "Not a Ghost")
repeat
How do we handle the End of the Game?

View File

@ -36,9 +36,8 @@ public class Client {
}
/**
*
* Sends a message to the Server in a formatted way COMND$msg
*/
public void sendMessage() {
try {
Scanner sc = new Scanner(System.in);
@ -56,7 +55,6 @@ public class Client {
}
/**
* Starts a thread which listens for incoming messages
*/
@ -86,6 +84,7 @@ public class Client {
/**
* Sends a message to the server, as is.
*
* @param msg the message sent. Should already be protocol-formatted.
*/
public void sendMsgToServer(String msg) {
@ -101,6 +100,7 @@ public class Client {
/**
* parses a received message according to the client protocol.
*
* @param msg the message to be parsed.
*/
public void parse(String msg) {
@ -137,7 +137,8 @@ public class Client {
hostname = args[0];
}
String systemName = System.getProperty("user.name");
System.out.println("Choose a nickname (Suggestion: " + systemName + "): ");
System.out.println("Choose a nickname (Suggestion: " + systemName
+ "): "); //Suggests a name based on System username
String username = sc.next();
Socket socket;
try {

View File

@ -6,8 +6,9 @@ public class JClientProtocolParser {
/**
* Used by the client to parse an incoming protocol message.
*
* @param msg the encoded message that needs to be parsed
* @param c this Client(required so this method can access the Client's methods)
* @param c this Client(required so this method can access the Client's methods)
*/
public static void parse(String msg, Client c) {
String header = ""; //"header" is the first 5 characters, i.e. the protocol part

View File

@ -4,7 +4,7 @@ public class MessageFormatter {
/**
* Takes a given Message and reformats it to where the JServerProtocolParser.parse() method can
* handle it. May need to be redesigned one the games uses a GUI
* handle it (see Protocol.txt). May need to be redesigned once the games uses a GUI
*
* @param msg the Messaged to be reformatted
* @return the reformatted message in the form HEADR$msg

View File

@ -19,6 +19,7 @@ public class ClientPinger implements Runnable {
/**
* @param socket the socket the Client is connected to which is used to end the thread if the
* connection is lost.
*
* @param out the output through which the pings are sent.
*/
public ClientPinger(BufferedWriter out, Socket socket) {

View File

@ -19,6 +19,7 @@ public class ServerPinger implements Runnable {
/**
* @param socket the socket the ClientHandler is connected to; used to end the thread if the
* connection is lost.
*
* @param out the output through which the pings are sent.
*/
public ServerPinger(BufferedWriter out, Socket socket) {

View File

@ -1,22 +1,23 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.server;
/**
* This class is built to contain the usernames of all players in a single string.
* This allows a duplicate check (--> ClientHandler) when a new player chooses
* a name: does the string with all the previous names contain the new player's
* desired username? If yes, he is being assigned a random name. If no, he can keep
* his desired name.
* **/
* This class is built to contain the usernames of all players in a single string. This allows a
* duplicate check (--> ClientHandler) when a new player chooses a name: does the string with all
* the previous names contain the new player's desired username? If yes, he is being assigned a
* random name. If no, he can keep his desired name.
*/
public class AllClientNames {
static StringBuilder names = new StringBuilder();
/**
* Safes a new name to the List of all Names
* @param currentName the new name to be added
* @return All names adding the new currentName
*/
public static String allNames(String currentName) {
return names.append(currentName).toString();
}
static StringBuilder names = new StringBuilder();
/**
* Safes a new name to the List of all Names
*
* @param currentName the new name to be added
* @return All names adding the new currentName
*/
public static String allNames(String currentName) {
return names.append(currentName).toString();
}
}

View File

@ -99,7 +99,9 @@ public class ClientHandler implements Runnable {
}
/**
* Lets the client change their respective username, if the username is already taken, a similar option is chosen
* Lets the client change their respective username, if the username is already taken, a similar
* option is chosen.
*
* @param newName The desired new name to replace the old one with.
*/
public void changeUsername(String newName) {
@ -109,11 +111,12 @@ public class ClientHandler implements Runnable {
String h = this.clientUserName; //just a friendly little helper
this.clientUserName = newName;
AllClientNames.allNames(newName);
broadcastMessage(h +" have changed their nickname to " + clientUserName);
broadcastMessage(h + " have changed their nickname to " + clientUserName);
}
/**
* Broadcasts a Message to all active clients in the form "Username: msg"
*
* @param msg the Message to be broadcasted
*/
@ -124,9 +127,10 @@ public class ClientHandler implements Runnable {
}
//TODO: Documentation
/**
/** Sends a given message to client
*
* @param msg
* @param msg the given message
*/
public void sendMsgToClient(String msg) {
@ -139,11 +143,21 @@ public class ClientHandler implements Runnable {
}
}
/**
* Does what it sounds like
*/
public void removeClientHandler() {
connectedClients.remove(this);
broadcastMessage("SERVER: " + clientUserName + " has left the server");
}
/**
* Does exactly what it says on the tin, closes all connections of Client to Server.
*
* @param socket the socket to be closed
* @param in the in-Stream reader to be closed
* @param out the out-Stream Write to be closed
*/
public void closeEverything(Socket socket, BufferedReader in, BufferedWriter out) {
removeClientHandler();
try {

View File

@ -6,7 +6,8 @@ import java.util.Random;
public class NameGenerator {
/**
* Creates a random alteration of a Name by adding 4 numbers at the end of the Name that shall be alterd
* Creates a random alteration of a Name by adding 4 numbers at the end of the Name that shall be altered
*
* @param username the to be altered username
* @return username + four numbers
*/

View File

@ -17,6 +17,9 @@ public class Server {
this.serverSocket = serverSocket;
}
/**
* Starts up a Server that opens Port 42069 either located wia IP address or localhost
*/
public void startServer() {
try {
System.out.println("Port 42069 is open on " + this.serverSocket.getInetAddress());

View File

@ -1,67 +0,0 @@
package ch.unibas.dmi.dbis.cs108.example;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* An example test class.
* Checks the output of the {@link HelloWorld} class and makes sure it contains "Hello World"
*/
public class HelloWorldTest {
/*
* Streams to store system.out and system.err content
*/
private ByteArrayOutputStream outStream = new ByteArrayOutputStream();
private ByteArrayOutputStream errStream = new ByteArrayOutputStream();
/*
* Here we store the previous pointers to system.out / system.err
*/
private PrintStream outBackup;
private PrintStream errBackup;
/**
* This method is executed before each test.
* It redirects System.out and System.err to our variables {@link #outStream} and {@link #errStream}.
* This allows us to test their content later.
*/
@BeforeEach
public void redirectStdOutStdErr() {
outBackup = System.out;
errBackup = System.err;
System.setOut(new PrintStream(outStream));
System.setErr(new PrintStream(errStream));
}
/**
* This method is run after each test.
* It redirects System.out / System.err back to the normal streams.
*/
@AfterEach
public void reestablishStdOutStdErr() {
System.setOut(outBackup);
System.setErr(errBackup);
}
/**
* This is a normal JUnit-Test. It executes the HelloWorld-Method and verifies that it actually wrote "Hello World" to stdout
*/
@Test
public void testMain() {
HelloWorld.main(new String[0]);
String toTest = outStream.toString();
toTest = removeNewline(toTest);
assertTrue(toTest.contains("Hello World"));
}
private static String removeNewline(String str) {
return str.replace("\n", "").replace("\r", "");
}
}