Enha: simplier guesser prompt; player team-role recovery

This commit is contained in:
Grail Finder
2025-06-23 07:48:13 +03:00
parent 07c4d9295d
commit 23cbde0f7a
9 changed files with 188 additions and 65 deletions

View File

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

View File

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

View File

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

View File

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