commit f86a7833414091e74b560cd7ce9e72371ba55876
parent 07ae140164405cb88e691e3c71b097085abd76fd
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Sat, 17 Jun 2023 14:09:06 -0700
make sure only one analyze is done
Diffstat:
2 files changed, 28 insertions(+), 0 deletions(-)
diff --git a/pkg/web/handlers/chess.go b/pkg/web/handlers/chess.go
@@ -128,6 +128,9 @@ func ChessGameAnalyzeHandler(c echo.Context) error {
if err != nil {
return c.Redirect(http.StatusFound, "/")
}
+ if !interceptors.ChessInstance.SetAnalyzing(key) {
+ return c.String(http.StatusOK, "already analyzing")
+ }
game := g.Game
if game.Outcome() == chess.NoOutcome {
return c.String(http.StatusOK, "no outcome")
diff --git a/pkg/web/handlers/interceptors/chess.go b/pkg/web/handlers/interceptors/chess.go
@@ -57,6 +57,7 @@ type ChessGame struct {
Player2 *ChessPlayer
CreatedAt time.Time
piecesCache map[chess.Square]string
+ analyzing bool
}
func newChessPlayer(player database.User) *ChessPlayer {
@@ -580,6 +581,30 @@ func (g *ChessGame) DrawSpectatorCard(moveIdx int, key string, isFlipped, sounds
return g.drawPlayerCard(moveIdx, key, isFlipped, true, false, soundsEnabled, canUseChessAnalyze)
}
+func (b *Chess) SetAnalyzing(key string) bool {
+ b.Lock()
+ defer b.Unlock()
+ g, ok := b.games[key]
+ if !ok {
+ return false
+ }
+ if g.analyzing {
+ return false
+ }
+ g.analyzing = true
+ return true
+}
+
+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 (b *Chess) GetGame(key string) (*ChessGame, error) {
b.Lock()
defer b.Unlock()