From 058d501774d141813c10ecc7cb8c4d98eb29cb4b Mon Sep 17 00:00:00 2001 From: Grail Finder Date: Fri, 4 Jul 2025 12:44:15 +0300 Subject: [PATCH] Fix: save action on give-clue --- crons/main.go | 29 +++++++++++++++++++++++++++++ handlers/game.go | 5 +++++ repos/actions.go | 13 ++++++++++--- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/crons/main.go b/crons/main.go index 306dd5d..28c8273 100644 --- a/crons/main.go +++ b/crons/main.go @@ -24,6 +24,7 @@ func (cm *CronManager) Start() { go func() { for range ticker.C { cm.CleanupRooms() + cm.CleanupActions() } }() } @@ -99,3 +100,31 @@ func (cm *CronManager) CleanupRooms() { } } +func (cm *CronManager) CleanupActions() { + ctx, tx, err := cm.repo.InitTx(context.Background()) + if err != nil { + cm.log.Error("failed to init transaction for actions cleanup", "err", err) + return + } + defer func() { + if r := recover(); r != nil { + if err := tx.Rollback(); err != nil { + cm.log.Error("failed to rollback transaction for actions cleanup", "err", err) + } + panic(r) + } + }() + + if err := cm.repo.ActionsDeleteOrphaned(ctx); err != nil { + cm.log.Error("failed to delete orphaned actions", "err", err) + if err := tx.Rollback(); err != nil { + cm.log.Error("failed to rollback transaction for actions cleanup", "err", err) + } + return + } + + if err := tx.Commit(); err != nil { + cm.log.Error("failed to commit transaction for actions cleanup", "err", err) + } +} + diff --git a/handlers/game.go b/handlers/game.go index 0bccfb1..7db0a38 100644 --- a/handlers/game.go +++ b/handlers/game.go @@ -329,6 +329,7 @@ func HandleGiveClue(w http.ResponseWriter, r *http.Request) { } // === action := models.Action{ + RoomID: fi.Room.ID, Actor: fi.State.Username, ActorColor: string(fi.State.Team), WordColor: string(fi.State.Team), @@ -337,6 +338,10 @@ func HandleGiveClue(w http.ResponseWriter, r *http.Request) { Number: num, } fi.Room.ActionHistory = append(fi.Room.ActionHistory, action) + if err := repo.CreateAction(r.Context(), &action); err != nil { + abortWithError(w, err.Error()) + return + } fi.Room.MimeDone = true fi.Room.ThisTurnLimit = uint8(guessLimitU64) + 1 if guessLimitU64 == 0 { diff --git a/repos/actions.go b/repos/actions.go index 49388e6..5d1b29c 100644 --- a/repos/actions.go +++ b/repos/actions.go @@ -9,9 +9,10 @@ import ( type ActionsRepo interface { ListActions(ctx context.Context, roomID string) ([]models.Action, error) - CreateAction(ctx context.Context, roomID string, action *models.Action) error + CreateAction(ctx context.Context, action *models.Action) error GetLastClue(ctx context.Context, roomID string) (*models.Action, error) DeleteActionsByRoomID(ctx context.Context, roomID string) error + ActionsDeleteOrphaned(ctx context.Context) error } func (p *RepoProvider) ListActions(ctx context.Context, roomID string) ([]models.Action, error) { @@ -23,9 +24,9 @@ func (p *RepoProvider) ListActions(ctx context.Context, roomID string) ([]models return actions, nil } -func (p *RepoProvider) CreateAction(ctx context.Context, roomID string, a *models.Action) error { +func (p *RepoProvider) CreateAction(ctx context.Context, a *models.Action) error { db := getDB(ctx, p.DB) - _, err := db.ExecContext(ctx, `INSERT INTO actions (room_id, actor, actor_color, action_type, word, word_color, number_associated, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, roomID, a.Actor, a.ActorColor, a.Action, a.Word, a.WordColor, a.Number, a.CreatedAt.UnixNano()) + _, err := db.ExecContext(ctx, `INSERT INTO actions (room_id, actor, actor_color, action_type, word, word_color, number_associated, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, a.RoomID, a.Actor, a.ActorColor, a.Action, a.Word, a.WordColor, a.Number, a.CreatedAt.UnixNano()) return err } @@ -43,3 +44,9 @@ func (p *RepoProvider) DeleteActionsByRoomID(ctx context.Context, roomID string) _, err := db.ExecContext(ctx, `DELETE FROM actions WHERE room_id = ?`, roomID) return err } + +func (p *RepoProvider) ActionsDeleteOrphaned(ctx context.Context) error { + db := getDB(ctx, p.DB) + _, err := db.ExecContext(ctx, `DELETE FROM actions WHERE room_id NOT IN (SELECT id FROM rooms)`) + return err +}