def details(request, quest): # return params prize_url = '' active_attempt = get_active_attempt_if_any(request.user, quest) if request.POST.get('start'): # game started if not request.user.is_authenticated: return redirect('/login/github') messages.info(request, f'Quest started. Journey Forth') process_start(request, quest) elif request.POST.get('win'): # game won messages.info(request, f'You win.. Congrats') prize_url = process_win(request, active_attempt) elif request.POST.get('lose'): # game lost messages.info(request, f'You lose. Try again soon.') return redirect('/quests') else: if active_attempt: messages.info(request, f'You lose. Try again after the cooltown period.') return redirect('/quests') attempts = quest.attempts.filter( profile=request.user.profile ) if request.user.is_authenticated else quest.attempts.none() params = get_base_quest_view_params(request.user, quest) params['prize_url'] = prize_url params['started'] = request.POST.get('start', '') response = TemplateResponse(request, 'quests/types/example.html', params) return response
def details(request, quest): time_per_answer = quest.game_schema.get('seconds_per_question', 30) time_per_answer_buffer = 5 if not request.user.is_authenticated and request.GET.get('login'): return redirect('/login/github/?next=' + request.get_full_path()) # process form submission try: if request.body or request.GET.get('answers'): payload = {} try: payload = json.loads(request.body) except JSONDecodeError: payload = json.loads(request.GET['answers']) qn = payload.get('question_number') can_continue = True did_win = False prize_url = False if qn is not None and request.user.is_authenticated: save_attempt = qn == 0 reason = '' if save_attempt: process_start(request, quest) else: qa = get_active_attempt_if_any(request.user, quest, state=(qn - 1)) this_question = quest.questions[qn - 1] correct_answers = [ ele['answer'] for ele in this_question['responses'] if ele['correct'] ] their_answers = [ unescape(ele) for ele in payload.get('answers') ] this_time_per_answer = time_per_answer answer_level_seconds_to_respond = payload.get( 'seconds_to_respond', None) if answer_level_seconds_to_respond: this_time_per_answer = answer_level_seconds_to_respond time_used = ( timezone.now() - qa.modified_on).seconds if qa else timezone.now() is_out_of_time = time_used > this_time_per_answer + time_per_answer_buffer if is_out_of_time: # fix for silly issue where the time used is almost exactly # 24 hours off. cant figure out exactly why but it happens. if time_used > 86390 and time_used <= 86400: is_out_of_time = False did_they_do_correct = set(correct_answers) == set( their_answers) or (this_question.get( 'any_correct', False) and len(their_answers)) can_continue = did_they_do_correct and not is_out_of_time if not did_they_do_correct: reason = 'not correct' if is_out_of_time: reason = f'out of time- used {time_used} s from qa {qa.pk} ' if can_continue: qa.state += 1 qa.save() did_win = can_continue and len(quest.questions) <= qn if did_win: prize_url = process_win(request, qa) qa.save() response = { "question": quest.questions_safe(qn), "can_continue": can_continue, "did_win": did_win, "prize_url": prize_url, 'reason': reason, } response = JsonResponse(response) #response['X-Frame-Options'] = x_frame_option return response except Exception as e: logger.exception(e) pass # make sure that cooldown period is respected override_cooldown = request.user.is_staff and request.GET.get( 'force', False) if quest.is_within_cooldown_period(request.user) and not override_cooldown: cooldown_time_left = ( timezone.now() - quest.last_failed_attempt(request.user).created_on).seconds cooldown_time_left = round( (quest.cooldown_minutes - cooldown_time_left / 60), 1) messages.info( request, f'You are within this quest\'s {quest.cooldown_minutes} min cooldown period. Try again in {cooldown_time_left} mins.' ) return redirect('/quests') # return params attempts = quest.attempts.filter( profile=request.user.profile ) if request.user.is_authenticated else quest.attempts.none() params = get_base_quest_view_params(request.user, quest) response = TemplateResponse(request, 'quests/types/quiz_style.html', params) #response['X-Frame-Options'] = x_frame_option return response
def details(request, quest): time_per_answer = quest.game_schema.get('seconds_per_question', 30) time_per_answer_buffer = 5 if not request.user.is_authenticated and request.GET.get('login'): return redirect('/login/github?next=' + request.get_full_path()) # process form submission try: payload = json.loads(request.body) qn = payload.get('question_number') can_continue = True did_win = False prize_url = False if qn is not None and request.user.is_authenticated: save_attempt = qn == 0 if save_attempt: process_start(request, quest) else: qa = get_active_attempt_if_any(request.user, quest, state=(qn-1)) this_question = quest.questions[qn-1] correct_answers = [ele['answer'] for ele in this_question['responses'] if ele['correct']] their_answers = payload.get('answers') this_time_per_answer = time_per_answer answer_level_seconds_to_respond = payload.get('seconds_to_respond', None) if answer_level_seconds_to_respond: this_time_per_answer = answer_level_seconds_to_respond is_out_of_time = (timezone.now() - qa.modified_on).seconds > this_time_per_answer + time_per_answer_buffer did_they_do_correct = set(correct_answers) == set(their_answers) or (this_question.get('any_correct', False) and len(their_answers)) can_continue = did_they_do_correct and not is_out_of_time if can_continue: qa.state += 1 qa.save() did_win = can_continue and len(quest.questions) <= qn if did_win: prize_url = process_win(request, qa) qa.save() response = { "question": quest.questions_safe(qn), "can_continue": can_continue, "did_win": did_win, "prize_url": prize_url, } response = JsonResponse(response) #response['X-Frame-Options'] = x_frame_option return response except Exception as e: print(e) pass # make sure that cooldown period is respected override_cooldown = request.user.is_staff and request.GET.get('force', False) if quest.is_within_cooldown_period(request.user) and not override_cooldown: cooldown_time_left = (timezone.now() - quest.last_failed_attempt(request.user).created_on).seconds cooldown_time_left = round((quest.cooldown_minutes - cooldown_time_left/60),1) messages.info(request, f'You are within this quest\'s {quest.cooldown_minutes} min cooldown period. Try again in {cooldown_time_left} mins.') return redirect('/quests'); # return params attempts = quest.attempts.filter(profile=request.user.profile) if request.user.is_authenticated else quest.attempts.none() params = get_base_quest_view_params(request.user, quest) response = TemplateResponse(request, 'quests/types/quiz_style.html', params) #response['X-Frame-Options'] = x_frame_option return response