-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathPlaySetupModal.tsx
More file actions
436 lines (411 loc) · 14.6 KB
/
Copy pathPlaySetupModal.tsx
File metadata and controls
436 lines (411 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import Image from 'next/image'
import { Chess } from 'chess.ts'
import toast from 'react-hot-toast'
import { useRouter } from 'next/router'
import { AnimatePresence } from 'framer-motion'
import { useCallback, useContext, useState } from 'react'
import {
Color,
PlayType,
TimeControl,
TimeControlOptionNames,
TimeControlOptions,
} from 'src/types'
import { ModalContext } from 'src/contexts'
import { ModalContainer } from './ModalContainer'
import { Slider } from './Slider'
import {
customToPresetTimeControl,
parseTimeControl,
getPresetOptions,
} from 'src/lib/timeControlUtils'
const maiaOptions = [
'maia_kdd_1100',
'maia_kdd_1200',
'maia_kdd_1300',
'maia_kdd_1400',
'maia_kdd_1500',
'maia_kdd_1600',
'maia_kdd_1700',
'maia_kdd_1800',
'maia_kdd_1900',
]
interface OptionSelectProps<T> {
options: T[]
labels: string[]
selected: T
onChange: (selected: T) => void
}
function OptionSelect<T>({
options,
labels,
selected,
onChange,
}: OptionSelectProps<T>) {
return (
<div className="flex overflow-hidden rounded-lg">
{options.map((option, index) => {
return (
<button
key={index}
className={`flex-1 px-4 py-2 text-sm font-medium transition-colors ${
option === selected
? 'bg-human-4 text-white'
: 'bg-background-2 text-primary hover:bg-background-3'
} ${index === 0 ? 'rounded-l-lg' : ''} ${
index === options.length - 1 ? 'rounded-r-lg' : ''
}`}
onClick={() => onChange(option)}
>
{labels[index]}
</button>
)
})}
</div>
)
}
interface Props {
playType: PlayType
player?: Color
timeControl?: TimeControl
maiaPartnerVersion?: string
maiaVersion?: string
isBrain?: boolean
sampleMoves?: boolean
simulateMaiaTime?: boolean
startFen?: string
}
export const PlaySetupModal: React.FC<Props> = (props: Props) => {
const { setPlaySetupModalProps } = useContext(ModalContext)
const { push } = useRouter()
const [timeControl, setTimeControl] = useState<TimeControl>(
props.timeControl || TimeControlOptions[0],
)
const [useCustomTime, setUseCustomTime] = useState<boolean>(false)
const [customMinutes, setCustomMinutes] = useState<number>(10)
const [customIncrement, setCustomIncrement] = useState<number>(0)
const [isBrain, setIsBrain] = useState<boolean>(props.isBrain || false)
const [sampleMoves, setSampleMoves] = useState<boolean>(
props.sampleMoves || true,
)
const [simulateMaiaTime, setSimulateMaiaTime] = useState<boolean>(
props.simulateMaiaTime !== undefined ? props.simulateMaiaTime : true,
)
const [maiaPartnerVersion, setMaiaPartnerVersion] = useState<string>(
props.maiaPartnerVersion || maiaOptions[0],
)
const [maiaVersion, setMaiaVersion] = useState<string>(
props.maiaVersion || maiaOptions[0],
)
const [fen, setFen] = useState<string | undefined>(
props.startFen ? props.startFen : undefined,
)
const [openMoreOptions, setMoreOptionsOpen] = useState<boolean>(true)
// Helper function to get the effective time control
const getEffectiveTimeControl = (): TimeControl => {
if (useCustomTime) {
return customToPresetTimeControl(customMinutes, customIncrement)
}
return timeControl
}
const start = useCallback(
(color: Color | undefined) => {
const player = color ?? ['white', 'black'][Math.floor(Math.random() * 2)]
const effectiveTimeControl = getEffectiveTimeControl()
if (fen && !new Chess().validateFen(fen).valid) {
toast.error('Invalid Starting FEN provided')
return
}
setPlaySetupModalProps(undefined)
if (props.playType == 'againstMaia') {
push({
pathname: '/play/maia',
query: {
player: player,
//maiaPartnerVersion: maiaPartnerVersion,
maiaVersion: maiaVersion,
timeControl: effectiveTimeControl,
sampleMoves: sampleMoves,
simulateMaiaTime: simulateMaiaTime,
startFen: fen,
},
})
} else {
push({
pathname: '/play/hb',
query: {
player: player,
maiaPartnerVersion: maiaPartnerVersion,
maiaVersion: maiaVersion,
timeControl: effectiveTimeControl,
isBrain: isBrain,
sampleMoves: sampleMoves,
simulateMaiaTime: simulateMaiaTime,
startFen: fen,
},
})
}
},
[
setPlaySetupModalProps,
props.playType,
push,
maiaPartnerVersion,
maiaVersion,
getEffectiveTimeControl,
sampleMoves,
simulateMaiaTime,
fen,
isBrain,
],
)
return (
<AnimatePresence>
<ModalContainer dismiss={() => setPlaySetupModalProps(undefined)}>
<div className="relative flex h-[600px] w-[500px] max-w-[90vw] flex-col overflow-hidden rounded-lg bg-background-1">
<button
className="absolute right-4 top-4 z-10 text-secondary transition-colors hover:text-primary"
title="Close"
onClick={() => setPlaySetupModalProps(undefined)}
>
<span className="material-symbols-outlined">close</span>
</button>
{/* Header */}
<div className="border-b border-white/10 p-4">
<h2 className="text-xl font-bold text-primary">
{props.playType == 'againstMaia'
? 'Play Against Maia'
: 'Play Hand and Brain'}
</h2>
<p className="text-xs text-secondary">
{props.playType == 'againstMaia'
? 'Configure your game settings and choose your side'
: 'Team up with Maia in Hand and Brain chess'}
</p>
</div>
{/* Settings Section */}
<div className="flex-1 overflow-y-auto p-4">
<div className="space-y-4">
{props.playType == 'handAndBrain' ? (
<>
<div>
<label
htmlFor="play-as-select"
className="mb-1 block text-sm font-medium text-primary"
>
Play as:
</label>
<div id="play-as-select">
<OptionSelect
options={[false, true]}
labels={['Hand', 'Brain']}
selected={isBrain}
onChange={setIsBrain}
/>
</div>
</div>
<div>
<label
htmlFor="partner-select"
className="mb-1 block text-sm font-medium text-primary"
>
Partner:
</label>
<select
id="partner-select"
value={maiaPartnerVersion}
className="w-full rounded bg-background-2 px-3 py-2 text-sm text-primary focus:outline-none focus:ring-1 focus:ring-human-4"
onChange={(e) => setMaiaPartnerVersion(e.target.value)}
>
{maiaOptions.map((maia) => (
<option key={`partner_${maia}`} value={maia}>
{maia.replace('maia_kdd_', 'Maia ')}
</option>
))}
</select>
</div>
</>
) : null}
<div>
<label
htmlFor="opponent-select"
className="mb-1 block text-sm font-medium text-primary"
>
Opponent:
</label>
<select
id="opponent-select"
value={maiaVersion}
className="w-full rounded bg-background-2 px-3 py-2 text-sm text-primary focus:outline-none focus:ring-1 focus:ring-human-4"
onChange={(e) => setMaiaVersion(e.target.value)}
>
{maiaOptions.map((maia) => (
<option key={`opponent_${maia}`} value={maia}>
{maia.replace('maia_kdd_', 'Maia ')}
</option>
))}
</select>
</div>
<div>
<label
htmlFor="time-control-select"
className="mb-2 block text-sm font-medium text-primary"
>
Time control:
</label>
{/* Toggle between preset and custom */}
<div className="mb-3">
<OptionSelect
options={[false, true]}
labels={['Preset', 'Custom']}
selected={useCustomTime}
onChange={setUseCustomTime}
/>
</div>
{useCustomTime ? (
/* Custom time controls with sliders */
<div className="space-y-3 rounded bg-background-2 p-3">
<Slider
id="time-slider"
label="Time per side"
value={customMinutes}
min={1}
max={180}
step={1}
unit=" min"
onChange={setCustomMinutes}
/>
<Slider
id="increment-slider"
label="Increment per move"
value={customIncrement}
min={0}
max={60}
step={1}
unit=" sec"
onChange={setCustomIncrement}
/>
<div className="mt-2 text-center">
<span className="text-xs text-secondary">
Time control: {customMinutes}+{customIncrement}
</span>
</div>
</div>
) : (
/* Preset time controls */
<div id="time-control-select">
<OptionSelect
options={TimeControlOptions}
labels={TimeControlOptionNames}
selected={timeControl}
onChange={setTimeControl}
/>
</div>
)}
</div>
<div>
<label
htmlFor="maia-timing-select"
className="mb-1 block text-sm font-medium text-primary"
>
Maia thinking time:
</label>
<div id="maia-timing-select">
<OptionSelect
options={[false, true]}
labels={['Instant', 'Human-like']}
selected={simulateMaiaTime}
onChange={setSimulateMaiaTime}
/>
</div>
</div>
<div className="flex items-center gap-2">
<input
type="checkbox"
id="customPosition"
checked={fen !== undefined}
onChange={() => setFen(fen === undefined ? '' : undefined)}
className="accent-human-4"
/>
<label
htmlFor="customPosition"
className="text-sm text-primary"
>
Start from custom position
</label>
</div>
{fen !== undefined && (
<div className="rounded bg-background-2 p-3">
<label
htmlFor="fen-input"
className="mb-1 block text-sm font-medium text-primary"
>
Starting FEN position:
</label>
<input
id="fen-input"
type="text"
value={fen}
placeholder="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
onChange={(e) => setFen(e.target.value)}
className="w-full rounded border border-background-3 bg-background-1 px-3 py-2 font-mono text-xs text-primary placeholder-secondary focus:border-human-4 focus:outline-none"
/>
<p className="mt-1 text-xs text-secondary">
Enter a valid FEN string to start from a specific position
</p>
</div>
)}
</div>
</div>
{/* Color Selection Section */}
<div className="border-t border-white/10 p-4">
<p className="mb-3 text-center text-sm font-medium text-primary">
Choose your color:
</p>
<div className="flex items-center justify-center gap-3">
<button
onClick={() => start('black')}
title="Play as black"
className="flex h-16 w-16 cursor-pointer items-center justify-center rounded bg-background-2 transition-colors hover:bg-human-4"
>
<div className="relative h-10 w-10">
<Image
src="/assets/pieces/black king.svg"
fill={true}
alt="Play as black"
/>
</div>
</button>
<button
onClick={() => start(undefined)}
title="Play as random color"
className="flex h-20 w-20 cursor-pointer items-center justify-center rounded bg-background-2 transition-colors hover:bg-human-4"
>
<div className="relative h-12 w-12">
<Image
alt="Play as random color"
fill={true}
src="/assets/pieces/white black king.svg"
/>
</div>
</button>
<button
onClick={() => start('white')}
title="Play as white"
className="flex h-16 w-16 cursor-pointer items-center justify-center rounded bg-background-2 transition-colors hover:bg-human-4"
>
<div className="relative h-10 w-10">
<Image
src="/assets/pieces/white king.svg"
fill={true}
alt="Play as white"
/>
</div>
</button>
</div>
</div>
</div>
</ModalContainer>
</AnimatePresence>
)
}