Skip to content

Commit 6208682

Browse files
committed
Track timeSettings state
1 parent 0ef3d5c commit 6208682

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,14 @@ The state of an engine is represented by an object of the following structure:
287287

288288
```js
289289
{
290-
komi: <number | null>,
291-
boardsize: <[number, number] | null>,
292-
history: <Command[] | null>
290+
komi: <number>,
291+
boardsize: <[number, number]>,
292+
timeSettings: {
293+
mainTime: <number>,
294+
byoyomiTime: <number>,
295+
byoyomiStones: <number>
296+
},
297+
history: <Command[]>
293298
}
294299
```
295300

src/ControllerStateTracker.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const commandEquals = (cmd1, cmd2) =>
1212
const getDefaultState = () => ({
1313
komi: null,
1414
boardsize: null,
15+
timeSettings: null,
1516
history: []
1617
})
1718

@@ -63,6 +64,15 @@ class ControllerStateTracker {
6364
this.state.history = []
6465
} else if (command.name === 'komi' && command.args.length >= 1) {
6566
this.state.komi = +command.args[0]
67+
} else if (
68+
command.name === 'time_settings' &&
69+
command.args.length >= 3
70+
) {
71+
this.state.timeSettings = {
72+
mainTime: +command.args[0],
73+
byoyomiTime: +command.args[1],
74+
byoyomiStones: +command.args[2]
75+
}
6676
} else if (
6777
['fixed_handicap', 'place_free_handicap'].includes(command.name)
6878
) {
@@ -208,6 +218,28 @@ class ControllerStateTracker {
208218
throw new Error('Board size is not supported by engine')
209219
}
210220

221+
// Update timeSettings
222+
223+
if (
224+
state.timeSettings != null &&
225+
(this.state.timeSettings == null ||
226+
state.timeSettings.mainTime !== this.state.timeSettings.mainTime ||
227+
state.timeSettings.byoyomiTime !==
228+
this.state.timeSettings.byoyomiTime ||
229+
state.timeSettings.byoyomiStones !==
230+
this.state.timeSettings.byoyomiStones)
231+
) {
232+
let {error} = await controller.sendCommand({
233+
name: 'time_settings',
234+
args: [
235+
`${state.timeSettings.mainTime}`,
236+
`${state.timeSettings.byoyomiTime}`,
237+
`${state.timeSettings.byoyomiStones}`
238+
]
239+
})
240+
if (error) throw new Error('Time settings are not supported by engine')
241+
}
242+
211243
// Update history
212244

213245
if (state.history != null) {

tests/ControllerStateTracker.test.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ t.test('knowsCommand', async t => {
3131
t.equals(await stateTracker.knowsCommand('version'), true)
3232
})
3333

34-
t.test('parallel syncing', async t => {
34+
t.test('handle parallel syncing sequentially', async t => {
3535
let {stateTracker} = t.context
3636

3737
await Promise.all([
@@ -42,6 +42,7 @@ t.test('parallel syncing', async t => {
4242
t.strictDeepEquals(stateTracker.state, {
4343
komi: 8,
4444
boardsize: [18, 18],
45+
timeSettings: null,
4546
history: []
4647
})
4748
})
@@ -109,6 +110,28 @@ t.test('sync rectangular boardsize state', async t => {
109110
t.equals(stateTracker.state.history, null)
110111
})
111112

113+
t.test('sync time settings', async t => {
114+
let {stateTracker} = t.context
115+
116+
t.equals(stateTracker.state.timeSettings, null)
117+
118+
let timeSettings = {mainTime: 30, byoyomiTime: 10, byoyomiStones: 20}
119+
await stateTracker.sync({timeSettings})
120+
121+
t.strictDeepEquals(stateTracker.state.timeSettings, timeSettings)
122+
123+
await stateTracker.controller.sendCommand({
124+
name: 'time_settings',
125+
args: ['15', '1', '10']
126+
})
127+
128+
t.strictDeepEquals(stateTracker.state.timeSettings, {
129+
mainTime: 15,
130+
byoyomiTime: 1,
131+
byoyomiStones: 10
132+
})
133+
})
134+
112135
t.test('sync history state', async t => {
113136
t.test('sync playing commands', async t => {
114137
let {stateTracker} = t.context

tests/engines/testEngine.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ for (let commandName of [
2121
'boardsize',
2222
'rectangular_boardsize',
2323
'komi',
24+
'time_settings',
2425
'set_free_handicap',
2526
'loadsgf'
2627
]) {

0 commit comments

Comments
 (0)