From fefe2189f5a5e4417ec7226a7176362b9a4ad309 Mon Sep 17 00:00:00 2001 From: Jeremy Valentine <38669521+valentine195@users.noreply.github.com> Date: Wed, 16 Aug 2023 09:55:45 -0400 Subject: [PATCH] fix: Roll initiative for creatures added to encounters (close #81) --- src/main.ts | 1 - src/tracker/stores/tracker.ts | 91 +++++++++++++++++++++-------------- 2 files changed, 54 insertions(+), 38 deletions(-) diff --git a/src/main.ts b/src/main.ts index 47dbaa0c..842d86c8 100644 --- a/src/main.ts +++ b/src/main.ts @@ -91,7 +91,6 @@ export default class InitiativeTracker extends Plugin { dice = dice.replace(`%mod${i + 1}%`, `${modifier[i]}`); } } - console.log("🚀 ~ file: main.ts:87 ~ dice:", dice); const roller = this.getRoller(dice); const initiative = roller.rollSync(); if (isNaN(initiative)) return defaultIfNoResult; diff --git a/src/tracker/stores/tracker.ts b/src/tracker/stores/tracker.ts index 38ccac51..6f1eb41a 100644 --- a/src/tracker/stores/tracker.ts +++ b/src/tracker/stores/tracker.ts @@ -12,7 +12,10 @@ import type { import type { StackRoller } from "obsidian-overload"; import { OVERFLOW_TYPE, getRpgSystem } from "src/utils"; import type Logger from "../../logger/logger"; -import type { DifficultyLevel, DifficultyThreshold } from "src/utils/rpg-system"; +import type { + DifficultyLevel, + DifficultyThreshold +} from "src/utils/rpg-system"; type HPUpdate = { saved: boolean; @@ -307,8 +310,7 @@ function createTracker() { const creature = creatures.find((c) => c.name == name); if (creature) { if (!isNaN(Number(change.hp))) { - creature.hp = - change.hp; + creature.hp = change.hp; } if (change.max) { creature.current_max = Math.max( @@ -625,6 +627,13 @@ function createTracker() { roller.rollSync(); } } + + for (let creature of items) { + if (creature.static) continue; + creature.initiative = plugin.getInitiativeValue( + creature.modifier + ); + } creatures.push(...items); _logger?.log( _logger?.join(items.map((c) => c.name)), @@ -791,42 +800,50 @@ function createTracker() { updateState: () => update((c) => c), difficulty: (plugin: InitiativeTracker) => - derived, {difficulty: DifficultyLevel, thresholds: DifficultyThreshold[]}>( - creatures, - (values) => { - const players: number[] = []; - const creatureMap = new Map(); - const rpgSystem = getRpgSystem(plugin); - - for (const creature of values) { - if (!creature.enabled) continue; - if (creature.friendly) continue; - if (creature.player && creature.level) { - players.push(creature.level); - continue; - } - const stats = { - name: creature.name, - display: creature.display, - ac: creature.ac, - hp: creature.hp, - modifier: creature.modifier, - xp: creature.xp, - hidden: creature.hidden - }; - const existing = [...creatureMap].find(([c]) => equivalent(c, stats)); - if (!existing) { - creatureMap.set(creature, 1); - continue; - } - creatureMap.set(existing[0], existing[1] + 1); + derived< + Writable, + { + difficulty: DifficultyLevel; + thresholds: DifficultyThreshold[]; + } + >(creatures, (values) => { + const players: number[] = []; + const creatureMap = new Map(); + const rpgSystem = getRpgSystem(plugin); + + for (const creature of values) { + if (!creature.enabled) continue; + if (creature.friendly) continue; + if (creature.player && creature.level) { + players.push(creature.level); + continue; } - return { - difficulty: rpgSystem.getEncounterDifficulty(creatureMap, players), - thresholds: rpgSystem.getDifficultyThresholds(players) - }; + const stats = { + name: creature.name, + display: creature.display, + ac: creature.ac, + hp: creature.hp, + modifier: creature.modifier, + xp: creature.xp, + hidden: creature.hidden + }; + const existing = [...creatureMap].find(([c]) => + equivalent(c, stats) + ); + if (!existing) { + creatureMap.set(creature, 1); + continue; + } + creatureMap.set(existing[0], existing[1] + 1); } - ) + return { + difficulty: rpgSystem.getEncounterDifficulty( + creatureMap, + players + ), + thresholds: rpgSystem.getDifficultyThresholds(players) + }; + }) }; }