Merge remote-tracking branch 'origin/Jonas_Stuff' into Jonas_Stuff
This commit is contained in:
commit
2f7db70df7
@ -11,15 +11,16 @@ public class JServerProtocolParser {
|
||||
*/
|
||||
public static void parse(String msg, ClientHandler h) {
|
||||
String header = ""; //"header" is the first 5 characters, i.e. the protocol part
|
||||
String formattedMSG = MessageFormatter.formatMsg(msg);
|
||||
try {
|
||||
header = msg.substring(0, 5);
|
||||
header = formattedMSG.substring(0, 5);
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
System.out.println("Received unknown command");
|
||||
}
|
||||
//System.out.println(header); helpful for debugging
|
||||
switch (header) {
|
||||
case "CHATA":
|
||||
h.broadcastMessage(msg.substring(6));
|
||||
h.broadcastMessage(formattedMSG.substring(6));
|
||||
break;
|
||||
case "CPING":
|
||||
h.sendMsgToClient("PINGB");
|
||||
|
||||
@ -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