From 7ae255cc04135c1d5333f23a3018da5e65f0f107 Mon Sep 17 00:00:00 2001 From: Grail Finder Date: Mon, 7 Jul 2025 07:53:12 +0300 Subject: [PATCH] Enha: clear marks by room id --- handlers/elements.go | 15 ++++++++++----- repos/card_marks.go | 6 ++++++ todos.md | 2 +- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/handlers/elements.go b/handlers/elements.go index 605e2bc..9ce9c3d 100644 --- a/handlers/elements.go +++ b/handlers/elements.go @@ -87,6 +87,7 @@ func HandleShowColor(w http.ResponseWriter, r *http.Request) { fi.Room.ActionHistory = append(fi.Room.ActionHistory, action) // if opened card is of color of opp team, change turn oppositeColor := fi.Room.GetOppositeTeamColor() + var clearMarks bool fi.Room.OpenedThisTurn++ log.Debug("got show-color request", "word", word, "color", color, "limit", fi.Room.ThisTurnLimit, "opened", fi.Room.OpenedThisTurn, @@ -98,7 +99,7 @@ func HandleShowColor(w http.ResponseWriter, r *http.Request) { fi.Room.MimeDone = false fi.Room.OpenedThisTurn = 0 fi.Room.ThisTurnLimit = 0 - fi.Room.ClearMarks() + clearMarks = true StopTurnTimer(fi.Room.ID) } switch string(color) { @@ -118,7 +119,7 @@ func HandleShowColor(w http.ResponseWriter, r *http.Request) { fi.Room.OpenedThisTurn = 0 fi.Room.ThisTurnLimit = 0 fi.Room.ActionHistory = append(fi.Room.ActionHistory, action) - fi.Room.ClearMarks() + clearMarks = true StopTurnTimer(fi.Room.ID) case string(models.WordColorWhite), string(oppositeColor): log.Debug("opened white or opposite color word", "word", word, "opposite-color", oppositeColor) @@ -127,6 +128,7 @@ func HandleShowColor(w http.ResponseWriter, r *http.Request) { fi.Room.MimeDone = false fi.Room.OpenedThisTurn = 0 fi.Room.ThisTurnLimit = 0 + clearMarks = true StopTurnTimer(fi.Room.ID) // check if no cards left => game over if fi.Room.BlueCounter == 0 { @@ -142,7 +144,6 @@ func HandleShowColor(w http.ResponseWriter, r *http.Request) { Action: models.ActionTypeGameOver, } fi.Room.ActionHistory = append(fi.Room.ActionHistory, action) - fi.Room.ClearMarks() } if fi.Room.RedCounter == 0 { // red won @@ -157,7 +158,6 @@ func HandleShowColor(w http.ResponseWriter, r *http.Request) { Action: models.ActionTypeGameOver, } fi.Room.ActionHistory = append(fi.Room.ActionHistory, action) - fi.Room.ClearMarks() } default: // same color as the team // check if game over @@ -173,7 +173,12 @@ func HandleShowColor(w http.ResponseWriter, r *http.Request) { Action: models.ActionTypeGameOver, } fi.Room.ActionHistory = append(fi.Room.ActionHistory, action) - fi.Room.ClearMarks() + } + } + if clearMarks { + fi.Room.ClearMarks() + if err := repo.CardMarksRemoveByRoomID(r.Context(), fi.Room.ID); err != nil { + log.Error("failed to remove marks", "error", err, "room_id", fi.Room.ID) } } if err := saveFullInfo(r.Context(), fi); err != nil { diff --git a/repos/card_marks.go b/repos/card_marks.go index 255d0ca..621cf0b 100644 --- a/repos/card_marks.go +++ b/repos/card_marks.go @@ -12,6 +12,7 @@ type CardMarksRepo interface { CardMarksAdd(ctx context.Context, cm *models.CardMark) error CardMarksRemove(ctx context.Context, cardID uint32, username string) error CardMarksByRoomID(ctx context.Context, roomID string) ([]models.CardMark, error) + CardMarksRemoveByRoomID(ctx context.Context, roomID string) error } func (r *RepoProvider) CardMarksByCardID(ctx context.Context, cardID uint32) ([]models.CardMark, error) { @@ -36,3 +37,8 @@ func (r *RepoProvider) CardMarksByRoomID(ctx context.Context, roomID string) ([] err := sqlx.SelectContext(ctx, getDB(ctx, r.DB), &cardMarks, "SELECT * FROM card_marks WHERE card_id IN (select id from word_cards where room_id = ?)", roomID) return cardMarks, err } +func (r *RepoProvider) CardMarksRemoveByRoomID(ctx context.Context, roomID string) error { + db := getDB(ctx, r.DB) + _, err := db.ExecContext(ctx, "DELETE FROM card_marks WHERE room_id = ?;", roomID) + return err +} diff --git a/todos.md b/todos.md index 1d42a85..0119f66 100644 --- a/todos.md +++ b/todos.md @@ -78,7 +78,7 @@ - old cards are still around; + - bot mime makes a clue -> no update in the room for players; + -- red moves after bot gave (metal - 3) ended after two guesses (showed in logs, that opened 3. double click on the same card? or somewhere counter is not dropped?); the first guess moved counter to 2, in logs only two requests as expected; +- red moves after bot gave (metal - 3) ended after two guesses (showed in logs, that opened 3. double click on the same card? or somewhere counter is not dropped?); the first guess moved counter to 2, in logs only two requests as expected; + - bot mime gve a blue -> timer did not start; timer should be in third package, maybe in crons; + - log journal is not shown on the page; - marks did not clear after end of the turn (feature?)