Ejemplo n.º 1
0
async def start(context: commands.Context):
    game: Game = DatabaseFacade.get_game_by_channel_did(str(
        context.channel.id))
    if game.player_number != game.teams[0].size:
        await context.send("Cannot start game until game is full "
                           f"(current: {game.player_number}, "
                           f"needed: {game.teams[0].size})")
        # return
    elif game is not None:
        DatabaseFacade.start_game(game.id)

    start_embed = Embed()
    start_embed.title = "Starting game..."
    start_embed.description = (f"Game begun at {game.started_at} with teams:")
    print("counting teams: ", len(game.teams))

    if game.teams_available:
        if game.randomize_teams:
            for team in game.teams[1:]:
                empty_slots = team.size - len(team.players)
                if empty_slots > len(game.teams[0].players):
                    await context.send(
                        f"ERROR: Insufficient number of players "
                        f"to fill teams; ran out on team "
                        f"{team.number}")
                players = random.sample(game.teams[0].players, k=empty_slots)
                for player in players:
                    print(f"adding player {player.id} to team {team.number}")
                    DatabaseFacade.add_player_to_team(game.id, team.number,
                                                      player)
        else:
            if sum([len(team.players)
                    for team in game.teams[1:]]) < len(game.teams[0]):
                await context.send(
                    "All players must be in a team before game can"
                    " begin")
                return

        for team in game.teams[1:]:
            print("Players:", team.players, ":", int(team.players[0].did))
            player_names = [
                await bot.fetch_user(int(player.did)).name
                for player in team.players
            ]

            if len(player_names) > 0:
                player_string = "\n".join(player_names)
            else:
                player_string = "(no players)"
            start_embed.add_field(name=f"Team {team.number}",
                                  value=f"{player_string}",
                                  inline=False)
    else:
        player_names = [
            await bot.fetch_user(int(player.did)).name
            for player in game.teams[0].players
        ]
        if len(player_names) > 0:
            player_string = "\n".join(player_names)
        else:
            player_string = "(no players)"
        start_embed.add_field(name="Randomized teams",
                              value=f"Players:\n {player_string}",
                              inline=False)

    await context.send(embed=start_embed)
Ejemplo n.º 2
0
async def on_raw_reaction_add(payload: discord.RawReactionActionEvent):
    user = bot.get_user(payload.user_id)

    # DEBUG output, enable if needed.
    # print("RAW reaction received")
    # print("From user:"******"(" + str(payload.user_id) + ")")
    # print("reaction:", payload.emoji)
    # print("emoji name:", payload.emoji.name)
    # print("emoji id:", payload.emoji.id)
    # print(payload.emoji.name, "?=", UNICODE_1, ":", payload.emoji.name == UNICODE_1)
    # print("payload channel: {}".format(payload.channel_id))

    guild: Guild = bot.get_guild(payload.guild_id)

    # Looks like all possible send targets inherit from Messageable, and EITHER
    # GuildChannel or PrivateChannel (all 3 of which are in discord.abc)
    channel: discord.TextChannel = bot.get_channel(payload.channel_id)
    print(f"channel: {channel}")
    message: Message = await channel.fetch_message(payload.message_id)

    if not user.bot:

        create_game_prop = DatabaseFacade.get_property(
            property_name=CREATE_GAME_CHANNEL)

        # if the above evaluated true, then channel should have a name
        # attribute at this point.
        create_game_channel_name = create_game_prop.value

        join_game_prop = DatabaseFacade.get_property(
            property_name=JOIN_GAME_CHANNEL)

        join_game_channel_name = join_game_prop.value

        if type(channel) == DMChannel:
            print("checked as DMChannel")
            message_sequence: MessageSequenceTest \
                = message_states.get_user_sequence(user)

            # if the user has a sequence with the bot already...
            if message_sequence is not None:
                # pass the message along to the sequence's handler
                await message_sequence.run_next_handler(message)
                return
            # otherwise... just ignore them I think?
            else:
                return
        # this is the message the bot put in the create a game channel
        elif channel.name == create_game_channel_name:
            print("checked as a create game message")
            reactions = message.reactions
            for reaction in reactions:
                try:
                    await message.remove_reaction(reaction.emoji, user)
                except Exception as e:
                    print(f"ERROR: While removing reaction, got exception of "
                          f"type {type(e)}")

            new_game_sequence = NewGameSequence(user, guild)
            await message_states.add_user_sequence(user, new_game_sequence)
            await new_game_sequence.start_sequence()
        elif channel.name == join_game_channel_name:
            print("checked as a join game message")
            # reaction was added on a message in the games channel
            game: Game = DatabaseFacade.get_game_by_message_did(str(
                message.id))

            user_as_player = DatabaseFacade.get_player_by_did(str(user.id))
            if game is not None:
                DatabaseFacade.add_player_to_game(game.id, user_as_player)
                game_channel: TextChannel = bot.get_channel(
                    int(game.channel_id))
                await game_channel.set_permissions(user, read_messages=True)

                if game.is_full():
                    print("game is full, clearing all messages")
                    await message.clear_reactions()
            else:
                print(f"Game not found with message_did: {message.id}")

        # this should become a check against all of the game's private channel
        # messages, which I'll probably build team swapping into
        elif channel.category.name == DatabaseFacade.get_property(
                GAME_CATEGORY_PROPERTY_NAME).value:
            print("CHECKED as a channel in the game category")
            if message.author.bot:
                game: Game = DatabaseFacade.get_game_by_game_message_did(
                    str(message.id))

                user_as_player = DatabaseFacade.get_player_by_did(str(user.id))

                emoji_name = payload.emoji.name
                print("")
                if emoji_name == UNICODE_0:
                    team_number = 0
                elif emoji_name == UNICODE_1:
                    team_number = 1
                elif emoji_name == UNICODE_2:
                    team_number = 2
                elif emoji_name == UNICODE_3:
                    team_number = 3
                elif emoji_name == UNICODE_4:
                    team_number = 4
                elif emoji_name == UNICODE_5:
                    team_number = 5
                elif emoji_name == UNICODE_6:
                    team_number = 6
                else:
                    print(f"React with bad emoji: {emoji_name}")
                    try:
                        await message.remove_reaction(payload.emoji, user)
                    except Exception as e:
                        print(
                            f"ERROR: While removing reaction, got exception of "
                            f"type {type(e)}")

                    return

                if DatabaseFacade.add_player_to_team(game.id, team_number,
                                                     user_as_player):
                    await channel.send(
                        content=f"{user.display_name} joined team "
                        f"{team_number}")
                else:
                    await channel.send(
                        content=f"Could not add {user.display_name} to team "
                        f"{team_number}: team is full or does not exist.")

                try:
                    await message.remove_reaction(payload.emoji, user)
                except Exception as e:
                    print(f"ERROR: While removing reaction, got exception of "
                          f"type {type(e)}")

        else:
            print("CHECKED as other message")