async def _answer_submit(request): auth_token = request.headers.get('Authorization', None) admin = request.ctx.session['user']['admin'] if not admin and not in_time_open(): return response.json({'error' : 'unauthorized'}, status=401) payload = dict(urllib.parse.parse_qs(str(request.body, 'utf8'))) problem_no = int(payload['problem_no'][0]) team_answer = Decimal(payload['answer'][0]) team_id = request.ctx.session['user']['id'] current = await app.ctx.db.fetchrow(f"SELECT * from team{team_id} WHERE problem_no = $1", problem_no) if current['solved'] or current['attempts'] >= 3: return response.json({'error': 'forbidden'}, status=403) print("REQUEST IP", request.remote_addr) await app.ctx.db.execute_job("INSERT INTO log(team_id, problem_no, ip, answer, attempt_no, timestamp) VALUES ($1,$2,$3, $4,$5, current_timestamp)", team_id, problem_no, request.remote_addr, team_answer, current['attempts']+1) real_answer = await app.ctx.db.fetchval(f"SELECT (answer) FROM problems WHERE problem_no=$1", problem_no) error_bound = await app.ctx.db.fetchval("SELECT (error_bound) FROM problems where problem_no=$1", problem_no) is_correct = check_answer(attempt=team_answer, answer=real_answer, error=error_bound) solve_data = await app.ctx.db.fetchrow( f"UPDATE team{team_id} SET solved=$1, attempts = attempts + 1, answers=array_append(answers, $2), timestamp = current_timestamp WHERE problem_no = $3 and attempts < 3 RETURNING *;", is_correct, team_answer, problem_no ) if is_correct: await app.ctx.db.execute_job(f""" UPDATE rankings_2022 SET score = score + 1 WHERE team_id=$1 """, team_id ) stats = await fetch_team_stats(app.ctx.db, team_id) return response.json({ 'correct' : is_correct, 'attempts_left' : 3 - solve_data['attempts'], 'answers' : solve_data['answers'], 'rank' : stats.rank, 'problems_solved' : stats.score })
def test_lim(self): self.assertTrue(check_answer("limit({v1},x,0) == 0",{"v1":"sin(x)"})) self.assertFalse(check_answer("limit({v1},x,0) == 0",{"v1":"cos(x)"})) self.assertTrue(check_answer("limit({v1},x,oo) == 0",{"v1":"1/x"})) self.assertTrue(check_answer("limit({v1},x,0) == oo",{"v1":"1/x"})) self.assertFalse(check_answer("limit({v1},x,oo) == oo",{"v1":"1/x"}))
def test_numeric(self): answer = {"v1":"5"} self.assertTrue(check_answer("{v1} == 5",answer)) self.assertTrue(check_answer("{v1} >= 3",answer)) self.assertFalse(check_answer("{v1} > 5",answer))