Feat: which bot to move

This commit is contained in:
Grail Finder
2025-05-20 16:16:57 +03:00
parent 2342c56aed
commit 59cccbbe8e
6 changed files with 81 additions and 12 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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