def create_game(g, user_id, *args): """ Reset state. Let people join. annouce STATUS -> "WAITING_FOR_JOIN" """ # testing if i can append to outputs here. # Can only create a game # if g['STATUS'] == 'INACTIVE' result, message = mod_valid_action(user_id, 'create', g) if result: # allowed to create game if g['STATUS'] != 'INACTIVE': return 'Can not create new game.', None # reset game state. new_g = update_game_state(g, 'reset_game_state') # change game status to WAITING new_g = update_game_state(new_g, 'status', status='WAITING_FOR_JOIN') # send message to channel return '_Waiting for players..._ \n*Type !join to join.*', None else: return message, None # return error message, and basic channel.
def resolve_day_round(g, alert=None): """ Like resolve_night_round, but for the day! """ alive_v = alive_for_village(g) alive_w = alive_for_werewolf(g) # we want to show vote results. vote_list_str = list_votes(g)[0] + '\n' if len(alive_w) >= len(alive_v): new_g = update_game_state(g, 'status', status='INACTIVE') new_g = update_game_state(new_g, 'reset_game_state') return vote_list_str + make_end_round_str(new_g, alert, 'w') # returns and sends message elif len(alive_w) == 0: new_g = update_game_state(g, 'status', status='INACTIVE') new_g = update_game_state(new_g, 'reset_game_state') return vote_list_str + make_end_round_str(new_g, alert, 'v') # returns and sends message else: # turn it into night and start night round round_end_str = vote_list_str + make_end_round_str(g) + start_night_round(g) return round_end_str
def resolve_night_round(game_state, alert=None): """Reconcile all night actions and update the game_state. The game can either continue into day mode or it will end with the night actions. """ alive_v = status.alive_for_village(game_state) alive_w = status.alive_for_werewolf(game_state) if len(alive_w) >= len(alive_v): new_game = update_game_state(game_state, 'status', status='INACTIVE') new_game = update_game_state(new_game, 'reset_game_state') return make_end_round_str(new_game, alert, 'w') elif len(alive_w) == 0: new_game = update_game_state(game_state, 'status', status='INACTIVE') new_game = update_game_state(new_game, 'reset_game_state') return make_end_round_str(new_game, alert, 'v') else: #TODO: aggregate of all night action game messages with channel id(dm) in game_state # and then send all messages at once round_end_str = make_end_round_str(game_state) + start_day_round(game_state) return round_end_str
def player_vote(g, user_id, *args): """ ex. *args = (['vote', 'maksym'], ) arg_list = args[0] target_name = arg_list[1] user_name = u.id_dict.get(user_id) """ arg_list = args[0] if len(arg_list) < 1: # didn't vote return "Have to vote FOR someone.", None elif len(arg_list) > 2: # too many args return "Not a valid command.", None else: target_name = arg_list[1] target_id = u.get(name=target_name) # turn name into id result, message = is_valid_action(user_id, 'vote', g, target_name=target_name) if not result: # was not a valid kill return message, None else: # player voted # update state # change votes to reflect their vote new_g = update_game_state(g, 'vote', voter=user_id, votee=target_id) # after each vote need to check if total # everyone has voted. if did_everyone_vote(new_g): # resolve vote round result_id = resolve_votes(new_g) if result_id: # result is id of player # set player status to dead. result_name = u.get(user_id=result_id) new_g_2 = update_game_state(new_g, 'player_status', player=result_id, status='dead') # have to reset all the votes new_g_3 = update_game_state(new_g_2, 'reset_votes') # tell the players. lynch_str = "%s was lynched." % (result_name) # pass in game state before votes reset. return resolve_day_round(new_g_2, alert=lynch_str), None else: # list votes returns a tuple ('str', None) return resolve_day_round(new_g, alert='*No one dies.*'), None else: # valid vote, but not everyone voted yet. # suggestion to list vote summary every vote. return list_votes(new_g)[0] + '\n\n' + message return message, None
def assign_roles(g): players = players_in_game(g) create_wolf = random.choice(players) # id of player new_g = copy.deepcopy(g) for player in players: if player == create_wolf: new_g = update_game_state(new_g, 'role', player=player, role='w') else: new_g = update_game_state(new_g, 'role', player=player, role='v') return new_g
def start_night_round(game_state): """Start the night round by setting all villas without a night action to completed, set all villas/wolves with a night action to false and send night action PM's """ all_alive = status.get_all_alive(game_state) new_game = update_game_state(game_state, 'round', round='night') for player_id in all_alive: new_game = update_game_state(new_game, 'change_night_action_status', player=player_id, completed_night_action=status.does_have_night_action(game_state, player_id)) return "It is night time. \n Werewolves type_'/dm moderator !eat {who you want to eat}_ \n\n *Talking is NOT Allowed.*"
def eat_player(g, user_id, *args): """ ex. *args = (['kill', 'maksym'], ) arg_list = target_name = args[1] user_name = u.id_dict.get(user_id) """ arg_list = args[0] if len(arg_list) < 1: # no target no good return "Have to pick a target.", None elif len(arg_list) > 2: # too many args return "Not a valid command.", None else: u = UserMap() # get usermap target_name = arg_list[1] target_id = u.get(name=target_name) # turn name into id result, message = is_valid_action(user_id, 'kill', g, target_name=target_name) if not result: # was not a valid kill return message, None else: # player is eaten # update state # changes targeted player's status to dead new_g = update_game_state(g, 'player_status', player=target_id, status='dead') # tell the players. eaten_str = "%s was eaten." % (target_name) return resolve_night_round(new_g, alert=eaten_str), None
def join(g, user_id, *args): """ See if player is allowed to join. If so let add them to the game. """ result, message = mod_valid_action(user_id, 'join', g) if not result: return message, None # could not join # if player successfully joins. u = UserMap() user_name = u.get(user_id=user_id) if not user_name: # user not in user_map yet # get_user_name polls slack and adds to user map user_name = get_user_name(user_id) # update state with new player mutated_g = update_game_state(g, 'join', player=user_id) # tell the channel the player joined. join_message = "%s joined the game." % user_name return join_message, None
def night_kill(game_state, user_id, *args): """Night kill the selected player.""" arg_list = args[0] print arg_list print len(arg_list) if len(arg_list) < 1: return "Have to pick a target.", None elif len(arg_list) > 2: return "Not a valid command.", None else: user_map = UserMap() target_name = arg_list[1] target_id = user_map.get(name=target_name) result, message = is_valid_action(user_id, 'night_kill', game_state, target_name=target_name) if not result: return message, None else: new_game = update_game_state(game_state, 'player_status', player=target_id, status='dead') eaten_str = "%s was night killed." % (target_name) return resolve_night_round(new_game, alert=eaten_str), None
def create_game(game_state, user_id, *kwargs): """Create a new werewolf game, reset the game_state of previous games, and announce that we're waiting for players to join. Set game status as `WAITING_FOR_JOIN` """ result, message = mod_valid_action(user_id, 'create', game_state) if result: if game_state['STATUS'] != 'INACTIVE': return 'Can not create new game.', None new_game = update_game_state(game_state, 'reset_game_state') new_game = update_game_state(new_game, 'status', status='WAITING_FOR_JOIN') return 'Waiting for players... \n*Type !join to join.*\nhttps://media.giphy.com/media/d2DbJNid60ts4/giphy.gif', None else: return message, None
def resolve_night_round(g, alert=None): """ Makes sure everyone has done all their roles. - if yes see if game is over. if yes set game to over. display results. if no change round to day. """ # TODO: for each player in the game, # check if completed their action for the night. alive_v = alive_for_village(g) alive_w = alive_for_werewolf(g) if len(alive_w) >= len(alive_v): new_g = update_game_state(g, 'status', status='INACTIVE') # reset game state. new_g = update_game_state(new_g, 'reset_game_state') return make_end_round_str(new_g, alert, 'w') # returns and sends message elif len(alive_w) == 0: new_g = update_game_state(g, 'status', status='INACTIVE') # reset game state. new_g = update_game_state(new_g, 'reset_game_state') return make_end_round_str(new_g, alert, 'v') # returns and sends message else: # turn it into morning and start day round. # idea: # game state has 'GAME_MESSAGES' : {'channel': <channel_id>, 'message': thing to say} # every night action adds game_message. # If all night actions have finished. Go through and send all those messages. # reset GAME_MESSAGES. round_end_str = make_end_round_str(g) + start_day_round(g) return round_end_str
def start_night_round(g): """ 1.) Set state to night round. 2.) For each player, if it is a character without a night action (ie. 'v'): set completed_night_action: True else completed_night_action: False 3.) Send night message. """ all_alive = get_all_alive(g) new_g = update_game_state(g, 'round', round='night') for player_id in all_alive: new_g = update_game_state(new_g, 'change_night_action_status', player=player_id, completed_night_action=does_have_night_action(g, player_id)) return "It is night time. \n Werewolf type_'/dm moderator !kill {who you want to eat}_ \n\n *Talking is NOT Allowed.*"
def player_vote(game_state, user_id, *args): """Update the game_state with a players lynch vote.""" user_map = UserMap() arg_list = args[0] if len(arg_list) < 1: return "Have to vote FOR someone.", None elif len(arg_list) > 2: return "Not a valid command.", None else: target_name = arg_list[1] target_id = user_map.get(name=target_name) result, message = is_valid_action(user_id, 'vote', game_state, target_name=target_name) if not result: return message, None else: new_game = update_game_state(game_state, 'vote', voter=user_id, votee=target_id) if status.did_everyone_vote(new_game): result_id = resolve_votes(new_game) if result_id: result_name = user_map.get(user_id=result_id) set_deaths_in_game_state = update_game_state(new_game, 'player_status', player=result_id, status='dead') reset_game_votes = update_game_state(set_deaths_in_game_state, 'reset_votes') lynch_str = "%s was lynched." % (result_name) return resolve_day_round(set_deaths_in_game_state, alert=lynch_str), None else: return resolve_day_round(new_game, alert='*No one dies.*'), None else: return list_votes(new_game)[0] + '\n\n' + message, None return message, None
def resolve_day_round(game_state, alert=None): """Reconcile all votes for the day and enter night mode.""" alive_v = status.alive_for_village(game_state) alive_w = status.alive_for_werewolf(game_state) vote_list_str = list_votes(game_state)[0] + '\n' if len(alive_w) >= len(alive_v): new_game = update_game_state(game_state, 'status', status='INACTIVE') new_game = update_game_state(new_game, 'reset_game_state') return vote_list_str + make_end_round_str(new_game, alert, 'w') elif len(alive_w) == 0: new_game = update_game_state(game_state, 'status', status='INACTIVE') new_game = update_game_state(new_game, 'reset_game_state') return vote_list_str + make_end_round_str(new_game, alert, 'v') else: return vote_list_str + make_end_round_str(game_state) + start_night_round(game_state)
def join(game_state, user_id, *args): """Join the game if the player hasn't joined yet.""" result, message = mod_valid_action(user_id, 'join', game_state) if not result: return message, None user_map = UserMap() user_name = user_map.get(user_id=user_id) if not user_name: user_name = get_user_name(user_id) new_game_w_new_player = update_game_state(game_state, 'join', player=user_id) join_message = "%s joined the game." % user_name return join_message, None
def start_game(game_state, user_id, *kwargs): """Start the game and set game status to `RUNNING`.""" result, message = mod_valid_action(user_id, 'start', game_state) if not result: return message, None send_message("@channel: Game is starting...") players = status.players_in_game(game_state) num_werewolves = status.alive_for_werewolf(game_state) p1_str = "_There are *%d* players in the game._\n" % len(players) p2_str = "_There are *%d* werewolves in the game._\n" % len(num_werewolves) p3_str = "https://media.giphy.com/media/3o72FkgwrI7P4G1amI/giphy.gif" send_message(p1_str + p2_str + p3_str) game_state = update_game_state(game_state, 'status', status='RUNNING') new_game = assign_roles(game_state) message_everyone_roles(new_game) return start_day_round(new_game), None
def start_game(g, user_id, *args): """ Check if enough players. (optional) Pick config setup that works w/ num of players. Outut to channel (@channel) game is starting. Assign Roles. Message Users their roles. STATUS -> "RUNNING" """ # can only start a game # if enough players # if g['STATUS'] == 'WAITING_FOR_JOIN' result, message = mod_valid_action(user_id, 'start', g) if not result: # cant start print(result, message) return message, None # tell everyone the game is starting send_message("@channel: Game is starting...") """ message everyone details of game. """ players = players_in_game(g) num_werewolves = 1 # only one for now. p1_str = "_There are *%d* players in the game._\n" % len(players) p2_str = "There are *%d* werewolves in the game._" % num_werewolves send_message(p1_str + p2_str) g = update_game_state(g, 'status', status='RUNNING') # Go through and assign everyone in the game roles. new_g = assign_roles(g) # updated state w/ roles message_everyone_roles(new_g) # go to night round. return start_day_round(new_g), None # idk when this will return.
def start_day_round(g): update_game_state(g, 'round', round='day') return "It is now day time. \n type _!vote {username}_. If user has more than half the votes. They die."
def start_day_round(game_state): update_game_state(game_state, 'round', round='day') return "It is now day time. \n type _!vote {username}_. If user has more than half the votes. They die."
def assign_roles(game_state): """Assign all players in the game a role.""" total_players = status.players_in_game(game_state) num_of_wolves = 1 num_of_players = len(total_players) seer = None angel = None wolf = None villa = None # if it's only 2 players that's lame but whatev's if num_of_players <= 2: if num_of_players == 1: random_ints = range(20) decision = random.choice(random_ints) if decision <= 4: villa = total_players[0] elif decision >= 5 and decision <= 9: wolf = total_players[0] elif decision >= 10 and decision <= 14: seer = total_players[0] elif decision >= 15 and decision <= 20: angel = total_players[0] else: villa = total_players[0] wolf = total_players[1] new_game = copy.deepcopy(game_state) if num_of_players > 2: if num_of_players >= 1 and num_of_players < 13: num_of_wolves = 2 elif num_of_players >= 13 and num_of_players < 23: num_of_wolves = 3 elif num_of_players >= 23: num_of_wolves = 6 wolves = random.sample(total_players, num_of_wolves) non_wolves = [player for player in total_players if player not in created_wolves] ramdom.shuffle(non_wolves) seer = non_wolves[0] angel = non_wolves[1] villas = [x for x in non_wolves if x != seer and x != angel] if seer and angel: new_game = update_game_state(new_game, 'role', player=seer, role='s') new_game = update_game_state(new_game, 'role', player=angel, role='a') for villa in villas: new_game = update_game_state(new_game, 'role', player=villa, role='v') for wolf in wolves: new_game = update_game_state(new_game, 'role', player=wolf, role='w') else: if villa: new_game = update_game_state(new_game, 'role', player=villa, role='v') if wolf: new_game = update_game_state(new_game, 'role', player=wolf, role='w') if seer: new_game = update_game_state(new_game, 'role', player=seer, role='s') if angel: new_game = update_game_state(new_game, 'role', player=angel, role='a') return new_game