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 <noreply@anthropic.com>
This commit is contained in:
+18
-3
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user