fix(gowlers-tracking-ledger): improve encounter tracking with extended time window and detailed logging

- Increase XP linking time window from 5 to 30 seconds after combat ends
- Add detailed console logging to debug encounter status updates
- Log XP entry creation with encounter ID information
- Log encounter summary updates to verify status changes are persisted
- Track active combat state during XP recording

This helps debug why encounters show as 'ongoing' and XP not linked to encounters.

Update version to 0.1.9

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
centron\schwoerer
2025-11-20 09:58:45 +01:00
parent 8646ebd3e1
commit 049f47c4b3
2 changed files with 16 additions and 5 deletions

View File

@@ -1,6 +1,6 @@
const MODULE_ID = "gowlers-tracking-ledger";
const MODULE_VERSION = "0.1.8";
const MODULE_VERSION = "0.1.9";
const TRACK_SETTING = "actorSettings";
const FLAG_SCOPE = "world";
const MAX_HISTORY_ROWS = 100;
@@ -547,14 +547,17 @@ async function recordHistoryEntry(actor, statId, previous, nextValue, userId) {
// Determine encounter ID: use active combat, or if none, check if combat just ended
let encounterId = game.combat?.id ?? null;
console.log(`[GowlersTracking] Recording ${statId} change - Active combat: ${encounterId ? "Yes (" + encounterId + ")" : "No"}`);
// If no active combat but XP is being recorded shortly after combat ended, link to last encounter
if (!encounterId && statId === "xp" && ledgerState.lastCombatId) {
const timeSinceEnd = Date.now() - ledgerState.lastCombatEndTime;
// Link to last encounter if within 5 seconds (allows time for XP award after combat ends)
if (timeSinceEnd < 5000) {
// Link to last encounter if within 30 seconds (allows time for XP award after combat ends)
if (timeSinceEnd < 30000) {
encounterId = ledgerState.lastCombatId;
console.log("[GowlersTracking] Linking XP entry to last encounter:", encounterId);
console.log(`[GowlersTracking] Linking XP entry to last encounter ${encounterId} (${timeSinceEnd}ms after combat end)`);
} else {
console.log(`[GowlersTracking] XP recorded ${timeSinceEnd}ms after combat end (too late to link, window is 30s)`);
}
}
@@ -567,6 +570,8 @@ async function recordHistoryEntry(actor, statId, previous, nextValue, userId) {
encounterId: encounterId,
};
console.log(`[GowlersTracking] History entry created for ${statId}:`, entry);
const existing = (await actor.getFlag(FLAG_SCOPE, config.flag)) ?? [];
existing.unshift(entry);
if (existing.length > MAX_HISTORY_ROWS) existing.splice(MAX_HISTORY_ROWS);
@@ -994,12 +999,15 @@ async function onCombatEnd(combat) {
async function updateEncounterSummary(actor, combat, status = "ongoing") {
if (!actor || !combat) return;
console.log(`[GowlersTracking] updateEncounterSummary for ${actor.name}, combat ${combat.id}, status: ${status}`);
const existing = (await actor.getFlag(FLAG_SCOPE, ENCOUNTER_FLAG)) ?? [];
// Find existing entry for this combat
let encEntry = existing.find((entry) => entry.encounterID === combat.id);
if (!encEntry) {
console.log(`[GowlersTracking] Creating new encounter entry for combat ${combat.id}`);
encEntry = {
encounterID: combat.id,
dateCreated: Date.now(),
@@ -1011,6 +1019,7 @@ async function updateEncounterSummary(actor, combat, status = "ongoing") {
};
existing.unshift(encEntry);
} else {
console.log(`[GowlersTracking] Updating existing encounter entry for combat ${combat.id}, new status: ${status}`);
encEntry.dateUpdated = Date.now();
encEntry.status = status;
encEntry.rounds = combat.round || 0;
@@ -1030,7 +1039,9 @@ async function updateEncounterSummary(actor, combat, status = "ongoing") {
existing.splice(50);
}
console.log(`[GowlersTracking] Encounter entry after update:`, encEntry);
await actor.setFlag(FLAG_SCOPE, ENCOUNTER_FLAG, existing);
console.log(`[GowlersTracking] Encounter flag saved for ${actor.name}`);
}
function sendChatNotification(statId, actor, previous, nextValue, entry) {

View File

@@ -3,7 +3,7 @@
"type": "module",
"title": "Gowler's Tracking Ledger",
"description": "Adds HP/XP/Currency log buttons to PF1 sheets and opens the tracking dialog preloaded with the actor's logs.",
"version": "0.1.8",
"version": "0.1.9",
"authors": [
{ "name": "Gowler", "url": "https://foundryvtt.com" }
],