mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
Rather than a high frequency loop sending all packets a low frequency loop is made then destroyed on button press for sending turn packets. This means a fast response time on button press but fewer packets sent.
#implement #issue[38]
This commit is contained in:
@@ -4,7 +4,7 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import seng302.gameServer.server.messages.BoatActionType;
|
||||
import seng302.gameServer.server.messages.BoatAction;
|
||||
import seng302.model.Player;
|
||||
import seng302.model.Yacht;
|
||||
|
||||
@@ -119,7 +119,7 @@ public class GameState implements Runnable {
|
||||
return yachts;
|
||||
}
|
||||
|
||||
public static void updateBoat(Integer sourceId, BoatActionType actionType) {
|
||||
public static void updateBoat(Integer sourceId, BoatAction actionType) {
|
||||
Yacht playerYacht = yachts.get(sourceId);
|
||||
// System.out.println("-----------------------");
|
||||
switch (actionType) {
|
||||
|
||||
@@ -42,7 +42,6 @@ public class HeartbeatThread extends Thread{
|
||||
*/
|
||||
private void sendHeartbeatToAllPlayers(){
|
||||
Message heartbeat = new Heartbeat(seqNum);
|
||||
System.out.println(GameState.getPlayers());
|
||||
for (Player player : GameState.getPlayers()){
|
||||
if (!player.getSocket().isConnected()) {
|
||||
playerLostConnection(player);
|
||||
@@ -54,7 +53,6 @@ public class HeartbeatThread extends Thread{
|
||||
playerLostConnection(player);
|
||||
}
|
||||
}
|
||||
|
||||
updateDelegate();
|
||||
seqNum++;
|
||||
}
|
||||
@@ -71,7 +69,6 @@ public class HeartbeatThread extends Thread{
|
||||
|
||||
public void run(){
|
||||
Timer t = new Timer();
|
||||
|
||||
t.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
@@ -2,17 +2,17 @@ package seng302.gameServer;
|
||||
|
||||
import java.util.Arrays;
|
||||
import seng302.model.stream.packets.StreamPacket;
|
||||
import seng302.gameServer.server.messages.BoatActionType;
|
||||
import seng302.gameServer.server.messages.BoatAction;
|
||||
|
||||
|
||||
public class ServerPacketParser {
|
||||
|
||||
|
||||
public static BoatActionType extractBoatAction(StreamPacket packet) {
|
||||
public static BoatAction extractBoatAction(StreamPacket packet) {
|
||||
byte[] payload = packet.getPayload();
|
||||
int messageVersionNo = payload[0];
|
||||
long actionTypeValue = bytesToLong(Arrays.copyOfRange(payload, 0, 1));
|
||||
return BoatActionType.getType((int) actionTypeValue);
|
||||
return BoatAction.getType((int) actionTypeValue);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -25,7 +25,7 @@ import seng302.model.stream.packets.StreamPacket;
|
||||
import seng302.model.stream.xml.generator.Race;
|
||||
import seng302.model.stream.xml.generator.Regatta;
|
||||
import seng302.utilities.XMLGenerator;
|
||||
import seng302.gameServer.server.messages.BoatActionType;
|
||||
import seng302.gameServer.server.messages.BoatAction;
|
||||
import seng302.gameServer.server.messages.BoatLocationMessage;
|
||||
import seng302.gameServer.server.messages.BoatStatus;
|
||||
import seng302.gameServer.server.messages.BoatSubMessage;
|
||||
@@ -168,7 +168,7 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
//System.out.println("RECEIVED A PACKET");
|
||||
switch (PacketType.assignPacketType(type, payload)) {
|
||||
case BOAT_ACTION:
|
||||
BoatActionType actionType = ServerPacketParser
|
||||
BoatAction actionType = ServerPacketParser
|
||||
.extractBoatAction(
|
||||
new StreamPacket(type, payloadLength, timeStamp, payload));
|
||||
GameState.updateBoat(sourceId, actionType);
|
||||
|
||||
+5
-5
@@ -6,7 +6,7 @@ import java.util.Map;
|
||||
/**
|
||||
* Created by kre39 on 12/07/17.
|
||||
*/
|
||||
public enum BoatActionType {
|
||||
public enum BoatAction {
|
||||
|
||||
VMG(1),
|
||||
SAILS_IN(2),
|
||||
@@ -17,19 +17,19 @@ public enum BoatActionType {
|
||||
MAINTAIN_HEADING(7);
|
||||
|
||||
private final int type;
|
||||
private static final Map<Integer, BoatActionType> intToTypeMap = new HashMap<>();
|
||||
private static final Map<Integer, BoatAction> intToTypeMap = new HashMap<>();
|
||||
|
||||
static {
|
||||
for (BoatActionType type : BoatActionType.values()) {
|
||||
for (BoatAction type : BoatAction.values()) {
|
||||
intToTypeMap.put(type.getValue(), type);
|
||||
}
|
||||
}
|
||||
|
||||
BoatActionType(int type){
|
||||
BoatAction(int type){
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public static BoatActionType getType(int value) {
|
||||
public static BoatAction getType(int value) {
|
||||
return intToTypeMap.get(value);
|
||||
}
|
||||
|
||||
@@ -6,9 +6,9 @@ package seng302.gameServer.server.messages;
|
||||
public class BoatActionMessage extends Message{
|
||||
private final MessageType MESSAGE_TYPE = MessageType.BOAT_ACTION;
|
||||
private final int MESSAGE_SIZE = 1;
|
||||
private BoatActionType actionType;
|
||||
private BoatAction actionType;
|
||||
|
||||
public BoatActionMessage(BoatActionType actionType) {
|
||||
public BoatActionMessage(BoatAction actionType) {
|
||||
this.actionType = actionType;
|
||||
setHeader(new Header(MessageType.BOAT_ACTION, 0, (short) 1)); // the second variable is the source id
|
||||
allocateBuffer();
|
||||
|
||||
@@ -48,7 +48,7 @@ public class Yacht {
|
||||
private Integer legNumber = 0;
|
||||
|
||||
//SERVER SIDE
|
||||
public static final Double TURN_STEP = 1.0; //This should be in some utils class somewhere 2bh. Public for tests sake.
|
||||
public static final Double TURN_STEP = 5.0; //This should be in some utils class somewhere 2bh. Public for tests sake.
|
||||
private Double lastHeading;
|
||||
private Boolean sailIn;
|
||||
private GeoPoint location;
|
||||
|
||||
@@ -17,10 +17,10 @@ import java.util.zip.Checksum;
|
||||
import javafx.application.Platform;
|
||||
import javafx.scene.control.Alert;
|
||||
import javafx.scene.control.Alert.AlertType;
|
||||
import seng302.gameServer.server.messages.BoatActionType;
|
||||
import seng302.model.stream.packets.StreamPacket;
|
||||
import seng302.gameServer.server.messages.BoatActionMessage;
|
||||
import seng302.gameServer.server.messages.BoatAction;
|
||||
import seng302.gameServer.server.messages.Message;
|
||||
import seng302.model.stream.packets.StreamPacket;
|
||||
|
||||
/**
|
||||
* A class describing a single connection to a Server for the purposes of sending and receiving on
|
||||
@@ -53,10 +53,10 @@ public class ClientToServerThread implements Runnable {
|
||||
|
||||
//Output stream
|
||||
private OutputStream os;
|
||||
private Timer osTimer = new Timer();
|
||||
private Queue<BoatActionType> eventQueue = new ConcurrentLinkedQueue<>();
|
||||
private boolean upwindFlag = false, downwindFlag = false;
|
||||
static public final int PACKET_SENDING_INTERVAL_MS = 20;
|
||||
private Timer upWindPacketTimer = new Timer();
|
||||
private Timer downWindPacketTimer = new Timer();
|
||||
private boolean upwindTimerFlag = false, downwindTimerFlag = false;
|
||||
static public final int PACKET_SENDING_INTERVAL_MS = 100;
|
||||
|
||||
private int clientId;
|
||||
private ByteArrayOutputStream crcBuffer;
|
||||
@@ -90,15 +90,6 @@ public class ClientToServerThread implements Runnable {
|
||||
|
||||
thread = new Thread(this);
|
||||
thread.start();
|
||||
|
||||
osTimer.scheduleAtFixedRate(
|
||||
new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
processBoatActionQueue();
|
||||
}
|
||||
}, 0, PACKET_SENDING_INTERVAL_MS
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -163,7 +154,6 @@ public class ClientToServerThread implements Runnable {
|
||||
clientLog(e.getMessage(), 1);
|
||||
return;
|
||||
}
|
||||
// System.out.println("streamPackets = " + streamPackets.size());
|
||||
}
|
||||
closeSocket();
|
||||
clientLog("Closed connection to Server", 0);
|
||||
@@ -195,30 +185,66 @@ public class ClientToServerThread implements Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends packets for the given boat action. Special cases are: \n
|
||||
* - DOWNWIND = Packets are sent every ClientToServerThread.PACKET_SENDING_INTERVAL_MS
|
||||
* - UPWIND = Packets are sent every ClientToServerThread.PACKET_SENDING_INTERVAL_MS
|
||||
* - MAINTAIN_HEADING = DOWNWIND and UPWIND packets stop being sent.
|
||||
* @param actionType The boat action that will dictate packets sent.
|
||||
*/
|
||||
public void sendBoatAction(BoatAction actionType) {
|
||||
switch (actionType) {
|
||||
case MAINTAIN_HEADING:
|
||||
if (upwindTimerFlag) {
|
||||
cancelTimer(upWindPacketTimer);
|
||||
upwindTimerFlag = false;
|
||||
upWindPacketTimer = new Timer();
|
||||
}
|
||||
if (downwindTimerFlag) {
|
||||
cancelTimer(downWindPacketTimer);
|
||||
downwindTimerFlag = false;
|
||||
downWindPacketTimer = new Timer();
|
||||
}
|
||||
break;
|
||||
case DOWNWIND:
|
||||
if (!downwindTimerFlag) {
|
||||
downwindTimerFlag = true;
|
||||
downWindPacketTimer.scheduleAtFixedRate(
|
||||
new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
sendBoatAction(new BoatActionMessage(BoatAction.DOWNWIND));
|
||||
}
|
||||
}, 0, PACKET_SENDING_INTERVAL_MS
|
||||
);
|
||||
}
|
||||
break;
|
||||
case UPWIND:
|
||||
if (!upwindTimerFlag) {
|
||||
upwindTimerFlag = true;
|
||||
upWindPacketTimer.scheduleAtFixedRate(
|
||||
new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
sendBoatAction(new BoatActionMessage(BoatAction.UPWIND));
|
||||
}
|
||||
}, 0, PACKET_SENDING_INTERVAL_MS
|
||||
);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
sendBoatAction(new BoatActionMessage(actionType));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes next element in the queue of events to send.
|
||||
* Cancels a packet sending timer.
|
||||
* @param timer The timer to cancel.
|
||||
*/
|
||||
private void processBoatActionQueue() {
|
||||
BoatActionType action = eventQueue.poll();
|
||||
if (action != null) {
|
||||
switch (action) {
|
||||
case MAINTAIN_HEADING:
|
||||
downwindFlag = upwindFlag = false; break;
|
||||
case DOWNWIND:
|
||||
downwindFlag = true; break;
|
||||
case UPWIND:
|
||||
upwindFlag = true; break;
|
||||
default:
|
||||
sendBoatAction(new BoatActionMessage(action)); break;
|
||||
}
|
||||
}
|
||||
if (downwindFlag) {
|
||||
sendBoatAction(new BoatActionMessage(BoatActionType.DOWNWIND));
|
||||
}
|
||||
if (upwindFlag) {
|
||||
sendBoatAction(new BoatActionMessage(BoatActionType.UPWIND));
|
||||
}
|
||||
private void cancelTimer (Timer timer) {
|
||||
timer.cancel();
|
||||
timer.purge();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -289,7 +315,4 @@ public class ClientToServerThread implements Runnable {
|
||||
return clientId;
|
||||
}
|
||||
|
||||
public void sendBoatEvent(BoatActionType actionType) {
|
||||
eventQueue.add(actionType);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ import javafx.scene.Node;
|
||||
import javafx.scene.input.KeyEvent;
|
||||
import javafx.scene.layout.Pane;
|
||||
import seng302.gameServer.MainServerThread;
|
||||
import seng302.gameServer.server.messages.BoatActionType;
|
||||
import seng302.gameServer.server.messages.BoatAction;
|
||||
import seng302.model.RaceState;
|
||||
import seng302.model.Yacht;
|
||||
import seng302.model.stream.packets.StreamPacket;
|
||||
@@ -274,13 +274,13 @@ public class GameClient {
|
||||
private void keyPressed(KeyEvent e) {
|
||||
switch (e.getCode()) {
|
||||
case SPACE: // align with vmg
|
||||
socketThread.sendBoatEvent(BoatActionType.VMG); break;
|
||||
socketThread.sendBoatAction(BoatAction.VMG); break;
|
||||
case PAGE_UP: // upwind
|
||||
socketThread.sendBoatEvent(BoatActionType.UPWIND); break;
|
||||
socketThread.sendBoatAction(BoatAction.UPWIND); break;
|
||||
case PAGE_DOWN: // downwind
|
||||
socketThread.sendBoatEvent(BoatActionType.DOWNWIND); break;
|
||||
socketThread.sendBoatAction(BoatAction.DOWNWIND); break;
|
||||
case ENTER: // tack/gybe
|
||||
socketThread.sendBoatEvent(BoatActionType.TACK_GYBE); break;
|
||||
socketThread.sendBoatAction(BoatAction.TACK_GYBE); break;
|
||||
//TODO Allow a zoom in and zoom out methods
|
||||
case Z: // zoom in
|
||||
System.out.println("Zoom in");
|
||||
@@ -295,10 +295,10 @@ public class GameClient {
|
||||
switch (e.getCode()) {
|
||||
//TODO 12/07/17 Determine the sail state and send the appropriate packet (eg. if sails are in, send a sail out packet)
|
||||
case SHIFT: // sails in/sails out
|
||||
socketThread.sendBoatEvent(BoatActionType.SAILS_IN); break;
|
||||
socketThread.sendBoatAction(BoatAction.SAILS_IN); break;
|
||||
case PAGE_UP:
|
||||
case PAGE_DOWN:
|
||||
socketThread.sendBoatEvent(BoatActionType.MAINTAIN_HEADING); break;
|
||||
socketThread.sendBoatAction(BoatAction.MAINTAIN_HEADING); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -492,7 +492,6 @@ public class GameView extends Pane {
|
||||
distanceFromReference = GeoUtility.getDistance(
|
||||
minLatPoint, new GeoPoint(unscaledLat, unscaledLon)
|
||||
);
|
||||
// System.out.println("distanceFromReference = " + distanceFromReference);
|
||||
if (angleFromReference >= 0 && angleFromReference <= Math.PI / 2) {
|
||||
xAxisLocation += Math.round(distanceScaleFactor * Math.sin(angleFromReference) * distanceFromReference);
|
||||
yAxisLocation -= Math.round(distanceScaleFactor * Math.cos(angleFromReference) * distanceFromReference);
|
||||
@@ -512,8 +511,6 @@ public class GameView extends Pane {
|
||||
if(horizontalInversion) {
|
||||
xAxisLocation = canvasWidth - bufferSize - (xAxisLocation - bufferSize);
|
||||
}
|
||||
// System.out.println("yAxisLocation = " + yAxisLocation + " " + unscaledLat);
|
||||
// System.out.println("xAxisLocation = " + xAxisLocation + " " + unscaledLon);
|
||||
return new Point2D(xAxisLocation, yAxisLocation);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user