def coordination_quest_ajax(request, quest_id): if request.is_ajax(): quest = get_object_or_404(Quest, pk=quest_id, parent__isnull=True) request = is_quest_player(request, quest) player = request.user messages = quest.messages().filter(is_show=True) html_messages = render_to_string('coordination/messages/_list.html', {'messages': messages}) data = {'messages': html_messages, } if quest.nonlinear: rest_quest = None if quest.started and not quest.is_game_over: rest_quest = get_timedelta(quest.game_over) count = 0 missions = quest.missions().filter(order_number__gt=0, is_finish=False) for mission in missions: is_completed = mission.is_completed(request.user) if not is_completed: count += 1 mission_finish = None if missions and count == 0 or quest.is_game_over or quest.ended: mission_finish = quest.finish_mission() html_mission_finish = render_to_string('coordination/quests/coordination/_mission_finish.html', {'mission_finish': mission_finish}) data.update({'rest_quest': rest_quest, 'mission_finish': html_mission_finish}) else: current_mission = get_object_or_404(CurrentMission, mission__quest=quest, player=player) mission = current_mission.mission hints = current_mission.display_hints() next_hint_time = current_mission.next_hint_time() delay = None if next_hint_time: delay = get_timedelta(next_hint_time) completed_missions = Mission.completed_missions(quest, player) wrong_keys_str = None if quest.started and not mission.is_finish: wrong_keys_str = Keylog.wrong_keylogs_format(player, mission) json_mission = mission.as_json() html_picture = render_to_string('coordination/quests/coordination/_picture.html', {'mission': mission}) html_hints = render_to_string('coordination/hints/_list.html', {'hints': hints}) html_wrong_keys = render_to_string('coordination/quests/coordination/_wrong_keys.html', {'wrong_keys': wrong_keys_str}) html_completed_missions = render_to_string('coordination/quests/coordination/_completed_missions.html', {'completed_missions': completed_missions}) hide_form = not quest.started or mission.is_finish data.update({'mission': json_mission, 'hints': html_hints, 'delay': delay, 'wrong_keys': html_wrong_keys, 'completed_missions': html_completed_missions, 'hide_form': hide_form, 'picture': html_picture}) return JsonResponse(data) else: raise Http404
def nonlinear_coordination(request, quest): player = request.user if request.method == 'POST': mission = get_object_or_404(Mission, pk=request.POST['mission_id']) if quest.started and not quest.is_game_over and not mission.is_completed(player): form = KeyForm(request.POST, quest=quest) if form.is_valid(): key = form.cleaned_data["key"] right_key = mission.key is_right = len(right_key) > 0 and right_key == key keylog = Keylog(key=key, fix_time=timezone.now(), player=player, mission=mission, is_right=is_right) if is_right: keylog.points = mission.points keylog.save() if mission.order_number > 1: url = '{0}#m{1}'.format(resolve_url('coordination:quest_coordination', quest_id=quest.id), mission.order_number - 1) else: url = resolve_url('coordination:quest_coordination', quest_id=quest.id) return redirect(url) else: rest_quest = None missions = None mission_start = None mission_finish = None display_hints = None rest_hints = None form = None if quest.not_started: mission_start = quest.start_mission() else: if quest.started and not quest.is_game_over: rest_quest = get_timedelta(quest.game_over) form = KeyForm(quest=quest) count = 0 missions = quest.missions().filter(order_number__gt=0, is_finish=False) for mission in missions: mission.wrong_keys = Keylog.wrong_keylogs_format(player, mission) is_completed = mission.is_completed(request.user) mission.is_completed = is_completed if not is_completed: count += 1 if missions and count == 0 or quest.is_game_over or quest.ended: mission_finish = quest.finish_mission() display_hints, rest_hints = Mission.hints_in_nl(quest, missions) points = Keylog.total_points(quest, player) messages = quest.messages().filter(is_show=True) context = {'quest': quest, 'messages': messages, 'missions': missions, 'mission_finish': mission_finish, 'mission_start': mission_start, 'rest_quest': rest_quest, 'points': points, 'form': form, 'display_hints': display_hints, 'rest_hints': rest_hints} return render(request, 'coordination/quests/coordination/nonlinear.html', context)
def control_quest(request, quest_id): quest = get_object_or_404(Quest, pk=quest_id, parent__isnull=True) request = is_quest_organizer(request, quest) context = {'quest': quest, } if quest.nonlinear: rest_quest = 0 if quest.started: rest_quest = get_timedelta(quest.game_over) context.update({'rest_quest': rest_quest}) elif quest.linear: current_missions = quest.current_missions() context.update({'current_missions': current_missions}) return render(request, 'coordination/quests/control.html', context)
def tables_quest(request, quest_id): quest = get_object_or_404(Quest, pk=quest_id, parent__isnull=True) if quest.nonlinear or quest.multilinear: request = is_quest_organizer_or_agent(request, quest) rest_quest = 0 if quest.started: rest_quest = get_timedelta(quest.game_over) players = quest.players_ext() missions = quest.missions_ext() context = {'quest': quest, 'players': players, 'missions': missions, 'rest_quest': rest_quest} if quest.nonlinear: return render(request, 'coordination/quests/tables/nonlinear.html', context) else: return render(request, 'coordination/quests/tables/multilinear.html', context) else: return redirect('coordination:quest_tables_current', quest_id=quest_id)
def multilinear_coordination(request, quest): player = request.user if request.method == 'POST': mission = get_object_or_404(Mission, pk=request.POST['mission_id']) if quest.started and not quest.is_game_over and not mission.is_completed(player): form = KeyForm(request.POST, quest=quest) if form.is_valid(): key = form.cleaned_data["key"] right_key = mission.key is_right = len(right_key) > 0 and right_key == key keylog = Keylog(key=key, fix_time=timezone.now(), player=player, mission=mission, is_right=is_right) if is_right: keylog.points = mission.points line = mission.quest next_mission = Mission.objects.filter(quest=line, order_number=mission.order_number + 1).first() current_mission = get_object_or_404(CurrentMission, mission__quest=line, player=player) current_mission.mission = next_mission current_mission.start_time = keylog.fix_time current_mission.save() keylog.save() url = '{0}#m{1}'.format(resolve_url('coordination:quest_coordination', quest_id=quest.id), mission.id) return redirect(url) else: rest_quest = None lines = None mission_start = None mission_finish = None form = None if quest.not_started: mission_start = quest.start_mission() else: if quest.started and not quest.is_game_over: rest_quest = get_timedelta(quest.game_over) form = KeyForm(quest=quest) lines = quest.lines() for line in lines: current_mission = get_object_or_404(CurrentMission, mission__quest=line, player=player) line.mission = current_mission.mission if quest.is_game_over or quest.ended: mission_finish = quest.finish_mission() points = Keylog.total_points(quest, player) messages = quest.messages().filter(is_show=True) context = {'quest': quest, 'messages': messages, 'lines': lines, 'mission_finish': mission_finish, 'mission_start': mission_start, 'rest_quest': rest_quest, 'points': points, 'form': form} return render(request, 'coordination/quests/coordination/multilinear.html', context)
def linear_coordination(request, quest): player = request.user current_mission = get_object_or_404(CurrentMission, mission__quest=quest, player=player) mission = current_mission.mission if request.method == 'POST': if quest.started and not mission.is_finish: form = KeyForm(request.POST, quest=quest) if form.is_valid(): key = form.cleaned_data["key"] next_missions = Mission.objects.filter(quest=quest, order_number=mission.order_number + 1) if quest.linear: right_key = mission.key next_mission = next_missions.first() is_right = len(right_key) > 0 and right_key == key else: next_mission = next_missions.filter(key=key).first() is_right = next_mission is not None keylog = Keylog(key=key, fix_time=timezone.now(), player=player, mission=mission, is_right=is_right) keylog.save() if is_right: current_mission.mission = next_mission current_mission.start_time = keylog.fix_time current_mission.save() return redirect('coordination:quest_coordination', quest_id=quest.id) else: hints = current_mission.display_hints() next_hint_time = current_mission.next_hint_time() delay = get_timedelta(next_hint_time) if next_hint_time else None completed_missions = Mission.completed_missions(quest, player) form = None wrong_keys_str = None messages = quest.messages().filter(is_show=True) if quest.started and not mission.is_finish: form = KeyForm(quest=quest) wrong_keys_str = Keylog.wrong_keylogs_format(player, mission) context = {'quest': quest, 'mission': mission, 'hints': hints, 'form': form, 'messages': messages, 'wrong_keys': wrong_keys_str, 'delay': delay, 'completed_missions': completed_missions} return render(request, 'coordination/quests/coordination/general.html', context)