Пример #1
0
 def assign_nick(self, full_name, nick_name):
     if full_name not in self.players:
         pass
     elif nick_name in Caseless(self.nick_dict.values()):
         raise GameError("Nick already in use. Defaulting to full name.")
     else:
         self.nick_dict[full_name] = nick_name
Пример #2
0
 async def private_info(self, user, role):
     '''async coroutine function to send each player their relevant private
     information given their particular role in the game.
     Takes a discord.py User model and a Role object.
     '''
     await user.send("You are " + str(role))
     if role.can_see_spies:
         if role.is_res:         # Merlin
             known_spies = [self.get_nick(pl) for pl in self.players
                             if self.players[pl].role.spy_seen_by_merl]
         else:                   # spy seeing fellow (non-Oberon) spies
             known_spies = [self.get_nick(pl) for pl in self.players
                             if self.players[pl].role.spy_seen_by_spies]
         if len(known_spies) == 0:
             known_spies = ["Unknown"]
         if self.number.spies > len(known_spies):
             known_spies.append("another unknown player")
         await user.send(
             "The following players are SPIES: " + ", ".join(known_spies))
     if role.can_see_merl:       # Percival
         merls = [self.get_nick(pl) for pl in self.players
                     if self.players[pl].role.looks_like_merl]
         if len(merls) == 1:     # no Morgana in game
             await user.send("MERLIN is " + merls[0])
         elif len(merls) == 2:   # Merlin and Morgana
             await user.send("MERLIN is either {0} or {1}".format(*merls))
         else:
             raise GameError('''Invalid number of Merlin(s)/Morgana(s).
                 Dumping: {0}'''.format(self.__dict__))
Пример #3
0
 def start(self):
     '''Pretty self-explanatory. Starts the game, sets flags as such,
     initiates many starting values for later reference including turn order
     and assigning roles randomly using the random.shuffle function. Adds
     Mission 1 to the game, which itself creates Round 1.
     '''
     if self.has_started:
         raise GameError("Game has already started!")
     self.has_started = True
     self.number = Number(len(self.players))
     self.order = [name for name in self.players] # with ID#
     shuffle(self.order)
     self.all_roles = self.generate_roles()
     self.special_roles = [r for r in self.all_roles if r not in VANROLES]
     shuffle(self.all_roles)
     for i, player in enumerate(self.order):
         self.players[player].assign_role(self.all_roles[i])
     shuffle(self.all_roles) # hide the role order info from assignment
     self.add_mission(1, self.li)
Пример #4
0
 def generate_roles(self):
     '''uses self.special_roles (a list of up to five optional roles)
     and self.number (object that handles the numbers of types of players)
     to generate a list of all roles in game
     '''
     def role_swap(lst, search_for, replace_with):
         '''A helper function that replaces the first instance of str
         search_for in list lst with new value str replace_with and returns
         the new modified lst. Returns -1 if search_for was not found in lst.
         '''
         for i, val in enumerate(lst):
             if val == search_for:
                 lst[i] = replace_with
                 return lst
         return -1
     roles = [VANSPY] * self.number.spies + [VANRES] * self.number.res
     if MERLIN in self.special_roles:
         roles = role_swap(roles, VANRES, MERLIN)
         roles = role_swap(roles, VANSPY, ASSASSIN)
     if PERCIVAL in self.special_roles:
         roles = role_swap(roles, VANRES, PERCIVAL)
     if MORGANA in self.special_roles:
         roles = role_swap(roles, VANSPY, MORGANA)
     if MORDRED in self.special_roles:
         if VANSPY in roles:
             roles = role_swap(roles, VANSPY, MORDRED)
         elif ASSASSIN in roles:
             roles = role_swap(roles, ASSASSIN, MORDASS)
         else:
             raise GameError("Invalid roles; wrong number of spies")
     if OBERON in self.special_roles:
         if VANSPY in roles:
             roles = role_swap(roles, VANSPY, OBERON)
         elif ASSASSIN in roles and MORDRED in roles:
             roles = role_swap(roles, MORDRED, MORDASS)
             roles = role_swap(roles, ASSASSIN, OBERON)
         else:
             print("Oberon role being ignored. Insufficient spies.")
     return roles
Пример #5
0
 def curr_round(self):
     if self.missions:
         return self.missions[-1].rounds[-1]
     raise GameError("Game has not yet begun; no mission exists")
Пример #6
0
 def add_mission(self, n, leader_index):
     if not (1 <= n <= 5):
         raise GameError("Invalid mission number: %s" % n)
     self.missions.append(Mission(n))
     self.missions[-1].add_round(leader_index)
Пример #7
0
 def remove_player(self, sender):
     if self.has_started:
         raise GameError("Cannot remove player from game in progress!")
     del self.players[str(sender)]
     del self.ids[str(sender)]
     del self.nick_dict[str(sender)]