Merge remote-tracking branch 'origin/Jonas_Stuff' into Jonas_Stuff

This commit is contained in:
Jonas 2022-03-27 13:29:16 +02:00
commit 2f7db70df7
2 changed files with 45 additions and 2 deletions

View File

@ -11,15 +11,16 @@ public class JServerProtocolParser {
*/ */
public static void parse(String msg, ClientHandler h) { public static void parse(String msg, ClientHandler h) {
String header = ""; //"header" is the first 5 characters, i.e. the protocol part String header = ""; //"header" is the first 5 characters, i.e. the protocol part
String formattedMSG = MessageFormatter.formatMsg(msg);
try { try {
header = msg.substring(0, 5); header = formattedMSG.substring(0, 5);
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
System.out.println("Received unknown command"); System.out.println("Received unknown command");
} }
//System.out.println(header); helpful for debugging //System.out.println(header); helpful for debugging
switch (header) { switch (header) {
case "CHATA": case "CHATA":
h.broadcastMessage(msg.substring(6)); h.broadcastMessage(formattedMSG.substring(6));
break; break;
case "CPING": case "CPING":
h.sendMsgToClient("PINGB"); h.sendMsgToClient("PINGB");

View File

@ -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();
}
}