mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
RaceController now uses new parsers & deleted OldApp and OldFileParser
#story[377]
This commit is contained in:
@@ -14,9 +14,6 @@ public class App extends Application
|
||||
primaryStage.setTitle("RaceVision");
|
||||
primaryStage.setScene(new Scene(root));
|
||||
|
||||
// seng302.models.OldApp.main(); // Run this to show how positions are updated
|
||||
// seng302.controllers.RaceController.initializeRace();
|
||||
|
||||
primaryStage.show();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
package seng302.controllers;
|
||||
|
||||
import seng302.models.Boat;
|
||||
import seng302.models.OldFileParser;
|
||||
import seng302.models.Race;
|
||||
import seng302.models.parsers.ConfigParser;
|
||||
import seng302.models.parsers.CourseParser;
|
||||
import seng302.models.parsers.TeamsParser;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
@@ -21,11 +20,11 @@ public class RaceController {
|
||||
Race race = null;
|
||||
|
||||
public void initializeRace() {
|
||||
String raceConfigFile;
|
||||
raceConfigFile = "doc/examples/config.json";
|
||||
String raceConfigFile = "doc/examples/config.xml";
|
||||
String teamsConfigFile = "doc/examples/teams.xml";
|
||||
|
||||
try {
|
||||
race = createRace(raceConfigFile);
|
||||
race = createRace(raceConfigFile, teamsConfigFile);
|
||||
} catch (Exception e) {
|
||||
System.out.println("There was an error creating the race.");
|
||||
}
|
||||
@@ -37,36 +36,28 @@ public class RaceController {
|
||||
}
|
||||
}
|
||||
|
||||
public Race createRace(String configFile) throws Exception {
|
||||
public Race createRace(String configFile, String teamsConfigFile) throws Exception {
|
||||
Race race = new Race();
|
||||
OldFileParser fp;
|
||||
|
||||
// Read team names from file
|
||||
try{
|
||||
fp = new OldFileParser(configFile);
|
||||
}
|
||||
catch (FileNotFoundException e){
|
||||
System.out.println("Config file does not exist");
|
||||
return null;
|
||||
}
|
||||
TeamsParser tp = new TeamsParser(teamsConfigFile);
|
||||
|
||||
// Read course from file
|
||||
ConfigParser config = new ConfigParser(configFile);
|
||||
|
||||
ArrayList<String> boatNames = new ArrayList<>();
|
||||
ArrayList<Map<String, Object>> teams = fp.getTeams();
|
||||
ArrayList<Boat> teams = tp.getBoats();
|
||||
|
||||
//get race size
|
||||
int numberOfBoats = (int) fp.getRaceSize();
|
||||
int boatsAdded = 0;
|
||||
int numberOfBoats = teams.size();
|
||||
|
||||
//get time scale
|
||||
double timeScale = fp.getTimeScale();
|
||||
double timeScale = config.getTimeScale();
|
||||
race.setTimeScale(timeScale);
|
||||
|
||||
for (Map<String, Object> team : teams) {
|
||||
if (boatsAdded < numberOfBoats){
|
||||
boatNames.add((String) team.get("team-name"));
|
||||
race.addBoat(new Boat(team.get("team-name").toString(), (Double) (team.get("velocity"))));
|
||||
}
|
||||
boatsAdded++;
|
||||
for (Boat boat : teams) {
|
||||
boatNames.add(boat.getTeamName());
|
||||
race.addBoat(boat);
|
||||
}
|
||||
|
||||
// Shuffle team names
|
||||
@@ -77,8 +68,8 @@ public class RaceController {
|
||||
return null;
|
||||
}
|
||||
|
||||
CourseParser cp = new CourseParser("doc/examples/course.xml");
|
||||
race.addCourse(cp.getCourse());
|
||||
CourseParser course = new CourseParser("doc/examples/course.xml");
|
||||
race.addCourse(course.getCourse());
|
||||
|
||||
return race;
|
||||
}
|
||||
|
||||
@@ -1,105 +0,0 @@
|
||||
package seng302.models;
|
||||
|
||||
import seng302.models.mark.SingleMark;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
public class OldApp {
|
||||
|
||||
/**
|
||||
* Builds a race object for the AC35 course
|
||||
*
|
||||
* @return a Race object for the AC35 course
|
||||
*/
|
||||
public static Race createRace(String configFile) throws Exception {
|
||||
Race race = new Race();
|
||||
OldFileParser fp;
|
||||
|
||||
// Read team names from file
|
||||
try{
|
||||
fp = new OldFileParser(configFile);
|
||||
}
|
||||
catch (FileNotFoundException e){
|
||||
System.out.println("Config file does not exist");
|
||||
return null;
|
||||
}
|
||||
|
||||
ArrayList<String> boatNames = new ArrayList<>();
|
||||
ArrayList<Map<String, Object>> teams = fp.getTeams();
|
||||
|
||||
//get race size
|
||||
int numberOfBoats = (int) fp.getRaceSize();
|
||||
|
||||
//get time scale
|
||||
double timeScale = fp.getTimeScale();
|
||||
race.setTimeScale(timeScale);
|
||||
|
||||
for (Map<String, Object> team : teams) {
|
||||
boatNames.add((String) team.get("team-name"));
|
||||
}
|
||||
|
||||
// Shuffle team names
|
||||
long seed = System.nanoTime();
|
||||
Collections.shuffle(boatNames, new Random(seed));
|
||||
|
||||
if (numberOfBoats > Array.getLength(boatNames.toArray())) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Add boats to the race
|
||||
for (int i = 0; i < numberOfBoats; i++) {
|
||||
race.addBoat(new Boat(boatNames.get(i), (Double) (teams.get(i).get("velocity"))));
|
||||
}
|
||||
|
||||
// Add marks to race in order
|
||||
// race.addMark(new SingleMark("Start", 32.296038,-64.854401 ));
|
||||
// race.addMark(new SingleMark("Mid SingleMark", 32.292881,-64.843231 ));
|
||||
// race.addMark(new SingleMark("Leeward Gate", 32.283808,-64.850012 ));
|
||||
// race.addMark(new SingleMark("Windward Gate", 32.309908,-64.833665 ));
|
||||
// race.addMark(new SingleMark("Finish", 32.318439,-64.837367 ));
|
||||
|
||||
return race;
|
||||
}
|
||||
|
||||
public static void main() {
|
||||
Race race = null;
|
||||
String raceConfigFile;
|
||||
|
||||
raceConfigFile = "doc/examples/config.json";
|
||||
|
||||
|
||||
try {
|
||||
race = createRace(raceConfigFile);
|
||||
} catch (Exception e) {
|
||||
System.out.println("There was an error creating the race.");
|
||||
}
|
||||
|
||||
// If race was created
|
||||
if (race != null) {
|
||||
//race.displayStartingBoats();
|
||||
|
||||
System.out.println("\n\n");
|
||||
System.out.println("######################");
|
||||
System.out.println("# Live Race Updates ");
|
||||
System.out.println("######################");
|
||||
|
||||
race.startRace();
|
||||
|
||||
System.out.println("\n\n");
|
||||
System.out.println("######################");
|
||||
System.out.println("# Race Results ");
|
||||
System.out.println("######################");
|
||||
|
||||
//race.showRaceMarkerResults();
|
||||
//race.displayFinishingOrder();
|
||||
|
||||
} else {
|
||||
System.out.println("There was an error creating the race. Exiting.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,132 +0,0 @@
|
||||
package seng302.models;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* Read team name from a given Json file. So that user can extract information
|
||||
* efficiently from external files.
|
||||
*/
|
||||
|
||||
public class OldFileParser {
|
||||
|
||||
private String filePath;
|
||||
private JSONObject content;
|
||||
|
||||
/**
|
||||
* used to construct an instance of file parser
|
||||
*
|
||||
* @param filePath a string like path to show location of desired file to
|
||||
* be parsed
|
||||
*/
|
||||
public OldFileParser(String filePath) throws Exception {
|
||||
this.filePath = filePath;
|
||||
this.readFile();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads content from a given file, and return the content as JSONObject.
|
||||
* Throws FileNotFoundException, if the given file cannot be found.
|
||||
*/
|
||||
private void readFile() throws FileNotFoundException {
|
||||
JSONParser parser = new JSONParser();
|
||||
try {
|
||||
this.content = (JSONObject) parser.parse(new FileReader(filePath));
|
||||
|
||||
} catch (FileNotFoundException e) {
|
||||
throw e;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets time scale setting parameter.
|
||||
*
|
||||
* @return long time scale. -1 if parameter is invalid (eg. scale is
|
||||
* negative number, or containing non numeric character) or cannot be found.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public double getTimeScale() {
|
||||
try {
|
||||
double timeScale = (double) this.content.get("time-scale");
|
||||
return timeScale >= 0 ? timeScale : -1;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets race name in the setting file.
|
||||
*
|
||||
* @return a string of race name. null if race name is invalid or cannot
|
||||
* be found.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public String getRaceName() {
|
||||
try {
|
||||
return (String) this.content.get("race-name");
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an array of teams who participate the race.
|
||||
*
|
||||
* @return an ArrayList containing strings of team names. null if teams
|
||||
* setting is invalid or there is no team.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public ArrayList<Map<String, Object>> getTeams() {
|
||||
try {
|
||||
return (ArrayList<Map<String, Object>>) this.content.get("teams");
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the total number of teams.
|
||||
*
|
||||
* @return the number of teams. 0 if no teams or anything goes wrong.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public long getTotalNumberOfTeams() {
|
||||
ArrayList<Map<String, Object>> teams = getTeams();
|
||||
try {
|
||||
return teams.size();
|
||||
} catch (Exception e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the number of boats that would compete during a race. Returns the
|
||||
* total number of race size if parameter is invalid or cannot be found.
|
||||
*
|
||||
* @return an int of the race size.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public long getRaceSize() {
|
||||
long totalTeams = this.getTotalNumberOfTeams();
|
||||
try {
|
||||
long raceSize = (long) this.content.get("race-size");
|
||||
return raceSize >= 0 && raceSize <= totalTeams ? raceSize : totalTeams;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return totalTeams;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user