Skip to content

Commit

Permalink
feat: migrate from original -> legacy filenames, and refactor client …
Browse files Browse the repository at this point in the history
…-> original
  • Loading branch information
kevinjosethomas committed Jan 22, 2025
1 parent 31bbc4a commit bf921af
Show file tree
Hide file tree
Showing 18 changed files with 1,379 additions and 1,403 deletions.
316 changes: 309 additions & 7 deletions src/api/analysis/analysis.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import {
Player,
MoveMap,
AnalyzedGame,
MaiaEvaluation,
ClientAnalyzedGame,
LegacyAnalyzedGame,
PositionEvaluation,
StockfishEvaluation,
AnalysisTournamentGame,
} from 'src/types'
import { buildUrl } from '../utils'
Expand Down Expand Up @@ -85,7 +89,9 @@ export const getLichessGamePGN = async (id: string) => {
return res.text()
}

export const getAnalyzedTournamentGame = async (gameId = ['FkgYSri1']) => {
export const getLegacyAnalyzedTournamentGame = async (
gameId = ['FkgYSri1'],
) => {
const res = await fetch(
buildUrl(`analysis/analysis_list/${gameId.join('/')}`),
)
Expand Down Expand Up @@ -169,10 +175,10 @@ export const getAnalyzedTournamentGame = async (gameId = ['FkgYSri1']) => {
gameType,
termination,
positionEvaluations,
} as any as AnalyzedGame
} as any as LegacyAnalyzedGame
}

export const getAnalyzedLichessGame = async (
export const getLegacyAnalyzedLichessGame = async (
id: string,
pgn: string,
maia_model = 'maia_kdd_1500',
Expand Down Expand Up @@ -270,10 +276,10 @@ export const getAnalyzedLichessGame = async (
termination,
positionEvaluations,
pgn,
} as AnalyzedGame
} as LegacyAnalyzedGame
}

export const getAnalyzedUserGame = async (
export const getLegacyAnalyzedUserGame = async (
id: string,
game_type: 'play' | 'hand' | 'brain',
maia_model = 'maia_kdd_1500',
Expand Down Expand Up @@ -380,5 +386,301 @@ export const getAnalyzedUserGame = async (
gameType,
termination,
positionEvaluations,
} as AnalyzedGame
} as LegacyAnalyzedGame
}

export const getClientAnalyzedTournamentGame = async (
gameId = ['FkgYSri1'],
) => {
const res = await fetch(
buildUrl(`analysis/analysis_list/${gameId.join('/')}`),
)

if (res.status === 401) {
throw new Error('Unauthorized')
}

const data = await res.json()
const id = data['id']
const termination = {
...data['termination'],
condition: 'Normal',
}

const gameType = 'blitz'
const blackPlayer = data['black_player'] as Player
const whitePlayer = data['white_player'] as Player

const maiaEvals: { [model: string]: MoveMap[] } = {}
const stockfishEvaluations: MoveMap[] = data['stockfish_evals']

const availableMoves: AvailableMoves[] = []

for (const model of data['maia_versions']) {
maiaEvals[model] = data['maia_evals'][model]
}

for (const position of data['move_maps']) {
const moves: AvailableMoves = {}
for (const move of position) {
const fromTo = move.move.join('')
const san = move['move_san']
const { check, fen } = move

moves[fromTo] = {
board: fen,
check,
san,
lastMove: move.move,
}
}
availableMoves.push(moves)
}

const gameStates = data['game_states']

const moves = gameStates.map((gameState: any) => {
const {
last_move: lastMove,
fen,
check,
last_move_san: san,
evaluations: maia_values,
} = gameState

return {
board: fen,
lastMove,
san,
check,
maia_values,
}
})

const maiaEvaluations = [] as { [rating: number]: MaiaEvaluation }[]

return {
id,
blackPlayer,
whitePlayer,
moves,
maiaEvaluations,
stockfishEvaluations,
availableMoves,
gameType,
termination,
} as any as ClientAnalyzedGame
}

