Enha: simplier guesser prompt; player team-role recovery
This commit is contained in:
		| @@ -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) | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Grail Finder
					Grail Finder