Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions crates/openlogs-analysis/tests/combat_pull.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,24 @@ fn neutral_mob_that_fights_us_counts_as_damage_done() {
let report = damage_done(&fight, &ReportFilter::whole(Attribution::Augmented));
assert_eq!(report.total, 30 + 35);
}

#[test]
fn a_boss_pull_counts_its_first_hit_before_encounter_start() {
let hit = |ts: &str| {
format!(
"{ts} SPELL_DAMAGE,Player-4101-8E8C0182,\"Faelan-Emberfall-EU\",0x512,0x80000000,Creature-0-3892-2649-1387-207946-0000468002,\"Captain Dailcry\",0x10a48,0x80000000,44425,\"Arcane Barrage\",0x40,Creature-0-3892-2649-1387-207946-0000468002,0000000000000000,1000,50000000,0,0,0,0,0,0,1,0,0,0,1590.00,-3.00,2308,1.0000,80,100,100,-1,64,0,0,0,nil,nil,nil,ST"
)
};
let log = [
hit("10/8/2026 20:00:02.9900"),
"10/8/2026 20:00:03.0000 ENCOUNTER_START,2847,\"Captain Dailcry\",8,5,2649".to_string(),
hit("10/8/2026 20:00:10.0000"),
"10/8/2026 20:01:03.0000 ENCOUNTER_END,2847,\"Captain Dailcry\",8,5,1,60000".to_string(),
]
.join("\n");
let index = index_bytes(log.as_bytes());
let fight = load_fight(log.as_bytes(), &index.fights[0]);

let report = damage_done(&fight, &ReportFilter::whole(Attribution::Augmented));
assert_eq!(report.total, 200);
}
95 changes: 76 additions & 19 deletions crates/openlogs-index/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::combat::{DeadEnemies, Exchange, OpenCombat, counted_damage, exchange}
use crate::markers::MarkerState;
use crate::model::{
BOSS_TAIL_MICROS, ChallengeMode, END_GRACE_LINES, END_GRACE_MICROS, FightSummary, GameFlavor,
LogIndex, MYTHIC_KEYSTONE_DIFFICULTY, MapBounds, Outcome, PRELUDE_MICROS,
LEAD_MAX_MICROS, LogIndex, MYTHIC_KEYSTONE_DIFFICULTY, MapBounds, Outcome, PRELUDE_MICROS,
RESUME_MAX_GAP_MICROS, RosterEntry, SUPPORTED_LOG_VERSION, WorldMarker,
};

