Exemplo n.º 1
0
def start_game(chat_id, user_id):
    game = storage.get_game(chat_id)
    user = storage.get_user(user_id)

    if game is None:
        return telegram.send_message(
            chat_id,
            "Trying to start non existing game!")

    if user is None:
        return telegram.send_message(
            chat_id,
            "You aren't registered yet.\n" +
            "Please send a '/start' to @Spyfall_Bot")

    if game.admin_user != user:
        return telegram.send_message(
            chat_id,
            "You are not the gamemaster.")

    if game.state != GameState.preparing.value:
        return telegram.send_message(
            chat_id,
            "This game is already running.")

    if len(game.users) < 3:
        return telegram.send_message(
            chat_id,
            "Not enough players.")

    storage.update_game(chat_id, state=GameState.running)
    telegram.send_message(
        chat_id,
        "Game started! Roles will be distributed...")
    distribute_roles(game)
Exemplo n.º 2
0
def handle_config(update):
    user_id = dt.traverse(update, "message.from.id")
    user = storage.get_user(user_id)
    if user is None:
        return telegram.send_message(
            user_id,
            "You are not registered yet. Please do so with /start.")

    storage.update_user(user_id, state=UserState.config)
    telegram.send_message(
        user_id,
        "What would you like to change?",
        reply_markup={"keyboard": [[CONFIG_USERNAME]],
                      "one_time_keyboard": True,
                      "resize_keyboard": True})
Exemplo n.º 3
0
def player_join_game(chat_id, player_id):
    game = storage.get_game(chat_id)
    user = storage.get_user(player_id)

    if game is None:
        return telegram.send_message(
            chat_id,
            "There is no preparing game to join.")

    if user is None:
        return telegram.send_message(
            chat_id,
            "You aren't registered yet.\n" +
            "Please send me a private message (@Spyfall_Bot)")

    if user.username is None:
        return telegram.send_message(
            chat_id,
            "You haven't set a username yet.\n" +
            "Please do so with /config.")

    if game.state != GameState.preparing.value:
        return telegram.send_message(
            chat_id,
            "This game is already running.")

    if len(game.users) >= 8:
        return telegram.send_message(
            chat_id,
            "There are already 8 players in this game.")

    if user in game.users:
        return telegram.send_message(
            chat_id,
            "You already joined this game.")

    success = storage.join_game(chat_id, player_id)
    if success:
        return telegram.send_message(
            chat_id,
            "Player {} has joined the game.".format(user.username))

    return telegram.send_message(
        chat_id,
        "You already joined another game.")
Exemplo n.º 4
0
def handle_plain(text, update):
    user_id = dt.traverse(update, "message.from.id")
    text = dt.traverse(update, "message.text")
    user = storage.get_user(user_id)

    if user is None:
        return

    if user.state == UserState.config and text == CONFIG_USERNAME:
        storage.update_user(user_id, state=UserState.config_username)
        return telegram.send_message(
            user_id,
            "Please send me your new username.",
            reply_markup={"hide_keyboard": True})

    if user.state == UserState.config_username:
        storage.update_user(user_id, username=text, state=UserState.none)
        return telegram.send_message(
            user_id,
            "Updated your username to {}.".format(text))
Exemplo n.º 5
0
def handle_register(update):
    user_id = dt.traverse(update, "message.from.id")
    username = dt.traverse(update, "message.from.username")

    existing = storage.get_user(user_id)
    if existing is not None:
        return telegram.send_message(
            user_id,
            "You are already registered.")

    storage.register_user(user_id, username)

    message = "Welcome. Thank you for registering."
    if username is None:
        message += "\nBe sure to set a username with /config."
    else:
        message += "\nYour username is: {}".format(username)

    telegram.send_message(
        user_id,
        message)
Exemplo n.º 6
0
def end_game(chat_id, user_id):
    game = storage.get_game(chat_id)
    user = storage.get_user(user_id)

    if game is None:
        return telegram.send_message(
            chat_id,
            "Trying to end non existing game!")

    if user is None:
        return telegram.send_message(
            chat_id,
            "You aren't registered yet.\n" +
            "Please send a '/start' to @Spyfall_Bot")

    if game.admin_user != user:
        return telegram.send_message(
            chat_id,
            "You are not the gamemaster.")

    storage.update_game(chat_id, state=GameState.finished)
    telegram.send_message(
        chat_id,
        "Game ended!")
Exemplo n.º 7
0
def create_game(chat_id, user_id):
    game = storage.get_game(chat_id)
    if game is not None:
        return telegram.send_message(
            chat_id,
            "There is already a game in this chat.")

    user = storage.get_user(user_id)
    if user is None:
        return telegram.send_message(
            chat_id,
            "You aren't registered yet.\n" +
            "Please send a '/start' to @Spyfall_Bot")

    if user.username is None:
        return telegram.send_message(
            chat_id,
            "You haven't set a username yet.\n" +
            "Please do so with /config.")

    storage.register_game(chat_id, user)
    telegram.send_message(
        chat_id,
        "Game created and joined.")