Fix: save action on give-clue

This commit is contained in:
Grail Finder
2025-07-04 12:44:15 +03:00
parent c2d6812230
commit 058d501774
3 changed files with 44 additions and 3 deletions

View File

@ -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)
}
}

View File

@ -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 {

View File

@ -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
}