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:
parent
846d0a8187
commit
2b10ff3294
@ -1,16 +1,16 @@
|
|||||||
package ch.unibas.dmi.dbis.cs108.Multiplayer.Client;
|
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;
|
import java.util.HashMap;
|
||||||
|
|
||||||
public class InputToProtocolMap extends HashMap<String, NTtBCommands> {
|
public class InputToProtocolMap extends HashMap<String, NightTrainProtocol.NTtBCommands> {
|
||||||
public InputToProtocolMap(){
|
public InputToProtocolMap(){
|
||||||
super();
|
super();
|
||||||
this.put("chat", NTtBCommands.CHATA);
|
this.put("chat", NightTrainProtocol.NTtBCommands.CHATA);
|
||||||
this.put("cn", NTtBCommands.CUSRN);
|
this.put("cn", NightTrainProtocol.NTtBCommands.CUSRN);
|
||||||
this.put("list", NTtBCommands.LISTP);
|
this.put("list", NightTrainProtocol.NTtBCommands.LISTP);
|
||||||
this.put("exit", NTtBCommands.LEAVG);
|
this.put("exit", NightTrainProtocol.NTtBCommands.LEAVG);
|
||||||
//TODO extend according to extended function
|
//TODO extend according to extended function
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,11 +1,6 @@
|
|||||||
package ch.unibas.dmi.dbis.cs108.Multiplayer.Client;
|
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.ArrayList;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Locale;
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -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.
|
||||||
|
*/
|
||||||
@ -1,9 +1,26 @@
|
|||||||
package ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol;
|
package ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
The NightTrainProtocol implements the Communication-Protocol of the
|
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
|
"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
|
piped through this protocol, in order for the Server to execute the correct action. It is used by the ClientHandler
|
||||||
for this purpose.
|
for this purpose.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class NightTrainProtocol {
|
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.
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,11 +7,11 @@ import java.util.HashSet;
|
|||||||
public class ProtocolValidator {
|
public class ProtocolValidator {
|
||||||
|
|
||||||
//TODO String or NTtBCommands HashSet?
|
//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.
|
//Initialize the legalCommands set with the protocol values.
|
||||||
private static HashSet<NTtBCommands> initializeLegalCommands(){
|
private static HashSet<NightTrainProtocol.NTtBCommands> initializeLegalCommands(){
|
||||||
EnumSet<NTtBCommands> enumVals = EnumSet.allOf(NTtBCommands.class);
|
EnumSet<NightTrainProtocol.NTtBCommands> enumVals = EnumSet.allOf(NightTrainProtocol.NTtBCommands.class);
|
||||||
return new HashSet<>(enumVals);
|
return new HashSet<>(enumVals);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,13 +1,12 @@
|
|||||||
package ch.unibas.dmi.dbis.cs108.Multiplayer.Server;
|
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 ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.ProtocolDecoder;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
public class ClientMsgDecoder implements ProtocolDecoder {
|
public class ClientMsgDecoder implements ProtocolDecoder {
|
||||||
private NTtBCommands protocol;
|
private NightTrainProtocol.NTtBCommands protocol;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String decodeMsg(String msg) {
|
public String decodeMsg(String msg) {
|
||||||
|
|||||||
Reference in New Issue
Block a user