|
1 | 1 | /* eslint-disable @typescript-eslint/no-explicit-any */ |
2 | 2 | import { |
| 3 | + Player, |
3 | 4 | MoveMap, |
4 | | - AnalyzedGame, |
| 5 | + MaiaEvaluation, |
| 6 | + ClientAnalyzedGame, |
| 7 | + LegacyAnalyzedGame, |
5 | 8 | PositionEvaluation, |
| 9 | + StockfishEvaluation, |
6 | 10 | AnalysisTournamentGame, |
7 | 11 | } from 'src/types' |
8 | 12 | import { buildUrl } from '../utils' |
@@ -85,7 +89,9 @@ export const getLichessGamePGN = async (id: string) => { |
85 | 89 | return res.text() |
86 | 90 | } |
87 | 91 |
|
88 | | -export const getAnalyzedTournamentGame = async (gameId = ['FkgYSri1']) => { |
| 92 | +export const getLegacyAnalyzedTournamentGame = async ( |
| 93 | + gameId = ['FkgYSri1'], |
| 94 | +) => { |
89 | 95 | const res = await fetch( |
90 | 96 | buildUrl(`analysis/analysis_list/${gameId.join('/')}`), |
91 | 97 | ) |
@@ -169,10 +175,10 @@ export const getAnalyzedTournamentGame = async (gameId = ['FkgYSri1']) => { |
169 | 175 | gameType, |
170 | 176 | termination, |
171 | 177 | positionEvaluations, |
172 | | - } as any as AnalyzedGame |
| 178 | + } as any as LegacyAnalyzedGame |
173 | 179 | } |
174 | 180 |
|
175 | | -export const getAnalyzedLichessGame = async ( |
| 181 | +export const getLegacyAnalyzedLichessGame = async ( |
176 | 182 | id: string, |
177 | 183 | pgn: string, |
178 | 184 | maia_model = 'maia_kdd_1500', |
@@ -270,10 +276,10 @@ export const getAnalyzedLichessGame = async ( |
270 | 276 | termination, |
271 | 277 | positionEvaluations, |
272 | 278 | pgn, |
273 | | - } as AnalyzedGame |
| 279 | + } as LegacyAnalyzedGame |
274 | 280 | } |
275 | 281 |
|
276 | | -export const getAnalyzedUserGame = async ( |
| 282 | +export const getLegacyAnalyzedUserGame = async ( |
277 | 283 | id: string, |
278 | 284 | game_type: 'play' | 'hand' | 'brain', |
279 | 285 | maia_model = 'maia_kdd_1500', |
@@ -380,5 +386,301 @@ export const getAnalyzedUserGame = async ( |
380 | 386 | gameType, |
381 | 387 | termination, |
382 | 388 | positionEvaluations, |
383 | | - } as AnalyzedGame |
| 389 | + } as LegacyAnalyzedGame |
| 390 | +} |
| 391 | + |
| 392 | +export const getClientAnalyzedTournamentGame = async ( |
| 393 | + gameId = ['FkgYSri1'], |
| 394 | +) => { |
| 395 | + const res = await fetch( |
| 396 | + buildUrl(`analysis/analysis_list/${gameId.join('/')}`), |
| 397 | + ) |
| 398 | + |
| 399 | + if (res.status === 401) { |
| 400 | + throw new Error('Unauthorized') |
| 401 | + } |
| 402 | + |
| 403 | + const data = await res.json() |
| 404 | + const id = data['id'] |
| 405 | + const termination = { |
| 406 | + ...data['termination'], |
| 407 | + condition: 'Normal', |
| 408 | + } |
| 409 | + |
| 410 | + const gameType = 'blitz' |
| 411 | + const blackPlayer = data['black_player'] as Player |
| 412 | + const whitePlayer = data['white_player'] as Player |
| 413 | + |
| 414 | + const maiaEvals: { [model: string]: MoveMap[] } = {} |
| 415 | + const stockfishEvaluations: MoveMap[] = data['stockfish_evals'] |
| 416 | + |
| 417 | + const availableMoves: AvailableMoves[] = [] |
| 418 | + |
| 419 | + for (const model of data['maia_versions']) { |
| 420 | + maiaEvals[model] = data['maia_evals'][model] |
| 421 | + } |
| 422 | + |
| 423 | + for (const position of data['move_maps']) { |
| 424 | + const moves: AvailableMoves = {} |
| 425 | + for (const move of position) { |
| 426 | + const fromTo = move.move.join('') |
| 427 | + const san = move['move_san'] |
| 428 | + const { check, fen } = move |
| 429 | + |
| 430 | + moves[fromTo] = { |
| 431 | + board: fen, |
| 432 | + check, |
| 433 | + san, |
| 434 | + lastMove: move.move, |
| 435 | + } |
| 436 | + } |
| 437 | + availableMoves.push(moves) |
| 438 | + } |
| 439 | + |
| 440 | + const gameStates = data['game_states'] |
| 441 | + |
| 442 | + const moves = gameStates.map((gameState: any) => { |
| 443 | + const { |
| 444 | + last_move: lastMove, |
| 445 | + fen, |
| 446 | + check, |
| 447 | + last_move_san: san, |
| 448 | + evaluations: maia_values, |
| 449 | + } = gameState |
| 450 | + |
| 451 | + return { |
| 452 | + board: fen, |
| 453 | + lastMove, |
| 454 | + san, |
| 455 | + check, |
| 456 | + maia_values, |
| 457 | + } |
| 458 | + }) |
| 459 | + |
| 460 | + const maiaEvaluations = [] as { [rating: number]: MaiaEvaluation }[] |
| 461 | + |
| 462 | + return { |
| 463 | + id, |
| 464 | + blackPlayer, |
| 465 | + whitePlayer, |
| 466 | + moves, |
| 467 | + maiaEvaluations, |
| 468 | + stockfishEvaluations, |
| 469 | + availableMoves, |
| 470 | + gameType, |
| 471 | + termination, |
| 472 | + } as any as ClientAnalyzedGame |
| 473 | +} |
| 474 | + |
| 475 | +export const getClientAnalyzedLichessGame = async ( |
| 476 | + id: string, |
| 477 | + pgn: string, |
| 478 | + maia_model = 'maia_kdd_1500', |
| 479 | +) => { |
| 480 | + const res = await fetch( |
| 481 | + buildUrl( |
| 482 | + 'analysis/analyze_user_game?' + |
| 483 | + new URLSearchParams({ |
| 484 | + maia_model, |
| 485 | + }), |
| 486 | + ), |
| 487 | + { |
| 488 | + method: 'POST', |
| 489 | + body: pgn, |
| 490 | + headers: { |
| 491 | + 'Content-Type': 'text/plain', |
| 492 | + }, |
| 493 | + }, |
| 494 | + ) |
| 495 | + |
| 496 | + if (res.status === 401) { |
| 497 | + throw new Error('Unauthorized') |
| 498 | + } |
| 499 | + |
| 500 | + const data = await res.json() |
| 501 | + |
| 502 | + const termination = { |
| 503 | + ...data['termination'], |
| 504 | + condition: 'Normal', |
| 505 | + } |
| 506 | + |
| 507 | + const gameType = 'blitz' |
| 508 | + const blackPlayer = data['black_player'] as Player |
| 509 | + const whitePlayer = data['white_player'] as Player |
| 510 | + |
| 511 | + const maiaEvals: { [model: string]: MoveMap[] } = {} |
| 512 | + const positionEvaluations: { [model: string]: PositionEvaluation[] } = {} |
| 513 | + const availableMoves: AvailableMoves[] = [] |
| 514 | + |
| 515 | + for (const model of data['maia_versions']) { |
| 516 | + maiaEvals[model] = data['maia_evals'][model] |
| 517 | + positionEvaluations[model] = Object.keys(data['maia_evals'][model]).map( |
| 518 | + () => ({ |
| 519 | + trickiness: 1, |
| 520 | + performance: 1, |
| 521 | + }), |
| 522 | + ) |
| 523 | + } |
| 524 | + |
| 525 | + for (const position of data['move_maps']) { |
| 526 | + const moves: AvailableMoves = {} |
| 527 | + for (const move of position) { |
| 528 | + const fromTo = move.move.join('') |
| 529 | + const san = move['move_san'] |
| 530 | + const { check, fen } = move |
| 531 | + |
| 532 | + moves[fromTo] = { |
| 533 | + board: fen, |
| 534 | + check, |
| 535 | + san, |
| 536 | + lastMove: move.move, |
| 537 | + } |
| 538 | + } |
| 539 | + availableMoves.push(moves) |
| 540 | + } |
| 541 | + |
| 542 | + const gameStates = data['game_states'] |
| 543 | + |
| 544 | + const moves = gameStates.map((gameState: any) => { |
| 545 | + const { |
| 546 | + last_move: lastMove, |
| 547 | + fen, |
| 548 | + check, |
| 549 | + last_move_san: san, |
| 550 | + evaluations: maia_values, |
| 551 | + } = gameState |
| 552 | + |
| 553 | + return { |
| 554 | + board: fen, |
| 555 | + lastMove, |
| 556 | + san, |
| 557 | + check, |
| 558 | + maia_values, |
| 559 | + } |
| 560 | + }) |
| 561 | + |
| 562 | + const maiaEvaluations = [] as { [rating: number]: MaiaEvaluation }[] |
| 563 | + const stockfishEvaluations: StockfishEvaluation[] = [] |
| 564 | + |
| 565 | + return { |
| 566 | + id, |
| 567 | + blackPlayer, |
| 568 | + whitePlayer, |
| 569 | + moves, |
| 570 | + availableMoves, |
| 571 | + gameType, |
| 572 | + termination, |
| 573 | + maiaEvaluations, |
| 574 | + stockfishEvaluations, |
| 575 | + type: 'brain', |
| 576 | + pgn, |
| 577 | + } as ClientAnalyzedGame |
| 578 | +} |
| 579 | + |
| 580 | +export const getClientAnalyzedUserGame = async ( |
| 581 | + id: string, |
| 582 | + game_type: 'play' | 'hand' | 'brain', |
| 583 | + maia_model = 'maia_kdd_1500', |
| 584 | +) => { |
| 585 | + const res = await fetch( |
| 586 | + buildUrl( |
| 587 | + `analysis/user/analyze_user_maia_game/${id}?` + |
| 588 | + new URLSearchParams({ |
| 589 | + game_type, |
| 590 | + maia_model, |
| 591 | + }), |
| 592 | + ), |
| 593 | + { |
| 594 | + method: 'GET', |
| 595 | + headers: { |
| 596 | + 'Content-Type': 'text/plain', |
| 597 | + }, |
| 598 | + }, |
| 599 | + ) |
| 600 | + |
| 601 | + if (res.status === 401) { |
| 602 | + throw new Error('Unauthorized') |
| 603 | + } |
| 604 | + |
| 605 | + const data = await res.json() |
| 606 | + |
| 607 | + const termination = { |
| 608 | + ...data['termination'], |
| 609 | + condition: 'Normal', |
| 610 | + } |
| 611 | + |
| 612 | + const gameType = 'blitz' |
| 613 | + const blackPlayer = data['black_player'] as Player |
| 614 | + const whitePlayer = data['white_player'] as Player |
| 615 | + |
| 616 | + const maiaPattern = /maia_kdd_1\d00/ |
| 617 | + |
| 618 | + if (maiaPattern.test(blackPlayer.name)) { |
| 619 | + blackPlayer.name = blackPlayer.name.replace('maia_kdd_', 'Maia ') |
| 620 | + } |
| 621 | + |
| 622 | + if (maiaPattern.test(whitePlayer.name)) { |
| 623 | + whitePlayer.name = whitePlayer.name.replace('maia_kdd_', 'Maia ') |
| 624 | + } |
| 625 | + |
| 626 | + const maiaEvals: { [model: string]: MoveMap[] } = {} |
| 627 | + |
| 628 | + const availableMoves: AvailableMoves[] = [] |
| 629 | + |
| 630 | + for (const model of data['maia_versions']) { |
| 631 | + maiaEvals[model] = data['maia_evals'][model] |
| 632 | + } |
| 633 | + |
| 634 | + for (const position of data['move_maps']) { |
| 635 | + const moves: AvailableMoves = {} |
| 636 | + for (const move of position) { |
| 637 | + const fromTo = move.move.join('') |
| 638 | + const san = move['move_san'] |
| 639 | + const { check, fen } = move |
| 640 | + |
| 641 | + moves[fromTo] = { |
| 642 | + board: fen, |
| 643 | + check, |
| 644 | + san, |
| 645 | + lastMove: move.move, |
| 646 | + } |
| 647 | + } |
| 648 | + availableMoves.push(moves) |
| 649 | + } |
| 650 | + |
| 651 | + const gameStates = data['game_states'] |
| 652 | + |
| 653 | + const moves = gameStates.map((gameState: any) => { |
| 654 | + const { |
| 655 | + last_move: lastMove, |
| 656 | + fen, |
| 657 | + check, |
| 658 | + last_move_san: san, |
| 659 | + evaluations: maia_values, |
| 660 | + } = gameState |
| 661 | + |
| 662 | + return { |
| 663 | + board: fen, |
| 664 | + lastMove, |
| 665 | + san, |
| 666 | + check, |
| 667 | + maia_values, |
| 668 | + } |
| 669 | + }) |
| 670 | + |
| 671 | + const maiaEvaluations = [] as { [rating: number]: MaiaEvaluation }[] |
| 672 | + const stockfishEvaluations: StockfishEvaluation[] = [] |
| 673 | + |
| 674 | + return { |
| 675 | + id, |
| 676 | + blackPlayer, |
| 677 | + whitePlayer, |
| 678 | + moves, |
| 679 | + availableMoves, |
| 680 | + gameType, |
| 681 | + termination, |
| 682 | + maiaEvaluations, |
| 683 | + stockfishEvaluations, |
| 684 | + type: 'brain', |
| 685 | + } as ClientAnalyzedGame |
384 | 686 | } |
0 commit comments