Skip to content

Commit 6473c9d

Browse files
Merge pull request #388 from CSSLab/claude/b1-unlimited-timing
Tell Maia the game's clocks, increment included (B1)
2 parents d27757b + 42626f0 commit 6473c9d

6 files changed

Lines changed: 103 additions & 44 deletions

File tree

‎src/api/play.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ describe('fetchGameMove', () => {
2323
})
2424
})
2525

26+
it('sends the clocks, increment included', async () => {
27+
respond(200, { top_move: 'e2e4', move_delay: 0.4 })
28+
await fetchGameMove(['e2e4'], 'maia_kdd_1500', null, null, 600, 412.5, 5)
29+
const url = new URL(
30+
(global.fetch as jest.Mock).mock.calls[0][0],
31+
'https://example.com',
32+
)
33+
expect(Object.fromEntries(url.searchParams)).toEqual({
34+
maia_name: 'maia_kdd_1500',
35+
initial_clock: '600',
36+
increment: '5',
37+
current_clock: '412.5',
38+
maia_version: 'maia3',
39+
})
40+
})
41+
2642
it('throws, with the status, when the server rejects the request', async () => {
2743
respond(400, { detail: 'Illegal move order given' })
2844
const error = await fetchGameMove(['e2e5']).catch((e) => e)

‎src/api/play.ts‎

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -123,44 +123,19 @@ export const fetchGameMove = async (
123123
piece: string | null = null,
124124
initial_clock = 0,
125125
current_clock = 0,
126+
increment = 0,
126127
) => {
128+
const params: Record<string, string> = {
129+
maia_name: maiaVersion,
130+
initial_clock: initial_clock.toString(),
131+
increment: increment.toString(),
132+
current_clock: current_clock.toString(),
133+
maia_version: 'maia3',
134+
}
135+
if (fen) params.fen = fen
136+
if (piece) params.piece = piece
127137
const res = await fetch(
128-
buildUrl(
129-
'play/get_move?' +
130-
new URLSearchParams(
131-
piece
132-
? fen
133-
? {
134-
fen: fen || '',
135-
maia_name: maiaVersion,
136-
piece: piece || '',
137-
initial_clock: initial_clock.toString(),
138-
current_clock: current_clock.toString(),
139-
maia_version: 'maia3',
140-
}
141-
: {
142-
maia_name: maiaVersion,
143-
piece: piece || '',
144-
initial_clock: initial_clock.toString(),
145-
current_clock: current_clock.toString(),
146-
maia_version: 'maia3',
147-
}
148-
: fen
149-
? {
150-
fen: fen || '',
151-
maia_name: maiaVersion,
152-
initial_clock: initial_clock.toString(),
153-
current_clock: current_clock.toString(),
154-
maia_version: 'maia3',
155-
}
156-
: {
157-
maia_name: maiaVersion,
158-
initial_clock: initial_clock.toString(),
159-
current_clock: current_clock.toString(),
160-
maia_version: 'maia3',
161-
},
162-
),
163-
),
138+
buildUrl('play/get_move?' + new URLSearchParams(params)),
164139
{
165140
method: 'POST',
166141
headers: {

‎src/hooks/usePlayController/useHandBrainController.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
} from 'src/components/Common/maiaMoveFailedToast'
1515
import { useSound } from 'src/hooks/useSound'
1616
import { safeUpdateRating } from 'src/lib/ratingUtils'
17-
import { initialClockSeconds } from 'src/lib/timeControl'
17+
import { maiaMoveClocks } from 'src/lib/timeControl'
1818
import { useGameLog } from './useGameLog'
1919

2020
const brainStatsLoader = async () => {
@@ -151,7 +151,7 @@ export const useHandBrainController = (
151151
(controller.player == 'white'
152152
? controller.blackClock
153153
: controller.whiteClock) / 1000
154-
const initialClock = initialClockSeconds(controller.timeControl)
154+
const clocks = maiaMoveClocks(controller.timeControl, maiaClock)
155155

156156
let maiaMoves
157157
try {
@@ -160,8 +160,9 @@ export const useHandBrainController = (
160160
playGameConfig.maiaVersion,
161161
playGameConfig.startFen,
162162
null,
163-
simulateMaiaTime ? initialClock : 0,
164-
simulateMaiaTime ? maiaClock : 0,
163+
clocks.initialClock,
164+
clocks.currentClock,
165+
clocks.increment,
165166
)
166167
} catch (error) {
167168
if (!canceled) reportMaiaMoveFailed(error, retryMaia)

‎src/hooks/usePlayController/useVsMaiaController.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from 'src/components/Common/maiaMoveFailedToast'
1212
import { useSound } from 'src/hooks/useSound'
1313
import { safeUpdateRating } from 'src/lib/ratingUtils'
14-
import { initialClockSeconds } from 'src/lib/timeControl'
14+
import { maiaMoveClocks } from 'src/lib/timeControl'
1515
import { useGameLog } from './useGameLog'
1616
import {
1717
UnfinishedGame,
@@ -111,7 +111,7 @@ export const useVsMaiaPlayController = (
111111
(controller.player == 'white'
112112
? controller.blackClock
113113
: controller.whiteClock) / 1000
114-
const initialClock = initialClockSeconds(controller.timeControl)
114+
const clocks = maiaMoveClocks(controller.timeControl, maiaClock)
115115

116116
let maiaMoves
117117
try {
@@ -120,8 +120,9 @@ export const useVsMaiaPlayController = (
120120
playGameConfig.maiaVersion,
121121
playGameConfig.startFen,
122122
null,
123-
simulateMaiaTime ? initialClock : 0,
124-
simulateMaiaTime ? maiaClock : 0,
123+
clocks.initialClock,
124+
clocks.currentClock,
125+
clocks.increment,
125126
)
126127
} catch (error) {
127128
if (canceled) return

‎src/lib/timeControl.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import {
22
INCREMENT_SECONDS_STEPS,
33
TIME_MINUTES_STEPS,
4+
incrementSeconds,
45
initialClockSeconds,
6+
maiaMoveClocks,
57
nearestStepIndex,
68
pgnTimeControl,
79
normalizeTimeControl,
@@ -55,3 +57,31 @@ describe('pgnTimeControl', () => {
5557
expect(pgnTimeControl('unlimited')).toBe('-')
5658
})
5759
})
60+
61+
describe('incrementSeconds', () => {
62+
it('reads the increment, and 0 for unlimited', () => {
63+
expect(incrementSeconds('10+5')).toBe(5)
64+
expect(incrementSeconds('0+2')).toBe(2)
65+
expect(incrementSeconds('3+0')).toBe(0)
66+
expect(incrementSeconds('unlimited')).toBe(0)
67+
})
68+
})
69+
70+
describe('maiaMoveClocks', () => {
71+
it("sends the game's clocks, increment included", () => {
72+
expect(maiaMoveClocks('10+5', 412.5)).toEqual({
73+
initialClock: 600,
74+
increment: 5,
75+
currentClock: 412.5,
76+
})
77+
})
78+
79+
it('gives a game without clocks a 3-minute one, as the backend assumes', () => {
80+
// A clock of 0 is the backend's sign to move at once.
81+
expect(maiaMoveClocks('unlimited', 0)).toEqual({
82+
initialClock: 180,
83+
increment: 0,
84+
currentClock: 180,
85+
})
86+
})
87+
})

‎src/lib/timeControl.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,42 @@ export const initialClockSeconds = (timeControl: string): number => {
4545
return minutes === 0 ? ZERO_BASE_START_SECONDS : minutes * 60
4646
}
4747

48+
/** The increment in seconds (0 for unlimited). */
49+
export const incrementSeconds = (timeControl: string): number => {
50+
if (timeControl === 'unlimited' || !timeControl.includes('+')) return 0
51+
const seconds = parseInt(timeControl.split('+')[1], 10)
52+
return Number.isFinite(seconds) ? seconds : 0
53+
}
54+
55+
// What the backend tells Maia about a game without clocks: 3 minutes each.
56+
// Sent as a real clock, so that "Human-like" thinking time takes time there
57+
// too (no clock at all is the backend's sign to move at once).
58+
export const UNLIMITED_MAIA_CLOCK_SECONDS = 180
59+
60+
/**
61+
* The clocks sent with a request for Maia's move: the game's own, so that
62+
* Maia plays the time control being played, and spends time as it would.
63+
* With "Instant" thinking time, its reply's delay is not waited for.
64+
*/
65+
export const maiaMoveClocks = (
66+
timeControl: string,
67+
maiaClockSeconds: number,
68+
): { initialClock: number; increment: number; currentClock: number } => {
69+
const initialClock = initialClockSeconds(timeControl)
70+
if (initialClock === 0) {
71+
return {
72+
initialClock: UNLIMITED_MAIA_CLOCK_SECONDS,
73+
increment: 0,
74+
currentClock: UNLIMITED_MAIA_CLOCK_SECONDS,
75+
}
76+
}
77+
return {
78+
initialClock,
79+
increment: incrementSeconds(timeControl),
80+
currentClock: maiaClockSeconds,
81+
}
82+
}
83+
4884
/**
4985
* A time control as PGN's TimeControl header writes it: the starting time and
5086
* increment in seconds ("300+3"), or "-" for a game without clocks.

0 commit comments

Comments
 (0)