Enha: simplier guesser prompt; player team-role recovery
This commit is contained in:
@ -58,6 +58,9 @@
|
||||
{{end}}
|
||||
</ul>
|
||||
</div>
|
||||
<div sse-swap="journal_{{.Room.ID}}">
|
||||
sse div
|
||||
<div>
|
||||
<div id="cardtable">
|
||||
{{template "cardtable" .Room}}
|
||||
</div>
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
157
llmapi/main.go
157
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,68 +103,28 @@ func convertToSliceOfStrings(value any) ([]string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// StartBot
|
||||
func (b *Bot) StartBot() {
|
||||
for {
|
||||
select {
|
||||
case <-SignalChanMap[b.BotName]:
|
||||
b.log.Debug("got signal", "bot-team", b.Team, "bot-role", b.Role)
|
||||
// get room cards and actions
|
||||
room, err := getRoomByID(b.RoomID)
|
||||
if err != nil {
|
||||
b.log.Error("bot loop", "error", err)
|
||||
continue
|
||||
}
|
||||
// form prompt
|
||||
prompt := b.BuildPrompt(room)
|
||||
b.log.Debug("got prompt", "prompt", prompt)
|
||||
// call llm
|
||||
llmResp, err := b.CallLLM(prompt)
|
||||
if err != nil {
|
||||
b.log.Error("bot loop", "error", err)
|
||||
continue
|
||||
}
|
||||
tempMap, err := b.LLMParser.ParseBytes(llmResp)
|
||||
if err != nil {
|
||||
b.log.Error("bot loop", "error", err, "resp", string(llmResp))
|
||||
continue
|
||||
}
|
||||
eventName := models.NotifyBacklogPrefix + room.ID
|
||||
eventPayload := ""
|
||||
switch b.Role {
|
||||
case models.UserRoleMime:
|
||||
mimeResp := MimeResp{}
|
||||
b.log.Info("mime resp log", "mimeResp", tempMap)
|
||||
mimeResp.Clue = tempMap["clue"].(string)
|
||||
mimeResp.Number = tempMap["number"].(string)
|
||||
action := models.Action{
|
||||
Actor: b.BotName,
|
||||
ActorColor: b.Team,
|
||||
WordColor: b.Team,
|
||||
Action: "gave clue",
|
||||
Word: mimeResp.Clue,
|
||||
Number: mimeResp.Number,
|
||||
}
|
||||
room.ActionHistory = append(room.ActionHistory, action)
|
||||
room.MimeDone = true
|
||||
eventPayload = mimeResp.Clue + mimeResp.Number
|
||||
guessLimitU64, err := strconv.ParseUint(mimeResp.Number, 10, 8)
|
||||
if err != nil {
|
||||
b.log.Warn("failed to parse bot given limit", "mimeResp", mimeResp, "bot_name", b.BotName)
|
||||
}
|
||||
room.ThisTurnLimit = uint8(guessLimitU64)
|
||||
case models.UserRoleGuesser:
|
||||
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)
|
||||
continue
|
||||
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 {
|
||||
continue
|
||||
return fmt.Errorf("fn: checkGuess; %s does not exists", word)
|
||||
}
|
||||
room.RevealSpecificWord(word)
|
||||
room.UpdateCounter()
|
||||
@ -204,10 +164,94 @@ func (b *Bot) StartBot() {
|
||||
}
|
||||
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)
|
||||
if err != nil {
|
||||
b.log.Error("bot loop", "error", err)
|
||||
continue
|
||||
}
|
||||
// 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
|
||||
}
|
||||
eventName := models.NotifyBacklogPrefix + room.ID
|
||||
eventPayload := ""
|
||||
switch b.Role {
|
||||
case models.UserRoleMime:
|
||||
mimeResp := MimeResp{}
|
||||
b.log.Info("mime resp log", "mimeResp", tempMap)
|
||||
mimeResp.Clue = tempMap["clue"].(string)
|
||||
mimeResp.Number = tempMap["number"].(string)
|
||||
action := models.Action{
|
||||
Actor: b.BotName,
|
||||
ActorColor: b.Team,
|
||||
WordColor: b.Team,
|
||||
Action: "gave clue",
|
||||
Word: mimeResp.Clue,
|
||||
Number: mimeResp.Number,
|
||||
}
|
||||
room.ActionHistory = append(room.ActionHistory, action)
|
||||
room.MimeDone = true
|
||||
eventPayload = mimeResp.Clue + mimeResp.Number
|
||||
guessLimitU64, err := strconv.ParseUint(mimeResp.Number, 10, 8)
|
||||
if err != nil {
|
||||
b.log.Warn("failed to parse bot given limit", "mimeResp", mimeResp, "bot_name", b.BotName)
|
||||
}
|
||||
room.ThisTurnLimit = uint8(guessLimitU64)
|
||||
case models.UserRoleGuesser:
|
||||
// // 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)
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
|
@ -13,4 +13,5 @@ var (
|
||||
NotifyRoomListUpdate = "roomlistupdate"
|
||||
NotifyRoomUpdatePrefix = "roomupdate_"
|
||||
NotifyBacklogPrefix = "backlog_"
|
||||
NotifyJournalPrefix = "journal_"
|
||||
)
|
||||
|
@ -61,7 +61,7 @@ type Room struct {
|
||||
RoomPass string `json:"room_pass"`
|
||||
RoomLink string
|
||||
CreatorName string `json:"creator_name"`
|
||||
PlayerList []string `json:"player_list"`
|
||||
// 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")
|
||||
@ -233,7 +253,7 @@ func (rr *RoomReq) CreateRoom(creator string) *Room {
|
||||
RoomPass: rr.RoomPass,
|
||||
ID: roomID,
|
||||
CreatedAt: time.Now(),
|
||||
PlayerList: []string{creator},
|
||||
// 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 {
|
||||
|
Reference in New Issue
Block a user