commit 061b58d665f28b354abd847d699c15080c41fbe3
parent 90c38ee312bfdfa79ed1fb89f49ef06b4c2071a9
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Thu, 15 Jun 2023 21:39:45 -0700
fix board
Diffstat:
2 files changed, 47 insertions(+), 20 deletions(-)
diff --git a/pkg/web/handlers/chess.go b/pkg/web/handlers/chess.go
@@ -815,7 +815,7 @@ Loop:
return
}
if payload.IDStr2 != "" {
- send(fmt.Sprintf(`<style>#%s { display: none; }</style>`, payload.IDStr2))
+ send(fmt.Sprintf(`<style>#%s { display: none !important; }</style>`, payload.IDStr2))
}
if payload.Move.Promo() != chess.NoPieceType {
pieceColor := utils.Ternary(payload.Move.S2().Rank() == chess.Rank8, chess.White, chess.Black)
diff --git a/pkg/web/handlers/interceptors/chess.go b/pkg/web/handlers/interceptors/chess.go
@@ -207,7 +207,6 @@ func (g *ChessGame) renderBoardHTML1(moveIdx int, position *chess.Position, isFl
last = moves[moveIdx-1]
}
}
- boardMap := position.Board().SquareMap()
deadBoardMap := chess.NewGame().Position().Board().SquareMap()
pieceInCheck := func(p chess.Piece) bool {
@@ -220,7 +219,7 @@ func (g *ChessGame) renderBoardHTML1(moveIdx int, position *chess.Position, isFl
return last != nil && (last.S1() == sq || last.S2() == sq)
}
getPieceFileName := func(p chess.Piece) string {
- return p.Color().String() + strings.ToUpper(p.Type().String())
+ return "/public/img/chess/" + p.Color().String() + strings.ToUpper(p.Type().String()) + ".png"
}
htmlTmpl := ChessCSS + `
@@ -231,26 +230,38 @@ func (g *ChessGame) renderBoardHTML1(moveIdx int, position *chess.Position, isFl
{{ $id := GetID $row $col }}
{{ $sq := Square $id }}
{{ $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) }}
<div id="arrow"><div class="triangle-up"></div><div class="rectangle"></div></div>
- {{ range $.Dead }}
- {{ $p1 := DeadPieceFromSq . }}
- <div id="piece_{{ . }}" class="img" style="
- 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);
- background-color: {{ if PieceInCheck $p }}{{ $.CheckColor | css }}{{ else }}transparent{{ end }};"></div>
+ {{ if $pidStr }}
+ {{ $p := PieceFromSq $sq }}
+ <div id="{{ $pidStr }}" class="img" style=" display: none; background-image: url({{ GetPieceFileName $p }});"></div>
+ {{ end }}
</td>
{{ end }}
</tr>
{{ end }}
-</table>`
+</table>
+<style>
+{{- range $row := .Rows -}}
+ {{ range $col := $.Cols -}}
+ {{- $id := GetID $row $col -}}
+ {{- $sq := Square $id -}}
+ {{- $p := PieceFromSq1 $sq -}}
+ {{- $pidStr := GetPid1 $sq -}}
+ {{- if $pidStr -}}
+ #{{ $pidStr }} {
+ display: block !important;
+ background-image: url("{{ GetPieceFileName $p }}") !important;
+ left: calc({{ $col }}*12.5%); top: calc({{ $row }}*12.5%);
+ background-color: {{ if PieceInCheck $p }}{{ $.CheckColor | css }}{{ else }}transparent{{ end }};
+ }
+ {{- end -}}
+ {{- end -}}
+{{- end -}}
+</style>
+`
allPieces := []chess.Square{
chess.A8, chess.B8, chess.C8, chess.D8, chess.E8, chess.F8, chess.G8, chess.H8,
@@ -287,11 +298,27 @@ func (g *ChessGame) renderBoardHTML1(moveIdx int, position *chess.Position, isFl
"IsLastMove": sqIsLastMove,
"PieceInCheck": pieceInCheck,
"GetPieceFileName": getPieceFileName,
- "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) },
+ "GetPid": func(sq chess.Square) string {
+ if sq.Rank() == chess.Rank1 || sq.Rank() == chess.Rank2 || sq.Rank() == chess.Rank7 || sq.Rank() == chess.Rank8 {
+ return "piece_" + sq.String()
+ }
+ return ""
+ },
+ "GetPid1": 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 {
+ game := chess.NewGame()
+ boardMap := game.Position().Board().SquareMap()
+ return boardMap[sq]
+ },
+ "PieceFromSq1": func(sq chess.Square) chess.Piece {
+ boardMap := game.Position().Board().SquareMap()
+ return boardMap[sq]
+ },
+ "DeadPieceFromSq": func(sq chess.Square) chess.Piece { return deadBoardMap[sq] },
+ "css": func(s string) template.CSS { return template.CSS(s) },
+ "cssUrl": func(s string) template.URL { return template.URL(s) },
}
var buf bytes.Buffer