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:
+9
-3
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user