Merge remote-tracking branch 'origin/splitIntoTwoModules' into splitIntoTwoModules

main
Joseph Gardner 9 years ago
commit 9eefbbd7fa

@ -32,6 +32,8 @@ public class Race implements Runnable {
protected long totalTimeElapsed;
protected int scaleFactor;
protected int PRERACE_TIME = 12; //time in milliseconds to pause during pre-race
protected boolean countdownFinish = false;
protected boolean runRace = true;
private int lastFPS = 20;
private int raceId;
private int dnfChance = 0; //percentage chance a boat fails at each checkpoint
@ -123,8 +125,11 @@ public class Race implements Runnable {
timeLeft = startTime - currentTime;
System.out.println(timeLeft);
if (timeLeft <= 0) {
countdownFinish = true;
stop();
simulateRace();
if (runRace) {
simulateRace();
}
} else {
currentTimeInSeconds = (timeLeft * scaleFactor) / 1000;
minutes = currentTimeInSeconds / 60;
@ -136,6 +141,7 @@ public class Race implements Runnable {
}
};
timer.start();
//countdownFinish = true;
}
/**

@ -1,7 +1,9 @@
package seng302.Model;
import SharedModel.*;
import javafx.application.Application;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
@ -29,192 +31,227 @@ import static org.mockito.Mockito.verify;
/**
* Created by esa46 on 15/03/17.
*/
public class RaceTest {
//
// private static MockOutput mockOutput;
// Leg START_LEG = new Leg("Start", new Marker(new GPSCoordinate(0, 0)), new Marker(new GPSCoordinate(1, 1)), 0);
// Leg FINISH_LEG = new Leg("Finish", new Marker(new GPSCoordinate(1, 1)), new Marker(new GPSCoordinate(2, 2)), 0);
public class RaceTest{
private static MockOutput mockOutput;
SharedModel.Leg START_LEG = new SharedModel.Leg("Start", new SharedModel.Marker(new SharedModel.GPSCoordinate(0, 0)), new SharedModel.Marker(new SharedModel.GPSCoordinate(1, 1)), 0);
SharedModel.Leg FINISH_LEG = new SharedModel.Leg("Finish", new SharedModel.Marker(new SharedModel.GPSCoordinate(1, 1)), new SharedModel.Marker(new SharedModel.GPSCoordinate(2, 2)), 0);
// @Override
// public void start(Stage primaryStage) throws Exception{}
//
// @BeforeClass
// public static void setUp() {
// try {
// mockOutput = new MockOutput();
// } catch(IOException e) {
// fail();
// }
// public static void main(String[] args) {
// launch(args);
// }
//
// @Ignore
// @Test
// public void boatLocationMessagesAreGenerated() {
// try {
// RaceDataSource raceDataSource = new RaceXMLReader("raceXML/bermuda_AC35.xml");
//
// MockOutput mockOutput = Mockito.mock(MockOutput.class);
// Race race = new Race(raceDataSource, 100, mockOutput);
// new Thread((race)).start();
//
// try {
// Thread.sleep(5000);
// } catch (InterruptedException e) {
// fail();
// @BeforeClass
// public static void initJFX() {
// Thread t = new Thread("JavaFX Init Thread") {
// public void run() {
// Application.launch(TestApp.class, new String[0]);
// }
//
//
// verify(mockOutput, atLeast(400)).parseBoatLocation(any());
//
// } catch (IOException e) {
// fail();
// } catch (SAXException e) {
// fail();
// } catch (ParserConfigurationException e) {
// fail();
// }
// }
//
// @Test
// public void timerCanBeDisabled() {
// Boat boat1 = new Boat("Test 1", 10000, Color.ALICEBLUE, "t1", 1);
// Boat boat2 = new Boat("Test 2", 10000, Color.BURLYWOOD, "t2", 2);
// List<Boat> boats = new ArrayList<>();
// boats.add(boat1);
// boats.add(boat2);
//
// ArrayList<.Leg> legs = new ArrayList<>();
// legs.add(START_LEG);
// legs.add(FINISH_LEG);
//
//
// Race race = new Race(boats, legs, 5, mockOutput);
// race.setDnfChance(0);
// long timeStarted = System.currentTimeMillis();
// race.run();
// assertTrue(System.currentTimeMillis() - timeStarted < 4000);
// }
//
// @Test
// public void checkPositionUpdatesNumberFinishedBoats() {
//
// Boat finishedBoat = new Boat("Test", 1000, Color.ALICEBLUE, "tt", 1);
// finishedBoat.setDistanceTravelledInLeg(500);
//
// finishedBoat.setCurrentLeg(FINISH_LEG);
//
// ArrayList<Boat> boats = new ArrayList<>();
// boats.add(finishedBoat);
//
// ArrayList<.Leg> legs = new ArrayList<>();
// legs.add(FINISH_LEG);
//
// Race race = new Race(boats, legs, 5, mockOutput);
// race.setDnfChance(0);
// assertEquals(race.boatsFinished, 0);
//
//
// race.checkPosition(finishedBoat, 100000);
// assertEquals(race.boatsFinished, 1);
// assertEquals(finishedBoat.getTimeFinished(), 100000);
//
// }
//
// @Test
// public void checkPositionDoesntUpdateNumberFinishedBoats() {
//
// Boat unFinishedBoat = new Boat("Test", 10, Color.ALICEBLUE, "tt", 1);
// unFinishedBoat.setDistanceTravelledInLeg(0);
//
// unFinishedBoat.setCurrentLeg(FINISH_LEG);
//
// ArrayList<Boat> boats = new ArrayList<>();
// boats.add(unFinishedBoat);
//
// ArrayList<.Leg> legs = new ArrayList<>();
// legs.add(FINISH_LEG);
//
//
// Race race = new Race(boats, legs, 1, mockOutput);
// race.setDnfChance(0);
// assertEquals(race.boatsFinished, 0);
// race.checkPosition(unFinishedBoat, 100);
// assertEquals(race.boatsFinished, 0);
//
// }
//
// @Test
// public void distanceTravelledBeforeUpdatingLegIsRetained() {
//
// ArrayList<Boat> boats = new ArrayList<>();
//
// ArrayList<.Leg> legs = new ArrayList<>();
//
// legs.add(START_LEG);
// legs.add(FINISH_LEG);
//
// Race race = new Race(boats, legs, 1, mockOutput);
// race.setDnfChance(0);
//
// Boat unFinishedBoat = new Boat("Test", 10, Color.ALICEBLUE, "tt", 4);
// unFinishedBoat.setDistanceTravelledInLeg(100);
// unFinishedBoat.setCurrentLeg(START_LEG);
//
// race.checkPosition(unFinishedBoat, 100);
// assertEquals(unFinishedBoat.getCurrentLeg().getName(), "Finish");
// assertTrue(unFinishedBoat.getDistanceTravelledInLeg() > 0);
// assertTrue(unFinishedBoat.getDistanceTravelledInLeg() < 100);
// }
//
// @Ignore
// @Test
// public void timerDelaysByHalfSecond() throws InterruptedException {
//
// ArrayList<Boat> boats = new ArrayList<>();
//
// ArrayList<.Leg> legs = new ArrayList<>();
// legs.add(START_LEG);
//
// Race race = new Race(boats, legs, 1, mockOutput);
// race.PRERACE_TIME = 500;
//
// long timeStarted = System.currentTimeMillis();
// race.countdownTimer();
//
// Thread.sleep(500);
//
// assertTrue(System.currentTimeMillis() - timeStarted > 500);
//
//
// }
//
// @Test
// public void scalerScalesVelocityCorrectly() {
//
// int scaleFactor = 3;
// float vel1 = 0;
// float vel2 = (float) 1.999;
// float vel3 = (float) 32.5;
// float vel4 = 500;
// Boat boat1 = new Boat("test", vel1, Color.ALICEBLUE, "tt",1);
// Boat boat2 = new Boat("test", vel2, Color.ALICEBLUE, "tt", 2);
// Boat boat3 = new Boat("test", vel3, Color.ALICEBLUE, "tt", 3);
// Boat boat4 = new Boat("test", vel4, Color.ALICEBLUE, "tt", 4);
// ArrayList<Boat> boats = new ArrayList<>();
// boats.add(boat1);
// boats.add(boat2);
// boats.add(boat3);
// boats.add(boat4);
//
// ArrayList<.Leg> legs = new ArrayList<>();
// legs.add(START_LEG);
//
//
// Race race = new Race(boats, legs, scaleFactor, mockOutput);
// race.setDnfChance(0);
//
// assertEquals(race.getStartingBoats().get(0).getScaledVelocity(), vel1 * scaleFactor, 1e-6);
// assertEquals(race.getStartingBoats().get(1).getScaledVelocity(), vel2 * scaleFactor, 1e-6);
// assertEquals(race.getStartingBoats().get(2).getScaledVelocity(), vel3 * scaleFactor, 1e-6);
// assertEquals(race.getStartingBoats().get(3).getScaledVelocity(), vel4 * scaleFactor, 1e-6);
//
// };
// t.setDaemon(true);
// t.start();
// }
@BeforeClass
public static void setUp() {
try {
mockOutput = new MockOutput();
} catch(IOException e) {
fail();
}
}
@Ignore
@Test
public void boatLocationMessagesAreGenerated() {
try {
RaceDataSource raceDataSource = new RaceXMLReader("raceXML/bermuda_AC35.xml");
MockOutput mockOutput = Mockito.mock(MockOutput.class);
Race race = new Race(raceDataSource, 100, mockOutput);
new Thread((race)).start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
fail();
}
verify(mockOutput, atLeast(400)).parseBoatLocation(any());
} catch (IOException e) {
fail();
} catch (SAXException e) {
fail();
} catch (ParserConfigurationException e) {
fail();
}
}
@Test
public void timerCanBeDisabled() {
SharedModel.BoatInRace boat1 = new SharedModel.BoatInRace("Test 1", 10000, Color.ALICEBLUE, "t1", 1);
SharedModel.BoatInRace boat2 = new SharedModel.BoatInRace("Test 2", 10000, Color.BURLYWOOD, "t2", 2);
List<SharedModel.BoatInRace> boats = new ArrayList<>();
boats.add(boat1);
boats.add(boat2);
ArrayList<SharedModel.Leg> legs = new ArrayList<>();
legs.add(START_LEG);
legs.add(FINISH_LEG);
Race race = new Race(boats, legs, 5, mockOutput);
race.setDnfChance(0);
long timeStarted = System.currentTimeMillis();
race.run();
assertTrue(System.currentTimeMillis() - timeStarted < 4000);
}
@Test
public void checkPositionUpdatesNumberFinishedBoats() {
SharedModel.BoatInRace finishedBoat = new SharedModel.BoatInRace("Test", 1000, Color.ALICEBLUE, "tt", 1);
finishedBoat.setDistanceTravelledInLeg(500);
finishedBoat.setCurrentLeg(FINISH_LEG);
ArrayList<SharedModel.BoatInRace> boats = new ArrayList<>();
boats.add(finishedBoat);
ArrayList<SharedModel.Leg> legs = new ArrayList<>();
legs.add(FINISH_LEG);
Race race = new Race(boats, legs, 5, mockOutput);
race.setDnfChance(0);
assertEquals(race.boatsFinished, 0);
race.checkPosition(finishedBoat, 100000);
assertEquals(race.boatsFinished, 1);
assertEquals(finishedBoat.getTimeFinished(), 100000);
}
@Test
public void checkPositionDoesntUpdateNumberFinishedBoats() {
SharedModel.BoatInRace unFinishedBoat = new SharedModel.BoatInRace("Test", 10, Color.ALICEBLUE, "tt", 1);
unFinishedBoat.setDistanceTravelledInLeg(0);
unFinishedBoat.setCurrentLeg(FINISH_LEG);
ArrayList<SharedModel.BoatInRace> boats = new ArrayList<>();
boats.add(unFinishedBoat);
ArrayList<SharedModel.Leg> legs = new ArrayList<>();
legs.add(FINISH_LEG);
Race race = new Race(boats, legs, 1, mockOutput);
race.setDnfChance(0);
assertEquals(race.boatsFinished, 0);
race.checkPosition(unFinishedBoat, 100);
assertEquals(race.boatsFinished, 0);
}
@Test
public void distanceTravelledBeforeUpdatingLegIsRetained() {
ArrayList<SharedModel.BoatInRace> boats = new ArrayList<>();
ArrayList<SharedModel.Leg> legs = new ArrayList<>();
legs.add(START_LEG);
legs.add(FINISH_LEG);
Race race = new Race(boats, legs, 1, mockOutput);
race.setDnfChance(0);
SharedModel.BoatInRace unFinishedBoat = new SharedModel.BoatInRace("Test", 10, Color.ALICEBLUE, "tt", 4);
unFinishedBoat.setDistanceTravelledInLeg(100);
unFinishedBoat.setCurrentLeg(START_LEG);
race.checkPosition(unFinishedBoat, 100);
assertEquals(unFinishedBoat.getCurrentLeg().getName(), "Finish");
assertTrue(unFinishedBoat.getDistanceTravelledInLeg() > 0);
assertTrue(unFinishedBoat.getDistanceTravelledInLeg() < 100);
}
@Ignore
@Test
public void timerDelaysByHalfSecond() throws InterruptedException {
ArrayList<SharedModel.BoatInRace> boats = new ArrayList<>();
ArrayList<SharedModel.Leg> legs = new ArrayList<>();
legs.add(START_LEG);
Race race = new Race(boats, legs, 1, mockOutput);
race.PRERACE_TIME = 500;
race.runRace = false;
race.countdownTimer();
long timeStarted = System.currentTimeMillis();
long currentTime = System.currentTimeMillis();
while (!race.countdownFinish) {
currentTime = System.currentTimeMillis();
}
assertTrue(currentTime - timeStarted > 500);
}
@Test
public void scalerScalesVelocityCorrectly() {
int scaleFactor = 3;
float vel1 = 0;
float vel2 = (float) 1.999;
float vel3 = (float) 32.5;
float vel4 = 500;
SharedModel.BoatInRace boat1 = new SharedModel.BoatInRace("test", vel1, Color.ALICEBLUE, "tt",1);
SharedModel.BoatInRace boat2 = new SharedModel.BoatInRace("test", vel2, Color.ALICEBLUE, "tt", 2);
SharedModel.BoatInRace boat3 = new SharedModel.BoatInRace("test", vel3, Color.ALICEBLUE, "tt", 3);
SharedModel.BoatInRace boat4 = new SharedModel.BoatInRace("test", vel4, Color.ALICEBLUE, "tt", 4);
ArrayList<SharedModel.BoatInRace> boats = new ArrayList<>();
boats.add(boat1);
boats.add(boat2);
boats.add(boat3);
boats.add(boat4);
ArrayList<SharedModel.Leg> legs = new ArrayList<>();
legs.add(START_LEG);
Race race = new Race(boats, legs, scaleFactor, mockOutput);
race.setDnfChance(0);
assertEquals(race.getStartingBoats().get(0).getScaledVelocity(), vel1 * scaleFactor, 1e-6);
assertEquals(race.getStartingBoats().get(1).getScaledVelocity(), vel2 * scaleFactor, 1e-6);
assertEquals(race.getStartingBoats().get(2).getScaledVelocity(), vel3 * scaleFactor, 1e-6);
assertEquals(race.getStartingBoats().get(3).getScaledVelocity(), vel4 * scaleFactor, 1e-6);
}
@Test
public void doNotFinishCorrectly() {
ArrayList<SharedModel.BoatInRace> boats = new ArrayList<>();
ArrayList<SharedModel.Leg> legs = new ArrayList<>();
legs.add(START_LEG);
Race race = new Race(boats, legs, 1, mockOutput);
race.setDnfChance(100);
assertTrue(race.doNotFinish());
race.setDnfChance(0);
assertTrue(!race.doNotFinish());
}
}

@ -10,6 +10,11 @@ public class AC35UnitConverter {
return (double) value * 180.0/21474836418.0;//2^31 = 21474836418
}
public static int convertGPSToInt(double value){
//converts latitude or longitue to angle
return (int) (value * 21474836418.0/180.0);//2^31 = 21474836418
}
public static double convertHeading(long value){
return (double) value * 360.0/65536.0;//2^15
}

@ -7,6 +7,8 @@ package seng302.Networking.Utils;
import SharedModel.Constants;
import seng302.Model.Boat;
import static seng302.Networking.Utils.AC35UnitConverter.convertGPSToInt;
/**
* Represents the information in a boat location message (AC streaming spec: 4.9).
*/
@ -156,8 +158,8 @@ public class BoatLocationMessage extends AC35Data
this.sourceID = boat.getSourceID();
this.sequenceNumber = sequenceNumber;
this.deviceType = 1;
this.latitude = 0;//boat.getCurrentPosition().getLatitude();
this.longitude = 0;//boat.getCurrentPosition().getLongitude();
this.latitude = convertGPSToInt(boat.getCurrentPosition().getLatitude());
this.longitude = convertGPSToInt(boat.getCurrentPosition().getLongitude());
this.altitude = 0;
this.heading = 0;
this.pitch = 0;

Loading…
Cancel
Save