-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add damage history tracking and assist logic
Signed-off-by: Pugzy <[email protected]>
- Loading branch information
Showing
11 changed files
with
360 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
core/src/main/java/tc/oc/pgm/damagehistory/DamageEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package tc.oc.pgm.damagehistory; | ||
|
||
import org.jetbrains.annotations.Nullable; | ||
import tc.oc.pgm.api.player.ParticipantState; | ||
|
||
public class DamageEntry { | ||
|
||
@Nullable private ParticipantState damager; | ||
private double damage; | ||
|
||
public DamageEntry(@Nullable ParticipantState damager, double damage) { | ||
this.damager = damager; | ||
this.damage = damage; | ||
} | ||
|
||
@Nullable | ||
public ParticipantState getDamager() { | ||
return damager; | ||
} | ||
|
||
public double getDamage() { | ||
return damage; | ||
} | ||
|
||
public void addDamage(@Nullable ParticipantState damager, double damage) { | ||
this.damager = damager; | ||
this.damage += damage; | ||
} | ||
|
||
public void removeDamage(double damage) { | ||
this.damage -= damage; | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
core/src/main/java/tc/oc/pgm/damagehistory/DamageHistory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package tc.oc.pgm.damagehistory; | ||
|
||
import java.util.Deque; | ||
import java.util.HashMap; | ||
import java.util.LinkedList; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import org.jetbrains.annotations.Nullable; | ||
import tc.oc.pgm.api.player.MatchPlayer; | ||
import tc.oc.pgm.api.player.ParticipantState; | ||
|
||
public class DamageHistory { | ||
|
||
public static final double EPSILON = 0.00001; | ||
|
||
private final Map<UUID, Deque<DamageEntry>> allPlayerDamage = new HashMap<>(); | ||
|
||
public DamageHistory() {} | ||
|
||
public Deque<DamageEntry> getPlayerHistory(UUID uuid) { | ||
return allPlayerDamage.computeIfAbsent(uuid, item -> new LinkedList<>()); | ||
} | ||
|
||
public void addDamage( | ||
MatchPlayer target, double damageAmount, @Nullable ParticipantState attacker) { | ||
Deque<DamageEntry> playerHistory = getPlayerHistory(target.getId()); | ||
|
||
// Update existing if same player causing damage | ||
if (!playerHistory.isEmpty()) { | ||
DamageEntry last = playerHistory.getLast(); | ||
if (shouldMergeParticipants(last.getDamager(), attacker)) { | ||
last.addDamage(attacker, damageAmount); | ||
return; | ||
} | ||
} | ||
|
||
playerHistory.addLast(new DamageEntry(attacker, damageAmount)); | ||
} | ||
|
||
public void removeDamage(MatchPlayer target, double damageAmount) { | ||
Deque<DamageEntry> playerHistory = getPlayerHistory(target.getId()); | ||
if (playerHistory.isEmpty()) return; | ||
|
||
double subtractAmount = damageAmount; | ||
while (!playerHistory.isEmpty() && subtractAmount > 0) { | ||
DamageEntry first = playerHistory.getFirst(); | ||
if (first.getDamage() < subtractAmount + EPSILON) { | ||
subtractAmount -= first.getDamage(); | ||
playerHistory.removeFirst(); | ||
} else { | ||
first.removeDamage(subtractAmount); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
public boolean shouldMergeParticipants(ParticipantState firstItem, ParticipantState secondItem) { | ||
if (firstItem == null || secondItem == null) return firstItem == secondItem; | ||
|
||
// Only allow if they share the same UUID and party | ||
if (!firstItem.getId().equals(secondItem.getId())) return false; | ||
return (firstItem.getParty().equals(secondItem.getParty())); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
core/src/main/java/tc/oc/pgm/damagehistory/DamageHistoryKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package tc.oc.pgm.damagehistory; | ||
|
||
import static tc.oc.pgm.util.Assert.assertNotNull; | ||
|
||
import java.util.Objects; | ||
import java.util.UUID; | ||
import tc.oc.pgm.api.party.Competitor; | ||
import tc.oc.pgm.api.player.ParticipantState; | ||
|
||
public class DamageHistoryKey { | ||
|
||
private final ParticipantState state; | ||
|
||
public DamageHistoryKey(ParticipantState state) { | ||
this.state = state; | ||
} | ||
|
||
public static DamageHistoryKey from(DamageEntry damageEntry) { | ||
ParticipantState damager = damageEntry.getDamager(); | ||
assertNotNull(damager); | ||
return new DamageHistoryKey(damager); | ||
} | ||
|
||
public ParticipantState getState() { | ||
return state; | ||
} | ||
|
||
public UUID getPlayer() { | ||
return state.getId(); | ||
} | ||
|
||
public Competitor getParty() { | ||
return state.getParty(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
DamageHistoryKey that = (DamageHistoryKey) o; | ||
return Objects.equals(getPlayer(), that.getPlayer()) | ||
&& Objects.equals(getParty(), that.getParty()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(getPlayer(), getParty()); | ||
} | ||
} |
Oops, something went wrong.