export const getClientAnalyzedLichessGame = async (
id: string,
pgn: string,
maia_model = 'maia_kdd_1500',
) => {
const res = await fetch(
buildUrl(
'analysis/analyze_user_game?' +
new URLSearchParams({
maia_model,
}),
),
{
method: 'POST',
body: pgn,
headers: {
'Content-Type': 'text/plain',
},
},
)

if (res.status === 401) {
throw new Error('Unauthorized')
}

const data = await res.json()

const termination = {
...data['termination'],
condition: 'Normal',
}

const gameType = 'blitz'
const blackPlayer = data['black_player'] as Player
const whitePlayer = data['white_player'] as Player

const maiaEvals: { [model: string]: MoveMap[] } = {}
const positionEvaluations: { [model: string]: PositionEvaluation[] } = {}
const availableMoves: AvailableMoves[] = []

for (const model of data['maia_versions']) {
maiaEvals[model] = data['maia_evals'][model]
positionEvaluations[model] = Object.keys(data['maia_evals'][model]).map(
() => ({
trickiness: 1,
performance: 1,
}),
)
}

for (const position of data['move_maps']) {
const moves: AvailableMoves = {}
for (const move of position) {
const fromTo = move.move.join('')
const san = move['move_san']
const { check, fen } = move

moves[fromTo] = {
board: fen,
check,
san,
lastMove: move.move,
}
}
availableMoves.push(moves)
}

const gameStates = data['game_states']

const moves = gameStates.map((gameState: any) => {
const {
last_move: lastMove,
fen,
check,
last_move_san: san,
evaluations: maia_values,
} = gameState

return {
board: fen,
lastMove,
san,
check,
maia_values,
}
})

const maiaEvaluations = [] as { [rating: number]: MaiaEvaluation }[]
const stockfishEvaluations: StockfishEvaluation[] = []

return {
id,
blackPlayer,
whitePlayer,
moves,
availableMoves,
gameType,
termination,
maiaEvaluations,
stockfishEvaluations,
type: 'brain',
pgn,
} as ClientAnalyzedGame
}

export const getClientAnalyzedUserGame = async (
id: string,
game_type: 'play' | 'hand' | 'brain',
maia_model = 'maia_kdd_1500',
) => {
const res = await fetch(
buildUrl(
`analysis/user/analyze_user_maia_game/${id}?` +
new URLSearchParams({
game_type,
maia_model,
}),
),
{
method: 'GET',
headers: {
'Content-Type': 'text/plain',
},
},
)

if (res.status === 401) {
throw new Error('Unauthorized')
}

const data = await res.json()

const termination = {
...data['termination'],
condition: 'Normal',
}

const gameType = 'blitz'
const blackPlayer = data['black_player'] as Player
const whitePlayer = data['white_player'] as Player

const maiaPattern = /maia_kdd_1\d00/

if (maiaPattern.test(blackPlayer.name)) {
blackPlayer.name = blackPlayer.name.replace('maia_kdd_', 'Maia ')
}

if (maiaPattern.test(whitePlayer.name)) {
whitePlayer.name = whitePlayer.name.replace('maia_kdd_', 'Maia ')
}

const maiaEvals: { [model: string]: MoveMap[] } = {}

const availableMoves: AvailableMoves[] = []

for (const model of data['maia_versions']) {
maiaEvals[model] = data['maia_evals'][model]
}

for (const position of data['move_maps']) {
const moves: AvailableMoves = {}
for (const move of position) {
const fromTo = move.move.join('')
const san = move['move_san']
const { check, fen } = move

moves[fromTo] = {
board: fen,
check,
san,
lastMove: move.move,
}
}
availableMoves.push(moves)
}

const gameStates = data['game_states']

const moves = gameStates.map((gameState: any) => {
const {
last_move: lastMove,
fen,
check,
last_move_san: san,
evaluations: maia_values,
} = gameState

return {
board: fen,
lastMove,
san,
check,
maia_values,
}
})

const maiaEvaluations = [] as { [rating: number]: MaiaEvaluation }[]
const stockfishEvaluations: StockfishEvaluation[] = []

return {
id,
blackPlayer,
whitePlayer,
moves,
availableMoves,
gameType,
termination,
maiaEvaluations,
stockfishEvaluations,
type: 'brain',
} as ClientAnalyzedGame
}
Loading

0 comments on commit bf921af

Please sign in to comment.