Skip to content

Commit

Permalink
fix: Fix war info (dps, estimated time) not being accurate (#2471)
Browse files Browse the repository at this point in the history
  • Loading branch information
kristofbolyai committed May 7, 2024
1 parent 9267c1e commit 79805be
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public void onWarEnd(GuildWarEvent.Ended event) {
.append(Component.literal("⚔ DPS: ")
.withStyle(ChatFormatting.GOLD)
.append(Component.literal("%s"
.formatted(StringUtils.integerToShortString(
warBattleInfo.getDps(warBattleInfo.getTotalLengthSeconds()))))
.formatted(
StringUtils.integerToShortString(warBattleInfo.getDps(Long.MAX_VALUE))))
.withStyle(ChatFormatting.WHITE)))
.append(Component.literal("\n"));

Expand Down
4 changes: 2 additions & 2 deletions common/src/main/java/com/wynntils/functions/WarFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ public Long getValue(FunctionArguments arguments) {

@Override
public FunctionArguments.Builder getArgumentsBuilder() {
return new FunctionArguments.RequiredArgumentBuilder(
List.of(new FunctionArguments.Argument<>("seconds", Long.class, null)));
return new FunctionArguments.OptionalArgumentBuilder(
List.of(new FunctionArguments.Argument<>("seconds", Long.class, Long.MAX_VALUE)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.wynntils.utils.type.RangedValue;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class WarBattleInfo {
private final String territory;
Expand Down Expand Up @@ -37,22 +38,40 @@ public WarTowerState getCurrentState() {
}

public long getTotalLengthSeconds() {
return (states.get(states.size() - 1).timestamp() - states.get(0).timestamp()) / 1000L;
return (long) Math.ceil(
(states.get(states.size() - 1).timestamp() - states.get(0).timestamp()) / 1000d);
}

public long getDps(long seconds) {
// Get the dps over the entire war
if (seconds == Long.MAX_VALUE) {
WarTowerState initialState = getInitialState();
WarTowerState currentState = getCurrentState();

return (long) Math.floor((initialState.health() - currentState.health())
/ ((currentState.timestamp() - initialState.timestamp()) / 1000d));
}

// Get the dps over the last x seconds
long now = states.get(states.size() - 1).timestamp();
long start = now - seconds * 1000L;
long now = System.currentTimeMillis();
long start = now - TimeUnit.SECONDS.toMillis(seconds);

WarTowerState firstRelevantState = null;
WarTowerState lastRelevantState = null;

for (WarTowerState state : states) {
if (state.timestamp() >= start) {
if (firstRelevantState == null) {
firstRelevantState = state;
}

List<WarTowerState> relevantStates =
states.stream().filter(state -> state.timestamp() >= start).toList();
lastRelevantState = state;
}
}

return relevantStates.size() < 2 || seconds == 0
return firstRelevantState == null || lastRelevantState == null
? 0
: (relevantStates.get(0).health()
- relevantStates.get(relevantStates.size() - 1).health())
/ seconds;
: (long) Math.floor((firstRelevantState.health() - lastRelevantState.health()) / seconds);
}

public long getTowerEffectiveHp() {
Expand All @@ -70,7 +89,7 @@ public RangedValue getTowerDps() {
public long getEstimatedTimeRemaining() {
WarTowerState currentState = getCurrentState();
long effectiveHp = getTowerEffectiveHp();
long dps = getDps(getTotalLengthSeconds());
long dps = getDps(Long.MAX_VALUE);
return dps == 0 ? -1L : effectiveHp / dps;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class TowerStatsOverlay extends TextOverlay {
§f❖ Tower DPS: §b{range_low(tower_dps):F0}§8 - §b{range_high(tower_dps):F0}
§f❖ Team DPS/1s: §c{team_dps(long(1)):F0}
§f❖ Team DPS/5s: §c{team_dps(long(5)):F0}
§f❖ Total Team DPS: §e{team_dps(time_in_war):F0}
§f❖ Total Team DPS: §e{team_dps:F0}
§f❖ Estimated Time: §a{if_str(eq(estimated_time_to_finish_war;-1);"-";concat(str(int(estimated_time_to_finish_war));"s"))}
""";

Expand Down

0 comments on commit 79805be

Please sign in to comment.