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
69 changes: 54 additions & 15 deletions crates/openlogs-index/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ impl IndexBuilder {
self.last_specs.insert(info.guid.to_string(), known);
}
Event::Combat(event) => {
if let Some(run) = &mut self.run {
run.learn_copy(event);
if self.run.as_mut().is_some_and(|run| run.learn_copy(event)) {
self.settle_copy();
}
if let CombatKind::UnitDied { unconscious: false } = event.kind
&& event.target.is_enemy_npc()
Expand Down Expand Up @@ -679,23 +679,20 @@ impl IndexBuilder {
}

/// `index` of the open run for a pull opening now. A dungeon visit's first pull numbers
/// it, unless the visit continues the left run: then the pulls listed since that run
/// was left join it.
/// it, unless the visit continues the left run. While the visit's copy is unknown, the
/// left run waits for it; see [`Self::settle_copy`].
fn number_run(&mut self) -> Option<u32> {
if let Some(mut visit) = self.run.take_if(|run| !run.numbered) {
match self.left_run.take_if(|left| left.continued_by(&visit)) {
Some(mut left) => {
let index = left.summary.index;
for fight in &mut self.index.fights {
if fight.index > index && fight.run_index.is_none() && !fight.is_run() {
fight.run_index = Some(index);
}
}
left.absorb(visit);
self.run = Some(left);
}
Some(left) => self.continue_run(left, visit),
None => {
self.list_left_run();
if !self
.left_run
.as_ref()
.is_some_and(|left| left.may_be_continued_by(&visit))
{
self.list_left_run();
}
visit.summary.index = self.next_index();
visit.numbered = true;
self.run = Some(visit);
Expand All @@ -705,6 +702,48 @@ impl IndexBuilder {
self.run.as_ref().map(|run| run.summary.index)
}

/// The open visit, numbered before its copy was seen, has just learned it: it joins the
/// left run of the same copy, or that run is listed.
fn settle_copy(&mut self) {
if !self.run.as_ref().is_some_and(|run| run.numbered) {
return;
}
let Some(left) = self.left_run.take() else {
return;
};
match self.run.take_if(|visit| left.continued_by(visit)) {
Some(visit) => self.continue_run(left, visit),
None => self.index.fights.push(left.finish(Outcome::Ended)),
}
}

/// Continues the left run with `visit`: the pulls listed since it was left, and the
/// visit's own if it was numbered already, join it.
fn continue_run(&mut self, mut left: OpenRun, visit: OpenRun) {
let index = left.summary.index;
let visit_index = visit.numbered.then_some(visit.summary.index);
let joins = |run_index: Option<u32>| run_index.is_none() || run_index == visit_index;
for fight in &mut self.index.fights {
if fight.index > index && !fight.is_run() && joins(fight.run_index) {
fight.run_index = Some(index);
}
}
if let Some(visit_index) = visit_index {
let open = self.open.as_mut().into_iter().flat_map(|fight| {
let lead = fight.lead.as_mut().map(|lead| &mut lead.pull);
std::iter::once(&mut fight.summary).chain(lead)
});
let combat = self.combat.as_mut().map(|combat| &mut combat.summary);
for summary in open.chain(combat) {
if summary.run_index == Some(visit_index) {
summary.run_index = Some(index);
}
}
}
left.absorb(visit);
self.run = Some(left);
}

/// Leaving the instance closes a key's run. A dungeon visit with a pull becomes the left
/// run, which a later visit may continue; one without is dropped.
fn leave_run(&mut self) {
Expand Down
2 changes: 1 addition & 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 = 17;
pub const INDEX_FORMAT_VERSION: u32 = 18;
/// 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 Down
14 changes: 12 additions & 2 deletions crates/openlogs-index/src/runs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ impl OpenRun {
matches!(self.summary.run, Some(RunKind::Dungeon))
}

pub(crate) fn learn_copy(&mut self, event: &CombatEvent<'_>) {
/// Whether this line showed the dungeon run's copy for the first time.
pub(crate) fn learn_copy(&mut self, event: &CombatEvent<'_>) -> bool {
if !self.is_dungeon() || self.copy.is_some() {
return;
return false;
}
self.copy = [event.source.guid, event.target.guid]
.into_iter()
.find_map(|guid| instance_copy(guid, self.summary.instance_id))
.map(str::to_string);
self.copy.is_some()
}

/// Whether `visit` is in the same instance copy, which only an NPC seen in both proves.
Expand All @@ -52,6 +54,14 @@ impl OpenRun {
&& self.copy == visit.copy
}

/// Whether `visit` may still turn out to be in the same copy, which it has not shown yet.
pub(crate) fn may_be_continued_by(&self, visit: &OpenRun) -> bool {
self.summary.instance_id == visit.summary.instance_id
&& self.summary.difficulty == visit.summary.difficulty
&& self.copy.is_some()
&& visit.copy.is_none()
}

/// Extends this run, left earlier, by a later visit of the same copy.
pub(crate) fn absorb(&mut self, visit: OpenRun) {
let summary = &mut self.summary;
Expand Down
27 changes: 25 additions & 2 deletions crates/openlogs-index/tests/dungeon_runs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ fn runs(fights: &[FightSummary]) -> Vec<&FightSummary> {
}

fn gerstahn(minute: u32, success: bool) -> String {
gerstahn_fighting(minute, success, &GUARDSMAN)
}

/// Gerstahn's pull, whose only hit lands on `enemy`, so the copy is known only inside it.
fn gerstahn_fighting(minute: u32, success: bool, enemy: &Unit) -> String {
[
boss_start(
&format!("11/17/2026 20:{minute:02}:30.0000"),
Expand All @@ -36,7 +41,7 @@ fn gerstahn(minute: u32, success: bool) -> String {
hit(
&format!("11/17/2026 20:{minute:02}:31.0000"),
&RAVEL,
&GUARDSMAN,
enemy,
100,
),
boss_end(
Expand Down Expand Up @@ -189,7 +194,7 @@ fn a_visit_without_a_pull_is_not_listed() {
}

#[test]
fn a_revisit_starting_with_a_boss_pull_starts_a_new_run() {
fn a_revisit_starting_with_a_boss_pull_continues_the_run_of_its_copy() {
let log = [
enter_brd(0),
pack(0, &GUARDSMAN),
Expand All @@ -200,6 +205,24 @@ fn a_revisit_starting_with_a_boss_pull_starts_a_new_run() {
]
.concat();
let index = index_bytes(log.as_bytes());
assert_eq!(
entries(&index.fights),
[(1, true, None), (2, false, Some(1)), (4, false, Some(1))]
);
}

#[test]
fn a_revisit_of_another_copy_starting_with_a_boss_pull_starts_a_new_run() {
let log = [
enter_brd(0),
pack(0, &GUARDSMAN),
leave(1),
enter_brd(2),
gerstahn_fighting(2, true, &OTHER_COPY_GUARDSMAN),
leave(3),
]
.concat();
let index = index_bytes(log.as_bytes());
assert_eq!(
entries(&index.fights),
[
Expand Down
4 changes: 3 additions & 1 deletion ui/src/currentGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ export function currentGame(): Game | null {
return game;
}

/** Listeners hear only of another game, not of every refresh of the open log. */
export function setCurrentGame(next: Game | null) {
const changed = JSON.stringify(next) !== JSON.stringify(game);
game = next;
listeners.forEach((listener) => listener());
if (changed) listeners.forEach((listener) => listener());
}

export function subscribeCurrentGame(listener: () => void): () => void {
Expand Down
15 changes: 15 additions & 0 deletions ui/src/gameData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,19 @@ describe('game data tables', () => {
setCurrentGame({ kind: 'forever' });
expect(bearForm(), 'a loaded table is shown again at once').toBe('ability_racial_bearform');
});

it('leaves the icons alone when a refreshed log keeps its game', async () => {
vi.stubGlobal('fetch', vi.fn(async () => new Response('', { status: 404 })));
const { setCurrentGame } = await import('./currentGame');
const { iconTables, subscribeIconTables } = await import('./gameData');
setCurrentGame({ kind: 'forever' });
const shown = iconTables();
const notified = vi.fn();
subscribeIconTables(notified);

setCurrentGame({ kind: 'forever' });

expect(notified).not.toHaveBeenCalled();
expect(iconTables()).toBe(shown);
});
});
Loading