Merge request fixes

#story[1246]
This commit is contained in:
William Muir
2017-09-08 15:52:01 +12:00
parent cff15ba07d
commit e2bc613c9d
6 changed files with 80 additions and 70 deletions
@@ -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;
}
}
@@ -16,8 +16,6 @@ import seng302.visualiser.ClientToServerThread;
*/
public class ChatCommandsTest {
// @Rule
// public Timeout globalTimeout = new Timeout(3, TimeUnit.SECONDS);
private boolean dcSent = false;
private ClientToServerThread client;
@@ -44,8 +42,6 @@ public class ChatCommandsTest {
RaceStatusData rsd = StreamParser.extractRaceStatus(packet);
if (rsd.getBoatData().get(0)[4] == BoatStatus.FINISHED.getCode()) {
mst.terminate();
System.out.println("AY WE DID IT");
// host.setSocketToClose();
Assert.assertTrue(dcSent);
}
break;
@@ -108,9 +104,7 @@ public class ChatCommandsTest {
ie.printStackTrace();
}
Assert.assertEquals(5.0, GameState.getSpeedMultiplier(), 0.00001);
System.out.println("the thing " + GameState.getSpeedMultiplier());
mst.terminate();
// host.setSocketToClose();
try {
Thread.sleep(2000);
} catch (InterruptedException ie) {
@@ -149,9 +143,7 @@ public class ChatCommandsTest {
ie.printStackTrace();
}
mst.terminate();
// host.setSocketToClose();
Assert.assertEquals(1.0, GameState.getSpeedMultiplier(), 0.00001);
System.out.println("value " + GameState.getSpeedMultiplier());
try {
Thread.sleep(2000);
} catch (InterruptedException ie) {
@@ -175,7 +167,6 @@ public class ChatCommandsTest {
ie.printStackTrace();
}
client = new ClientToServerThread("localhost", 4942);
System.out.println("done client and host assigning");
} catch (IOException ioe) {
ioe.printStackTrace();
}
@@ -239,9 +230,6 @@ public class ChatCommandsTest {
RaceStatusData rsd = StreamParser.extractRaceStatus(packet);
if (rsd.getBoatData().get(0)[4] == BoatStatus.FINISHED.getCode()) {
mst.terminate();
System.out.println("TEST COMPLETE");
// client.setSocketToClose();
// host.setSocketToClose();
Assert.assertTrue(dcSent);
}
break;
@@ -253,30 +241,7 @@ public class ChatCommandsTest {
} catch (IOException ioe) {
ioe.printStackTrace();
}
// try {
// Thread.sleep(100);
// } catch (InterruptedException ie) {
// ie.printStackTrace();
// }
// mst.startGame();
// try {
// Thread.sleep(100);
// } catch (InterruptedException ie) {
// ie.printStackTrace();
// }
host.sendChatterMessage("[time_prefix] <name_prefix> >finish");
dcSent = true;
// try {
// Thread.sleep(200);
// } catch (InterruptedException ie) {
// ie.printStackTrace();
// }
//// host.setSocketToClose();
// mst.terminate();
try {
Thread.sleep(2000);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
-1
View File
@@ -21,7 +21,6 @@ public class SendChatSteps {
private MainServerThread mst;
//TODO Need to mock the controller pane in order to run the full game client
@Given("^There are two games running$")
public void the_are_two_games_running() throws Throwable {
mst = new MainServerThread();