From 439f48871eb481bf96325930f8065f187efc28e1 Mon Sep 17 00:00:00 2001 From: robert Date: Sat, 27 Jun 2026 16:57:08 -0400 Subject: [PATCH] Fix mid-round activation causing participants to be skipped in round 2+ When a participant was activated mid-combat, computeTurnOrderAfterAddition appended them to the end of turnOrderIds. The visual display sorted by initiative (putting them at the top), but the turn pointer followed the append order, making it look like the top-initiative participants were skipped when the round wrapped. Fix: at the round boundary in handleNextTurn, rebuild turnOrderIds from all active participants sorted by initiative. Mid-round additions go last in round 1 (standard D&D ruling), then slot into proper initiative order from round 2 onwards. Also adds turnOrderIds to the next-turn undo snapshot. Co-Authored-By: Claude Sonnet 4.6 --- src/App.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/App.js b/src/App.js index ba3b706..9a8e0bb 100644 --- a/src/App.js +++ b/src/App.js @@ -1732,21 +1732,36 @@ function InitiativeControls({ campaignId, encounter, encounterPath }) { } let nextIndex = (currentIndex + 1) % activePsInOrder.length; + let newTurnOrderIds = encounter.turnOrderIds; if (nextIndex === 0 && currentIndex !== -1) { nextRound += 1; + // Rebuild turn order by initiative at the start of each new round so that participants + // activated mid-round (appended to the end) slot into proper initiative position next round. + const activePs = encounter.participants.filter(p => p.isActive); + const sorted = sortParticipantsByInitiative(activePs, encounter.participants); + newTurnOrderIds = sorted.map(p => p.id); } + // When wrapping to a new round the next participant is first in the rebuilt order + const nextParticipant = (nextIndex === 0 && currentIndex !== -1) + ? encounter.participants.find(p => p.id === newTurnOrderIds[0]) + : activePsInOrder[nextIndex]; + + if (!nextParticipant) return; + try { await updateDoc(doc(db, encounterPath), { - currentTurnParticipantId: activePsInOrder[nextIndex].id, - round: nextRound + currentTurnParticipantId: nextParticipant.id, + round: nextRound, + turnOrderIds: newTurnOrderIds, }); - logAction(`${activePsInOrder[nextIndex].name}'s turn (Round ${nextRound})`, { encounterName: encounter.name }, { + logAction(`${nextParticipant.name}'s turn (Round ${nextRound})`, { encounterName: encounter.name }, { encounterPath, updates: { currentTurnParticipantId: encounter.currentTurnParticipantId, round: encounter.round, + turnOrderIds: [...encounter.turnOrderIds], }, }); } catch (err) {