mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 06:18:44 +00:00
Merge request fixes
#story[1246]
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package seng302.gameServer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -40,6 +41,7 @@ import seng302.utilities.XMLParser;
|
||||
*/
|
||||
public class GameState implements Runnable {
|
||||
|
||||
|
||||
@FunctionalInterface
|
||||
interface NewMessageListener {
|
||||
void notify(Message message);
|
||||
@@ -675,6 +677,36 @@ public class GameState implements Runnable {
|
||||
}
|
||||
|
||||
|
||||
public static void processChatter(ChatterMessage chatterMessage, boolean isHost) {
|
||||
String chatterText = chatterMessage.getMessage();
|
||||
String[] words = chatterText.split("\\s+");
|
||||
if (words.length > 2 && isHost) {
|
||||
switch (words[2].trim()) {
|
||||
case ">speed":
|
||||
try {
|
||||
setSpeedMultiplier(Double.valueOf(words[3]));
|
||||
broadcastChatter(new ChatterMessage(
|
||||
chatterMessage.getMessage_type(),
|
||||
"SERVER: Speed modifier set to x" + words[3]
|
||||
));
|
||||
} catch (Exception e) {
|
||||
Logger logger = LoggerFactory.getLogger(GameState.class);
|
||||
logger.error("cannot parse >speed value");
|
||||
}
|
||||
return;
|
||||
case ">finish":
|
||||
broadcastChatter(new ChatterMessage(
|
||||
chatterMessage.getMessage_type(),
|
||||
"SERVER: Game will now finish"
|
||||
));
|
||||
endRace();
|
||||
return;
|
||||
}
|
||||
}
|
||||
broadcastChatter(chatterMessage);
|
||||
}
|
||||
|
||||
|
||||
public static void addMarkPassListener(NewMessageListener listener) {
|
||||
messageListeners.add(listener);
|
||||
}
|
||||
|
||||
@@ -35,5 +35,12 @@ public class ServerPacketParser {
|
||||
payload[1], new String(Arrays.copyOfRange(payload, 3, payload.length))
|
||||
);
|
||||
}
|
||||
|
||||
public static ChatterMessage extractChatterText(StreamPacket packet) {
|
||||
byte[] payload = packet.getPayload();
|
||||
return new ChatterMessage(
|
||||
payload[1], new String(Arrays.copyOfRange(payload, 3, payload.length))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -211,10 +211,10 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
completeRegistration(requestedType);
|
||||
break;
|
||||
case CHATTER_TEXT:
|
||||
// GameState.broadcastChatter(
|
||||
// ServerPacketParser.extractChatterText(payload)
|
||||
// );
|
||||
parseChatter(payload);
|
||||
ChatterMessage chatterMessage = ServerPacketParser
|
||||
.extractChatterText(
|
||||
new StreamPacket(type, payloadLength, timeStamp, payload));
|
||||
GameState.processChatter(chatterMessage, isHost);
|
||||
break;
|
||||
case RACE_CUSTOMIZATION_REQUEST:
|
||||
Long sourceID = Message
|
||||
@@ -381,35 +381,36 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
isHost = true;
|
||||
}
|
||||
|
||||
private void parseChatter(byte[] chatterPayload) {
|
||||
String chatterText = new String(
|
||||
Arrays.copyOfRange(chatterPayload, 3, 3 + chatterPayload.length)
|
||||
);
|
||||
String[] words = chatterText.split("\\s+");
|
||||
if (words.length > 2 && isHost) {
|
||||
switch (words[2].trim()) {
|
||||
case ">speed":
|
||||
try {
|
||||
GameState.setSpeedMultiplier(Double.valueOf(words[3]));
|
||||
GameState.broadcastChatter(new ChatterMessage(
|
||||
Byte.toUnsignedInt(chatterPayload[1]),
|
||||
"SERVER: Speed modifier set to x" + words[3]
|
||||
));
|
||||
} catch (Exception e) {
|
||||
logger.error("cannot parse >speed value");
|
||||
}
|
||||
return;
|
||||
case ">finish":
|
||||
GameState.broadcastChatter(new ChatterMessage(
|
||||
chatterPayload[1],
|
||||
"SERVER: Game will now finish"
|
||||
));
|
||||
GameState.endRace();
|
||||
return;
|
||||
}
|
||||
}
|
||||
GameState.broadcastChatter(
|
||||
ServerPacketParser.extractChatterText(chatterPayload)
|
||||
);
|
||||
private void checkChatterForCommands(ChatterMessage chatterMessage) {
|
||||
|
||||
// String chatterText = new String(
|
||||
// Arrays.copyOfRange(chatterPayload, 3, 3 + chatterPayload.length)
|
||||
// );
|
||||
// String[] words = chatterText.split("\\s+");
|
||||
// if (words.length > 2 && isHost) {
|
||||
// switch (words[2].trim()) {
|
||||
// case ">speed":
|
||||
// try {
|
||||
// GameState.setSpeedMultiplier(Double.valueOf(words[3]));
|
||||
// GameState.broadcastChatter(new ChatterMessage(
|
||||
// Byte.toUnsignedInt(chatterPayload[1]),
|
||||
// "SERVER: Speed modifier set to x" + words[3]
|
||||
// ));
|
||||
// } catch (Exception e) {
|
||||
// logger.error("cannot parse >speed value");
|
||||
// }
|
||||
// return;
|
||||
// case ">finish":
|
||||
// GameState.broadcastChatter(new ChatterMessage(
|
||||
// chatterPayload[1],
|
||||
// "SERVER: Game will now finish"
|
||||
// ));
|
||||
// GameState.endRace();
|
||||
// return;
|
||||
// }
|
||||
// }
|
||||
// GameState.broadcastChatter(
|
||||
// ServerPacketParser.extractChatterText(chatterPayload)
|
||||
// );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,5 +36,11 @@ public class ChatterMessage extends Message {
|
||||
return MESSAGE_SIZE + message_size;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public int getMessage_type() {
|
||||
return message_type;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user