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 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 check():
        check_game = get_game_state()

        result, message = mod_valid_action(user_id, 'countdown', check_game)
        if not result:
            return False, message

        all_alive = status.get_all_alive(game_state)
        yet_to_vote_list = [player_id for player_id in all_alive if not status.has_voted(game_state, player_id)]
        if len(yet_to_vote_list) > 1:
            return False, 'Countdown cannot start now.'

        yet_to_vote = yet_to_vote_list[0]
        return True, yet_to_vote
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 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 check():
        # who hasn't voted.
        # if round is still day
        check_g = get_game_state() # make sure state hasn't changed.
        result, message = mod_valid_action(user_id, 'countdown', check_g)
        if not result:
            return False, message

        all_alive = get_all_alive(g)
        yet_to_vote_list = [
                p_id for p_id in all_alive
                if not has_voted(g, p_id)]

        if len(yet_to_vote_list) > 1:
            return False, 'Countdown cannot start now.'

        yet_to_vote = yet_to_vote_list[0]
        return True, yet_to_vote
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.