Skip to content

Commit 67945be

Browse files
authored
Merge pull request #94 from geracosta/assists-log
Add assists_log with the victim of each assist
2 parents 927adf6 + 792c1a6 commit 67945be

3 files changed

Lines changed: 241 additions & 2 deletions

File tree

‎src/main/java/opendota/CreateParsedDataBlob.java‎

Lines changed: 162 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class PlayerData {
6565
public List<Map<String, Object>> purchase_log = new ArrayList<>();
6666
public List<Map<String, Object>> kills_log = new ArrayList<>();
6767
public List<Map<String, Object>> deaths_log = new ArrayList<>();
68+
public List<Map<String, Object>> assists_log = new ArrayList<>();
6869
public List<Entry> buyback_log = new ArrayList<>();
6970
public List<Map<String, Object>> runes_log = new ArrayList<>();
7071
public List<Entry> connection_log = new ArrayList<>();
@@ -355,7 +356,8 @@ private void handleArrayField(Entry e, PlayerData player) {
355356
} else if (e.interval != null && e.interval) {
356357
List<Integer> targetList = getPlayerIntegerList(player, e.type);
357358
targetList.add(e.value);
358-
} else if (Arrays.asList("purchase_log", "kills_log", "deaths_log", "runes_log", "neutral_tokens_log").contains(e.type)) {
359+
} else if (Arrays.asList("purchase_log", "kills_log", "deaths_log", "assists_log", "runes_log",
360+
"neutral_tokens_log").contains(e.type)) {
359361
Map<String, Object> obj = new HashMap<>();
360362
obj.put("time", e.time);
361363
obj.put("key", e.key);
@@ -376,6 +378,10 @@ private void handleArrayField(Entry e, PlayerData player) {
376378
obj.put("smoke", e.smoke);
377379
}
378380

381+
if ("assists_log".equals(e.type) && e.ambiguous != null) {
382+
obj.put("ambiguous", e.ambiguous);
383+
}
384+
379385
if ("deaths_log".equals(e.type)) {
380386
obj.put("gold_lost", e.gold_lost);
381387
obj.put("gold_fed", e.gold_fed);
@@ -393,6 +399,8 @@ private void handleArrayField(Entry e, PlayerData player) {
393399
player.kills_log.add(obj);
394400
} else if ("deaths_log".equals(e.type)) {
395401
player.deaths_log.add(obj);
402+
} else if ("assists_log".equals(e.type)) {
403+
player.assists_log.add(obj);
396404
} else if ("runes_log".equals(e.type)) {
397405
player.runes_log.add(obj);
398406
} else if ("neutral_tokens_log".equals(e.type)) {
@@ -559,6 +567,15 @@ private String computeIllusionString(String input, Boolean isIllusion) {
559567
private boolean sharedGoldFed = false;
560568
private Integer wkSlot = null;
561569

570+
// assists_log: the parser reports the assist and death counters on the tick
571+
// they move, and the game credits an assist on the same tick it counts the
572+
// death, so every assist lands on a tick with the deaths it can belong to.
573+
// One death there settles it. With two, the player can't have assisted the
574+
// one they killed, and anyone the combat log lists on a death other than its
575+
// killer did get the assist. What is still left open is marked ambiguous.
576+
private List<Entry> assistLog = new ArrayList<>();
577+
private int ambiguousAssists = 0;
578+
562579
private void precomputeReincarnations(List<Entry> entries, Metadata meta) {
563580
wkSlot = meta.hero_to_slot.get(WK_HERO);
564581
Map<Integer, Integer> lastState = new HashMap<>();
@@ -608,6 +625,136 @@ private void precomputeReincarnations(List<Entry> entries, Metadata meta) {
608625
// their length is unknown, so those deaths get no time_dead
609626
}
610627

628+
private static class CountedDeath {
629+
Integer slot;
630+
Integer time;
631+
String victim;
632+
Integer killerSlot;
633+
List<Integer> listed;
634+
}
635+
636+
private void precomputeAssists(List<Entry> entries, Metadata meta) {
637+
Map<Integer, List<Entry>> heroDeathsByTick = new HashMap<>();
638+
Map<Integer, List<Integer>> deathTicks = new HashMap<>();
639+
List<Entry> assistTicks = new ArrayList<>();
640+
for (Entry e : entries) {
641+
if ("assist_tick".equals(e.type)) {
642+
assistTicks.add(e);
643+
} else if ("death_tick".equals(e.type)) {
644+
deathTicks.computeIfAbsent(e.tick, k -> new ArrayList<>()).add(e.slot);
645+
} else if ("DOTA_COMBATLOG_DEATH".equals(e.type) && e.tick != null
646+
&& (e.targetillusion == null || !e.targetillusion)) {
647+
heroDeathsByTick.computeIfAbsent(e.tick, k -> new ArrayList<>()).add(e);
648+
}
649+
}
650+
Map<Integer, List<CountedDeath>> countedByTick = new HashMap<>();
651+
for (Map.Entry<Integer, List<Integer>> tick : deathTicks.entrySet()) {
652+
for (Integer slot : tick.getValue()) {
653+
CountedDeath death = new CountedDeath();
654+
death.slot = slot;
655+
Entry logged = loggedDeath(heroDeathsByTick, tick.getKey(), slot, meta);
656+
if (logged != null) {
657+
death.victim = logged.targetname;
658+
death.time = logged.time;
659+
death.killerSlot = meta.hero_to_slot.get(logged.sourcename);
660+
death.listed = logged.assist_slots;
661+
} else {
662+
// no combat log entry to take the name from: any name the
663+
// match maps to that slot
664+
for (Map.Entry<String, Integer> hero : meta.hero_to_slot.entrySet()) {
665+
if (slot.equals(hero.getValue())) {
666+
death.victim = hero.getKey();
667+
break;
668+
}
669+
}
670+
}
671+
countedByTick.computeIfAbsent(tick.getKey(), k -> new ArrayList<>()).add(death);
672+
}
673+
}
674+
for (Entry increment : assistTicks) {
675+
List<CountedDeath> deaths = countedByTick.get(increment.tick);
676+
Integer playerSlot = meta.slot_to_player_slot.get(increment.slot);
677+
if (deaths == null || playerSlot == null) {
678+
continue;
679+
}
680+
int n = increment.value;
681+
List<CountedDeath> enemies = new ArrayList<>();
682+
for (CountedDeath death : deaths) {
683+
Integer victimSlot = meta.slot_to_player_slot.get(death.slot);
684+
if (victimSlot != null && isRadiant(victimSlot) != isRadiant(playerSlot)) {
685+
enemies.add(death);
686+
}
687+
}
688+
// the game also credits some assists on a death on the player's own
689+
// side, on the same tick; those count on the scoreboard too
690+
List<CountedDeath> candidates = enemies.isEmpty() ? deaths : enemies;
691+
if (candidates.size() > n) {
692+
List<CountedDeath> notKilled = new ArrayList<>();
693+
for (CountedDeath death : candidates) {
694+
if (!increment.slot.equals(death.killerSlot)) {
695+
notKilled.add(death);
696+
}
697+
}
698+
if (notKilled.size() >= n) {
699+
candidates = notKilled;
700+
}
701+
}
702+
if (candidates.size() > n) {
703+
List<CountedDeath> listed = new ArrayList<>();
704+
for (CountedDeath death : candidates) {
705+
if (death.listed != null && death.listed.contains(increment.slot)) {
706+
listed.add(death);
707+
}
708+
}
709+
if (listed.size() >= n) {
710+
candidates = listed;
711+
}
712+
}
713+
boolean tied = candidates.size() > n;
714+
if (tied) {
715+
ambiguousAssists += n;
716+
}
717+
for (int i = 0; i < n; i++) {
718+
CountedDeath death = candidates.get(Math.min(i, candidates.size() - 1));
719+
Entry assist = new Entry();
720+
assist.time = death.time != null ? death.time : increment.time;
721+
assist.slot = increment.slot;
722+
assist.key = death.victim;
723+
assist.type = "assists_log";
724+
if (tied) {
725+
assist.ambiguous = true;
726+
}
727+
assistLog.add(assist);
728+
}
729+
}
730+
}
731+
732+
// the combat log entry for a counted death, one tick before the counter moves.
733+
// A Meepo death is logged once per Meepo, with the clones dying to Meepo
734+
// himself, so the entry with another attacker is the one that counts
735+
private Entry loggedDeath(Map<Integer, List<Entry>> heroDeathsByTick, Integer tick, Integer slot,
736+
Metadata meta) {
737+
for (int offset : new int[] { 1, 0, 2 }) {
738+
List<Entry> logged = heroDeathsByTick.get(tick - offset);
739+
if (logged == null) {
740+
continue;
741+
}
742+
Entry match = null;
743+
for (Entry e : logged) {
744+
if (slot.equals(meta.hero_to_slot.get(e.targetname))) {
745+
if (match == null || (e.targetname.equals(match.sourcename)
746+
&& !e.targetname.equals(e.sourcename))) {
747+
match = e;
748+
}
749+
}
750+
}
751+
if (match != null) {
752+
return match;
753+
}
754+
}
755+
return null;
756+
}
757+
611758
private boolean consumeReincarnationDeath(Integer slot, Integer time) {
612759
if (slot == null || time == null) {
613760
return false;
@@ -781,6 +928,7 @@ private List<Entry> processExpand(List<Entry> entries, Metadata meta) {
781928
precomputeReincarnations(entries, meta);
782929
precomputeMinuteSeries(entries, meta);
783930
precomputeSmokeWindows(entries, meta);
931+
precomputeAssists(entries, meta);
784932

785933
for (Entry e : entries) {
786934
String type = e.type;
@@ -896,6 +1044,17 @@ private List<Entry> processExpand(List<Entry> entries, Metadata meta) {
8961044
}
8971045
}
8981046

1047+
// emitted after the loop because each assist is paired with its death by
1048+
// the tick both counters moved, which the pre-scan resolves in full
1049+
for (Entry assist : assistLog) {
1050+
expand(assist, output, meta);
1051+
}
1052+
if (ambiguousAssists > 0) {
1053+
System.err.format("assists_log: %s of %s assists shared a tick with"
1054+
+ " more deaths than could be told apart\n",
1055+
ambiguousAssists, assistLog.size());
1056+
}
1057+
8991058
return output;
9001059
}
9011060

@@ -1756,7 +1915,8 @@ private boolean isArrayField(String type) {
17561915
return Arrays.asList("times", "gold_t", "lh_t", "dn_t", "xp_t", "networth_t",
17571916
"camps_stacked_t", "tower_damage_t", "hero_damage_t", "hero_healing_t", "obs_log",
17581917
"sen_log", "obs_left_log", "sen_left_log", "purchase_log", "kills_log",
1759-
"deaths_log", "buyback_log", "runes_log", "connection_log", "neutral_tokens_log",
1918+
"deaths_log", "assists_log", "buyback_log", "runes_log", "connection_log",
1919+
"neutral_tokens_log",
17601920
"neutral_item_history").contains(type);
17611921
}
17621922

‎src/main/java/opendota/Entry.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ public class Entry implements Cloneable {
9090
public String event;
9191
public Integer killer;
9292
public Boolean smoke;
93+
// assists_log fields
94+
public Boolean ambiguous;
95+
public Integer tick;
96+
public List<Integer> assist_slots;
9397
// deaths_log fields
9498
public Integer gold_lost;
9599
public Integer gold_fed;

‎src/main/java/opendota/Parse.java‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,74 @@ public void onFileInfo(Context ctx, CDemoFileInfo message) {
364364
flushLogBuffer();
365365
}
366366

367+
// assists_log reads the assist and death counters on every tick, not once a
368+
// second with the intervals: an assist is credited on the exact tick the death
369+
// it belongs to is counted, which is what lets the two be paired
370+
private Integer[] lastAssists = new Integer[numPlayers];
371+
private Integer[] lastDeaths = new Integer[numPlayers];
372+
373+
// read on every tick, so the field paths are resolved once per player
374+
private FieldPath[] assistsPaths;
375+
private FieldPath[] deathsPaths;
376+
377+
private void trackAssistCounters(Context ctx, Entity pr) {
378+
if (assistsPaths == null) {
379+
assistsPaths = new FieldPath[numPlayers];
380+
deathsPaths = new FieldPath[numPlayers];
381+
for (int i = 0; i < numPlayers; i++) {
382+
assistsPaths[i] = fieldPath(pr, indexedName("m_vecPlayerTeamData.%i.m_iAssists", validIndices[i]));
383+
deathsPaths[i] = fieldPath(pr, indexedName("m_vecPlayerTeamData.%i.m_iDeaths", validIndices[i]));
384+
}
385+
}
386+
for (int i = 0; i < numPlayers; i++) {
387+
try {
388+
Integer assists = assistsPaths[i] == null ? null : pr.getPropertyForFieldPath(assistsPaths[i]);
389+
Integer deaths = deathsPaths[i] == null ? null : pr.getPropertyForFieldPath(deathsPaths[i]);
390+
emitCounterIncrease("assist_tick", i, assists, lastAssists, ctx.getTick());
391+
emitCounterIncrease("death_tick", i, deaths, lastDeaths, ctx.getTick());
392+
} catch (Exception e) {
393+
// a player index the resource doesn't carry
394+
}
395+
}
396+
}
397+
398+
private void emitCounterIncrease(String type, int slot, Integer value, Integer[] last, int tick) {
399+
if (value == null) {
400+
return;
401+
}
402+
Integer prev = last[slot];
403+
last[slot] = value;
404+
if (prev != null && value > prev) {
405+
Entry entry = new Entry(time);
406+
entry.type = type;
407+
entry.slot = slot;
408+
entry.value = value - prev;
409+
entry.tick = tick;
410+
output(entry);
411+
}
412+
}
413+
414+
// the players the game lists on a hero death, as slots. Not the full assist
415+
// credit (it can leave assisters out), but everyone on it other than the
416+
// killer did get the assist, which settles two deaths on the same tick
417+
private List<Integer> assistSlots(CombatLogEntry cle) {
418+
try {
419+
List<Integer> slots = new ArrayList<>();
420+
for (Object id : cle.getAssistPlayers()) {
421+
int playerId = ((Number) id).intValue();
422+
for (int i = 0; i < numPlayers; i++) {
423+
if (validIndices[i] == playerId) {
424+
slots.add(i);
425+
}
426+
}
427+
}
428+
return slots.isEmpty() ? null : slots;
429+
} catch (Exception e) {
430+
// older replays don't carry the list
431+
return null;
432+
}
433+
}
434+
367435
@OnCombatLogEntry
368436
public void onCombatLogEntry(Context ctx, CombatLogEntry cle) {
369437
try {
@@ -378,6 +446,10 @@ public void onCombatLogEntry(Context ctx, CombatLogEntry cle) {
378446
combatLogEntry.targetsourcename = cle.getTargetSourceName();
379447
combatLogEntry.inflictor = cle.getInflictorName();
380448
combatLogEntry.attackerhero = cle.isAttackerHero();
449+
if (cle.getType() == DOTA_COMBATLOG_TYPES.DOTA_COMBATLOG_DEATH && cle.isTargetHero()) {
450+
combatLogEntry.tick = ctx.getTick();
451+
combatLogEntry.assist_slots = assistSlots(cle);
452+
}
381453
combatLogEntry.targethero = cle.isTargetHero();
382454
combatLogEntry.attackerillusion = cle.isAttackerIllusion();
383455
combatLogEntry.targetillusion = cle.isTargetIllusion();
@@ -495,6 +567,9 @@ public void onTickStart(Context ctx, boolean synthetic) {
495567
Entity pr = playerResource;
496568
Entity dData = dataDire;
497569
Entity rData = dataRadiant;
570+
if (pr != null && init) {
571+
trackAssistCounters(ctx, pr);
572+
}
498573

499574
// Create draftStage variable
500575
Integer draftStage = getEntityProperty(grp, "m_pGameRules.m_nGameState", null);

0 commit comments

Comments
 (0)