From 59cccbbe8e87a25f35391c787b945ca59d33b8d4 Mon Sep 17 00:00:00 2001 From: Grail Finder Date: Tue, 20 May 2025 16:16:57 +0300 Subject: [PATCH] Feat: which bot to move --- handlers/actions.go | 5 +++++ handlers/elements.go | 2 +- handlers/game.go | 29 ++++++++++++++++++++++--- llmapi/main.go | 5 +++++ models/main.go | 51 +++++++++++++++++++++++++++++++++++++------- todos.md | 1 + 6 files changed, 81 insertions(+), 12 deletions(-) diff --git a/handlers/actions.go b/handlers/actions.go index 0c70821..18ef302 100644 --- a/handlers/actions.go +++ b/handlers/actions.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "golias/broker" + "golias/llmapi" "golias/models" "golias/utils" "golias/wordloader" @@ -121,6 +122,10 @@ func loadState(username string) (*models.UserState, error) { return resp, nil } +func loadBot(botName, roomID string) (*llmapi.Bot, error) { + return nil, nil +} + func getAllNames() []string { names := []string{} // will not scale diff --git a/handlers/elements.go b/handlers/elements.go index a601062..1ae0795 100644 --- a/handlers/elements.go +++ b/handlers/elements.go @@ -85,7 +85,7 @@ func HandleShowColor(w http.ResponseWriter, r *http.Request) { fi.Room.IsRunning = false fi.Room.IsOver = true fi.Room.TeamWon = oppositeColor - case "white", oppositeColor: + case "white", string(oppositeColor): // end turn fi.Room.TeamTurn = oppositeColor } diff --git a/handlers/game.go b/handlers/game.go index 720d83a..f79799b 100644 --- a/handlers/game.go +++ b/handlers/game.go @@ -3,6 +3,7 @@ package handlers import ( "context" "errors" + "fmt" "golias/models" "html/template" "net/http" @@ -146,9 +147,9 @@ func HandleEndTurn(w http.ResponseWriter, r *http.Request) { return } // check if one who pressed it is from the team who has the turn - if fi.Room.TeamTurn != string(fi.State.Team) { - err = errors.New("unexpected team turn:" + fi.Room.TeamTurn) - abortWithError(w, err.Error()) + if fi.Room.TeamTurn != fi.State.Team { + msg := fmt.Sprintln("unexpected team turn:" + fi.Room.TeamTurn) + abortWithError(w, msg) return } fi.Room.ChangeTurn() @@ -162,6 +163,17 @@ func HandleEndTurn(w http.ResponseWriter, r *http.Request) { abortWithError(w, err.Error()) return } + if botName := fi.Room.WhichBotToMove(); botName != "" { + // get bot from memcache + bot, err := loadBot(botName, fi.Room.ID) + if err != nil { + log.Error("failed to load bot", "bot_name", botName, "room_id", fi.Room.ID) + abortWithError(w, err.Error()) + return + } + // send signal to bot + bot.SignalsCh <- true + } notify(models.NotifyRoomUpdatePrefix+fi.Room.ID, "") tmpl.ExecuteTemplate(w, "base", fi) } @@ -203,6 +215,17 @@ func HandleStartGame(w http.ResponseWriter, r *http.Request) { abortWithError(w, err.Error()) return } + if botName := fi.Room.WhichBotToMove(); botName != "" { + // get bot from memcache + bot, err := loadBot(botName, fi.Room.ID) + if err != nil { + log.Error("failed to load bot", "bot_name", botName, "room_id", fi.Room.ID) + abortWithError(w, err.Error()) + return + } + // send signal to bot + bot.SignalsCh <- true + } // to update only the room that should be updated notify(models.NotifyRoomUpdatePrefix+fi.Room.ID, "") // notify(models.NotifyBacklogPrefix+fi.Room.ID, "game started") diff --git a/llmapi/main.go b/llmapi/main.go index 879cad1..3bc8d0a 100644 --- a/llmapi/main.go +++ b/llmapi/main.go @@ -80,6 +80,11 @@ func NewBot(role, team, name, roomID string, cfg *config.Config) (*Bot, error) { return nil, errors.New("cannot join after game started") } room.PlayerList = append(room.PlayerList, name) + bp := models.BotPlayer{ + Role: models.StrToUserRole(role), + Team: models.StrToUserTeam(team), + } + room.BotMap[name] = bp switch team { case "red": if role == "mime" { diff --git a/models/main.go b/models/main.go index b785f2d..0b46359 100644 --- a/models/main.go +++ b/models/main.go @@ -47,6 +47,11 @@ type Action struct { Number string // for clue } +type BotPlayer struct { + Role UserRole // gueeser | mime + Team UserTeam +} + type Room struct { ID string `json:"id" db:"id"` CreatedAt time.Time `json:"created_at" db:"created_at"` @@ -56,12 +61,13 @@ type Room struct { CreatorName string `json:"creator_name"` PlayerList []string `json:"player_list"` ActionHistory []Action - TeamTurn string + TeamTurn UserTeam RedTeam Team BlueTeam Team Cards []WordCard WCMap map[string]WordColor - Result uint8 // 0 for unknown; 1 is win for red; 2 if for blue; + BotMap map[string]BotPlayer // key is bot name + Result uint8 // 0 for unknown; 1 is win for red; 2 if for blue; BlueCounter uint8 RedCounter uint8 RedTurn bool // false is blue turn @@ -73,19 +79,48 @@ type Room struct { RoundTime int32 `json:"round_time"` // ProgressPct uint32 `json:"progress_pct"` IsOver bool - TeamWon string // blue | red + TeamWon UserTeam // blue | red } -func (r *Room) GetOppositeTeamColor() string { +// WhichBotToMove returns bot name that have to move or empty string +func (r *Room) WhichBotToMove() string { + if !r.IsRunning { + return "" + } switch r.TeamTurn { - case "red": - return "blue" - case "blue": - return "red" + case UserTeamBlue: + if !r.MimeDone { + _, ok := r.BotMap[r.BlueTeam.Mime] + if ok { + return r.BlueTeam.Mime + } + } + // check gussers + case UserTeamRed: + if !r.MimeDone { + _, ok := r.BotMap[r.RedTeam.Mime] + if ok { + return r.RedTeam.Mime + } + } + // check gussers + default: + // how did we got here? + return "" } return "" } +func (r *Room) GetOppositeTeamColor() UserTeam { + switch r.TeamTurn { + case "red": + return UserTeamBlue + case "blue": + return UserTeamRed + } + return UserTeamNone +} + func (r *Room) UpdateCounter() { redCounter := uint8(0) blueCounter := uint8(0) diff --git a/todos.md b/todos.md index 146cb08..093de79 100644 --- a/todos.md +++ b/todos.md @@ -6,6 +6,7 @@ - invite link; - login with invite link; - add html icons of whos turn it is (like an image of big ? when mime is thinking); +- there two places for bot to check if its its move: start-game; end-turn; #### sse points - clue sse update;