Skip to content

Commit

Permalink
chore: sync up faster
Browse files Browse the repository at this point in the history
  • Loading branch information
Topvennie committed Apr 20, 2024
1 parent a968e8b commit 59b96bf
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 28 deletions.
3 changes: 0 additions & 3 deletions src/main/java/telraam/api/TeamResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ public Team update(Team team, Optional<Integer> 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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
import java.util.Comparator;
import java.util.List;

public class PositionList extends ArrayList<Detection> {
public class DetectionList extends ArrayList<Detection> {

private final int interval;
private final List<Integer> stations;
@Getter
private Detection currentPosition;
private Timestamp newestDetection;

public PositionList(int interval, List<Station> stations) {
public DetectionList(int interval, List<Station> stations) {
this.interval = interval;
this.stations = stations.stream().sorted(Comparator.comparing(Station::getDistanceFromStart)).map(Station::getId).toList();
this.currentPosition = new Detection(-1, 0, -100);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long> averageTimes, // List containing the times (in ms) that was needed to run from this station to the next one.
List<Long> 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<Station> 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)
);
}
}
31 changes: 15 additions & 16 deletions src/main/java/telraam/logic/positioner/nostradamus/TeamData.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
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<Integer, StationData> stations; // Station list
private long previousStationArrival; // Arrival time of previous station. Used to calculate the average times
private StationData currentStation; // Current station location
private StationData previousStation; // Previous station location
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<Station> stations, int averageAmount, double sprintingSpeed, int finishOffset) {
Expand All @@ -33,18 +33,18 @@ public TeamData(int teamId, int interval, List<Station> 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);

Expand All @@ -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;

Expand All @@ -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() {
Expand All @@ -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;
Expand All @@ -103,13 +104,11 @@ public void updatePosition() {
}

// Get the medium of the average times
private long getMedian() {
List<Long> sortedList = new ArrayList<>(currentStation.averageTimes());
private long getMedian(List<Long> times) {
List<Long> 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

0 comments on commit 59b96bf

Please sign in to comment.