Skip to content

Commit 340a340

Browse files
committed
Fix drag & drop reordering with new initiative ties code
When any creature is dragged in the initiative order, the current order of every creature is stored. The order of all creatures is resolved in this order: 1. Initiative 2. Manual order 3. Initiative ties option If a creature is dragged out of its current initiative, the initiative value is updated, as previously. Manual ordering lets you reorder initiative to override other settings, for example in Pathfinder where by default NPCs always come before players on the same initiative, you can drag a player to come before an NPC if they have the feat that allows this. Manual ordering is wiped when initiative is rerolled. It isn't wiped if the initiative ties setting is changed, because this might not be what the player wants.
1 parent f348a24 commit 340a340

File tree

3 files changed

+39
-18
lines changed

3 files changed

+39
-18
lines changed

src/tracker/stores/tracker.ts

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function createTracker() {
9696
return data.descending;
9797
});
9898
let _settings: InitiativeTrackerData | null;
99-
99+
100100
const condensed = derived(creatures, (values) => {
101101
if (_settings?.condense) {
102102
values.forEach((creature, _, arr) => {
@@ -117,24 +117,40 @@ function createTracker() {
117117
const ordered = derived([condensed, data], ([values, data]) => {
118118
const sort = [...values];
119119
sort.sort((a, b) => {
120-
if (a.initiative == b.initiative) {
121-
switch (_settings.resolveTies) {
122-
case RESOLVE_TIES.random:
123-
return Math.random() < 0.5 ? 1 : -1;
124-
case RESOLVE_TIES.playerFirst:
125-
case RESOLVE_TIES.npcFirst:
126-
const aPlayer = a.player ? 1 : 0;
127-
const bPlayer = b.player ? 1 : 0;
128-
if (_settings.resolveTies == RESOLVE_TIES.playerFirst) {
129-
return bPlayer - aPlayer
130-
} else {
131-
return aPlayer - bPlayer
132-
}
133-
}
134-
}
135-
return data.descending
120+
/* Order creatures in this order:
121+
1. By initiative
122+
2. By manual order (drag & drop)
123+
3. According to the resolveTies setting */
124+
if (a.initiative != b.initiative) {
125+
return data.descending
136126
? b.initiative - a.initiative
137127
: a.initiative - b.initiative;
128+
}
129+
130+
if (
131+
a.manualOrder !== null && a.manualOrder !== undefined &&
132+
b.manualOrder !== null && b.manualOrder !== undefined &&
133+
a.manualOrder !== b.manualOrder
134+
) {
135+
const aOrder = a.manualOrder || 0;
136+
const bOrder = b.manualOrder || 0;
137+
return aOrder - bOrder;
138+
}
139+
140+
switch (_settings.resolveTies) {
141+
case RESOLVE_TIES.random:
142+
return Math.random() < 0.5 ? 1 : -1;
143+
case RESOLVE_TIES.playerFirst:
144+
case RESOLVE_TIES.npcFirst:
145+
const aPlayer = a.player ? 1 : 0;
146+
const bPlayer = b.player ? 1 : 0;
147+
if (_settings.resolveTies == RESOLVE_TIES.playerFirst) {
148+
return bPlayer - aPlayer
149+
} else {
150+
return aPlayer - bPlayer
151+
}
152+
}
153+
138154
});
139155
current_order = sort;
140156
return sort;
@@ -352,6 +368,7 @@ function createTracker() {
352368
creature.modifier
353369
);
354370
}
371+
creature.manualOrder = null;
355372
}
356373
return creatures;
357374
}

src/tracker/ui/creatures/Table.svelte

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@
5454
tracker.logNewInitiative(dropped.creature);
5555
}
5656
items = e.detail.items;
57-
$tracker = [...items.map(({ creature }) => creature)];
57+
$tracker = [...items.map(({ creature }, i) => {
58+
creature.manualOrder = i;
59+
return creature;
60+
})];
5861
}
5962
6063
const diceIcon = (node: HTMLElement) => {

src/utils/creature.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export class Creature {
3535
status: Set<Condition> = new Set();
3636
marker: string;
3737
initiative: number;
38+
manualOrder: number;
3839
static: boolean = false;
3940
source: string | string[];
4041
id: string;

0 commit comments

Comments
 (0)