@@ -5,6 +5,7 @@ import { useConfigState } from "./config-state";
5
5
import { createContextualStore } from "./zustand/contextual-zustand" ;
6
6
import { useDrawState } from "./draw-state" ;
7
7
import {
8
+ CHART_PLACEHOLDER ,
8
9
Drawing ,
9
10
EligibleChart ,
10
11
PlayerActionOnChart ,
@@ -115,7 +116,9 @@ const {
115
116
...self . pocketPicks . map ( ( pick ) => pick . chartId ) ,
116
117
...self . protects . map ( ( pick ) => pick . chartId ) ,
117
118
] ) ;
118
- const keepCharts = self . charts . filter ( ( c ) => keepChartIds . has ( c . id ) ) ;
119
+ const keepCharts = self . charts . filter (
120
+ ( c ) => keepChartIds . has ( c . id ) || c . type === CHART_PLACEHOLDER ,
121
+ ) ;
119
122
const newCharts = draw ( useDrawState . getState ( ) . gameData ! , {
120
123
...useConfigState . getState ( ) ,
121
124
chartCount : get ( ) . charts . length - keepCharts . length ,
@@ -130,19 +133,35 @@ const {
130
133
const charts = drawing . charts . slice ( ) ;
131
134
const key = keyFromAction ( action ) ;
132
135
const arr = drawing [ key ] . slice ( ) as PlayerActionOnChart [ ] | PocketPick [ ] ;
136
+ const targetChartIdx = charts . findIndex ( ( chart ) => chart . id === chartId ) ;
137
+ const targetChart = charts [ targetChartIdx ] ;
133
138
134
- if ( useConfigState . getState ( ) . orderByAction ) {
135
- const indexToCut = charts . findIndex ( ( chart ) => chart . id === chartId ) ;
136
- const [ shiftedChart ] = charts . splice ( indexToCut , 1 ) ;
139
+ if (
140
+ useConfigState . getState ( ) . orderByAction &&
141
+ targetChart ?. type !== CHART_PLACEHOLDER
142
+ ) {
143
+ charts . splice ( targetChartIdx , 1 ) ;
137
144
if ( action === "ban" ) {
138
145
// insert at tail of list
139
146
const insertPoint = charts . length ;
140
- charts . splice ( insertPoint , 0 , shiftedChart ) ;
147
+ charts . splice ( insertPoint , 0 , targetChart ) ;
141
148
} else {
142
- // insert at head of list, behind other picks
143
- const insertPoint =
144
- drawing . protects . length + drawing . pocketPicks . length ;
145
- charts . splice ( insertPoint , 0 , shiftedChart ) ;
149
+ const frontLockedCardCount =
150
+ // number of placeholder cards total (picked and unpicked)
151
+ charts . reduce < number > (
152
+ ( total , curr ) =>
153
+ total + ( curr . type === CHART_PLACEHOLDER ? 1 : 0 ) ,
154
+ 0 ,
155
+ ) +
156
+ // number of protects
157
+ drawing . protects . length +
158
+ // number of picks NOT targeting placeholder cards
159
+ drawing . pocketPicks . filter (
160
+ ( p ) => p . targetType !== CHART_PLACEHOLDER ,
161
+ ) . length ;
162
+
163
+ // insert at head of list, behind other picks/placeholders
164
+ charts . splice ( frontLockedCardCount , 0 , targetChart ) ;
146
165
}
147
166
set ( {
148
167
charts,
@@ -154,7 +173,12 @@ const {
154
173
arr . splice ( existingIndex , 1 ) ;
155
174
} else {
156
175
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
157
- arr . push ( { player, pick : newChart ! , chartId } ) ;
176
+ arr . push ( {
177
+ player,
178
+ pick : newChart ! ,
179
+ chartId,
180
+ targetType : targetChart . type ,
181
+ } ) ;
158
182
}
159
183
set ( {
160
184
[ key ] : arr ,
0 commit comments