def create_finish_mission(request, quest_id): quest = get_object_or_404(Quest, pk=quest_id, type=Quest.LINE_NONLINEAR) if quest.not_started: is_quest_organizer(request, quest) finish = Mission.objects.create(quest=quest, name_in_table='Финиш', order_number=1, is_finish=True) Mission.update_finish_number(quest) return redirect('coordination:mission_detail', mission_id=finish.id) return redirect('coordination:quest_missions', quest_id=quest.pk)
def delete_mission(request, mission_id): mission = get_object_or_404(Mission, pk=mission_id) quest = mission.quest if not quest.started: is_quest_organizer(request, quest) if quest.linear or quest.nonlinear: if not mission.is_start and not mission.is_finish: mission.delete() Mission.update_finish_number(quest) else: if not mission.is_start: mission.delete() Mission.update_finish_number(quest) return redirect('coordination:quest_missions', quest_id=quest.pk)
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 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 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)