def test_match_blocking(self): # create an extra arena client for this test self.arenaclientUser2 = ArenaClient.objects.create( username='******', email='*****@*****.**', type='ARENA_CLIENT', trusted=True, owner=self.staffUser1) self.client.force_login(User.objects.get(username='******')) self._create_map('test_map') self._create_open_season() bot1 = self._create_active_bot(self.regularUser1, 'testbot1', 'T') bot2 = self._create_active_bot(self.regularUser1, 'testbot2', 'Z') # this should tie up both bots response = self.client.post('/api/arenaclient/matches/') self.assertEqual(response.status_code, 201) # we shouldn't be able to get a new match self.client.force_login(User.objects.get(username='******')) response = self.client.post('/api/arenaclient/matches/') self.assertEqual(response.status_code, 500) self.assertEqual( u"Failed to start match. There might not be any available participants.", response.data['detail']) Matches.request_match(self.regularUser2, bot1, bot1.get_random_active_excluding_self()) # now we should be able to get a match - the requested one response = self.client.post('/api/arenaclient/matches/') self.assertEqual(response.status_code, 201)
def _generate_full_data_set(self): self.client.login(username='******', password='******') self._create_map('testmap2') self._generate_extra_users() self._generate_extra_bots() self._generate_match_activity() # generate a bot match request to ensure it doesn't bug things out bot = Bot.get_random_active() Matches.request_match(self.regularUser2, bot) self.client.logout() # child tests can login if they require
def create_new_match(self, requesting_user): match = Matches.start_next_match(requesting_user) match.bot1 = MatchParticipation.objects.select_related('bot').only('bot')\ .get(match_id=match.id, participant_number=1).bot match.bot2 = MatchParticipation.objects.select_related('bot').only('bot')\ .get(match_id=match.id, participant_number=2).bot serializer = self.get_serializer(match) return Response(serializer.data, status=status.HTTP_201_CREATED)
def form_valid(self, form): if config.ALLOW_REQUESTED_MATCHES: if form.cleaned_data['bot1'] != form.cleaned_data['bot2']: if self.request.user.match_request_count_left >= form.cleaned_data['match_count']: with transaction.atomic(): # do this all in one commit match_list = [] if form.cleaned_data['matchup_type'] == 'random_ladder_bot': bot1 = form.cleaned_data['bot1'] for _ in range(0, form.cleaned_data['match_count']): bot2 = bot1.get_random_active_excluding_self() if form.cleaned_data['matchup_race'] == 'any' \ else bot1.get_random_active_excluding_self(plays_race=form.cleaned_data['matchup_race']) if bot2 is None: messages.error(self.request, "No opponents of that type could be found.") return self.render_to_response(self.get_context_data(form=form)) match_list.append(Matches.request_match( self.request.user, form.cleaned_data['bot1'], bot2, form.cleaned_data['map'])) else: # specific_matchup for _ in range(0, form.cleaned_data['match_count']): match_list.append(Matches.request_match( self.request.user, form.cleaned_data['bot1'], form.cleaned_data['bot2'], form.cleaned_data['map'])) message = "" for match in match_list: message += f"<a href='{reverse('match', kwargs={'pk': match.id})}'>Match {match.id}</a> created.<br/>" messages.success(self.request, mark_safe(message)) return super().form_valid(form) else: messages.error(self.request, "That number of matches exceeds your match request limit.") return self.render_to_response(self.get_context_data(form=form)) else: messages.error(self.request, "Sorry - you cannot request matches between the same bot.") return self.render_to_response(self.get_context_data(form=form)) else: messages.error(self.request, "Sorry. Requested matches are currently disabled.") return self.render_to_response(self.get_context_data(form=form))
def handle(self, *args, **options): with transaction.atomic(): Matches.timeout_overtime_bot_games()
def create_match(as_user): return Matches.start_next_match(as_user)
def request_match(self, user): return [Matches.request_match(user, self.cleaned_data['bot1'], self.cleaned_data['bot2'], self.cleaned_data['map']) for _ in range(0, self.cleaned_data['match_count'])]
def test_create_results(self): self.client.force_login(User.objects.get(username='******')) self._create_map('test_map') self._create_open_season() bot1 = self._create_active_bot(self.regularUser1, 'bot1') bot2 = self._create_active_bot(self.regularUser1, 'bot2', 'Z') # post a standard result response = self._post_to_matches() self.assertEqual(response.status_code, 201) match = response.data response = self._post_to_results(match['id'], 'Player1Win') self.assertEqual(response.status_code, 201) p1 = MatchParticipation.objects.get(match_id=match['id'], participant_number=1) p2 = MatchParticipation.objects.get(match_id=match['id'], participant_number=2) # check bot datas exist self.assertTrue( os.path.exists(self.uploaded_bot_data_path.format(bot1.id))) self.assertTrue( os.path.exists(self.uploaded_bot_data_path.format(bot2.id))) # check hashes match set 0 self._check_hashes(bot1, bot2, match['id'], 0) # check match logs exist self.assertTrue( os.path.exists( self.uploaded_arenaclient_log_path.format(match['id']))) self.assertTrue( os.path.exists(self.uploaded_match_log_path.format(p1.id))) self.assertTrue( os.path.exists(self.uploaded_match_log_path.format(p2.id))) # Post a result with different bot datas response = self._post_to_matches() self.assertEqual(response.status_code, 201) match = response.data self._post_to_results_bot_datas_set_1(match['id'], 'Player1Win') self.assertEqual(response.status_code, 201) # check bot datas and their backups exist self.assertTrue( os.path.exists(self.uploaded_bot_data_path.format(bot1.id))) self.assertTrue( os.path.exists(self.uploaded_bot_data_path.format(bot2.id))) self.assertTrue( os.path.exists(self.uploaded_bot_data_backup_path.format(bot1.id))) self.assertTrue( os.path.exists(self.uploaded_bot_data_backup_path.format(bot2.id))) # check hashes - should be updated to set 1 self._check_hashes(bot1, bot2, match['id'], 1) # check match logs exist self.assertTrue( os.path.exists(self.uploaded_match_log_path.format(match['id']))) self.assertTrue( os.path.exists(self.uploaded_match_log_path.format(p1.id))) self.assertTrue( os.path.exists(self.uploaded_match_log_path.format(p2.id))) # post a standard result with no bot1 data match = self._post_to_matches().data response = self._post_to_results_no_bot1_data(match['id'], 'Player1Win', 1) self.assertEqual(response.status_code, 201) # check hashes - nothing should have changed self._check_hashes(bot1, bot2, match['id'], 1) # ensure no files got wiped self.assertTrue(Bot.objects.get(id=bot1.id).bot_data) self.assertTrue(Bot.objects.get(id=bot2.id).bot_data) # post a standard result with no bot2 data match = self._post_to_matches().data response = self._post_to_results_no_bot2_data(match['id'], 'Player1Win', 1) self.assertEqual(response.status_code, 201) # check hashes - nothing should have changed self._check_hashes(bot1, bot2, match['id'], 1) # test that requested matches don't update bot_data match5 = Matches.request_match(self.staffUser1, bot1, bot2, Map.random_active()) self._post_to_results_bot_datas_set_1(match5.id, 'Player1Win') # check hashes - nothing should have changed self._check_hashes(bot1, bot2, match5.id, 1) # post a win without a replay - should fail match = self._post_to_matches().data response = self._post_to_results_no_replay(match['id'], 'Player2Win') self.assertEqual(response.status_code, 400) self.assertTrue('non_field_errors' in response.data) self.assertEqual( response.data['non_field_errors'][0], 'A win/loss or tie result must be accompanied by a replay file.') # no hashes should have changed self._check_hashes(bot1, bot2, match['id'], 1)