Skip to content

Commit

Permalink
feat: exponential backoff and jitter to required play requests
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinjosethomas committed Nov 25, 2024
1 parent 84b4586 commit 28df5cb
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 53 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"chess.ts": "^0.16.2",
"chroma-js": "^2.4.2",
"classnames": "^2.3.1",
"exponential-backoff": "^3.1.1",
"framer-motion": "^11.5.6",
"gray-matter": "^4.0.3",
"lila-stockfish-web": "^0.0.7",
Expand Down
102 changes: 64 additions & 38 deletions src/pages/play/hb.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import { NextPage } from 'next/types'
import { usePlayController } from 'src/hooks/usePlayController'
import { GameplayInterface } from 'src/components/GameplayInterface'
import { PieceSymbol } from 'chess.ts'
import { useRouter } from 'next/router'
import type { Key } from 'chessground/types'
import { backOff } from 'exponential-backoff'
import type { DrawShape } from 'chessground/draw'
import { useCallback, useContext, useEffect, useMemo, useState } from 'react'

import {
getGameMove,
getPlayPlayerStats,
startGame,
getGameMove,
submitGameMove,
getPlayPlayerStats,
} from 'src/api'
import { PlayControllerContext } from 'src/contexts/PlayControllerContext/PlayControllerContext'
import { useCallback, useContext, useEffect, useMemo, useState } from 'react'
import type { DrawShape } from 'chessground/draw'
import type { Key } from 'chessground/types'
import { HandBrainPlayControls } from 'src/components/HandBrainPlayControls'
import { PieceSymbol } from 'chess.ts'
import { Color, PlayGameConfig, TimeControl } from 'src/types'
import { useRouter } from 'next/router'
import { Loading } from 'src/components'
import { useStats } from 'src/hooks/useStats'
import { ModalContext } from 'src/contexts'
import { useStats } from 'src/hooks/useStats'
import { Color, PlayGameConfig, TimeControl } from 'src/types'
import { usePlayController } from 'src/hooks/usePlayController'
import { GameplayInterface } from 'src/components/GameplayInterface'
import { HandBrainPlayControls } from 'src/components/HandBrainPlayControls'
import { PlayControllerContext } from 'src/contexts/PlayControllerContext/PlayControllerContext'

const brainStatsLoader = async () => {
const stats = await getPlayPlayerStats()
Expand Down Expand Up @@ -79,10 +81,16 @@ const useHandBrainPlayController = (
let canceled = false

const maiaChoosePiece = async () => {
const maiaMoves = await getGameMove(
controller.moves,
playGameConfig.maiaPartnerVersion,
playGameConfig.startFen,
const maiaMoves = await backOff(
() =>
getGameMove(
controller.moves,
playGameConfig.maiaPartnerVersion,
playGameConfig.startFen,
),
{
jitter: 'full',
},
)
const nextMove = maiaMoves['top_move']

Expand Down Expand Up @@ -134,13 +142,19 @@ const useHandBrainPlayController = (
? parseInt(controller.timeControl.split('+')[0]) * 60
: 0

const maiaMoves = await getGameMove(
controller.moves,
playGameConfig.maiaVersion,
playGameConfig.startFen,
null,
playGameConfig.simulateMaiaTime ? initialClock : 0,
playGameConfig.simulateMaiaTime ? maiaClock : 0,
const maiaMoves = await backOff(
() =>
getGameMove(
controller.moves,
playGameConfig.maiaVersion,
playGameConfig.startFen,
null,
playGameConfig.simulateMaiaTime ? initialClock : 0,
playGameConfig.simulateMaiaTime ? maiaClock : 0,
),
{
jitter: 'full',
},
)
const nextMove = maiaMoves['top_move']
const moveDelay = maiaMoves['move_delay']
Expand Down Expand Up @@ -183,11 +197,17 @@ const useHandBrainPlayController = (
setSelectedPiece(piece)
setBrainMoves([...brainMoves, piece])

const maiaMoves = await getGameMove(
controller.moves,
playGameConfig.maiaPartnerVersion,
playGameConfig.startFen,
piece,
const maiaMoves = await backOff(
() =>
getGameMove(
controller.moves,
playGameConfig.maiaPartnerVersion,
playGameConfig.startFen,
piece,
),
{
jitter: 'full',
},
)
const nextMove = maiaMoves['top_move']
makeMove(nextMove)
Expand Down Expand Up @@ -236,15 +256,21 @@ const useHandBrainPlayController = (
const winner = controller.game.termination?.winner

const submitFn = async () => {
await submitGameMove(
controller.game.id,
controller.moves,
controller.moveTimes,
gameOverState,
playGameConfig.isBrain ? 'brain' : 'hand',
playGameConfig.startFen || undefined,
winner,
brainMoves,
await backOff(
() =>
submitGameMove(
controller.game.id,
controller.moves,
controller.moveTimes,
gameOverState,
playGameConfig.isBrain ? 'brain' : 'hand',
playGameConfig.startFen || undefined,
winner,
brainMoves,
),
{
jitter: 'full',
},
)
if (controller.game.termination) {
const winner = controller.game.termination?.winner
Expand Down
43 changes: 28 additions & 15 deletions src/pages/play/maia.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { NextPage } from 'next/types'
import { useRouter } from 'next/router'
import { backOff } from 'exponential-backoff'
import { useContext, useEffect, useMemo } from 'react'

import {
Expand Down Expand Up @@ -59,13 +60,19 @@ const useVsMaiaPlayController = (
? parseInt(controller.timeControl.split('+')[0]) * 60
: 0

const maiaMoves = await getGameMove(
controller.moves,
playGameConfig.maiaVersion,
playGameConfig.startFen,
null,
playGameConfig.simulateMaiaTime ? initialClock : 0,
playGameConfig.simulateMaiaTime ? maiaClock : 0,
const maiaMoves = await backOff(
() =>
getGameMove(
controller.moves,
playGameConfig.maiaVersion,
playGameConfig.startFen,
null,
playGameConfig.simulateMaiaTime ? initialClock : 0,
playGameConfig.simulateMaiaTime ? maiaClock : 0,
),
{
jitter: 'full',
},
)
const nextMove = maiaMoves['top_move']
const moveDelay = maiaMoves['move_delay']
Expand Down Expand Up @@ -106,14 +113,20 @@ const useVsMaiaPlayController = (
const winner = controller.game.termination?.winner

const submitFn = async () => {
await submitGameMove(
controller.game.id,
controller.moves,
controller.moveTimes,
gameOverState,
'play',
playGameConfig.startFen || undefined,
winner,
await backOff(
() =>
submitGameMove(
controller.game.id,
controller.moves,
controller.moveTimes,
gameOverState,
'play',
playGameConfig.startFen || undefined,
winner,
),
{
jitter: 'full',
},
)

// Only update stats after final move submitted
Expand Down

0 comments on commit 28df5cb

Please sign in to comment.