コード例 #1
0
 def get_num_bot_cases(self):
     num_cases = 1
     for app_name in self['app_sequence']:
         bots_module = get_bots_module(app_name)
         cases = bots_module.PlayerBot.cases
         num_cases = max(num_cases, len(cases))
     return num_cases
コード例 #2
0
def make_bots(*, session_pk, case_number,
              use_browser_bots) -> List[ParticipantBot]:
    update_kwargs = {'_is_bot': True}
    if use_browser_bots:
        update_kwargs['is_browser_bot'] = True

    Participant.objects.filter(session_id=session_pk).update(**update_kwargs)
    bots = []

    # can't use .distinct('player_pk') because it only works on Postgres
    # this implicitly orders by round also
    session = Session.objects.get(pk=session_pk)

    participant_codes = session.participant_set.order_by('id').values_list(
        'code', flat=True)

    player_bots_dict = {pcode: [] for pcode in participant_codes}

    for app_name in session.config['app_sequence']:
        bots_module = get_bots_module(app_name)
        models_module = get_models_module(app_name)
        players = (models_module.Player.objects.filter(
            session_id=session_pk).order_by('round_number').values(
                'id', 'participant_id', 'participant__code', 'subsession_id'))
        for player in players:
            participant_code = player['participant__code']
            player_bot = bots_module.PlayerBot(
                case_number=case_number,
                app_name=app_name,
                player_pk=player['id'],
                subsession_pk=player['subsession_id'],
                participant_code=participant_code,
                session_pk=session_pk,
            )
            player_bots_dict[participant_code].append(player_bot)

    executed_live_methods = set()

    for participant_code, player_bots in player_bots_dict.items():
        bot = ParticipantBot(
            participant_code,
            player_bots=player_bots,
            executed_live_methods=executed_live_methods,
        )
        bots.append(bot)

    return bots
コード例 #3
0
def make_bots(*, session_pk, case_number,
              use_browser_bots) -> List[ParticipantBot]:
    update_kwargs = {Participant._is_bot: True}
    if use_browser_bots:
        update_kwargs[Participant.is_browser_bot] = True

    Participant.objects_filter(session_id=session_pk).update(update_kwargs)
    bots = []

    # can't use .distinct('player_pk') because it only works on Postgres
    # this implicitly orders by round also
    session = Session.objects_get(id=session_pk)

    participant_codes = values_flat(session.pp_set.order_by('id'),
                                    Participant.code)

    player_bots_dict = {pcode: [] for pcode in participant_codes}

    for app_name in session.config['app_sequence']:
        bots_module = get_bots_module(app_name)
        models_module = get_models_module(app_name)
        Player = models_module.Player
        players = (Player.objects_filter(session_id=session_pk).join(
            Participant).order_by('round_number').with_entities(
                Player.id, Participant.code, Player.subsession_id))
        for player_id, participant_code, subsession_id in players:
            player_bot = bots_module.PlayerBot(
                case_number=case_number,
                app_name=app_name,
                player_pk=player_id,
                subsession_pk=subsession_id,
                participant_code=participant_code,
                session_pk=session_pk,
            )
            player_bots_dict[participant_code].append(player_bot)

    executed_live_methods = set()

    for participant_code, player_bots in player_bots_dict.items():
        bot = ParticipantBot(
            participant_code,
            player_bots=player_bots,
            executed_live_methods=executed_live_methods,
        )
        bots.append(bot)

    return bots
コード例 #4
0
    def __init__(
        self,
        participant: Participant = None,
        *,
        lookups: List[ParticipantToPlayerLookup] = None,
        load_player_bots=True,
        case_number=None,
    ):

        # usually lookups should be passed in. for ad-hoc testing,
        # ok to pass a participant
        if not lookups:
            lookups_with_duplicates = ParticipantToPlayerLookup.objects.filter(
                participant_id=participant.id).order_by('player_pk')
            seen_player_pks = set()
            lookups = []
            for lookup in lookups_with_duplicates:
                if not lookup.player_pk in seen_player_pks:
                    lookups.append(lookup)
                    seen_player_pks.add(lookup.player_pk)

        self.participant_id = lookups[0].participant_id
        self.participant_code = lookups[0].participant_code

        self.url = None
        self._response = None
        self._html = None
        self.path = None
        self.submits = None
        super().__init__()

        self.player_bots = []

        # load_player_bots can be set to False when it's convenient for
        # internal testing
        if load_player_bots:
            for lookup in lookups:
                app_name = lookup.app_name

                bots_module = get_bots_module(app_name)
                player_bot = bots_module.PlayerBot(lookup=lookup,
                                                   case_number=case_number,
                                                   participant_bot=self)
                self.player_bots.append(player_bot)
            self.submits_generator = self.get_submits()