commit 95040ebadc092d9e6dfa72026950779c19846ea3
parent 5361fca930a87d24162ae6bb7589e599d4533bf0
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Wed, 14 Jun 2023 23:46:35 -0700
fix replay
Diffstat:
2 files changed, 57 insertions(+), 2 deletions(-)
diff --git a/pkg/web/handlers/chess.go b/pkg/web/handlers/chess.go
@@ -631,10 +631,33 @@ Loop:
// If game was over when we loaded the page
if game.Outcome() != chess.NoOutcome && gameLoadedOver {
if payload.MoveIdx != 0 {
- var styles StylesBuilder
- styles.Append(`.img { display: none !important; }`)
pos := game.Positions()[payload.MoveIdx]
moves := game.Moves()[:payload.MoveIdx]
+ lastMove := moves[len(moves)-1]
+ checkIDStr := ""
+ if lastMove.HasTag(chess.Check) && pos.Turn() == chess.White {
+ checkIDStr = interceptors.WhiteKingID
+ } else if lastMove.HasTag(chess.Check) && pos.Turn() == chess.Black {
+ checkIDStr = interceptors.BlackKingID
+ }
+
+ var styles StylesBuilder
+ // Reset kings background to transparent
+ styles.Appendf(`#%s, #%s { background-color: transparent !important; }`, interceptors.WhiteKingID, interceptors.BlackKingID)
+ // Render "checks" red background
+ if checkIDStr != "" {
+ styles.Appendf(`#%s { background-color: %s !important; }`, checkIDStr, interceptors.CheckColor)
+ }
+
+ for i := 0; i < 64; i++ {
+ styles.Appendf(`#piece_%s { display: none !important; }`, chess.Square(i).String())
+ }
+
+ // Render last move
+ 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)
+
piecesCache := interceptors.InitPiecesCache(moves)
for i := 0; i < 64; i++ {
sq := chess.Square(i)
diff --git a/pkg/web/handlers/interceptors/chess.go b/pkg/web/handlers/interceptors/chess.go
@@ -180,6 +180,7 @@ func (g *ChessGame) renderBoardHTML1(moveIdx int, position *chess.Position, isFl
}
}
boardMap := position.Board().SquareMap()
+ deadBoardMap := chess.NewGame().Position().Board().SquareMap()
pieceInCheck := func(p chess.Piece) bool {
return last != nil && p.Color() == position.Turn() && p.Type() == chess.King && last.HasTag(chess.Check)
@@ -204,6 +205,15 @@ func (g *ChessGame) renderBoardHTML1(moveIdx int, position *chess.Position, isFl
{{ $pidStr := GetPid $sq }}
{{ $p := PieceFromSq $sq }}
<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 }};">
+ {{ if and (eq $col 0) (eq $row 0) }}
+ {{ range $.Dead }}
+ {{ $p1 := DeadPieceFromSq . }}
+ <div id="piece_{{ . }}" class="img" style="
+ left: calc(0*12.5%); top: calc(0*12.5%);
+ background-image: url(/public/img/chess/{{ GetPieceFileName $p1 }}.png);
+ background-color: transparent; display: none;"></div>
+ {{ end }}
+ {{ 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);
@@ -214,12 +224,33 @@ func (g *ChessGame) renderBoardHTML1(moveIdx int, position *chess.Position, isFl
{{ end }}
</table>`
+ allPieces := []chess.Square{
+ chess.A8, chess.B8, chess.C8, chess.D8, chess.E8, chess.F8, chess.G8, chess.H8,
+ chess.A7, chess.B7, chess.C7, chess.D7, chess.E7, chess.F7, chess.G7, chess.H7,
+ chess.A2, chess.B2, chess.C2, chess.D2, chess.E2, chess.F2, chess.G2, chess.H2,
+ chess.A1, chess.B1, chess.C1, chess.D1, chess.E1, chess.F1, chess.G1, chess.H1,
+ }
+ dead := make([]chess.Square, 0)
+ for _, p := range allPieces {
+ found := false
+ for _, v := range g.piecesCache {
+ if v == "piece_"+p.String() {
+ found = true
+ break
+ }
+ }
+ if !found {
+ dead = append(dead, p)
+ }
+ }
+
data := map[string]any{
"ImgB64": imgB64,
"Rows": []int{0, 1, 2, 3, 4, 5, 6, 7},
"Cols": []int{0, 1, 2, 3, 4, 5, 6, 7},
"LastMoveColor": LastMoveColor,
"CheckColor": CheckColor,
+ "Dead": dead,
}
fns := template.FuncMap{
@@ -231,6 +262,7 @@ func (g *ChessGame) renderBoardHTML1(moveIdx int, position *chess.Position, isFl
"GetPid": func(sq chess.Square) string { return g.piecesCache[sq] },
"Square": func(id int) chess.Square { return chess.Square(id) },
"PieceFromSq": func(sq chess.Square) chess.Piece { return boardMap[sq] },
+ "DeadPieceFromSq": func(sq chess.Square) chess.Piece { return deadBoardMap[sq] },
"css": func(s string) template.CSS { return template.CSS(s) },
}