Пример #1
0
def check(game: Game, message: discord.Message) -> List[str]:
    if game.state == GameState.NO_GAME:
        return [
            "No game has been started yet. Message `.p newgame` to start one."
        ]
    elif game.state == GameState.WAITING:
        return ["You can't check because the game hasn't started yet."]
    elif not game.is_player(message.author):
        return [
            "You can't check, because you're not playing, "
            f"{message.author.name}."
        ]
    elif game.state == GameState.NO_HANDS:
        return ["You can't check because the hands haven't been dealt yet."]
    elif game.current_player.user != message.author:
        return [
            f"You can't check, {message.author.name}, because it's "
            f"{game.current_player.user.name}'s turn."
        ]
    elif game.current_player.cur_bet != game.cur_bet:
        return [
            f"You can't check, {message.author.name} because you need to "
            f"put in ${game.cur_bet - game.current_player.cur_bet} to "
            "call."
        ]
    else:
        return game.check()
Пример #2
0
def raise_bet(game: Game, message: discord.Message) -> List[str]:
    if game.state == GameState.NO_GAME:
        return ["No game has been started yet. Message `.p newgame` to start one."]
    elif game.state == GameState.WAITING:
        return ["You can't raise because the game hasn't started yet."]
    elif not game.is_player(message.author):
        return ["You can't raise, because you're not playing, " f"{message.author.name}."]
    elif game.state == GameState.NO_HANDS:
        return ["You can't raise because the hands haven't been dealt yet."]
    elif game.current_player.user != message.author:
        return [f"You can't raise, {message.author.name}, because it's " f"{game.current_player.name}'s turn."]

    tokens = message.content.split()
    if len(tokens) < 3:
        return ["Please follow `.p raise` with the amount that you would like to raise it by."]
    try:
        amount = int(tokens[2])
        if amount < 0:
            return ["You cannot raise by a negative amount."]
        elif game.cur_bet >= game.current_player.max_bet:
            return ["You don't have enough money to raise the current bet " f"of ${game.cur_bet}."]
        elif game.cur_bet + amount > game.current_player.max_bet:
            return [
                f"You don't have enough money to raise by ${amount}.",
                "The most you can raise it by is " f"${game.current_player.max_bet - game.cur_bet}.",
            ]
        return game.raise_bet(amount)
    except ValueError:
        return ["Please follow `.p raise` with an integer. " f"'{tokens[2]}' is not an integer."]
Пример #3
0
def new_game(game: Game, message: discord.Message) -> List[str]:
    if game.state == GameState.NO_GAME:
        game.new_game()
        game.add_player(message.author)
        game.state = GameState.WAITING
        return [f"A new game has been started by {message.author.name}!", "Message `.p join` to join the game."]
    else:
        messages = ["There is already a game in progress, " "you can't start a new game."]
        if game.state == GameState.WAITING:
            messages.append("It still hasn't started yet, so you can still " "message `.p join` to join that game.")
        return messages
Пример #4
0
def call_bet(game: Game, message: discord.Message) -> List[str]:
    if game.state == GameState.NO_GAME:
        return ["No game has been started yet. Message `.p newgame` to start one."]
    elif game.state == GameState.WAITING:
        return ["You can't call any bets because the game hasn't started yet."]
    elif not game.is_player(message.author):
        return ["You can't call, because you're not playing, " f"{message.author.name}."]
    elif game.state == GameState.NO_HANDS:
        return ["You can't call any bets because the hands haven't been " "dealt yet."]
    elif game.current_player.user != message.author:
        return [f"You can't call {message.author.name}, because it's " f"{game.current_player.user.name}'s turn."]
    else:
        return game.call()
Пример #5
0
def start_game(game: Game, message: discord.Message) -> List[str]:
    if game.state == GameState.NO_GAME:
        return ["Message `.p newgame` if you would like to start a new game."]
    elif game.state != GameState.WAITING:
        return [f"The game has already started, {message.author.name}.", "It can't be started twice."]
    elif not game.is_player(message.author):
        return [
            f"You are not a part of that game yet, {message.author.name}.",
            "Please message join if you are interested in playing.",
        ]
    elif len(game.players) < 2:
        return ["The game must have at least two players before " "it can be started."]
    else:
        return game.start()
Пример #6
0
def chip_count(game: Game, message: discord.Message) -> List[str]:
    if game.state in (GameState.NO_GAME, GameState.WAITING):
        return [
            "You can't request a chip count because the game "
            "hasn't started yet."
        ]
    return game.cur_options()
