Added a Class and Method that can reformat the terminal commands into commands that the parser can read
This commit is contained in:
parent
6071ea4b2f
commit
2263483462
@ -0,0 +1,42 @@
|
|||||||
|
package ch.unibas.dmi.dbis.cs108.multiplayer.server;
|
||||||
|
|
||||||
|
import java.io.StringBufferInputStream;
|
||||||
|
|
||||||
|
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
|
||||||
|
*
|
||||||
|
* @param msg the Messaged to be reformatted
|
||||||
|
* @return the reformatted message
|
||||||
|
*/
|
||||||
|
|
||||||
|
public static String formatMsg(String msg) {
|
||||||
|
String header = ""; //header is first two characters
|
||||||
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
|
String s; // just a friendly helper to save message in
|
||||||
|
try {
|
||||||
|
header = msg.substring(0, 2);
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
switch (header) {
|
||||||
|
case "/c":
|
||||||
|
stringBuilder.append("CHATA");
|
||||||
|
s = msg.substring(2);
|
||||||
|
break;
|
||||||
|
case "/q":
|
||||||
|
stringBuilder.append("QUITS");
|
||||||
|
s = msg.substring(2);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
s = msg;
|
||||||
|
}
|
||||||
|
stringBuilder.append(s);
|
||||||
|
return stringBuilder.toString();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user