Re-added wind speed & direction variations

- Wind speed  & direction are randomly updated
- Removed two duplicate threads ;)

Tags: #story[1124]
This commit is contained in:
Michael Rausch
2017-08-16 20:48:37 +12:00
parent 28569506f0
commit 87d6799c10
4 changed files with 80 additions and 23 deletions
@@ -36,7 +36,6 @@ import seng302.utilities.XMLParser;
* Created by wmu16 on 10/07/17. * Created by wmu16 on 10/07/17.
*/ */
public class GameState implements Runnable { public class GameState implements Runnable {
@FunctionalInterface @FunctionalInterface
interface NewMessageListener { interface NewMessageListener {
@@ -180,6 +179,14 @@ public class GameState implements Runnable {
return windDirection; return windDirection;
} }
public static void setWindDirection(Double newWindDirection) {
windDirection = newWindDirection;
}
public static void setWindSpeed(Double newWindSpeed) {
windSpeed = newWindSpeed;
}
public static Double getWindSpeedMMS() { public static Double getWindSpeedMMS() {
return windSpeed; return windSpeed;
} }
@@ -250,7 +257,6 @@ public class GameState implements Runnable {
} }
} }
/** /**
* Called periodically in this GameState thread to update the GameState values * Called periodically in this GameState thread to update the GameState values
*/ */
@@ -271,6 +277,8 @@ public class GameState implements Runnable {
checkForLegProgression(yacht); checkForLegProgression(yacht);
raceFinished = false; raceFinished = false;
} }
} }
if (raceFinished) { if (raceFinished) {
@@ -329,7 +337,9 @@ public class GameState implements Runnable {
notifyMessageListeners( notifyMessageListeners(
new YachtEventCodeMessage(serverYacht.getSourceId()) new YachtEventCodeMessage(serverYacht.getSourceId())
); );
} else if (checkBoundaryCollision(serverYacht)) { }
else{
if (checkBoundaryCollision(serverYacht)) {
serverYacht.setLocation( serverYacht.setLocation(
calculateBounceBack(serverYacht, serverYacht.getLocation(), calculateBounceBack(serverYacht, serverYacht.getLocation(),
BOUNCE_DISTANCE_YACHT) BOUNCE_DISTANCE_YACHT)
@@ -343,6 +353,7 @@ public class GameState implements Runnable {
} }
} }
} }
}
private void updateVelocity(ServerYacht yacht) { private void updateVelocity(ServerYacht yacht) {
@@ -4,10 +4,7 @@ import gherkin.lexer.Fi;
import java.io.IOException; import java.io.IOException;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.ArrayList; import java.util.*;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import seng302.gameServer.server.messages.*; import seng302.gameServer.server.messages.*;
import seng302.model.GeoPoint; import seng302.model.GeoPoint;
@@ -25,12 +22,17 @@ import seng302.visualiser.GameClient;
public class MainServerThread implements Runnable, ClientConnectionDelegate { public class MainServerThread implements Runnable, ClientConnectionDelegate {
private static final int PORT = 4942; private static final int PORT = 4942;
private static final Integer CLIENT_UPDATES_PER_SECOND = 10; private static final Integer CLIENT_UPDATES_PER_SECOND = 60;
private static final int LOG_LEVEL = 1; private static final int LOG_LEVEL = 1;
private static final int WARNING_TIME = 10 * -1000; private static final int WARNING_TIME = 10 * -1000;
private static final int PREPATORY_TIME = 5 * -1000; private static final int PREPATORY_TIME = 5 * -1000;
public static final int TIME_TILL_START = 10 * 1000; public static final int TIME_TILL_START = 10 * 1000;
private static final int MAX_WIND_SPEED = 12000;
private static final int MIN_WIND_SPEED = 8000;
public static int windSpeed = 1000;
private boolean terminated; private boolean terminated;
private Thread thread; private Thread thread;
@@ -51,6 +53,7 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
GameState.addMarkPassListener(this::broadcastMessage); GameState.addMarkPassListener(this::broadcastMessage);
terminated = false; terminated = false;
thread = new Thread(this, "MainServer"); thread = new Thread(this, "MainServer");
startUpdatingWind();
thread.start(); thread.start();
} }
@@ -104,6 +107,45 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
} }
} }
private static void updateWind(){
Integer direction = GameState.getWindDirection().intValue();
Integer windSpeed = GameState.getWindSpeedMMS().intValue();
Random random = new Random();
if (Math.floorMod(random.nextInt(), 2) == 0){
direction += random.nextInt(4);
windSpeed += random.nextInt(100) + 500;
}
else{
direction -= random.nextInt(4);
windSpeed -= random.nextInt(100) + 500;
}
direction = Math.floorMod(direction, 360);
if (windSpeed > MAX_WIND_SPEED){
windSpeed -= random.nextInt(1000);
}
if (windSpeed <= MIN_WIND_SPEED){
windSpeed += random.nextInt(1000);
}
GameState.setWindSpeed(Double.valueOf(windSpeed));
GameState.setWindDirection(direction.doubleValue());
}
private static void startUpdatingWind(){
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
updateWind();
}
}, 0, 500);
}
static void serverLog(String message, int logLevel) { static void serverLog(String message, int logLevel) {
if (logLevel <= LOG_LEVEL) { if (logLevel <= LOG_LEVEL) {
@@ -39,7 +39,7 @@ public class RaceStatusMessage extends Message{
this.raceId = raceId; this.raceId = raceId;
this.raceStatus = raceStatus; this.raceStatus = raceStatus;
this.expectedStartTime = expectedStartTime; this.expectedStartTime = expectedStartTime;
this.raceWindDirection = raceWindDirection * windDirFactor; this.raceWindDirection = raceWindDirection * windDirFactor+100.0;
this.windSpeed = windSpeed; this.windSpeed = windSpeed;
this.numBoatsInRace = numBoatsInRace; this.numBoatsInRace = numBoatsInRace;
this.raceType = raceType; this.raceType = raceType;
@@ -73,6 +73,9 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel
private ComboBox<ClientYacht> yachtSelectionComboBox; private ComboBox<ClientYacht> yachtSelectionComboBox;
@FXML @FXML
private Text fpsDisplay; private Text fpsDisplay;
@FXML
private Text windSpeedText;
//Race Data //Race Data
private Map<Integer, ClientYacht> participants; private Map<Integer, ClientYacht> participants;
private Map<Integer, CompoundMark> markers; private Map<Integer, CompoundMark> markers;
@@ -102,8 +105,7 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel
public void loadRace ( public void loadRace (
Map<Integer, ClientYacht> participants, RaceXMLData raceData, RaceState raceState, Map<Integer, ClientYacht> participants, RaceXMLData raceData, RaceState raceState,
ClientYacht player ClientYacht player) {
) {
this.participants = participants; this.participants = participants;
this.courseData = raceData; this.courseData = raceData;
this.markers = raceData.getCompoundMarks(); this.markers = raceData.getCompoundMarks();
@@ -315,7 +317,7 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel
@Override @Override
public void run() { public void run() {
updateRaceTime(); updateRaceTime();
updateWindDirection(); updateWind();
updateOrder(); updateOrder();
// updateSparkLine(); // updateSparkLine();
} }
@@ -355,9 +357,11 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel
/** /**
* Updates the wind direction arrow and text as from info from the StreamParser * Updates the wind direction arrow and text as from info from the StreamParser
*/ */
private void updateWindDirection() { private void updateWind() {
windDirectionText.setText(String.format("%.1f°", raceState.getWindDirection())); windDirectionText.setText(String.format("%.1f°", raceState.getWindDirection()));
windArrowText.setRotate(raceState.getWindDirection()); windArrowText.setRotate(raceState.getWindDirection());
windSpeedText.setText("Speed: " + String.format("%.1f", raceState.getWindSpeed()) + " Knots");
} }