commit 101ab262f35795d381492ee2c87d7ff7f4c82064
parent a479d3c637758389e6ffe9e2f7970ad4f3e39e53
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Wed, 14 Jun 2023 15:20:03 -0700
display best move
Diffstat:
1 file changed, 18 insertions(+), 6 deletions(-)
diff --git a/pkg/web/handlers/interceptors/chess.go b/pkg/web/handlers/interceptors/chess.go
@@ -174,7 +174,7 @@ input[type=checkbox]:checked + label {
}
</style>`
-func (g *ChessGame) renderBoardHTML1(moveIdx int, position *chess.Position, isFlipped bool, imgB64 string) string {
+func (g *ChessGame) renderBoardHTML1(moveIdx int, position *chess.Position, isFlipped bool, imgB64 string, bestMove *chess.Move) string {
game := g.Game
moves := game.Moves()
var last *chess.Move
@@ -189,6 +189,9 @@ func (g *ChessGame) renderBoardHTML1(moveIdx int, position *chess.Position, isFl
pieceInCheck := func(p chess.Piece) bool {
return last != nil && p.Color() == game.Position().Turn() && p.Type() == chess.King && last.HasTag(chess.Check)
}
+ sqIsBestMove := func(sq chess.Square) bool {
+ return bestMove != nil && (bestMove.S1() == sq || bestMove.S2() == sq)
+ }
sqIsLastMove := func(sq chess.Square) bool {
return last != nil && (last.S1() == sq || last.S2() == sq)
}
@@ -205,7 +208,7 @@ func (g *ChessGame) renderBoardHTML1(moveIdx int, position *chess.Position, isFl
{{ $sq := Square $id }}
{{ $pidStr := GetPid $sq }}
{{ $p := PieceFromSq $sq }}
- <td class="square square_{{ $id }}" style="background-color: {{ if IsLastMove $sq }}{{ $.LastMoveColor | css }}{{ else }}transparent{{ end }};">
+ <td class="square square_{{ $id }}" style="background-color: {{ if IsBestMove $sq }}rgba(0, 0, 255, 0.2){{ else if IsLastMove $sq }}{{ $.LastMoveColor | css }}{{ else }}transparent{{ end }};">
<div id="{{ $pidStr }}" class="img" style="
left: calc({{ $col }}*12.5%); top: calc({{ $row }}*12.5%);
background-image: url(/public/img/chess/{{ GetPieceFileName $p }}.png);
@@ -226,6 +229,7 @@ func (g *ChessGame) renderBoardHTML1(moveIdx int, position *chess.Position, isFl
fns := template.FuncMap{
"GetID": func(row, col int) int { return GetID(row, col, isFlipped) },
+ "IsBestMove": sqIsBestMove,
"IsLastMove": sqIsLastMove,
"PieceInCheck": pieceInCheck,
"GetPieceFileName": getPieceFileName,
@@ -236,7 +240,9 @@ func (g *ChessGame) renderBoardHTML1(moveIdx int, position *chess.Position, isFl
}
var buf bytes.Buffer
- _ = utils.Must(template.New("").Funcs(fns).Parse(htmlTmpl)).Execute(&buf, data)
+ if err := utils.Must(template.New("").Funcs(fns).Parse(htmlTmpl)).Execute(&buf, data); err != nil {
+ logrus.Error(err)
+ }
return buf.String()
}
@@ -299,12 +305,12 @@ var pieceTypeMap = map[chess.PieceType]string{
chess.Pawn: "P",
}
-func (g *ChessGame) renderBoardHTML(moveIdx int, isFlipped bool, imgB64 string) string {
+func (g *ChessGame) renderBoardHTML(moveIdx int, isFlipped bool, imgB64 string, bestMove *chess.Move) string {
position := g.Game.Position()
if moveIdx != 0 && moveIdx < len(g.Game.Positions()) {
position = g.Game.Positions()[moveIdx]
}
- out := g.renderBoardHTML1(moveIdx, position, isFlipped, imgB64)
+ out := g.renderBoardHTML1(moveIdx, position, isFlipped, imgB64, bestMove)
return out
}
@@ -476,8 +482,14 @@ func (g *ChessGame) drawPlayerCard(moveIdx int, key string, isBlack, isSpectator
var columnWidth = 1
var stats *AnalyseResult
_ = json.Unmarshal(g.DbChessGame.Stats, &stats)
+ var bestMove *chess.Move
if stats != nil {
if len(stats.Scores) > 0 {
+ if moveIdx > 0 && moveIdx < len(g.Game.Positions()) {
+ position := g.Game.Positions()[moveIdx]
+ bestMoveStr := stats.Scores[moveIdx-1].BestMove
+ bestMove, _ = chess.AlgebraicNotation{}.Decode(position, bestMoveStr)
+ }
columnWidth = utils.MaxInt(graphWidth/len(stats.Scores), 1)
}
}
@@ -490,7 +502,7 @@ func (g *ChessGame) drawPlayerCard(moveIdx int, key string, isBlack, isSpectator
"White": player1,
"Black": player2,
"Username": enemy.Username,
- "Table": template.HTML(g.renderBoardHTML(moveIdx, isBlack, imgB64)),
+ "Table": template.HTML(g.renderBoardHTML(moveIdx, isBlack, imgB64, bestMove)),
"ImgB64": imgB64,
"Outcome": game.Outcome().String(),
"GameOver": game.Outcome() != chess.NoOutcome,