commit 10a415b50a5d18ab0276bc2467918ef26ef2c009
parent 912d36ca28b555b486a6bf3785b235691e137670
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Thu, 15 Jun 2023 10:30:12 -0700
improve chess
Diffstat:
3 files changed, 71 insertions(+), 64 deletions(-)
diff --git a/pkg/web/handlers/chess.go b/pkg/web/handlers/chess.go
@@ -293,15 +293,14 @@ func ChessGameStatsHandler(c echo.Context) error {
border-right: 1px solid #555;
}
</style>
-<table class="graph">
- <tr>
- {{ range $idx, $el := .Stats.Scores }}
- <td title="{{ $idx | fmtMove }} {{ $el.Move }} | Advantage: {{ if not $el.Mate }}{{ $el.CP | cp }}{{ else }}#{{ $el.Mate }}{{ end }}">
- {{ $el.BestMove | commentHTML }}
- <form method="post" target="iframeForm" action="/chess/{{ $.Key }}/form">
- <input type="hidden" name="csrf" value="{{ $.CSRF }}" />
- <input type="hidden" name="move_idx" value="{{ $idx | plus }}" />
- <button type="submit" class="column-wrapper-wrapper" style="display: block;{{ if eq $.MoveIdx ($idx | plus) }} background-color: rgba(255, 255, 0, 0.2);{{ end }}">
+<form method="post">
+ <input type="hidden" name="csrf" value="{{ $.CSRF }}" />
+ <table class="graph">
+ <tr>
+ {{ range $idx, $el := .Stats.Scores }}
+ <td title="{{ $idx | fmtMove }} {{ $el.Move }} | Advantage: {{ if not $el.Mate }}{{ $el.CP | cp }}{{ else }}#{{ $el.Mate }}{{ end }}">
+ {{ $el.BestMove | commentHTML }}
+ <button type="submit" name="move_idx" value="{{ $idx | plus }}" class="column-wrapper-wrapper" style="display: block;{{ if eq $.MoveIdx ($idx | plus) }} background-color: rgba(255, 255, 0, 0.2);{{ end }}">
<div class="column-wrapper" style="border-bottom: 1px solid #333; box-sizing: border-box;">
{{ if ge .CP 0 }}
<div class="column" style="height: {{ $el | renderCP "white" }}px; background-color: #eee; bottom: 0;"></div>
@@ -313,36 +312,17 @@ func ChessGameStatsHandler(c echo.Context) error {
{{ end }}
</div>
</button>
- </form>
- </td>
- {{ end }}
- </tr>
-</table>`
-
- moveIdx, _ := strconv.Atoi(c.QueryParam("m"))
- const graphWidth = 800
- var columnWidth = 1
- var stats *interceptors.AnalyseResult
- _ = json.Unmarshal(g.DbChessGame.Stats, &stats)
- var bestMove string
- if stats != nil {
- if len(stats.Scores) > 0 {
- columnWidth = utils.MaxInt(graphWidth/len(stats.Scores), 1)
- if moveIdx > 0 {
- bestMove = stats.Scores[moveIdx-1].BestMove
- }
- }
- }
- interceptors.ChessPubSub.Pub(key+"_"+authUser.Username.String(), interceptors.ChessMove{MoveIdx: moveIdx, BestMove: bestMove})
+ </td>
+ {{ end }}
+ </tr>
+ </table>
+</form>`
data := map[string]any{
"Key": key,
"IsAnalysed": g.DbChessGame.AccuracyWhite != 0 && g.DbChessGame.AccuracyBlack != 0,
"WhiteAccuracy": g.DbChessGame.AccuracyWhite,
"BlackAccuracy": g.DbChessGame.AccuracyBlack,
- "Stats": stats,
- "ColumnWidth": columnWidth,
- "MoveIdx": moveIdx,
"CSRF": csrf,
}
@@ -383,6 +363,59 @@ func ChessGameStatsHandler(c echo.Context) error {
},
}
+ currMoveIdx := -1
+ v, err := c.Cookie("chess_" + key)
+ if err == nil {
+ currMoveIdx, _ = strconv.Atoi(v.Value)
+ }
+
+ moveIdx := currMoveIdx
+ const graphWidth = 800
+ var columnWidth = 1
+
+ var stats *interceptors.AnalyseResult
+ _ = json.Unmarshal(g.DbChessGame.Stats, &stats)
+
+ if c.Request().Method == http.MethodPost {
+ moveIdxStr := c.Request().PostFormValue("move_idx")
+ if moveIdxStr == "" {
+ moveIdx = -1
+ }
+ moveIdx, err = strconv.Atoi(moveIdxStr)
+ if err != nil {
+ moveIdx = -1
+ }
+ if moveIdx == -1 {
+ moveIdx = currMoveIdx
+ }
+ btnSubmit := c.Request().PostFormValue("btn_submit")
+ if moveIdx == -1 {
+ moveIdx = len(stats.Scores)
+ }
+ if btnSubmit == "prev_position" {
+ moveIdx -= 1
+ } else if btnSubmit == "next_position" {
+ moveIdx += 1
+ }
+ }
+
+ moveIdx = utils.Clamp(moveIdx, 0, len(stats.Scores))
+ c.SetCookie(hutils.CreateCookie("chess_"+key, strconv.Itoa(moveIdx), utils.OneDaySecs))
+ var bestMove string
+ if stats != nil {
+ if len(stats.Scores) > 0 {
+ columnWidth = utils.MaxInt(graphWidth/len(stats.Scores), 1)
+ if moveIdx > 0 {
+ bestMove = stats.Scores[moveIdx-1].BestMove
+ }
+ }
+ }
+ interceptors.ChessPubSub.Pub(key+"_"+authUser.Username.String(), interceptors.ChessMove{MoveIdx: moveIdx, BestMove: bestMove})
+
+ data["Stats"] = stats
+ data["ColumnWidth"] = columnWidth
+ data["MoveIdx"] = moveIdx
+
var buf1 bytes.Buffer
if err := utils.Must(template.New("").Funcs(fns).Parse(htmlTmpl)).Execute(&buf1, data); err != nil {
logrus.Error(err)
@@ -414,7 +447,7 @@ button {
border: none;
}
</style>
-<form method="post">
+<form method="post" target="iframeStats" action="/chess/{{ .Key }}/stats">
<input type="hidden" name="csrf" value="{{ .CSRF }}" />
<input type="hidden" name="move_idx" value="{{ .MoveIdx }}" />
<div style="position: fixed; top: 0; left: 0; right: 0; bottom: 0;">
@@ -426,34 +459,7 @@ button {
data := map[string]any{
"CSRF": csrf,
"MoveIdx": -1,
- }
-
- if c.Request().Method == http.MethodPost {
- btnSubmit := c.Request().PostFormValue("btn_submit")
- moveIdx, _ := strconv.Atoi(c.Request().PostFormValue("move_idx"))
-
- var stats *interceptors.AnalyseResult
- _ = json.Unmarshal(g.DbChessGame.Stats, &stats)
-
- if moveIdx == -1 {
- moveIdx = len(stats.Scores)
- }
- if btnSubmit == "prev_position" {
- moveIdx -= 1
- } else if btnSubmit == "next_position" {
- moveIdx += 1
- }
- moveIdx = utils.Clamp(moveIdx, 0, len(stats.Scores))
- var bestMove string
- if stats != nil {
- if len(stats.Scores) > 0 {
- if moveIdx > 0 {
- bestMove = stats.Scores[moveIdx-1].BestMove
- }
- }
- }
- interceptors.ChessPubSub.Pub(key+"_"+authUser.Username.String(), interceptors.ChessMove{MoveIdx: moveIdx, BestMove: bestMove})
- data["MoveIdx"] = moveIdx
+ "Key": key,
}
var buf bytes.Buffer
diff --git a/pkg/web/handlers/interceptors/chess.go b/pkg/web/handlers/interceptors/chess.go
@@ -433,7 +433,7 @@ func (g *ChessGame) drawPlayerCard(moveIdx int, key string, isBlack, isSpectator
<td colspan="2">
{{ if .GameOver }}
<div style="position: relative;">
- <iframe name="iframeForm" src="/chess/{{ .Key }}/form" style="position: absolute; top: 0; left: 0; border: 0px solid red; z-index: 999; width: 100%; height: 100%;"></iframe>
+ <iframe src="/chess/{{ .Key }}/form" style="position: absolute; top: 0; left: 0; border: 0px solid red; z-index: 999; width: 100%; height: 100%;"></iframe>
<div style="aspect-ratio: 1/1; height: 70%;">
{{ .Table }}
</div>
@@ -466,7 +466,7 @@ func (g *ChessGame) drawPlayerCard(moveIdx int, key string, isBlack, isSpectator
</table>
{{ if .Stats }}
- <iframe src="/chess/{{ .Key }}/stats" style="width: 800px; height: 240px; margin: 10px 0; border: 3px solid black;"></iframe>
+ <iframe name="iframeStats" src="/chess/{{ .Key }}/stats" style="width: 800px; height: 240px; margin: 10px 0; border: 3px solid black;"></iframe>
{{ if .IsAnalysed }}
<div style="color: #eee;">White accuracy: <span id="white-accuracy">{{ .WhiteAccuracy | pct }}</span></div>
<div style="color: #eee;">Black accuracy: <span id="black-accuracy">{{ .BlackAccuracy | pct }}</span></div>
diff --git a/pkg/web/web.go b/pkg/web/web.go
@@ -106,6 +106,7 @@ func getMainServer(db *database.DkfDB, i18nBundle *i18n.Bundle, renderer *tmp.Te
authGroup.GET("/chess/:key/form", handlers.ChessGameFormHandler)
authGroup.POST("/chess/:key/form", handlers.ChessGameFormHandler)
authGroup.GET("/chess/:key/stats", handlers.ChessGameStatsHandler)
+ authGroup.POST("/chess/:key/stats", handlers.ChessGameStatsHandler)
authGroup.GET("/settings/chat", handlers.SettingsChatHandler)
authGroup.POST("/settings/chat", handlers.SettingsChatHandler, middlewares.AuthRateLimitMiddleware(2*time.Second, 1))
authGroup.GET("/settings/chat/pm", handlers.SettingsChatPMHandler)