dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

commit 6518cd3a23b642c7de7edd51686e4680f57009d6
parent 3736211ec63eb8254a694bf863501bafa0fccd84
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Sun, 11 Jun 2023 19:35:44 -0700

cleanup

Diffstat:
Mpkg/web/handlers/interceptors/chess.go | 156+++++++++++++++++++++++++++++---------------------------------------------------
1 file changed, 56 insertions(+), 100 deletions(-)

diff --git a/pkg/web/handlers/interceptors/chess.go b/pkg/web/handlers/interceptors/chess.go @@ -292,98 +292,10 @@ func (g *ChessGame) renderBoardB64(isFlipped bool) string { } func (g *ChessGame) DrawPlayerCard(key string, isBlack, isYourTurn bool) string { - return g.drawPlayerCard(key, isBlack, isYourTurn) + return g.drawPlayerCard(key, isBlack, false, isYourTurn) } -func (g *ChessGame) drawPlayerCard(key string, isBlack, isYourTurn bool) string { - enemy := utils.Ternary(isBlack, g.Player1, g.Player2) - - imgB64 := g.renderBoardB64(isBlack) - - htmlTmpl := ` -<style> -.tmp { - width: 100%; - height: 100%; -} -</style> -<table class="tmp"> - <tr><td align="center"> - <table style="aspect-ratio: 1/1; height: 70%; max-width: 90%;"> - <tr> - <td style="padding: 10px 0;"> - <div style="color: #eee;"> - <span {{ .White.UserStyle | attr }}>@{{ .White.Username }}</span> (white) VS - <span {{ .Black.UserStyle | attr }}>@{{ .Black.Username }}</span> (black) - </div> - </td> - </tr> - <tr> - <td> - {{ if .GameOver }} - {{ .Table }} - {{ else }} - <form method="post"> - <input type="hidden" name="message" value="resign" /> - <button type="submit" style="background-color: #aaa; margin: 5px 0;">Resign</button> - </form> - <div style="position: relative;"> - <iframe src="/chess/{{ .Key }}/form" style="position: absolute; top: 0; left: 0; border: 0px solid red; z-index: 999; width: 100%; height: 100%;"></iframe> - <form method="post"style="aspect-ratio: 1/1; height: 70%;"> - {{ .Table }} - <input type="hidden" name="message" value="/pm {{ .Username }} /c move" /> - <div style="width: 100%; display: flex; margin: 5px 0;"> - <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;"> - <option value="queen">Queen</option> - <option value="rook">Rook</option> - <option value="knight">Knight</option> - <option value="bishop">Bishop</option> - </select> - </div> - </div> - </form> - </div> - {{ end }} - </td> - </tr> - <tr style="height: 100%;"><td><div style="color: #eee;">Outcome: {{ .Outcome }}</div></td></tr> - - {{ if .GameOver }}<tr><td><div><textarea>{{ .PGN }}</textarea></div></td></tr>{{ end }} - </table> -</td></tr></table> -` - - data := map[string]any{ - "Key": key, - "White": g.Player1, - "Black": g.Player2, - "Username": enemy.Username, - "Table": template.HTML(g.renderBoardHTML(isBlack, imgB64, false)), - "ImgB64": imgB64, - "Outcome": g.Game.Outcome().String(), - "GameOver": g.Game.Outcome() != chess.NoOutcome, - "PGN": g.Game.String(), - } - - fns := template.FuncMap{ - "attr": func(s string) template.HTMLAttr { - return template.HTMLAttr(s) - }, - } - - var buf1 bytes.Buffer - _ = utils.Must(template.New("").Funcs(fns).Parse(htmlTmpl)).Execute(&buf1, data) - return buf1.String() -} - -func (g *ChessGame) DrawSpectatorCard(isFlipped bool) string { - imgB64 := g.renderBoardB64(isFlipped) - +func (g *ChessGame) drawPlayerCard(key string, isBlack, isSpectator, isYourTurn bool) string { htmlTmpl := ` <style> .tmp { @@ -403,8 +315,42 @@ func (g *ChessGame) DrawSpectatorCard(isFlipped bool) string { </div> </td> </tr> - <tr><td>{{ .Table }}</td></tr> - <tr><td style="padding: 10px 0;"><a href="?{{ if not .IsFlipped }}r=1{{ end }}" style="color: #eee;">Flip board</a></td></tr> + <tr> + <td> + {{ if or .IsSpectator .GameOver }} + {{ .Table }} + {{ else }} + <form method="post"> + <input type="hidden" name="message" value="resign" /> + <button type="submit" style="background-color: #aaa; margin: 5px 0;">Resign</button> + </form> + <div style="position: relative;"> + <iframe src="/chess/{{ .Key }}/form" style="position: absolute; top: 0; left: 0; border: 0px solid red; z-index: 999; width: 100%; height: 100%;"></iframe> + <form method="post"style="aspect-ratio: 1/1; height: 70%;"> + {{ .Table }} + <input type="hidden" name="message" value="/pm {{ .Username }} /c move" /> + <div style="width: 100%; display: flex; margin: 5px 0;"> + <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;"> + <option value="queen">Queen</option> + <option value="rook">Rook</option> + <option value="knight">Knight</option> + <option value="bishop">Bishop</option> + </select> + </div> + </div> + </form> + </div> + {{ end }} + </td> + </tr> + {{ if .IsSpectator }} + <tr><td style="padding: 10px 0;"><a href="?{{ if not .IsFlipped }}r=1{{ end }}" style="color: #eee;">Flip board</a></td></tr> + {{ end }} <tr style="height: 100%;"><td><div style="color: #eee;">Outcome: {{ .Outcome }}</div></td></tr> {{ if .GameOver }}<tr><td><div><textarea>{{ .PGN }}</textarea></div></td></tr>{{ end }} @@ -414,15 +360,21 @@ func (g *ChessGame) DrawSpectatorCard(isFlipped bool) string { </table> ` + enemy := utils.Ternary(isBlack, g.Player1, g.Player2) + imgB64 := g.renderBoardB64(isBlack) + data := map[string]any{ - "White": g.Player1, - "Black": g.Player2, - "Table": template.HTML(g.renderBoardHTML(isFlipped, imgB64, true)), - "ImgB64": imgB64, - "Outcome": g.Game.Outcome().String(), - "GameOver": g.Game.Outcome() != chess.NoOutcome, - "PGN": g.Game.String(), - "IsFlipped": isFlipped, + "Key": key, + "IsFlipped": isBlack, + "IsSpectator": isSpectator, + "White": g.Player1, + "Black": g.Player2, + "Username": enemy.Username, + "Table": template.HTML(g.renderBoardHTML(isBlack, imgB64, false)), + "ImgB64": imgB64, + "Outcome": g.Game.Outcome().String(), + "GameOver": g.Game.Outcome() != chess.NoOutcome, + "PGN": g.Game.String(), } fns := template.FuncMap{ @@ -436,6 +388,10 @@ func (g *ChessGame) DrawSpectatorCard(isFlipped bool) string { return buf1.String() } +func (g *ChessGame) DrawSpectatorCard(isFlipped bool) string { + return g.drawPlayerCard("", isFlipped, true, false) +} + func (b *Chess) GetGame(key string) *ChessGame { b.Lock() defer b.Unlock()