Expand Down Expand Up @@ -142,7 +142,7 @@ impl IndexBuilder {
{
fight.resume = Some(fight.snapshot(&self.index.unhandled_in_fights));
}
self.close_before(span, line);
let lead = self.close_before(span, line);
let starts_fight = matches!(line.event, Event::EncounterStart(_));
let starts_run = matches!(line.event, Event::ChallengeModeStart(_)) && !resumes_run;
if !starts_fight && let Some(fight) = &mut self.open {
Expand All @@ -161,7 +161,7 @@ impl IndexBuilder {
build,
project_id,
} => self.on_version(*version, *advanced, build, *project_id),
Event::EncounterStart(start) => self.open_fight(span, line.timestamp, start),
Event::EncounterStart(start) => self.open_fight(span, line.timestamp, start, lead),
Event::EncounterEnd(end) => self.end_fight(end, line.timestamp),
Event::ChallengeModeStart(start) if !resumes_run => {
self.open_run(span, line.timestamp, start)
Expand Down Expand Up @@ -302,12 +302,16 @@ impl IndexBuilder {

/// Ends, at the previous line, what this line leaves behind: leaving the instance or
/// another key ends the open pull and run; a run's END is held while a pull is open. A
/// boss pull, a key's start or end, or leaving the instance ends a combat pull.
fn close_before(&mut self, span: LineSpan, line: &LogLine<'_>) {
/// boss pull, a key's start or end, or leaving the instance ends a combat pull; one that
/// began right before a boss pull is returned as its lead.
fn close_before(&mut self, span: LineSpan, line: &LogLine<'_>) -> Option<Lead> {
let mut lead = None;
let ends_combat = match &line.event {
Event::EncounterStart(_)
| Event::ChallengeModeStart(_)
| Event::ChallengeModeEnd(_) => true,
Event::EncounterStart(_) => {
lead = self.take_lead(line.timestamp);
false
}
Event::ChallengeModeStart(_) | Event::ChallengeModeEnd(_) => true,
Event::ZoneChange { zone_id, .. } => self
.combat
.as_ref()
Expand Down Expand Up @@ -343,6 +347,7 @@ impl IndexBuilder {
}
_ => {}
}
lead
}

/// The open pull's `ENCOUNTER_END` may follow its run's END in the same moment.
Expand Down Expand Up @@ -487,9 +492,18 @@ impl IndexBuilder {
})
}

fn open_fight(&mut self, span: LineSpan, timestamp: Timestamp, start: &EncounterStart<'_>) {
fn open_fight(
&mut self,
span: LineSpan,
timestamp: Timestamp,
start: &EncounterStart<'_>,
lead: Option<Lead>,
) {
self.close_open(Outcome::Incomplete);
let index = self.next_index();
let index = match &lead {
Some(lead) => lead.pull.index,
None => self.next_index(),
};
let prelude_offset = self.prelude_offset(span, self.last_fight_end);
self.open = Some(OpenFight {
summary: FightSummary {
Expand Down Expand Up @@ -519,6 +533,8 @@ impl IndexBuilder {
hostile: HashMap::new(),
player_names: HashMap::new(),
resume: None,
lead,
encounter_end: None,
});
}

Expand All @@ -527,8 +543,7 @@ impl IndexBuilder {
if fight.summary.encounter_id != end.encounter_id {
return;
}
fight.summary.duration_ms =
u64::try_from(timestamp.millis_since(fight.summary.start)).unwrap_or(0);
fight.encounter_end = Some(timestamp);
self.close_open(if end.success {
Outcome::Kill
} else {
Expand Down Expand Up @@ -609,7 +624,16 @@ impl IndexBuilder {
{
self.restore(resume);
}
if let Some(fight) = self.open.take() {
if let Some(mut fight) = self.open.take() {
if let Some(lead) = fight.lead.take() {
if lead.fought_only(&fight.hostile) {
fight.summary.start = fight.summary.start.min(lead.pull.start);
fight.summary.start_offset = lead.pull.start_offset;
} else {
fight.summary.index = self.next_index();
self.list_combat(lead.pull);
}
}
self.last_fight_end = fight.summary.end_offset;
self.boss_tail = Some(BossTail {
hostile: fight.hostile.keys().cloned().collect(),
Expand Down Expand Up @@ -714,10 +738,29 @@ impl IndexBuilder {
fn close_combat(&mut self, outcome: Outcome) {
if let Some(combat) = self.combat.take() {
let summary = combat.finish(outcome, &self.last_specs);
self.last_combat_end = summary.end_offset;
self.index.fights.push(summary);
self.list_combat(summary);
}
}

fn list_combat(&mut self, summary: FightSummary) {
self.last_combat_end = summary.end_offset;
self.index.fights.push(summary);
}

/// Ends the open combat pull at an `ENCOUNTER_START`; one that began at most
/// [`LEAD_MAX_MICROS`] before is returned instead of listed.
fn take_lead(&mut self, timestamp: Timestamp) -> Option<Lead> {
let Some(combat) = self
.combat
.take_if(|combat| timestamp.0 - combat.summary.start.0 <= LEAD_MAX_MICROS)
else {
self.close_combat(Outcome::Ended);
return None;
};
let enemies = combat.enemy_guids().map(str::to_string).collect();
let pull = combat.finish(Outcome::Ended, &self.last_specs);
Some(Lead { pull, enemies })
}
}

/// Moves an open entry's end to this line; out-of-order timestamps never shrink it.
Expand Down Expand Up @@ -751,6 +794,22 @@ struct OpenFight {
/// The pull before its key was started again (reconnect, /reload): its `ENCOUNTER_END`
/// may have been missed offline, so an incomplete pull ends here.
resume: Option<PullSnapshot>,
/// The combat pull that began right before `ENCOUNTER_START` and shares this pull's index.
lead: Option<Lead>,
encounter_end: Option<Timestamp>,
}

/// A combat pull as listed when `ENCOUNTER_START` ended it, and the enemies it engaged.
#[derive(Clone)]
struct Lead {
pull: FightSummary,
enemies: Vec<String>,
}

impl Lead {
fn fought_only(&self, hostile: &HashMap<String, HostileUnit>) -> bool {
self.enemies.iter().all(|guid| hostile.contains_key(guid))
}
}

#[derive(Debug, Clone, Copy, Default)]
Expand Down Expand Up @@ -818,10 +877,8 @@ impl OpenFight {
fn finish(mut self, outcome: Outcome) -> FightSummary {
let summary = &mut self.summary;
summary.outcome = outcome;
if outcome == Outcome::Incomplete {
summary.duration_ms =
u64::try_from(summary.end.millis_since(summary.start)).unwrap_or(0);
}
let end = self.encounter_end.unwrap_or(summary.end);
summary.duration_ms = u64::try_from(end.millis_since(summary.start)).unwrap_or(0);
summary.boss_hp_percent = match outcome {
Outcome::Kill => Some(0.0),
Outcome::Wipe | Outcome::Incomplete | Outcome::Ended => boss_hp_percent(&self.hostile),
Expand Down
4 changes: 4 additions & 0 deletions crates/openlogs-index/src/combat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ impl OpenCombat {
.then_some(Outcome::Wipe)
}

pub(crate) fn enemy_guids(&self) -> impl Iterator<Item = &str> {
self.enemies.keys().map(String::as_str)
}

/// Whether more than [`COMBAT_QUIET_MICROS`] passed since the last exchange.
pub(crate) fn quiet_since(&self, timestamp: Timestamp) -> bool {
timestamp.0 - self.last_exchange.0 > COMBAT_QUIET_MICROS
Expand Down
5 changes: 4 additions & 1 deletion crates/openlogs-index/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use openlogs_parser::Timestamp;
use serde::{Deserialize, Serialize};

/// Bumped whenever the index layout or its rules change; invalidates cached indexes.
pub const INDEX_FORMAT_VERSION: u32 = 14;
pub const INDEX_FORMAT_VERSION: u32 = 15;
/// The combat log format this tool is built and tested against.
pub const SUPPORTED_LOG_VERSION: u32 = 22;
/// Lookback before `ENCOUNTER_START` that belongs to a fight (pre-pots, pre-casts).
Expand All @@ -23,6 +23,9 @@ pub const COMBAT_QUIET_MICROS: i64 = 10_000_000;
/// Time after a boss pull's end in which exchanges with its hostile units that did not die are
/// trailing lines of it (a last hit, a DoT tick) and start no combat pull.
pub const BOSS_TAIL_MICROS: i64 = 1_000_000;
/// Time before `ENCOUNTER_START` in which a combat pull that fought only the boss pull's enemies
/// began with the boss pull's first hit, which the log often writes a few ms before the START.
pub const LEAD_MAX_MICROS: i64 = 1_000_000;
/// Time without a line naming a dead enemy after which it is forgotten, which bounds the dead
/// enemies kept. NPC GUIDs are per spawn, so a forgotten one rarely comes back.
pub const DEAD_ENEMY_MEMORY_MICROS: i64 = 60_000_000;
Expand Down
116 changes: 116 additions & 0 deletions crates/openlogs-index/tests/combat_pulls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,3 +501,119 @@ fn boss_pulls_are_unchanged() {
]
);
}

const DAILCRY_START: &str =
"10/8/2026 20:00:03.0000 ENCOUNTER_START,2847,\"Captain Dailcry\",8,5,2649\n";
const DAILCRY_KILL: &str =
"10/8/2026 20:01:03.0000 ENCOUNTER_END,2847,\"Captain Dailcry\",8,5,1,60000\n";

fn first_hit_before_dailcry() -> String {
[
hit("10/8/2026 20:00:02.9900", &FAELAN, &DAILCRY, 100),
DAILCRY_START.to_string(),
hit("10/8/2026 20:00:10.0000", &FAELAN, &DAILCRY, 100),
DAILCRY_KILL.to_string(),
]
.concat()
}

#[test]
fn a_first_hit_just_before_encounter_start_joins_the_boss_pull() {
let index = index_bytes(first_hit_before_dailcry().as_bytes());
assert_eq!(index.fights.len(), 1, "{:?}", index.fights);
let boss = &index.fights[0];
assert_eq!(
(
boss.is_combat(),
boss.index,
boss.outcome,
boss.start,
boss.start_offset,
boss.duration_ms
),
(
false,
1,
Outcome::Kill,
ts("10/8/2026 20:00:02.9900"),
0,
60_010
)
);
}

#[test]
fn a_pack_still_fighting_when_the_boss_is_pulled_stays_its_own_pull() {
let data = [
hit("10/8/2026 20:00:02.9000", &FAELAN, &DAILCRY, 100),
hit("10/8/2026 20:00:02.9500", &FAELAN, &KNIGHT, 100),
DAILCRY_START.to_string(),
hit("10/8/2026 20:00:10.0000", &FAELAN, &DAILCRY, 100),
DAILCRY_KILL.to_string(),
]
.concat();
let index = index_bytes(data.as_bytes());
let fights: Vec<_> = index
.fights
.iter()
.map(|fight| (fight.index, fight.is_combat(), fight.outcome, fight.start))
.collect();
assert_eq!(
fights,
[
(1, true, Outcome::Ended, ts("10/8/2026 20:00:02.9000")),
(2, false, Outcome::Kill, ts("10/8/2026 20:00:03.0000")),
]
);
assert_eq!(index.fights[1].duration_ms, 60_000);
}

#[test]
fn an_unfinished_boss_pull_keeps_its_joined_first_hit() {
let data = [
hit("10/8/2026 20:00:02.9900", &FAELAN, &DAILCRY, 100),
DAILCRY_START.to_string(),
hit("10/8/2026 20:00:30.0000", &FAELAN, &DAILCRY, 100),
]
.concat();
let index = index_bytes(data.as_bytes());
assert_eq!(index.fights.len(), 1, "{:?}", index.fights);
let boss = &index.fights[0];
assert_eq!(
(boss.outcome, boss.start, boss.duration_ms),
(Outcome::Incomplete, ts("10/8/2026 20:00:02.9900"), 27_010)
);
}

#[test]
fn a_first_hit_waits_for_its_boss_pull_while_live() {
let data = first_hit_before_dailcry();
let before_end = data.find("10/8/2026 20:01:03.0000").unwrap();
let mut indexer = Indexer::new();
indexer.feed(&data.as_bytes()[..before_end]);
assert!(indexer.settled_index(None).fights.is_empty());

indexer.feed(&data.as_bytes()[before_end..]);
assert_eq!(
indexer.settled_index(None).fights,
index_bytes(data.as_bytes()).fights
);
}

#[test]
fn a_first_hit_logged_later_than_encounter_start_never_delays_the_boss_pull() {
let data = [
hit("10/8/2026 20:00:03.0100", &FAELAN, &DAILCRY, 100),
DAILCRY_START.to_string(),
hit("10/8/2026 20:00:10.0000", &FAELAN, &DAILCRY, 100),
DAILCRY_KILL.to_string(),
]
.concat();
let index = index_bytes(data.as_bytes());
assert_eq!(index.fights.len(), 1, "{:?}", index.fights);
let boss = &index.fights[0];
assert_eq!(
(boss.start, boss.start_offset, boss.duration_ms),
(ts("10/8/2026 20:00:03.0000"), 0, 60_000)
);
}
Loading