commit 40920e87567599b74aa051d6eebc41d599617082
parent 1db6a3c87e360eae60a09c4003085455dc670ddd
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Sun, 18 Jun 2023 00:35:16 -0700
adjustable time
Diffstat:
3 files changed, 22 insertions(+), 11 deletions(-)
diff --git a/pkg/web/handlers/chess.go b/pkg/web/handlers/chess.go
@@ -121,6 +121,7 @@ func ChessGameAnalyzeHandler(c echo.Context) error {
key := c.Param("key")
db := c.Get("database").(*database.DkfDB)
authUser := c.Get("authUser").(*database.User)
+ csrf, _ := c.Get("csrf").(string)
if !authUser.CanUseChessAnalyze {
return c.Redirect(http.StatusFound, "/")
}
@@ -132,9 +133,22 @@ func ChessGameAnalyzeHandler(c echo.Context) error {
if game.Outcome() == chess.NoOutcome {
return c.String(http.StatusOK, "no outcome")
}
+
+ if c.Request().Method == http.MethodGet && !g.IsAnalyzing() {
+ return c.HTML(http.StatusOK, `
+<style>html, body { background-color: #222; }</style>
+<form method="post">
+ <input type="hidden" name="csrf" value="`+csrf+`" />
+ <input type="number" name="t" value="15" min="15" max=60 />
+ <button type="submit">Analyze</button>
+</form>`)
+ }
+
+ t := utils.Clamp(utils.ParseInt64OrDefault(c.Request().PostFormValue("t"), 15), 15, 60)
+
if g.SetAnalyzing() {
go func() {
- res, err := interceptors.AnalyzeGame(g, game.String())
+ res, err := interceptors.AnalyzeGame(g, game.String(), t)
if err != nil {
logrus.Error(err)
return
diff --git a/pkg/web/handlers/interceptors/chess.go b/pkg/web/handlers/interceptors/chess.go
@@ -618,14 +618,10 @@ func (g *ChessGame) UnsetAnalyzing() {
g.analyzeProgrss = ChessAnalyzeProgress{}
}
-func (b *Chess) IsAnalyzing(key string) (bool, error) {
- b.Lock()
- defer b.Unlock()
- g, ok := b.games[key]
- if !ok {
- return false, errors.New("invalid game key")
- }
- return g.analyzing, nil
+func (g *ChessGame) IsAnalyzing() bool {
+ g.mtx.RLock()
+ defer g.mtx.RUnlock()
+ return g.analyzing
}
func (b *Chess) GetGame(key string) (*ChessGame, error) {
@@ -986,7 +982,7 @@ type AnalyzeResult struct {
Scores []Score
}
-func AnalyzeGame(gg *ChessGame, pgn string) (out AnalyzeResult, err error) {
+func AnalyzeGame(gg *ChessGame, pgn string, t int64) (out AnalyzeResult, err error) {
pgnOpt, _ := chess.PGN(strings.NewReader(pgn))
g := chess.NewGame(pgnOpt)
positions := g.Positions()
@@ -1017,7 +1013,7 @@ func AnalyzeGame(gg *ChessGame, pgn string) (out AnalyzeResult, err error) {
scores := make([]Score, 0)
cps := make([]int, 0)
- t := 15
+ t = utils.Clamp(t, 15, 60)
moveTime := time.Duration((float64(t)/float64(len(positions)-1))*1000) * time.Millisecond
for idx, position := range positions {
diff --git a/pkg/web/web.go b/pkg/web/web.go
@@ -103,6 +103,7 @@ func getMainServer(db *database.DkfDB, i18nBundle *i18n.Bundle, renderer *tmp.Te
authGroup.GET("/chess/:key", handlers.ChessGameHandler)
authGroup.POST("/chess/:key", handlers.ChessGameHandler)
authGroup.GET("/chess/:key/analyze", handlers.ChessGameAnalyzeHandler)
+ authGroup.POST("/chess/:key/analyze", handlers.ChessGameAnalyzeHandler)
authGroup.GET("/chess/:key/form", handlers.ChessGameFormHandler)
authGroup.POST("/chess/:key/form", handlers.ChessGameFormHandler)
authGroup.GET("/chess/:key/stats", handlers.ChessGameStatsHandler)