Пример #7
0
def set_option(game: Game, message: discord.Message) -> List[str]:
    tokens = message.content.split()
    if len(tokens) == 3:
        return [
            "You must specify a new value after the name of an option "
            "when using the `.p set` command."
        ]
    elif len(tokens) == 2:
        return [
            "You must specify an option and value to set when using "
            "the `.p set` command."
        ]
    elif tokens[2] not in GAME_OPTIONS:
        return [
            f"'{tokens[2]}' is not an option. Message `.p options` to see "
            "the list of options."
        ]
    elif game.state not in (GameState.NO_GAME, GameState.WAITING):
        return ["Cannot change game options while a game is running."]
    try:
        val = int(tokens[3])
        if val < 0:
            return [f"Cannot set {tokens[2]} to a negative value!"]
        game.options[tokens[2]] = val
        return [f"The {tokens[2]} is now set to {tokens[3]}."]
    except ValueError:
        return [
            f"{tokens[2]} must be set to an integer, and '{tokens[3]}'"
            " is not a valid integer."
        ]
Пример #8
0
def deal_hand(game: Game, message: discord.Message) -> List[str]:
    if game.state == GameState.NO_GAME:
        return ["No game has been started for you to deal. " "Message `.p newgame` to start one."]
    elif game.state == GameState.WAITING:
        return ["You can't deal because the game hasn't started yet."]
    elif game.state != GameState.NO_HANDS:
        return ["The cards have already been dealt."]
    elif game.dealer.user != message.author:
        return [f"You aren't the dealer, {message.author.name}.", f"Please wait for {game.dealer.user.name} to `.p deal`."]
    else:
        return game.deal_hands()
Пример #9
0
def join_game(game: Game, message: discord.Message) -> List[str]:
    if game.state == GameState.NO_GAME:
        return ["No game has been started yet for you to join.", "Message `.p newgame` to start a new game."]
    elif game.state != GameState.WAITING and game.options["tournament"]:
        return ["You're not allowed to join a tournament in progress."]
    elif game.add_player(message.author):
        return [
            f"{message.author.name} has joined the game!",
            "Message `.p join` to join the game, " "or `.p start` to start the game.",
        ]
    else:
        return [f"You've already joined the game {message.author.name}!"]
Пример #10
0
async def on_message(message: discord.Message):
    # Ignore messages sent by the bot itself
    if message.author == client.user:
        return
    # Ignore private messages
    if not isinstance(message.channel, discord.TextChannel):
        return

    message_split = message.content.split()
    # Ignore empty messages
    if len(message_split) == 0:
        return

    # Look for .p prefix
    if message_split[0] == '.p':
        # Look for command and return if not found
        if len(message_split) <= 1:
            await message.channel.send(
                "No command specified. "
                "Message `.p help` to see the list of commands.")
            return
        command = message_split[1]
        if command not in commands:
            await message.channel.send(
                f"{message.content} is not a valid command. "
                "Message `.p help` to see the list of commands.")
            return

        messages = []
        messages2 = []
        game = games.setdefault(message.channel, Game())
        deal_hands = command == 'deal' and game.state == GameState.NO_HANDS
        messages = commands[command].action(game, message)

        if game.options["auto-deal"] and game.state == GameState.NO_HANDS:
            messages2 = game.deal_hands()
            tell_hands = True
        elif deal_hands and game.state == GameState.HANDS_DEALT:
            tell_hands = True
        else:
            tell_hands = False

        await send_messages(messages, message.channel)
        # The messages to send to the channel and the messages to send to the
        # players individually must be done seperately, so we check the messages
        # to the channel to see if hands were just dealt, and if so, we tell the
        # players what their hands are.
        if tell_hands:
            await game.tell_hands(client)
        await send_messages(messages2, message.channel)
Пример #11
0
async def on_message(message):
    # Ignore messages sent by the bot itself
    if message.author == client.user:
        return
    # Ignore private messages
    if not isinstance(message.channel, discord.TextChannel):
        return

    message_split = message.content.split()
    # Ignore empty messages
    if len(message_split) == 0:
        return

    # Look for .p prefix
    if message_split[0] == '.p':
        # Look for command and return if not found
        if len(message_split) <= 1:
            await message.channel.send(
                "No command specified. "
                "Message `.p help` to see the list of commands.")
            return
        command = message_split[1]
        if command not in commands:
            await message.channel.send(
                f"{message.content} is not a valid command. "
                "Message `.p help` to see the list of commands.")
            return

        game = games.setdefault(message.channel, Game())
        messages = commands[command].action(game, message)

        # The messages to send to the channel and the messages to send to the
        # players individually must be done seperately, so we check the messages
        # to the channel to see if hands were just dealt, and if so, we tell the
        # players what their hands are.
        if command == 'deal' and messages[0] == 'The hands have been dealt!':
            await game.tell_hands(client)

        await message.channel.send('\n'.join(messages))