Put the enum NTtBProtocol into the NightTrainProtocol class to have a authority class to validate and keep track of legal commands.

This commit is contained in:
Sebastian Lenzlinger 2022-03-25 13:12:03 +01:00
parent 846d0a8187
commit 2b10ff3294
7 changed files with 45 additions and 61 deletions

View File

@ -1,16 +1,16 @@
package ch.unibas.dmi.dbis.cs108.Multiplayer.Client;
import ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.NTtBCommands;
import ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.NightTrainProtocol;
import java.util.HashMap;
public class InputToProtocolMap extends HashMap<String, NTtBCommands> {
public class InputToProtocolMap extends HashMap<String, NightTrainProtocol.NTtBCommands> {
public InputToProtocolMap(){
super();
this.put("chat", NTtBCommands.CHATA);
this.put("cn", NTtBCommands.CUSRN);
this.put("list", NTtBCommands.LISTP);
this.put("exit", NTtBCommands.LEAVG);
this.put("chat", NightTrainProtocol.NTtBCommands.CHATA);
this.put("cn", NightTrainProtocol.NTtBCommands.CUSRN);
this.put("list", NightTrainProtocol.NTtBCommands.LISTP);
this.put("exit", NightTrainProtocol.NTtBCommands.LEAVG);
//TODO extend according to extended function
}
}

View File

@ -1,11 +1,6 @@
package ch.unibas.dmi.dbis.cs108.Multiplayer.Client;
import ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.NTtBCommands;
import ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.NTtBFormatMsg;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.Scanner;
/**

View File

@ -1,44 +0,0 @@
package ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol;
import java.util.HashMap;
public enum NTtBCommands {
/**
* Client commands:
* CRTGM: Create a new game
* CHATA: chat to all
* CHATW: whisper chat
* CHATG: ghost chat
* LEAVG: leave a game
* JOING: join a game
* VOTEG: ghost voting who to infect
* VOTEH: humans voting whos the ghost
* QUITS: quit server/ leave servr
* LISTP: list players/clients in session with the Server
*/
//Client Commands
CRTGM, CHATA, CHATW, CHATG, LEAVG, JOING, VOTEG, QUITS, LISTP, CUSRN,
/**
* MSGRS: "Message recieved": Paramaters: a string detailing to the client that and what the server recieved as command.
*/
//Server Responses
MSGRS;
//Allowes to associate strings with the enum objects. the enum fields are easier for switch statements.
private HashMap<String,NTtBCommands> stringNTtBCommandsHashMap = new HashMap<>();
private NTtBCommands(){
for(NTtBCommands cmd : NTtBCommands.values()){
stringNTtBCommandsHashMap.put(cmd.name(), cmd);
}
}
public boolean isLegal(String s){
return false;
}
}

View File

@ -0,0 +1,17 @@
/**
* Client commands:
* CRTGM: Create a new game
* CHATA: chat to all
* CHATW: whisper chat
* CHATG: ghost chat
* LEAVG: leave a game
* JOING: join a game
* VOTEG: ghost voting who to infect
* VOTEH: humans voting whos the ghost
* QUITS: quit server/ leave servr
* LISTP: list players/clients in session with the Server
*/
/**
Server Commands:
* MSGRS: "Message recieved": Paramaters: a string detailing to the client that and what the server recieved as command.
*/

View File

@ -1,9 +1,26 @@
package ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol;
import java.util.HashMap;
import java.util.HashSet;
/*
The NightTrainProtocol implements the Communication-Protocol of the
"Night Train to Budapest"" game. It acts as an Interface between Client and server. All Client Messages are
piped through this protocol, in order for the Server to execute the correct action. It is used by the ClientHandler
for this purpose.
*/
public class NightTrainProtocol {
public static HashMap<String, NTtBCommands> stringNTtBCommandsHashMap = new HashMap<>();
public static ProtocolValidator protocolValidator;
public static HashSet<String> legalStrings;
public enum NTtBCommands {
//Client Commands
CRTGM, CHATA, CHATW, CHATG, LEAVG, JOING, VOTEG, QUITS, LISTP, CUSRN,
//Server Responses
MSGRS;
//Allowes to associate strings with the enum objects. the enum fields are easier for switch statements.
}
}

View File

@ -7,11 +7,11 @@ import java.util.HashSet;
public class ProtocolValidator {
//TODO String or NTtBCommands HashSet?
public static HashSet<NTtBCommands> legalCommands = initializeLegalCommands();
public static HashSet<NightTrainProtocol.NTtBCommands> legalCommands = initializeLegalCommands();
//Initialize the legalCommands set with the protocol values.
private static HashSet<NTtBCommands> initializeLegalCommands(){
EnumSet<NTtBCommands> enumVals = EnumSet.allOf(NTtBCommands.class);
private static HashSet<NightTrainProtocol.NTtBCommands> initializeLegalCommands(){
EnumSet<NightTrainProtocol.NTtBCommands> enumVals = EnumSet.allOf(NightTrainProtocol.NTtBCommands.class);
return new HashSet<>(enumVals);
}

View File

@ -1,13 +1,12 @@
package ch.unibas.dmi.dbis.cs108.Multiplayer.Server;
import ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.NTtBCommands;
import ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.NightTrainProtocol;
import ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.ProtocolDecoder;
import java.util.List;
import java.util.Scanner;
public class ClientMsgDecoder implements ProtocolDecoder {
private NTtBCommands protocol;
private NightTrainProtocol.NTtBCommands protocol;
@Override
public String decodeMsg(String msg) {