fix: end combat failing when character sync enabled
Character writeback in endEncounter wrote to a bare campaigns/{id} path,
which the firebase adapter passes straight to the SDK — the update hit a
nonexistent top-level doc, threw, and aborted the whole end-combat action
(round never reset, combat stayed running).
- endEncounter now uses ctx.campaignPath (full artifacts/... path from
App.js), falling back to the bare path for server/test adapters
- writeback failures no longer block ending combat
- regression tests: campaignPath routing, writeback-failure resilience,
and participants never cleared on end
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
+11
-3
@@ -1129,11 +1129,15 @@ async function endEncounter(encounter, ctx) {
|
||||
};
|
||||
|
||||
// Character writeback: sync maxHp/ac/currentHp to campaign players if enabled.
|
||||
// ctx.campaignPath: adapter-specific doc path (firebase needs the full
|
||||
// artifacts/{APP_ID}/public/data prefix); bare campaigns/{id} fallback for
|
||||
// server/test adapters. Writeback failure must not block ending combat.
|
||||
const wbCampaignId = encounter.campaignId || ctx.campaignId;
|
||||
if ((wbCampaignId || ctx.campaign) && ctx.storage) {
|
||||
const campaignPath = ctx.campaignPath || `campaigns/${wbCampaignId}`;
|
||||
let campaign;
|
||||
try {
|
||||
campaign = ctx.campaign || await ctx.storage.getDoc(`campaigns/${wbCampaignId}`);
|
||||
campaign = ctx.campaign || await ctx.storage.getDoc(campaignPath);
|
||||
} catch { campaign = null; }
|
||||
if (campaign && campaign.syncCharacters && Array.isArray(campaign.players)) {
|
||||
const charParticipants = (encounter.participants || []).filter(p => p.originalCharacterId);
|
||||
@@ -1153,8 +1157,12 @@ async function endEncounter(encounter, ctx) {
|
||||
return newPlayer;
|
||||
});
|
||||
if (changed) {
|
||||
await ctx.storage.updateDoc(`campaigns/${wbCampaignId}`, { players: updatedPlayers });
|
||||
log.undo.characterWriteback = oldValues;
|
||||
try {
|
||||
await ctx.storage.updateDoc(campaignPath, { players: updatedPlayers });
|
||||
log.undo.characterWriteback = oldValues;
|
||||
} catch (err) {
|
||||
console.error(`endEncounter: character writeback failed (${campaignPath}):`, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user