11import { Table } from '@/ps/games/render' ;
22import { Button , Form } from '@/utils/components/ps' ;
33
4- import type { RenderCtx } from '@/ps/games/chess/types' ;
4+ import type { RenderCtx , Turn } from '@/ps/games/chess/types' ;
55import type { CellRenderer } from '@/ps/games/render' ;
66import type { Chess , Square } from 'chess.js' ;
77import type { ReactElement } from 'react' ;
88
99type This = { msg : string } ;
1010
11- function getSquare ( x : number , y : number ) : Square {
12- return ( ( y + 1 ) . toLetter ( ) . toLowerCase ( ) + ( 8 - x ) ) as Square ;
11+ function getSquare ( x : number , y : number , side : Turn | null ) : Square {
12+ const flip = side === 'B' ;
13+ return ( ( flip ? 8 - y : y + 1 ) . toLetter ( ) . toLowerCase ( ) + ( flip ? x + 1 : 8 - x ) ) as Square ;
1314}
1415
1516type BoardCell = ReturnType < Chess [ 'board' ] > [ number ] [ number ] ;
@@ -29,11 +30,16 @@ const PIECE_IMAGES: Record<string, string> = {
2930 bp : 'https://partbot.partman.dev/public/chess/BP.png' ,
3031} ;
3132
33+ function adaptBoard ( board : BoardCell [ ] [ ] , side : Turn | null ) : BoardCell [ ] [ ] {
34+ if ( side !== 'B' ) return board ;
35+ return board . map ( row => row . toReversed ( ) ) . reverse ( ) ;
36+ }
37+
3238export function renderBoard ( this : This , ctx : RenderCtx ) {
3339 const size = ctx . small ? 30 : 45 ;
3440
3541 const Cell : CellRenderer < BoardCell > = ( { cell, i, j } ) => {
36- const square = getSquare ( i , j ) ;
42+ const square = getSquare ( i , j , ctx . side ) ;
3743 const action = ctx . showMoves . find ( move => move . to === square ) ;
3844 // Use the form during promotions instead
3945 const clickable =
@@ -64,7 +70,7 @@ export function renderBoard(this: This, ctx: RenderCtx) {
6470 ) ;
6571 } ;
6672
67- return < Table < BoardCell > board = { ctx . board } labels = { { row : '9-1' , col : 'A-Z' } } Cell = { Cell } /> ;
73+ return < Table < BoardCell > board = { adaptBoard ( ctx . board , ctx . side ) } labels = { { row : '9-1' , col : 'A-Z' } } Cell = { Cell } /> ;
6874}
6975
7076export function render ( this : This , ctx : RenderCtx ) : ReactElement {
0 commit comments