commit c8e23aaf9876b095d75928379b65c5dfdefbef5a
parent cd0a7de952f5db1e462d2e6463f1925fa5ca2c2f
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Sun, 11 Jun 2023 22:17:55 -0700
cleanup
Diffstat:
2 files changed, 35 insertions(+), 22 deletions(-)
diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go
@@ -26,6 +26,7 @@ import (
_ "golang.org/x/image/bmp"
_ "golang.org/x/image/webp"
html2 "html"
+ "html/template"
"image"
_ "image/gif"
"image/png"
@@ -4932,24 +4933,24 @@ func ChessGameFormHandler(c echo.Context) error {
g := interceptors.ChessInstance.GetGame(key)
isFlipped := authUser.ID == g.Player2.ID
- out := cssReset
- out += interceptors.ChessCSS
- out += fmt.Sprintf(`<form method="post" action="/chess/%s"><table class="newBoard">`, key)
- for row := 0; row < 8; row++ {
- out += "<tr>"
- for col := 0; col < 8; col++ {
- id := interceptors.GetID(row, col, isFlipped)
- out += fmt.Sprintf(`<td><input name="sq_%d" id="sq_%d" type="checkbox" value="1" /><label for="sq_%d"></label></td>`, id, id, id)
- }
- out += "</tr>"
- }
- out += `</table>
- <input type="hidden" name="csrf" value="` + csrf + `" />
- <input type="hidden" name="message" value="/pm {{ .Username }} /c move" />
+ htmlTmpl := cssReset + interceptors.ChessCSS + `
+<form method="post" action="/chess/{{ .Key }}">
+ <input type="hidden" name="csrf" value="{{ .CSRF }}" />
+ <table class="newBoard">
+ {{ range $row := .Rows }}
+ <tr>
+ {{ range $col := $.Cols }}
+ {{ $id := GetID $row $col }}
+ <td>
+ <input name="sq_{{ $id }}" id="sq_{{ $id }}" type="checkbox" value="1" />
+ <label for="sq_{{ $id }}"></label>
+ </td>
+ {{ end }}
+ </tr>
+ {{ end }}
+ </table>
<div style="width: 100%; display: flex; margin: 5px 0;">
- <div>
- <button type="submit" style="background-color: #aaa;">Move</button>
- </div>
+ <div><button type="submit" style="background-color: #aaa;">Move</button></div>
<div style="margin-left: auto;">
<span style="color: #aaa; margin-left: 20px;">Promo:</span>
<select name="promotion" style="background-color: #aaa;">
@@ -4961,7 +4962,22 @@ func ChessGameFormHandler(c echo.Context) error {
</div>
</div>
</form>`
- return c.HTML(http.StatusOK, out)
+
+ data := map[string]any{
+ "Rows": []int{0, 1, 2, 3, 4, 5, 6, 7},
+ "Cols": []int{0, 1, 2, 3, 4, 5, 6, 7},
+ "Key": key,
+ "CSRF": csrf,
+ }
+
+ fns := template.FuncMap{
+ "GetID": func(row, col int) int { return interceptors.GetID(row, col, isFlipped) },
+ }
+
+ var buf bytes.Buffer
+ _ = utils.Must(template.New("").Funcs(fns).Parse(htmlTmpl)).Execute(&buf, data)
+
+ return c.HTML(http.StatusOK, buf.String())
}
func ChessGameHandler(c echo.Context) error {
diff --git a/pkg/web/handlers/interceptors/chess.go b/pkg/web/handlers/interceptors/chess.go
@@ -136,9 +136,6 @@ var ChessCSS = `
aspect-ratio: 1 / 1;
width: 100%;
min-height: 360px;
- background-repeat: no-repeat;
- background-size: cover;
- background-image: url(data:image/png;base64,{{ .ImgB64 }});
}
.newBoard .img {
position: absolute;
@@ -188,7 +185,7 @@ func (g *ChessGame) renderBoardHTML1(position *chess.Position, isFlipped bool, i
}
htmlTmpl := ChessCSS + `
-<table class="newBoard">
+<table class="newBoard" style=" background-repeat: no-repeat; background-size: cover; background-image: url(data:image/png;base64,{{ .ImgB64 }});">
{{ range $row := .Rows }}
<tr>
{{ range $col := $.Cols }}