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