dkforest

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

commit a479d3c637758389e6ffe9e2f7970ad4f3e39e53
parent 723461b19d22da659088d4edf96a2d35ed8c731e
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Wed, 14 Jun 2023 14:43:04 -0700

can click on graph to load position

Diffstat:
Mpkg/web/handlers/chess.go | 8+++++---
Mpkg/web/handlers/interceptors/chess.go | 50++++++++++++++++++++++++++++++++------------------
2 files changed, 37 insertions(+), 21 deletions(-)

diff --git a/pkg/web/handlers/chess.go b/pkg/web/handlers/chess.go @@ -17,6 +17,7 @@ import ( "html/template" "math" "net/http" + "strconv" "strings" "time" ) @@ -409,7 +410,8 @@ func ChessGameHandler(c echo.Context) error { c.Response().WriteHeader(http.StatusOK) send(cssReset) send(`<style>html, body { background-color: #222; }</style>`) - card := g.DrawPlayerCard(key, isFlipped, false, authUser.ChessSoundsEnabled, authUser.IsAdmin) + m, _ := strconv.Atoi(c.QueryParam("m")) + card := g.DrawPlayerCard(m, key, isFlipped, false, authUser.ChessSoundsEnabled, authUser.IsAdmin) send(card) return nil } @@ -437,9 +439,9 @@ func ChessGameHandler(c echo.Context) error { var card1 string if isSpectator { - card1 = g.DrawSpectatorCard(isFlipped, authUser.ChessSoundsEnabled, authUser.IsAdmin) + card1 = g.DrawSpectatorCard(0, isFlipped, authUser.ChessSoundsEnabled, authUser.IsAdmin) } else { - card1 = g.DrawPlayerCard(key, isFlipped, false, authUser.ChessSoundsEnabled, authUser.IsAdmin) + card1 = g.DrawPlayerCard(0, key, isFlipped, false, authUser.ChessSoundsEnabled, authUser.IsAdmin) } send(card1) diff --git a/pkg/web/handlers/interceptors/chess.go b/pkg/web/handlers/interceptors/chess.go @@ -174,12 +174,15 @@ input[type=checkbox]:checked + label { } </style>` -func (g *ChessGame) renderBoardHTML1(position *chess.Position, isFlipped bool, imgB64 string) string { +func (g *ChessGame) renderBoardHTML1(moveIdx int, position *chess.Position, isFlipped bool, imgB64 string) string { game := g.Game moves := game.Moves() var last *chess.Move if len(moves) > 0 { last = moves[len(moves)-1] + if moveIdx > 0 && moveIdx < len(moves) { + last = moves[moveIdx-1] + } } boardMap := position.Board().SquareMap() @@ -296,9 +299,12 @@ var pieceTypeMap = map[chess.PieceType]string{ chess.Pawn: "P", } -func (g *ChessGame) renderBoardHTML(isFlipped bool, imgB64 string) string { +func (g *ChessGame) renderBoardHTML(moveIdx int, isFlipped bool, imgB64 string) string { position := g.Game.Position() - out := g.renderBoardHTML1(position, isFlipped, imgB64) + if moveIdx != 0 && moveIdx < len(g.Game.Positions()) { + position = g.Game.Positions()[moveIdx] + } + out := g.renderBoardHTML1(moveIdx, position, isFlipped, imgB64) return out } @@ -310,11 +316,11 @@ func (g *ChessGame) renderBoardB64(isFlipped bool) string { return imgB64 } -func (g *ChessGame) DrawPlayerCard(key string, isBlack, isYourTurn, soundsEnabled, isAdmin bool) string { - return g.drawPlayerCard(key, isBlack, false, isYourTurn, soundsEnabled, isAdmin) +func (g *ChessGame) DrawPlayerCard(moveIdx int, key string, isBlack, isYourTurn, soundsEnabled, isAdmin bool) string { + return g.drawPlayerCard(moveIdx, key, isBlack, false, isYourTurn, soundsEnabled, isAdmin) } -func (g *ChessGame) drawPlayerCard(key string, isBlack, isSpectator, isYourTurn, soundsEnabled, isAdmin bool) string { +func (g *ChessGame) drawPlayerCard(moveIdx int, key string, isBlack, isSpectator, isYourTurn, soundsEnabled, isAdmin bool) string { htmlTmpl := ` <style> #p1Status { @@ -345,6 +351,11 @@ func (g *ChessGame) drawPlayerCard(key string, isBlack, isSpectator, isYourTurn, .graph td:hover { background-color: #5c5c5c; } +.graph .column-wrapper-wrapper { + height: 100%; + width: {{ .ColumnWidth }}px; + position: relative; +} .graph .column-wrapper { height: 50%; width: {{ .ColumnWidth }}px; @@ -432,15 +443,17 @@ func (g *ChessGame) drawPlayerCard(key string, isBlack, isSpectator, isYourTurn, <tr> {{ range $idx, $el := .Stats.Scores }} <td title="{{ $idx | fmtMove }} {{ $el.Move }} | Advantage: {{ if not $el.Mate }}{{ $el.CP | cp }}{{ else }}#{{ $el.Mate }}{{ end }}"> - <div class="column-wrapper"> - {{ if ge .CP 0 }} - <div class="column" style="height: {{ $el | renderCP "white" }}px; background-color: #eee; bottom: 0;"></div> - {{ end }} - </div> - <div class="column-wrapper"> - {{ if le .CP 0 }} - <div class="column" style="height: {{ $el | renderCP "black" }}px; background-color: #111;"></div> - {{ end }} + <a class="column-wrapper-wrapper" href="/chess/{{ $.Key }}?m={{ $idx | plus }}"> + <div class="column-wrapper"> + {{ if ge .CP 0 }} + <div class="column" style="height: {{ $el | renderCP "white" }}px; background-color: #eee; bottom: 0;"></div> + {{ end }} + </div> + <div class="column-wrapper"> + {{ if le .CP 0 }} + <div class="column" style="height: {{ $el | renderCP "black" }}px; background-color: #111;"></div> + {{ end }} + </div> </div> </td> {{ end }} @@ -477,7 +490,7 @@ func (g *ChessGame) drawPlayerCard(key string, isBlack, isSpectator, isYourTurn, "White": player1, "Black": player2, "Username": enemy.Username, - "Table": template.HTML(g.renderBoardHTML(isBlack, imgB64)), + "Table": template.HTML(g.renderBoardHTML(moveIdx, isBlack, imgB64)), "ImgB64": imgB64, "Outcome": game.Outcome().String(), "GameOver": game.Outcome() != chess.NoOutcome, @@ -498,6 +511,7 @@ func (g *ChessGame) drawPlayerCard(key string, isBlack, isSpectator, isYourTurn, "attr": func(s string) template.HTMLAttr { return template.HTMLAttr(s) }, + "plus": func(v int) int { return v + 1 }, "pct": func(v float64) string { return fmt.Sprintf("%.1f%%", v) }, @@ -534,8 +548,8 @@ func (g *ChessGame) drawPlayerCard(key string, isBlack, isSpectator, isYourTurn, return buf1.String() } -func (g *ChessGame) DrawSpectatorCard(isFlipped, soundsEnabled, isAdmin bool) string { - return g.drawPlayerCard("", isFlipped, true, false, soundsEnabled, isAdmin) +func (g *ChessGame) DrawSpectatorCard(moveIdx int, isFlipped, soundsEnabled, isAdmin bool) string { + return g.drawPlayerCard(moveIdx, "", isFlipped, true, false, soundsEnabled, isAdmin) } func (b *Chess) GetGame(key string) (*ChessGame, error) {