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:
@@ -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