Exemple #1
0
async def make_match_from_cmd(
        cmd: Command,
        cmd_type: CommandType,
        racer_members=list(),
        racer_names=list(),
        match_info=matchinfo.MatchInfo()
):
    racers = []

    # Add the racers from member objects
    for member in racer_members:
        racer_as_necrouser = await userlib.get_user(discord_id=member.id)
        if racer_as_necrouser is not None:
            racers.append(racer_as_necrouser)
        else:
            await cmd_type.client.send_message(
                cmd.channel,
                'Unexpected error: Couldn\'t find `{0}` in the database.'.format(member.display_name)
            )
            return

    # Add the racers from names
    for name in racer_names:
        racer_as_necrouser = await userlib.get_user(any_name=name)

        if racer_as_necrouser is not None:
            racers.append(racer_as_necrouser)
        else:
            await cmd_type.client.send_message(
                cmd.channel,
                'Couldn\'t find a user with name `{0}`.'.format(name)
            )
            return

    # Check we have exactly two racers
    if len(racers) != 2:
        await cmd_type.client.send_message(
            cmd.channel,
            'Unexpected error: Tried to create a match with more than two racers.'
        )
        return

    # Create the Match object
    new_match = await matchutil.make_match(
        racer_1_id=racers[0].user_id,
        racer_2_id=racers[1].user_id,
        match_info=match_info,
        register=True
    )

    # Create the match room
    match_room = await matchutil.make_match_room(new_match)
    await match_room.send_channel_start_text()

    # Output success
    await cmd_type.client.send_message(
        cmd.channel,
        'Match created in channel {0}.'.format(
            match_room.channel.mention))
Exemple #2
0
async def make_match_from_cmd(
        cmd: Command,
        racer_members: Optional[List[Union[discord.User,
                                           discord.Member]]] = None,
        racer_names: Optional[List[str]] = None,
        match_info: matchinfo.MatchInfo = matchinfo.MatchInfo(),
        league_tag: Optional[str] = None,
        allow_duplicates: bool = True):
    if racer_members is None:
        racer_members = []
    if racer_names is None:
        racer_names = []

    racers = []

    # Add the racers from member objects
    for member in racer_members:
        racer_as_necrouser = await userlib.get_user(discord_id=member.id)
        if racer_as_necrouser is not None:
            racers.append(racer_as_necrouser)
        else:
            await cmd.channel.send(
                'Unexpected error: Couldn\'t find `{0}` in the database.'.
                format(member.display_name))
            return

    # Add the racers from names
    for name in racer_names:
        racer_as_necrouser = await userlib.get_user(any_name=name)

        if racer_as_necrouser is not None:
            racers.append(racer_as_necrouser)
        else:
            await cmd.channel.send(
                'Couldn\'t find a user with name `{0}`.'.format(name))
            return

    # Check we have exactly two racers
    if len(racers) != 2:
        await cmd.channel.send(
            'Unexpected error: Tried to create a match with more than two racers.'
        )
        return

    # Check for duplicating match
    if not allow_duplicates:
        duplicate = await matchutil.match_exists_between(racers[0], racers[1])
        if duplicate:
            raise necrobot.exception.DuplicateMatchException()

    # Create the Match object
    new_match = await matchutil.make_match(racer_1_id=racers[0].user_id,
                                           racer_2_id=racers[1].user_id,
                                           match_info=match_info,
                                           league_tag=league_tag,
                                           register=True)

    # Create the match room
    match_room = await matchchannelutil.make_match_room(match=new_match)
    await match_room.send_channel_start_text()

    # Output success
    await cmd.channel.send('Match created in channel {0}.'.format(
        match_room.channel.mention))

    return new_match
Exemple #3
0
    async def get_matches(self, **kwargs):
        """Read racer names and match types from the GSheet; create corresponding matches.
        
        Parameters
        ----------
        kwargs:
            Parameters to be passed to matchutil.make_match for every match made.
        
        Returns
        -------
        list[Match]
            The list of created Matches.
        """
        console.debug('get_matches begin...')
        await self.column_data.refresh_footer()

        matches = []
        self._not_found_matches = []
        write_match_ids = self.column_data.match_id is not None and 'register' in kwargs and kwargs[
            'register']
        match_ids = []

        async with Spreadsheets() as spreadsheets:
            value_range = await self.column_data.get_values(spreadsheets)
            console.debug('get_matches: Got values from spreadsheets.')

            if 'values' not in value_range:
                console.debug('get_matches: Values is empty.')
                return matches
            else:
                console.debug('get_matches: Values: {0}'.format(
                    value_range['values']))

            for row_idx, row_values in enumerate(value_range['values']):
                try:
                    racer_1_name = row_values[self.column_data.racer_1].rstrip(
                        ' ')
                    racer_2_name = row_values[self.column_data.racer_2].rstrip(
                        ' ')
                except IndexError:
                    console.warning(
                        'Failed to make match from sheet row: <{}>'.format(
                            row_values))
                    continue

                if not racer_1_name or not racer_2_name:
                    continue

                # if racer_1_name[0] not in string.ascii_letters or racer_2_name[0] not in string.ascii_letters:
                #     self._not_found_matches.append('{0}-{1}'.format(racer_1_name, racer_2_name))
                #     continue

                console.debug('get_matches: Creating {0}-{1}'.format(
                    racer_1_name, racer_2_name))

                racer_1 = await userlib.get_user(any_name=racer_1_name,
                                                 register=True)
                racer_2 = await userlib.get_user(any_name=racer_2_name,
                                                 register=True)
                if racer_1 is None or racer_2 is None:
                    console.warning(
                        'Couldn\'t find racers for match {0}-{1}.'.format(
                            racer_1_name, racer_2_name))
                    self._not_found_matches.append('{0}-{1}'.format(
                        racer_1_name, racer_2_name))
                    continue

                sheet_info = MatchGSheetInfo()
                sheet_info.wks_id = self.wks_id
                sheet_info.row = row_idx

                kwarg_copy = kwargs.copy()
                if self.column_data.type is not None:
                    match_info = kwarg_copy[
                        'match_info'] if 'match_info' in kwargs else matchinfo.MatchInfo(
                        )
                    try:
                        parsed_args = shlex.split(
                            row_values[self.column_data.type])
                        kwarg_copy['match_info'] = matchinfo.parse_args_modify(
                            parsed_args, match_info)
                    except IndexError:
                        pass

                new_match = await matchutil.make_match(
                    racer_1_id=racer_1.user_id,
                    racer_2_id=racer_2.user_id,
                    gsheet_info=sheet_info,
                    **kwarg_copy)
                matches.append(new_match)
                console.debug('get_matches: Created {0}-{1}'.format(
                    new_match.racer_1.rtmp_name, new_match.racer_2.rtmp_name))

                if write_match_ids:
                    match_ids.append([new_match.match_id])

        if write_match_ids:
            ids_range = self.column_data.get_range_for_column(
                self.column_data.match_id)
            await self.column_data.update_cells(sheet_range=ids_range,
                                                values=match_ids,
                                                raw_input=True)

        console.debug('get_matches: Returning Matches=<{}>'.format(matches))
        return matches