Skip to content

Commit

Permalink
Improved: Tracking data
Browse files Browse the repository at this point in the history
  • Loading branch information
Bram1903 committed Nov 5, 2024
1 parent 17773fe commit 39dcd68
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.deathmotion.totemguard.checks.TotemEventListener;
import com.deathmotion.totemguard.checks.impl.totem.processor.TotemProcessor;
import com.deathmotion.totemguard.models.TotemPlayer;
import com.deathmotion.totemguard.util.MathUtil;
import com.deathmotion.totemguard.util.MessageService;
import com.deathmotion.totemguard.util.datastructure.Pair;
import io.github.retrooper.packetevents.util.folia.FoliaScheduler;
Expand All @@ -36,6 +37,7 @@

import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -72,11 +74,20 @@ private Component getPrefix() {

@Override
public void onTotemEvent(Player player, TotemPlayer totemPlayer) {
long latestInterval = totemPlayer.totemData().getLatestIntervals(1).get(0);
List<Long> intervals = totemPlayer.totemData().getLatestIntervals(5);
if (intervals.isEmpty()) return;

long latestInterval = intervals.get(0);
double averageInterval = MathUtil.trim(2, MathUtil.getMean(intervals));

double stDev = 0;
if (intervals.size() > 1) {
stDev = MathUtil.trim(2, MathUtil.getStandardDeviation(intervals));
}

TargetTracker tracker = targetTrackers.get(player);
if (tracker != null) {
tracker.setLatestData(new TotemData(latestInterval, Instant.now()));
tracker.setLatestData(new TotemData(latestInterval, averageInterval, stDev, Instant.now()));
sendActionBarToViewers(tracker);
}
}
Expand Down Expand Up @@ -212,6 +223,12 @@ private Component createTrackingMessage(Player target, TotemData data) {
.append(Component.text("Speed: ", colorScheme.getY(), TextDecoration.BOLD))
.append(Component.text(data.latestInterval() + "ms", colorScheme.getX()))
.append(Component.text(" | ", NamedTextColor.DARK_GRAY))
.append(Component.text("Average: ", colorScheme.getY(), TextDecoration.BOLD))
.append(Component.text(data.averageInterval + "ms", colorScheme.getX()))
.append(Component.text(" | ", NamedTextColor.DARK_GRAY))
.append(Component.text("Std Dev: ", colorScheme.getY(), TextDecoration.BOLD))
.append(Component.text(data.stDev, colorScheme.getX()))
.append(Component.text(" | ", NamedTextColor.DARK_GRAY))
.append(Component.text("Updated: ", colorScheme.getY(), TextDecoration.BOLD))
.append(Component.text(timeAgo, colorScheme.getX()))
.build();
Expand All @@ -229,7 +246,7 @@ private String formatTimeAgo(Instant lastUpdated) {
}
}

private record TotemData(long latestInterval, Instant lastUpdated) {
private record TotemData(long latestInterval, double averageInterval, double stDev, Instant lastUpdated) {
}

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import lombok.Getter;
import lombok.Setter;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.stream.Collectors;

@Getter
public class TotemData {
Expand All @@ -45,9 +47,13 @@ public void addInterval(long interval) {
public List<Long> getLatestIntervals(int amount) {
return intervals.stream()
.skip(Math.max(0, intervals.size() - amount))
.toList();
.collect(Collectors.collectingAndThen(Collectors.toList(), list -> {
Collections.reverse(list);
return list;
}));
}


public void clear() {
latestStandardDeviation = 0;
intervals.clear();
Expand Down

0 comments on commit 39dcd68

Please sign in to comment.