Files
Party-Parrots-At-Sea/src/main/java/seng302/controllers/RaceController.java
T
Michael Rausch 9e22eac4d8 Added the race results to the RaceResultController. Also fixed some bugs
- Fixed a bug where the race results would be out of order.
- Changed the colour of the start and finish gates
- Added the race results to the RaceResultController and updated view

Tags: #fix #implement #story[13, 10, 11]
2017-03-22 22:30:49 +13:00

90 lines
2.4 KiB
Java

package seng302.controllers;
import seng302.models.Boat;
import seng302.models.OldFileParser;
import seng302.models.Race;
import seng302.models.parsers.CourseParser;
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;
/**
* Created by zyt10 on 17/03/17.
* run before CanvasController to initialize race events
* the CanvasController then uses the event data to make the animations
*/
public class RaceController {
Race race = null;
public void initializeRace() {
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 != null) {
race.startRace();
} else {
System.out.println("There was an error creating the race. Exiting.");
}
}
public 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();
int boatsAdded = 0;
//get time scale
double timeScale = fp.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++;
}
// Shuffle team names
long seed = System.nanoTime();
Collections.shuffle(boatNames, new Random(seed));
if (numberOfBoats > Array.getLength(boatNames.toArray())) {
return null;
}
CourseParser cp = new CourseParser("doc/examples/course.xml");
race.addCourse(cp.getCourse());
return race;
}
public Race getRace() {
return race;
}
}