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:
@@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
const MODULE_ID = "gowlers-tracking-ledger";
|
const MODULE_ID = "gowlers-tracking-ledger";
|
||||||
const MODULE_VERSION = "0.1.8";
|
const MODULE_VERSION = "0.1.9";
|
||||||
const TRACK_SETTING = "actorSettings";
|
const TRACK_SETTING = "actorSettings";
|
||||||
const FLAG_SCOPE = "world";
|
const FLAG_SCOPE = "world";
|
||||||
const MAX_HISTORY_ROWS = 100;
|
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
|
// Determine encounter ID: use active combat, or if none, check if combat just ended
|
||||||
let encounterId = game.combat?.id ?? null;
|
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 no active combat but XP is being recorded shortly after combat ended, link to last encounter
|
||||||
if (!encounterId && statId === "xp" && ledgerState.lastCombatId) {
|
if (!encounterId && statId === "xp" && ledgerState.lastCombatId) {
|
||||||
const timeSinceEnd = Date.now() - ledgerState.lastCombatEndTime;
|
const timeSinceEnd = Date.now() - ledgerState.lastCombatEndTime;
|
||||||
// Link to last encounter if within 5 seconds (allows time for XP award after combat ends)
|
// Link to last encounter if within 30 seconds (allows time for XP award after combat ends)
|
||||||
if (timeSinceEnd < 5000) {
|
if (timeSinceEnd < 30000) {
|
||||||
encounterId = ledgerState.lastCombatId;
|
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,
|
encounterId: encounterId,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
console.log(`[GowlersTracking] History entry created for ${statId}:`, entry);
|
||||||
|
|
||||||
const existing = (await actor.getFlag(FLAG_SCOPE, config.flag)) ?? [];
|
const existing = (await actor.getFlag(FLAG_SCOPE, config.flag)) ?? [];
|
||||||
existing.unshift(entry);
|
existing.unshift(entry);
|
||||||
if (existing.length > MAX_HISTORY_ROWS) existing.splice(MAX_HISTORY_ROWS);
|
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") {
|
async function updateEncounterSummary(actor, combat, status = "ongoing") {
|
||||||
if (!actor || !combat) return;
|
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)) ?? [];
|
const existing = (await actor.getFlag(FLAG_SCOPE, ENCOUNTER_FLAG)) ?? [];
|
||||||
|
|
||||||
// Find existing entry for this combat
|
// Find existing entry for this combat
|
||||||
let encEntry = existing.find((entry) => entry.encounterID === combat.id);
|
let encEntry = existing.find((entry) => entry.encounterID === combat.id);
|
||||||
|
|
||||||
if (!encEntry) {
|
if (!encEntry) {
|
||||||
|
console.log(`[GowlersTracking] Creating new encounter entry for combat ${combat.id}`);
|
||||||
encEntry = {
|
encEntry = {
|
||||||
encounterID: combat.id,
|
encounterID: combat.id,
|
||||||
dateCreated: Date.now(),
|
dateCreated: Date.now(),
|
||||||
@@ -1011,6 +1019,7 @@ async function updateEncounterSummary(actor, combat, status = "ongoing") {
|
|||||||
};
|
};
|
||||||
existing.unshift(encEntry);
|
existing.unshift(encEntry);
|
||||||
} else {
|
} else {
|
||||||
|
console.log(`[GowlersTracking] Updating existing encounter entry for combat ${combat.id}, new status: ${status}`);
|
||||||
encEntry.dateUpdated = Date.now();
|
encEntry.dateUpdated = Date.now();
|
||||||
encEntry.status = status;
|
encEntry.status = status;
|
||||||
encEntry.rounds = combat.round || 0;
|
encEntry.rounds = combat.round || 0;
|
||||||
@@ -1030,7 +1039,9 @@ async function updateEncounterSummary(actor, combat, status = "ongoing") {
|
|||||||
existing.splice(50);
|
existing.splice(50);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(`[GowlersTracking] Encounter entry after update:`, encEntry);
|
||||||
await actor.setFlag(FLAG_SCOPE, ENCOUNTER_FLAG, existing);
|
await actor.setFlag(FLAG_SCOPE, ENCOUNTER_FLAG, existing);
|
||||||
|
console.log(`[GowlersTracking] Encounter flag saved for ${actor.name}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendChatNotification(statId, actor, previous, nextValue, entry) {
|
function sendChatNotification(statId, actor, previous, nextValue, entry) {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
"type": "module",
|
"type": "module",
|
||||||
"title": "Gowler's Tracking Ledger",
|
"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.",
|
"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": [
|
"authors": [
|
||||||
{ "name": "Gowler", "url": "https://foundryvtt.com" }
|
{ "name": "Gowler", "url": "https://foundryvtt.com" }
|
||||||
],
|
],
|
||||||
|
|||||||
Reference in New Issue
Block a user