diff --git a/src/main/java/telraam/api/TeamResource.java b/src/main/java/telraam/api/TeamResource.java index 5983eff..9bf36c3 100644 --- a/src/main/java/telraam/api/TeamResource.java +++ b/src/main/java/telraam/api/TeamResource.java @@ -51,9 +51,6 @@ public Team update(Team team, Optional id) { Team previousTeam = this.get(id); Team ret = super.update(team, id); - System.out.println(previousTeam.getBatonId()); - System.out.println(team.getBatonId()); - if (!Objects.equals(previousTeam.getBatonId(), team.getBatonId())) { this.batonSwitchoverDAO.insert(new BatonSwitchover( team.getId(), diff --git a/src/main/java/telraam/logic/positioner/nostradamus/PositionList.java b/src/main/java/telraam/logic/positioner/nostradamus/DetectionList.java similarity index 94% rename from src/main/java/telraam/logic/positioner/nostradamus/PositionList.java rename to src/main/java/telraam/logic/positioner/nostradamus/DetectionList.java index e1c8944..4e128a2 100644 --- a/src/main/java/telraam/logic/positioner/nostradamus/PositionList.java +++ b/src/main/java/telraam/logic/positioner/nostradamus/DetectionList.java @@ -9,7 +9,7 @@ import java.util.Comparator; import java.util.List; -public class PositionList extends ArrayList { +public class DetectionList extends ArrayList { private final int interval; private final List stations; @@ -17,7 +17,7 @@ public class PositionList extends ArrayList { private Detection currentPosition; private Timestamp newestDetection; - public PositionList(int interval, List stations) { + public DetectionList(int interval, List stations) { this.interval = interval; this.stations = stations.stream().sorted(Comparator.comparing(Station::getDistanceFromStart)).map(Station::getId).toList(); this.currentPosition = new Detection(-1, 0, -100); diff --git a/src/main/java/telraam/logic/positioner/nostradamus/Nostradamus.java b/src/main/java/telraam/logic/positioner/nostradamus/Nostradamus.java index 8f5004d..f2a2669 100644 --- a/src/main/java/telraam/logic/positioner/nostradamus/Nostradamus.java +++ b/src/main/java/telraam/logic/positioner/nostradamus/Nostradamus.java @@ -1,11 +1,13 @@ package telraam.logic.positioner.nostradamus; import org.jdbi.v3.core.Jdbi; -import telraam.database.daos.BatonDAO; import telraam.database.daos.BatonSwitchoverDAO; import telraam.database.daos.StationDAO; import telraam.database.daos.TeamDAO; -import telraam.database.models.*; +import telraam.database.models.BatonSwitchover; +import telraam.database.models.Detection; +import telraam.database.models.Station; +import telraam.database.models.Team; import telraam.logic.positioner.PositionSender; import telraam.logic.positioner.Positioner; diff --git a/src/main/java/telraam/logic/positioner/nostradamus/StationData.java b/src/main/java/telraam/logic/positioner/nostradamus/StationData.java index e65101d..c3b3537 100644 --- a/src/main/java/telraam/logic/positioner/nostradamus/StationData.java +++ b/src/main/java/telraam/logic/positioner/nostradamus/StationData.java @@ -2,24 +2,36 @@ import telraam.database.models.Station; +import java.util.ArrayList; import java.util.List; public record StationData( Station station, // The station Station nextStation, // The next station - List averageTimes, // List containing the times (in ms) that was needed to run from this station to the next one. + List times, // List containing the times (in ms) that was needed to run from this station to the next one. int index, // Index of this station when sorting a station list by distanceFromStart - double currentProgress, // The progress value of this station - double nextProgress // The progress value of the next station + float currentProgress, // The progress value of this station + float nextProgress // The progress value of the next station ) { + public StationData() { + this( + new Station(-10), + new Station(-9), + new ArrayList<>(0), + -10, + 0F, + 0F + ); + } + public StationData(List stations, int index, int averageAmount, int totalDistance) { this( stations.get(index), stations.get((index + 1) % stations.size()), new CircularQueue<>(averageAmount), index, - stations.get(index).getDistanceFromStart() / totalDistance, - stations.get((index + 1) % stations.size()).getDistanceFromStart() / totalDistance + (float) (stations.get(index).getDistanceFromStart() / totalDistance), + (float) (stations.get((index + 1) % stations.size()).getDistanceFromStart() / totalDistance) ); } } diff --git a/src/main/java/telraam/logic/positioner/nostradamus/TeamData.java b/src/main/java/telraam/logic/positioner/nostradamus/TeamData.java index c173ea8..df2e844 100644 --- a/src/main/java/telraam/logic/positioner/nostradamus/TeamData.java +++ b/src/main/java/telraam/logic/positioner/nostradamus/TeamData.java @@ -9,7 +9,7 @@ import java.util.stream.Collectors; public class TeamData { - private final PositionList detections; // List with all relevant detections + private final DetectionList detections; // List with all relevant detections private final Map stations; // Station list private long previousStationArrival; // Arrival time of previous station. Used to calculate the average times private StationData currentStation; // Current station location @@ -17,7 +17,7 @@ public class TeamData { private final int totalDistance; // Total distance of the track @Getter private final Position position; // Data to send to the websocket - private final double maxDeviance; // Maximum deviance the animation can have from the reality + private final float maxDeviance; // Maximum deviance the animation can have from the reality public TeamData(int teamId, int interval, List stations, int averageAmount, double sprintingSpeed, int finishOffset) { @@ -33,18 +33,18 @@ public TeamData(int teamId, int interval, List stations, int averageAmo ) )); // Pre-populate with some data - this.stations.forEach((stationId, stationData) -> stationData.averageTimes().add( + this.stations.forEach((stationId, stationData) -> stationData.times().add( (long) (((stationData.nextStation().getDistanceFromStart() - stationData.station().getDistanceFromStart() + totalDistance) % totalDistance) / sprintingSpeed) )); - this.detections = new PositionList(interval, stations); + this.detections = new DetectionList(interval, stations); this.previousStationArrival = System.currentTimeMillis(); - this.currentStation = new StationData(new Station(-10), new Station(-9), new CircularQueue<>(0), -10, -10, -10); // Will never trigger `isNextStation` for the first station + this.currentStation = new StationData(); // Will never trigger `isNextStation` for the first station this.position = new Position(teamId); - this.maxDeviance = (double) 1 / stations.size(); + this.maxDeviance = (float) 1 / stations.size(); } // Add a new detection - // Returns true if the team is at a next station + // Returns true if the team is at a new station public boolean addDetection(Detection e) { boolean newStation = detections.add(e); @@ -55,7 +55,7 @@ public boolean addDetection(Detection e) { long now = System.currentTimeMillis(); if (isNextStation()) { // Only add the time if it goes forward by exactly one station - previousStation.averageTimes().add(now - previousStationArrival); + previousStation.times().add(now - previousStationArrival); } previousStationArrival = now; @@ -67,7 +67,7 @@ public boolean addDetection(Detection e) { private boolean isForwardStation(int oldStation, int newStation) { int stationDiff = (newStation - oldStation + stations.size()) % stations.size(); - return stationDiff > 0 && stationDiff < 3; + return stationDiff < 3; } private boolean isNextStation() { @@ -83,16 +83,17 @@ public void updatePosition() { double theoreticalProgress = (position.getProgress() + (position.getSpeed() * milliSecondsSince)) % 1; // Arrive at next station at timestamp y and progress z - long nextStationArrival = currentTime + getMedian(); + double median = getMedian(currentStation.times()); + double nextStationArrival = currentTime + median; double goalProgress = currentStation.nextProgress(); double speed, progress; // Determine whether to speed up / slow down the animation or teleport it double difference = (currentStation.currentProgress() - theoreticalProgress + 1) % 1; - if ((difference >= maxDeviance && difference <= 1 - maxDeviance) || currentStation.averageTimes().size() < 3) { + if ((difference >= maxDeviance && difference <= 1 - maxDeviance)) { // Animation was too far behind or ahead so teleport progress = currentStation.currentProgress(); - speed = ((currentStation.nextProgress() - progress + 1) % 1) / getMedian(); + speed = ((currentStation.nextProgress() - progress + 1) % 1) / median; } else { // Animation is close enough, adjust so that we're synced at the next station progress = theoreticalProgress; @@ -103,13 +104,11 @@ public void updatePosition() { } // Get the medium of the average times - private long getMedian() { - List sortedList = new ArrayList<>(currentStation.averageTimes()); + private long getMedian(List times) { + List sortedList = new ArrayList<>(times); Collections.sort(sortedList); int size = sortedList.size(); return size % 2 == 0 ? (sortedList.get(size / 2 - 1) + sortedList.get(size / 2)) / 2 : (sortedList.get(size / 2)); } } - -// TODO: Possible problem: Might arrive to early at station -> Move backwards stations by 5 meter