Fix addParticipants: slot by initiative instead of append

addParticipants (bulk add all characters) appended to list end, ignoring
initiative. Pre-combat list showed random order until start encounter.
Now slots each participant by initiative desc, preserves existing order +
drag ties. Matches addParticipant single-add semantics.

Tests: 5 bulk-add slot cases (turn.bulkadd.test.js).
This commit is contained in:
david raistrick
2026-07-07 16:07:20 -04:00
parent 43b1f6ce41
commit 9870d4df0d
2 changed files with 58 additions and 3 deletions
+9 -3
View File
@@ -479,14 +479,20 @@ async function addParticipant(encounter, participant, ctx) {
}
async function addParticipants(encounter, newParticipants, ctx) {
const updatedParticipants = [...(encounter.participants || []), ...newParticipants];
// Slot each new participant into list by initiative (desc), preserve
// existing order + drag-established ties. Matches addParticipant semantics.
let base = [...(encounter.participants || [])];
for (const np of newParticipants) {
const idx = slotIndexForInit(base, np.initiative);
base.splice(idx, 0, np);
}
const names = newParticipants.map(p => p.name).join(', ');
const patch = { participants: updatedParticipants };
const patch = { participants: base, ...syncTurnOrder(base) };
const log = {
type: 'add_participants',
message: `Added ${newParticipants.length} participant${newParticipants.length === 1 ? '' : 's'}: ${names}`,
delta: { count: newParticipants.length },
undo: { participants: newParticipants },
undo: { participants: newParticipants, newTurnOrderIds: patch.turnOrderIds },
};
return commit(encounter, patch, log, ctx);
}