diff --git a/shared/tests/turn.writeback.test.js b/shared/tests/turn.writeback.test.js
index 9bd8637..abe7d7d 100644
--- a/shared/tests/turn.writeback.test.js
+++ b/shared/tests/turn.writeback.test.js
@@ -93,6 +93,48 @@ describe('character writeback on endEncounter', () => {
expect(cleric.defaultCurrentHp).toBe(22);
});
+ test('one-time ctx override enables writeback without persistent campaign setting', async () => {
+ const storage = makeMockStorage();
+ storage._docs.set('campaigns/camp1', {
+ id: 'camp1', name: 'Camp', syncCharacters: false, players: [
+ { id: 'char1', name: 'Fighter', defaultMaxHp: 30, defaultAc: 17, defaultCurrentHp: 30 },
+ ],
+ });
+ const enc = makeEncounter({ participants: [
+ { id: 'p1', name: 'Fighter', type: 'character', originalCharacterId: 'char1', initiative: 15, maxHp: 28, currentHp: 11, ac: 19 },
+ ] });
+ const ctx = {
+ storage,
+ encounterPath: 'campaigns/camp1/encounters/enc1',
+ logPath: 'logs/log1',
+ syncCharacters: true,
+ };
+
+ await endEncounter(enc, ctx);
+
+ const campWrite = storage._writes.find(w => w.path === 'campaigns/camp1');
+ expect(campWrite).toBeDefined();
+ expect(campWrite.patch.players[0]).toMatchObject({ defaultMaxHp: 28, defaultCurrentHp: 11, defaultAc: 19 });
+ expect(storage._docs.get('campaigns/camp1').syncCharacters).toBe(false);
+ });
+
+ test('one-time ctx override disables writeback despite persistent campaign setting', async () => {
+ const storage = makeMockStorage();
+ storage._docs.set('campaigns/camp1', {
+ id: 'camp1', name: 'Camp', syncCharacters: true, players: [
+ { id: 'char1', name: 'Fighter', defaultMaxHp: 30, defaultAc: 17, defaultCurrentHp: 30 },
+ ],
+ });
+ await endEncounter(makeEncounter(), {
+ storage,
+ encounterPath: 'campaigns/camp1/encounters/enc1',
+ logPath: 'logs/log1',
+ syncCharacters: false,
+ });
+
+ expect(storage._writes.find(w => w.path === 'campaigns/camp1')).toBeUndefined();
+ });
+
test('writeback skips monsters (no originalCharacterId)', async () => {
const storage = makeMockStorage();
const players = [{ id: 'char1', name: 'Fighter', defaultMaxHp: 30, defaultAc: 17 }];
diff --git a/shared/turn.js b/shared/turn.js
index 8707f15..5312e0d 100644
--- a/shared/turn.js
+++ b/shared/turn.js
@@ -380,8 +380,7 @@ function makeParticipant(opts) {
};
}
-function buildCharacterParticipant(character) {
- const initiativeRoll = rollD20();
+function buildCharacterParticipant(character, initiativeRoll = rollD20()) {
const modifier = character.defaultInitMod || 0;
const finalInitiative = initiativeRoll + modifier;
const maxHp = character.defaultMaxHp || DEFAULT_MAX_HP;
@@ -1168,7 +1167,10 @@ async function endEncounter(encounter, ctx) {
try {
campaign = ctx.campaign || await ctx.storage.getDoc(campaignPath);
} catch { campaign = null; }
- if (campaign && campaign.syncCharacters && Array.isArray(campaign.players)) {
+ const syncCharacters = ctx.syncCharacters !== undefined
+ ? ctx.syncCharacters
+ : campaign?.syncCharacters;
+ if (campaign && syncCharacters && Array.isArray(campaign.players)) {
const charParticipants = (encounter.participants || []).filter(p => p.originalCharacterId);
const oldValues = [];
let changed = false;
diff --git a/src/App.js b/src/App.js
index 0369a3f..b99b0ec 100644
--- a/src/App.js
+++ b/src/App.js
@@ -385,7 +385,7 @@ function Modal({ onClose, title, children }) {
);
}
-function ConfirmationModal({ isOpen, onClose, onConfirm, title, message }) {
+function ConfirmationModal({ isOpen, onClose, onConfirm, title, message, children }) {
const [pending, setPending] = useState(false);
const [error, setError] = useState(null);
@@ -422,7 +422,8 @@ function ConfirmationModal({ isOpen, onClose, onConfirm, title, message }) {
{title || "Confirm Action"}
- {message || "Are you sure you want to proceed?"}
+ {message || "Are you sure you want to proceed?"}
+ {children && {children}
}
{error && (
{error}
)}
@@ -1263,6 +1264,7 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp
let modifier = 0;
let currentMaxHp = parseInt(maxHp, 10) || DEFAULT_MAX_HP;
let participantAsNpc = false;
+ let characterParticipant = null;
const manualInit = manualInitiative !== '' && !isNaN(parseInt(manualInitiative, 10));
const finalInitiative = manualInit ? parseInt(manualInitiative, 10) : null;
@@ -1275,9 +1277,11 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp
showInfo(`${character.name} is already in this encounter.`);
return;
}
- nameToAdd = character.name;
- currentMaxHp = character.defaultMaxHp || currentMaxHp;
- modifier = character.defaultInitMod || 0;
+ const built = shared.buildCharacterParticipant(character, initiativeRoll);
+ characterParticipant = built.participant;
+ nameToAdd = characterParticipant.name;
+ currentMaxHp = characterParticipant.maxHp;
+ modifier = characterParticipant.initMod || 0;
} else {
modifier = parseInt(monsterInitMod, 10) || 0;
participantAsNpc = asNpc;
@@ -1295,23 +1299,25 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp
if (character?.isNpc) finalType = 'npc';
}
const computedInitiative = manualInit ? finalInitiative : (initiativeRoll + modifier);
- const newParticipant = {
- id: generateId(),
- name: nameToAdd,
- type: finalType,
- originalCharacterId: ['character', 'npc'].includes(finalType) ? selectedCharacterId : null,
- initiative: computedInitiative,
- initMod: modifier,
- maxHp: currentMaxHp,
- currentHp: currentMaxHp,
- hpFormula: participantType === 'monster' ? (hpFormula.trim() || null) : null,
- ac: participantType === 'monster' && monsterAc !== '' ? parseInt(monsterAc, 10) : (participantType === 'character' ? (campaignCharacters.find(c => c.id === selectedCharacterId)?.defaultAc ?? null) : null),
- conditions: [],
- isActive: true,
- status: 'conscious',
- deathSaveSuccesses: 0,
- deathSaveFailures: 0,
- };
+ const newParticipant = characterParticipant
+ ? { ...characterParticipant, initiative: computedInitiative }
+ : {
+ id: generateId(),
+ name: nameToAdd,
+ type: finalType,
+ originalCharacterId: ['character', 'npc'].includes(finalType) ? selectedCharacterId : null,
+ initiative: computedInitiative,
+ initMod: modifier,
+ maxHp: currentMaxHp,
+ currentHp: currentMaxHp,
+ hpFormula: hpFormula.trim() || null,
+ ac: monsterAc !== '' ? parseInt(monsterAc, 10) : null,
+ conditions: [],
+ isActive: true,
+ status: 'conscious',
+ deathSaveSuccesses: 0,
+ deathSaveFailures: 0,
+ };
try {
await addParticipant(encounter, newParticipant, buildCtx(encounterPath));
@@ -1347,26 +1353,7 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp
const newParticipants = campaignCharacters
.filter(char => !existingCharacterIds.includes(char.id))
- .map(char => {
- const initiativeRoll = rollD20();
- const modifier = char.defaultInitMod || 0;
- const finalInitiative = initiativeRoll + modifier;
- return {
- id: generateId(),
- name: char.name,
- type: 'character',
- originalCharacterId: char.id,
- initiative: finalInitiative,
- initMod: modifier,
- maxHp: char.defaultMaxHp || DEFAULT_MAX_HP,
- currentHp: char.defaultMaxHp || DEFAULT_MAX_HP,
- conditions: [],
- isActive: true,
- status: 'conscious',
- deathSaveSuccesses: 0,
- deathSaveFailures: 0,
- };
- });
+ .map(char => shared.buildCharacterParticipant(char).participant);
if (newParticipants.length === 0) {
showInfo("All campaign characters are already in this encounter."); return;
@@ -2242,9 +2229,10 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp
// INITIATIVE CONTROLS COMPONENT
// ============================================================================
-function InitiativeControls({ campaignId, encounter, encounterPath }) {
+function InitiativeControls({ campaignId, encounter, encounterPath, syncCharacters }) {
const { showToast, showInfo } = useUIFeedback();
const [showEndConfirm, setShowEndConfirm] = useState(false);
+ const [endSyncCharacters, setEndSyncCharacters] = useState(false);
const { data: activeDisplayData } = useFirestoreDocument(getPath.activeDisplay());
const hidePlayerHp = activeDisplayData?.hidePlayerHp ?? true;
const hideNpcHp = activeDisplayData?.hideNpcHp ?? false;
@@ -2380,10 +2368,15 @@ function InitiativeControls({ campaignId, encounter, encounterPath }) {
}
};
+ const openEndConfirm = () => {
+ setEndSyncCharacters(!!syncCharacters);
+ setShowEndConfirm(true);
+ };
+
const confirmEndEncounter = async () => {
if (!db) return;
try {
- const ctx = { ...buildCtx(encounterPath), campaignId };
+ const ctx = { ...buildCtx(encounterPath), campaignId, syncCharacters: endSyncCharacters };
if (campaignId) {
ctx.campaignPath = getPath.campaign(campaignId);
try {
@@ -2439,7 +2432,7 @@ function InitiativeControls({ campaignId, encounter, encounterPath }) {
Next TurnNext