From 3e9a93fbb11ed2d43b7b1c318109a655dd765865 Mon Sep 17 00:00:00 2001 From: Grail Finder Date: Fri, 4 Jul 2025 21:48:01 +0300 Subject: [PATCH] Chore: remove unused WCMap --- handlers/actions.go | 8 ++++---- handlers/elements.go | 8 +++++--- handlers/timer.go | 3 +-- llmapi/main.go | 3 ++- models/main.go | 42 ++++++++++++++++++++++++++---------------- 5 files changed, 38 insertions(+), 26 deletions(-) diff --git a/handlers/actions.go b/handlers/actions.go index d4c037a..e98eeaf 100644 --- a/handlers/actions.go +++ b/handlers/actions.go @@ -205,10 +205,10 @@ func loadCards(room *models.Room) { fmt.Println("failed to load cards", "error", err) } room.Cards = cards - room.WCMap = make(map[string]models.WordColor) - for _, card := range room.Cards { - room.WCMap[card.Word] = card.Color - } + // room.WCMap = make(map[string]models.WordColor) + // for _, card := range room.Cards { + // room.WCMap[card.Word] = card.Color + // } } func recoverBots() { diff --git a/handlers/elements.go b/handlers/elements.go index 827c2ff..a3be4c8 100644 --- a/handlers/elements.go +++ b/handlers/elements.go @@ -50,7 +50,8 @@ func HandleShowColor(w http.ResponseWriter, r *http.Request) { abortWithError(w, err.Error()) return } - color, exists := fi.Room.WCMap[word] + // color, exists := fi.Room.WCMap[word] + color, exists := fi.Room.FindColor(word) if !exists { abortWithError(w, "word is not found") return @@ -191,8 +192,9 @@ func HandleMarkCard(w http.ResponseWriter, r *http.Request) { abortWithError(w, err.Error()) return } - color, exists := fi.Room.WCMap[word] - log.Debug("got show-color request", "word", word, "color", color) + // color, exists := fi.Room.WCMap[word] + color, exists := fi.Room.FindColor(word) + log.Debug("got mark-card request", "word", word, "color", color) if !exists { abortWithError(w, "word is not found") return diff --git a/handlers/timer.go b/handlers/timer.go index 1f09d31..d0dd319 100644 --- a/handlers/timer.go +++ b/handlers/timer.go @@ -2,7 +2,6 @@ package handlers import ( "context" - "fmt" "gralias/models" "log/slog" "strconv" @@ -55,7 +54,7 @@ func StartTurnTimer(roomID string, timeLeft uint32) { return } timeLeft-- - notify(models.NotifyTurnTimerPrefix+roomID, fmt.Sprintf("%d", timeLeft)) + notify(models.NotifyTurnTimerPrefix+roomID, strconv.FormatUint(uint64(timeLeft), 10)) } } }() diff --git a/llmapi/main.go b/llmapi/main.go index d2b203d..7b33425 100644 --- a/llmapi/main.go +++ b/llmapi/main.go @@ -68,7 +68,8 @@ func (b *Bot) checkGuesses(tempMap map[string]any, room *models.Room) error { } func (b *Bot) checkGuess(word string, room *models.Room) error { - color, exists := room.WCMap[word] + // color, exists := room.WCMap[word] + color, exists := room.FindColor(word) b.log.Debug("bot trying to open card", "word", word, "color", color, "exists", exists, "limit", room.ThisTurnLimit, "opened", room.OpenedThisTurn) diff --git a/models/main.go b/models/main.go index 52d5fd8..6c2a3db 100644 --- a/models/main.go +++ b/models/main.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "gralias/utils" + "strings" "time" "github.com/rs/xid" @@ -146,15 +147,24 @@ type Room struct { TeamWon UserTeam `db:"team_won"` RoomLink string `db:"room_link"` // fields not in db - ActionHistory []Action `db:"-"` - RedTeam Team `db:"-"` - BlueTeam Team `db:"-"` - Cards []WordCard `db:"-"` - WCMap map[string]WordColor `db:"-"` - BotMap map[string]BotPlayer `db:"-"` - Mark CardMark `db:"-"` - LogJournal []string `db:"-"` - Settings GameSettings `db:"-"` + ActionHistory []Action `db:"-"` + RedTeam Team `db:"-"` + BlueTeam Team `db:"-"` + Cards []WordCard `db:"-"` + // WCMap map[string]WordColor `db:"-"` + BotMap map[string]BotPlayer `db:"-"` + Mark CardMark `db:"-"` + LogJournal []string `db:"-"` + Settings GameSettings `db:"-"` +} + +func (r *Room) FindColor(word string) (WordColor, bool) { + for _, card := range r.Cards { + if strings.EqualFold(card.Word, word) { + return card.Color, true + } + } + return "", false } func (r *Room) ClearMarks() { @@ -386,13 +396,13 @@ type WordCard struct { // table: settings type GameSettings struct { - ID uint32 `json:"id" db:"id"` - RoomID string `db:"room_id"` - Language string `json:"language" example:"en" form:"language" db:"language"` - RoomPass string `json:"room_pass" db:"room_pass"` - - RoundTime uint32 `json:"round_time" db:"turn_time"` - CreatedAt time.Time `db:"created_at"` + ID uint32 `json:"id" db:"id"` + RoomID string `db:"room_id"` + Language string `json:"language" example:"en" form:"language" db:"language"` + RoomPass string `json:"room_pass" db:"room_pass"` + + RoundTime uint32 `json:"round_time" db:"turn_time"` + CreatedAt time.Time `db:"created_at"` } // =====