diff --git a/components/room.html b/components/room.html index bb1c84f..e49d4b8 100644 --- a/components/room.html +++ b/components/room.html @@ -58,6 +58,9 @@ {{end}} +
+ sse div +
{{template "cardtable" .Room}}
diff --git a/handlers/actions.go b/handlers/actions.go index 2a9386c..5d91684 100644 --- a/handlers/actions.go +++ b/handlers/actions.go @@ -275,6 +275,24 @@ func listBots() map[string]map[string]string { return resp } +// get players +func listPlayers() map[string]map[string]string { + cacheMap := memcache.GetAll() + resp := make(map[string]map[string]string) + // no way to know if room is public until unmarshal -_-; + for key, value := range cacheMap { + if strings.HasPrefix(key, models.CacheStatePrefix) { + playerMap := make(map[string]string) + if err := json.Unmarshal(value, &playerMap); err != nil { + log.Warn("failed to unmarshal player", "error", err) + continue + } + resp[playerMap["Username"]] = playerMap + } + } + return resp +} + func notify(event, msg string) { Notifier.Notifier <- broker.NotificationEvent{ EventName: event, @@ -317,3 +335,34 @@ func recoverBot(bm map[string]string) error { } return nil } + +func recoverPlayers() { + players := listPlayers() + for playerName, playerMap := range players { + if err := recoverPlayer(playerMap); err != nil { + log.Warn("failed to recover player", "playerName", playerName, "error", err) + } + } +} + +func recoverPlayer(pm map[string]string) error { + // check if room still exists + room, err := getRoomByID(pm["RoomID"]) + if err != nil { + return fmt.Errorf("no such room: %s; err: %w", pm["RoomID"], err) + } + log.Debug("recovering player", "player", pm) + role, team, ok := room.GetPlayerByName(pm["Username"]) + if !ok { + return fmt.Errorf("failed to find player %s in the room %v", pm["Username"], room) + } + // pm["Role"] = string(role) + // pm["Team"] = string(team) + us := &models.UserState{ + Username: pm["Username"], + RoomID: pm["RoomID"], + Team: team, + Role: role, + } + return saveState(pm["Username"], us) +} diff --git a/handlers/auth.go b/handlers/auth.go index 1272427..c740ee2 100644 --- a/handlers/auth.go +++ b/handlers/auth.go @@ -96,7 +96,7 @@ func HandleFrontLogin(w http.ResponseWriter, r *http.Request) { abortWithError(w, err.Error()) return } - room.PlayerList = append(room.PlayerList, fi.State.Username) + // room.PlayerList = append(room.PlayerList, fi.State.Username) fi.State.RoomID = room.ID fi.Room = room fi.List = nil diff --git a/handlers/game.go b/handlers/game.go index a3d00fe..2f1c261 100644 --- a/handlers/game.go +++ b/handlers/game.go @@ -204,7 +204,7 @@ func HandleJoinRoom(w http.ResponseWriter, r *http.Request) { // abortWithError(w, err.Error()) return } - room.PlayerList = append(room.PlayerList, fi.State.Username) + // room.PlayerList = append(room.PlayerList, fi.State.Username) fi.State.RoomID = room.ID fi.Room = room fi.List = nil diff --git a/handlers/handlers.go b/handlers/handlers.go index 749ce60..b9eefb7 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -31,6 +31,8 @@ func init() { // bot loader // check the rooms if it has bot_{digits} in them, create bots if have recoverBots() + // if player has a roomID, but no team and role, try to recover + recoverPlayers() } func HandlePing(w http.ResponseWriter, r *http.Request) { @@ -85,7 +87,7 @@ func HandleExit(w http.ResponseWriter, r *http.Request) { abortWithError(w, err.Error()) return } - if len(exitedRoom.PlayerList) == 0 || creatorLeft { + if creatorLeft { removeRoom(exitedRoom.ID) // TODO: notify users if creator left // and throw them away diff --git a/llmapi/main.go b/llmapi/main.go index ad5c962..941dc9d 100644 --- a/llmapi/main.go +++ b/llmapi/main.go @@ -24,7 +24,7 @@ var ( MimePrompt = `we are playing alias;\nyou are a mime (player who gives a clue of one noun word and number of cards you expect them to open) of the %s team (people who would guess by your clue want open the %s cards);\nplease return your clue, number of cards to open and what words you mean them to find using that clue in json like:\n{\n\"clue\": \"one-word-noun\",\n\"number\": \"number-from-0-to-9\",\n\"words_I_mean_my_team_to_open\": [\"this\", \"that\", ...]\n}\nthe team who openes all their cards first wins.\nplease return json only.\nunopen Blue cards left: %d;\nunopen Red cards left: %d;\nhere is the game info in json:\n%s` // TODO: simplify; bot gets confused; so show it only unrevealed cards and last clue (maybe older clues as well); GuesserPrompt = `we are playing alias;\nyou are to guess words of the %s team (you want open %s cards) by given clue and a number of meant guesses;\nplease return your guesses and words that could be meant by the clue, but you do not wish to open yet, in json like:\n{\n\"guesses\": [\"word1\", \"word2\", ...],\n\"could_be\": [\"this\", \"that\", ...]\n}\nthe team who openes all their cards first wins.\nplease return json only.\nunopen Blue cards left: %d;\nunopen Red cards left: %d;\nhere is the cards (and other info), you need to choose revealed==false words:\n%s` - GuesserSimplePrompt = `we are playing game of alias;\n you were given a clue: \"%s\";\nnumber of words you can open with that clue is:%s;\nplease return your guesses and words that could be meant by the clue, but you do not wish to open yet, in json like:\n{\n\"guesses\": [\"word1\", \"word2\", ...],\n\"could_be\": [\"this\", \"that\", ...]\n}\nhere is the words that left:\n%s` + GuesserSimplePrompt = `we are playing game of alias;\n you were given a clue: \"%s\";\nplease return your guess and words that could be meant by the clue, but you do not wish to open yet, in json like:\n{\n\"guess\": "most_relevant_word_to_the_clue",\n\"could_be\": [\"this\", \"that\", ...]\n}\nhere is the words that left:\n%s` ) type DSResp struct { @@ -103,11 +103,79 @@ func convertToSliceOfStrings(value any) ([]string, error) { } } +func (b *Bot) checkGuesses(tempMap map[string]any, room *models.Room) error { + guesses, err := convertToSliceOfStrings(tempMap["guesses"]) + if err != nil { + b.log.Warn("failed to parse bot given guesses", "mimeResp", tempMap, "bot_name", b.BotName) + return err + } + for _, word := range guesses { + if err := b.checkGuess(word, room); err != nil { + // log error + b.log.Warn("failed to check guess", "mimeResp", tempMap, "bot_name", b.BotName, "guess", word) + return err + } + } + return nil +} + +func (b *Bot) checkGuess(word string, room *models.Room) error { + color, exists := room.WCMap[word] + b.log.Debug("bot trying to open card", "word", word, "color", + color, "exists", exists) + if !exists { + return fmt.Errorf("fn: checkGuess; %s does not exists", word) + } + room.RevealSpecificWord(word) + room.UpdateCounter() + action := models.Action{ + Actor: b.BotName, + ActorColor: b.Team, + WordColor: string(color), + Action: "guessed", + Word: word, + } + room.ActionHistory = append(room.ActionHistory, action) + // if opened card is of color of opp team, change turn + oppositeColor := room.GetOppositeTeamColor() + switch string(color) { + case "black": + // game over + room.IsRunning = false + room.IsOver = true + room.TeamWon = oppositeColor + case "white", string(oppositeColor): + // end turn + room.TeamTurn = oppositeColor + room.MimeDone = false + } + // check if no cards left => game over + if room.BlueCounter == 0 { + // blue won + room.IsRunning = false + room.IsOver = true + room.TeamWon = "blue" + } + if room.RedCounter == 0 { + // red won + room.IsRunning = false + room.IsOver = true + room.TeamWon = "red" + } + if err := saveRoom(room); err != nil { + b.log.Error("failed to save room", "room", room) + err = fmt.Errorf("fn: checkGuess, failed to save room; err: %w", err) + return err + } + return nil +} + // StartBot func (b *Bot) StartBot() { for { select { case <-SignalChanMap[b.BotName]: + botJournalName := models.NotifyJournalPrefix + b.RoomID b.log.Debug("got signal", "bot-team", b.Team, "bot-role", b.Role) // get room cards and actions room, err := getRoomByID(b.RoomID) @@ -118,14 +186,26 @@ func (b *Bot) StartBot() { // form prompt prompt := b.BuildPrompt(room) b.log.Debug("got prompt", "prompt", prompt) + broker.Notifier.Notifier <- broker.NotificationEvent{ + EventName: botJournalName, + Payload: prompt, + } // call llm llmResp, err := b.CallLLM(prompt) if err != nil { + broker.Notifier.Notifier <- broker.NotificationEvent{ + EventName: botJournalName, + Payload: "failed to get bot resp", + } b.log.Error("bot loop", "error", err) continue } tempMap, err := b.LLMParser.ParseBytes(llmResp) if err != nil { + broker.Notifier.Notifier <- broker.NotificationEvent{ + EventName: botJournalName, + Payload: "failed to parse bot resp", + } b.log.Error("bot loop", "error", err, "resp", string(llmResp)) continue } @@ -154,60 +234,24 @@ func (b *Bot) StartBot() { } room.ThisTurnLimit = uint8(guessLimitU64) case models.UserRoleGuesser: - guesses, err := convertToSliceOfStrings(tempMap["guesses"]) - if err != nil { - b.log.Warn("failed to parse bot given guesses", "mimeResp", tempMap, "bot_name", b.BotName) - continue + // // deprecated + // if err := b.checkGuesses(tempMap, room); err != nil { + // b.log.Warn("failed to check guess", "mimeResp", tempMap, "bot_name", b.BotName) + // continue + // } + guess, ok := tempMap["guess"].(string) + if !ok || guess == "" { + b.log.Warn("failed to parse guess", "mimeResp", tempMap, "bot_name", b.BotName) } - for _, word := range guesses { - color, exists := room.WCMap[word] - b.log.Debug("bot trying to open card", "word", word, "color", - color, "exists", exists) - if !exists { - continue - } - room.RevealSpecificWord(word) - room.UpdateCounter() - action := models.Action{ - Actor: b.BotName, - ActorColor: b.Team, - WordColor: string(color), - Action: "guessed", - Word: word, - } - room.ActionHistory = append(room.ActionHistory, action) - // if opened card is of color of opp team, change turn - oppositeColor := room.GetOppositeTeamColor() - switch string(color) { - case "black": - // game over - room.IsRunning = false - room.IsOver = true - room.TeamWon = oppositeColor - case "white", string(oppositeColor): - // end turn - room.TeamTurn = oppositeColor - room.MimeDone = false - } - // check if no cards left => game over - if room.BlueCounter == 0 { - // blue won - room.IsRunning = false - room.IsOver = true - room.TeamWon = "blue" - } - if room.RedCounter == 0 { - // red won - room.IsRunning = false - room.IsOver = true - room.TeamWon = "red" - } - if err := saveRoom(room); err != nil { - b.log.Error("failed to save room", "room", room) - continue - } + if err := b.checkGuess(guess, room); err != nil { + b.log.Warn("failed to check guess", "mimeResp", tempMap, "bot_name", b.BotName, "guess", guess) } b.log.Info("mime resp log", "guesserResp", tempMap) + couldBe, err := convertToSliceOfStrings(tempMap["could_be"]) + if err != nil { + b.log.Warn("failed to parse could_be", "bot_resp", tempMap, "bot_name", b.BotName) + } + room.LogJournal = append(room.LogJournal, fmt.Sprintf("%s also considered this: %v", b.BotName, couldBe)) eventName = models.NotifyRoomUpdatePrefix + room.ID eventPayload = "" default: @@ -262,7 +306,7 @@ func NewBot(role, team, name, roomID string, cfg *config.Config, recovery bool) if role == "mime" && room.IsRunning && !recovery { return nil, errors.New("cannot join after game started") } - room.PlayerList = append(room.PlayerList, name) + // room.PlayerList = append(room.PlayerList, name) bp := models.BotPlayer{ Role: models.StrToUserRole(role), Team: models.StrToUserTeam(team), @@ -343,6 +387,9 @@ func (b *Bot) BuildSimpleGuesserPrompt(room *models.Room) string { clue := room.ActionHistory[len(room.ActionHistory)-1].Word number := room.ActionHistory[len(room.ActionHistory)-1].Number words := make([]string, len(room.Cards)) + for i, card := range room.Cards { + words[i] = card.Word + } return fmt.Sprintf(GuesserSimplePrompt, clue, number, words) } diff --git a/models/keys.go b/models/keys.go index ee4fc76..aa51b6c 100644 --- a/models/keys.go +++ b/models/keys.go @@ -13,4 +13,5 @@ var ( NotifyRoomListUpdate = "roomlistupdate" NotifyRoomUpdatePrefix = "roomupdate_" NotifyBacklogPrefix = "backlog_" + NotifyJournalPrefix = "journal_" ) diff --git a/models/main.go b/models/main.go index ff8f6c4..a8bf061 100644 --- a/models/main.go +++ b/models/main.go @@ -58,10 +58,10 @@ type Room struct { ID string `json:"id" db:"id"` CreatedAt time.Time `json:"created_at" db:"created_at"` // RoomName string `json:"room_name"` - RoomPass string `json:"room_pass"` - RoomLink string - CreatorName string `json:"creator_name"` - PlayerList []string `json:"player_list"` + RoomPass string `json:"room_pass"` + RoomLink string + CreatorName string `json:"creator_name"` + // PlayerList []string `json:"player_list"` ActionHistory []Action TeamTurn UserTeam RedTeam Team @@ -88,6 +88,26 @@ type Room struct { LogJournal []string } +func (r *Room) GetPlayerByName(name string) (role UserRole, team UserTeam, found bool) { + if r.RedTeam.Mime == name { + return "mime", "red", true + } + if r.BlueTeam.Mime == name { + return "mime", "blue", true + } + for _, guesser := range r.RedTeam.Guessers { + if guesser == name { + return "guesser", "red", true + } + } + for _, guesser := range r.BlueTeam.Guessers { + if guesser == name { + return "guesser", "blue", true + } + } + return "", "", false +} + func (r *Room) CanStart() error { if r.IsRunning { return errors.New("cannot start; game is already running") @@ -230,10 +250,10 @@ func (rr *RoomReq) CreateRoom(creator string) *Room { roomID := xid.New().String() return &Room{ // RoomName: , - RoomPass: rr.RoomPass, - ID: roomID, - CreatedAt: time.Now(), - PlayerList: []string{creator}, + RoomPass: rr.RoomPass, + ID: roomID, + CreatedAt: time.Now(), + // PlayerList: []string{creator}, CreatorName: creator, BotMap: make(map[string]BotPlayer), } @@ -249,7 +269,7 @@ type FullInfo struct { } func (f *FullInfo) ExitRoom() *Room { - f.Room.PlayerList = utils.RemoveFromSlice(f.State.Username, f.Room.PlayerList) + // f.Room.PlayerList = utils.RemoveFromSlice(f.State.Username, f.Room.PlayerList) f.Room.RedTeam.Guessers = utils.RemoveFromSlice(f.State.Username, f.Room.RedTeam.Guessers) f.Room.BlueTeam.Guessers = utils.RemoveFromSlice(f.State.Username, f.Room.BlueTeam.Guessers) if f.Room.RedTeam.Mime == f.State.Username { diff --git a/todos.md b/todos.md index 4cf8851..357912c 100644 --- a/todos.md +++ b/todos.md @@ -16,6 +16,7 @@ - gameover to backlog; - ended turn action to backlog; - clear indication that model (llm) is thinking / answered; +- instead of guessing all words at ones, ask only for 1 word to be open. #### sse points - clue sse update;