commit 21c8b9a724a0de6a79e821ffb4c19555290bfbd5
parent 6f5554e3f2f6ea714060995ea1a6ae62227eef16
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Thu, 15 Jun 2023 00:12:31 -0700
render best move
Diffstat:
2 files changed, 25 insertions(+), 4 deletions(-)
diff --git a/pkg/web/handlers/chess.go b/pkg/web/handlers/chess.go
@@ -304,16 +304,20 @@ func ChessGameStatsHandler(c echo.Context) error {
</table>`
moveIdx, _ := strconv.Atoi(c.QueryParam("m"))
- interceptors.ChessPubSub.Pub(key, interceptors.ChessMove{MoveIdx: moveIdx})
const graphWidth = 800
var columnWidth = 1
var stats *interceptors.AnalyseResult
_ = json.Unmarshal(g.DbChessGame.Stats, &stats)
+ var bestMove string
if stats != nil {
if len(stats.Scores) > 0 {
columnWidth = utils.MaxInt(graphWidth/len(stats.Scores), 1)
+ if moveIdx > 0 {
+ bestMove = stats.Scores[moveIdx-1].BestMove
+ }
}
}
+ interceptors.ChessPubSub.Pub(key, interceptors.ChessMove{MoveIdx: moveIdx, BestMove: bestMove})
data := map[string]any{
"Key": key,
@@ -630,11 +634,22 @@ Loop:
// If game was over when we loaded the page
if game.Outcome() != chess.NoOutcome && gameLoadedOver {
- if payload.MoveIdx != 0 {
- pos := game.Positions()[payload.MoveIdx]
+ moveIdx := payload.MoveIdx
+ if moveIdx != 0 {
+ pos := game.Positions()[moveIdx]
boardMap := pos.Board().SquareMap()
- moves := game.Moves()[:payload.MoveIdx]
+ moves := game.Moves()[:moveIdx]
lastMove := moves[len(moves)-1]
+
+ var bestMove *chess.Move
+ bestMoveStr := payload.BestMove
+ if bestMoveStr != "" {
+ bestMove, err = chess.UCINotation{}.Decode(pos, bestMoveStr)
+ if err != nil {
+ logrus.Error(err)
+ }
+ }
+
checkIDStr := ""
if lastMove.HasTag(chess.Check) && pos.Turn() == chess.White {
checkIDStr = interceptors.WhiteKingID
@@ -658,6 +673,11 @@ Loop:
styles.Appendf(`.square { background-color: transparent !important; }`)
styles.Appendf(`.square_%d, .square_%d { background-color: %s !important; }`,
int(lastMove.S1()), int(lastMove.S2()), interceptors.LastMoveColor)
+ // Render best move
+ if bestMove != nil {
+ styles.Appendf(`.square_%d, .square_%d { background-color: rgba(0, 0, 255, 0.2) !important; }`,
+ int(bestMove.S1()), int(bestMove.S2()))
+ }
piecesCache := interceptors.InitPiecesCache(moves)
for i := 0; i < 64; i++ {
diff --git a/pkg/web/handlers/interceptors/chess.go b/pkg/web/handlers/interceptors/chess.go
@@ -36,6 +36,7 @@ type ChessMove struct {
CheckIDStr string
Move chess.Move
MoveIdx int
+ BestMove string
}
var ChessPubSub = pubsub.NewPubSub[ChessMove]()