commit dcc0620c0abd50b97bf748c805840fc75ddab530
parent 62d62c5b16b35ca7ad00743733f1f3fc3f2d285c
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Sun, 11 Jun 2023 18:29:31 -0700
cleanup
Diffstat:
1 file changed, 60 insertions(+), 47 deletions(-)
diff --git a/pkg/web/handlers/interceptors/chess.go b/pkg/web/handlers/interceptors/chess.go
@@ -117,7 +117,28 @@ func (g *ChessGame) renderBoardHTML1(position *chess.Position, isFlipped bool, i
last = moves[len(moves)-1]
}
boardMap := position.Board().SquareMap()
- out := `<style>
+
+ getID := func(row, col int) int {
+ var id int
+ if isFlipped {
+ id = row*8 + (7 - col)
+ } else {
+ id = (7-row)*8 + col
+ }
+ return id
+ }
+ pieceInCheck := func(p chess.Piece) bool {
+ return last != nil && p.Color() == g.Game.Position().Turn() && p.Type() == chess.King && last.HasTag(chess.Check)
+ }
+ sqIsLastMove := func(sq chess.Square) bool {
+ return last != nil && (last.S1() == sq || last.S2() == sq)
+ }
+ getPieceFileName := func(p chess.Piece) string {
+ return p.Color().String() + pieceTypeMap[p.Type()]
+ }
+
+ htmlTmpl := `
+<style>
.newBoard {
position: relative;
aspect-ratio: 1 / 1;
@@ -125,7 +146,7 @@ func (g *ChessGame) renderBoardHTML1(position *chess.Position, isFlipped bool, i
min-height: 360px;
background-repeat: no-repeat;
background-size: cover;
- background-image: url(data:image/png;base64,` + imgB64 + `);
+ background-image: url(data:image/png;base64,{{ .ImgB64 }});
}
.newBoard td {
}
@@ -156,55 +177,47 @@ input[type=checkbox]:checked + label {
background-size: 100%;
border: 3px solid red;
}
-</style>`
+</style>
+<table class="newBoard">
+ {{ range $row := .Rows }}
+ <tr>
+ {{ range $col := $.Cols }}
+ {{ $id := GetID $row $col }}
+ {{ $sq := Square $id }}
+ {{ $pidStr := GetPid $sq }}
+ {{ $p := PieceFromSq $sq }}
+ <td class="square square_{{ $id }}" style="background-color: {{ if IsLastMove $sq }}{{ $.LastMoveColor }}{{ 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);
+ background-color: {{ if PieceInCheck $p }}{{ .CheckColor }}{{ else }}transparent{{ end }};"></div>
+ </td>
+ {{ end }}
+ </tr>
+ {{ end }}
+</table>`
- getID := func(row, col int) int {
- var id int
- if isFlipped {
- id = row*8 + (7 - col)
- } else {
- id = (7-row)*8 + col
- }
- return id
- }
- pieceInCheck := func(p chess.Piece) bool {
- return last != nil && p.Color() == g.Game.Position().Turn() && p.Type() == chess.King && last.HasTag(chess.Check)
- }
- sqIsLastMove := func(sq chess.Square) bool {
- return last != nil && (last.S1() == sq || last.S2() == sq)
- }
- getPieceFileName := func(p chess.Piece) string {
- return p.Color().String() + pieceTypeMap[p.Type()]
+ 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,
}
- out += `<table class="newBoard">`
- for row := 0; row < 8; row++ {
- out += "<tr>"
- for col := 0; col < 8; col++ {
- id := getID(row, col)
- sq := chess.Square(id)
- p := boardMap[sq]
- pidStr := g.PiecesCache[sq]
- isLastMove := sqIsLastMove(sq)
- fName := getPieceFileName(p)
- inCheck := pieceInCheck(p)
- lastMoveColor := utils.Ternary(isLastMove, LastMoveColor, "transparent")
- checkColor := utils.Ternary(inCheck, CheckColor, "transparent")
-
- out += fmt.Sprintf(`<td class="square square_%d" style="background-color: %s;">`, id, lastMoveColor)
- if p != chess.NoPiece {
- out += fmt.Sprintf(`<div id="%s" class="img" style="
- left: calc(%d*12.5%%); top: calc(%d*12.5%%);
- background-image: url(/public/img/chess/%s.png);
- background-color: %s;"></div>`,
- pidStr, col, row, fName, checkColor)
- }
- out += "</td>"
- }
- out += "</tr>"
+ fns := template.FuncMap{
+ "GetID": getID,
+ "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] },
}
- out += "</table>"
- return out
+
+ var buf bytes.Buffer
+ _ = utils.Must(template.New("").Funcs(fns).Parse(htmlTmpl)).Execute(&buf, data)
+ return buf.String()
}
func renderBoardPng(last *chess.Move, position *chess.Position, isFlipped bool) image.Image {