def load_level_for_editor(request, levelID): level = get_object_or_404(Level, id=levelID) if not permissions.can_load_level(request.user, level): return HttpResponseUnauthorized() level_dict = model_to_dict(level) level_dict['decor'] = level_management.get_decor(level) level_dict['blocks'] = level_management.get_blocks(level) response = {'owned': level.owner == request.user.userprofile, 'level': level_dict} return HttpResponse(json.dumps(response), content_type='application/javascript')
def play_level(request, level, from_editor=False): """ Loads a level for rendering in the game. **Context** ``RequestContext`` ``level`` Level that is about to be played. An instance of :model:`game.Level`. ``blocks`` Blocks that are available during the game. List of :model:`game.Block`. ``lesson`` Instruction shown at the load of the level. String from `game.messages`. ``hint`` Hint shown after a number of failed attempts. String from `game.messages`. **Template:** :template:`game/game.html` """ night_mode = False if not app_settings.NIGHT_MODE_FEATURE_ENABLED else 'night' in request.GET if not permissions.can_play_level(request.user, level, app_settings.EARLY_ACCESS_FUNCTION(request)): return renderError(request, messages.noPermissionTitle(), messages.notSharedLevel()) # Set default level description/hint lookups lesson = 'description_level_default' hint = 'hint_level_default' # If it's one of our levels, set level description/hint lookups # to point to what they should be if level.default: lesson = 'description_level' + str(level.name) hint = 'hint_level' + str(level.name) # Try to get the relevant message, and fall back on defaults try: lessonCall = getattr(messages, lesson) hintCall = getattr(messages, hint) except AttributeError: lessonCall = messages.description_level_default hintCall = messages.hint_level_default lesson = mark_safe(lessonCall()) hint = mark_safe(hintCall()) house = getDecorElement('house', level.theme).url cfc = getDecorElement('cfc', level.theme).url background = getDecorElement('tile1', level.theme).url character = level.character workspace = None python_workspace = None if not request.user.is_anonymous() and hasattr(request.user.userprofile, 'student'): student = request.user.userprofile.student attempt = Attempt.objects \ .filter(level=level, student=student, finish_time__isnull=True, night_mode=night_mode) \ .order_by('-start_time') \ .first() if not attempt: attempt = Attempt(level=level, student=student, score=None, night_mode=night_mode) fetch_workspace_from_last_attempt(attempt) attempt.save() else: attempt = close_and_reset(attempt) workspace = attempt.workspace python_workspace = attempt.python_workspace decorData = level_management.get_decor(level) character_url = character.top_down character_width = character.width character_height = character.height wreckage_url = 'van_wreckage.svg' if night_mode: block_data = level_management.get_night_blocks(level) night_mode_javascript = "true" lesson = messages.title_night_mode() model_solution = '[]' else: block_data = level_management.get_blocks(level) night_mode_javascript = "false" model_solution = level.model_solution return_view = 'level_editor' if from_editor else 'levels' context = RequestContext(request, { 'level': level, 'lesson': lesson, 'blocks': block_data, 'decor': decorData, 'character': character, 'background': background, 'house': house, 'cfc': cfc, 'hint': hint, 'workspace': workspace, 'python_workspace': python_workspace, 'return_url': reverse(return_view), 'character_url': character_url, 'character_width': character_width, 'character_height': character_height, 'wreckage_url': wreckage_url, 'night_mode': night_mode_javascript, 'night_mode_feature_enabled': str(app_settings.NIGHT_MODE_FEATURE_ENABLED).lower(), 'model_solution': model_solution, 'next_level_url': _next_level_url(level, night_mode), 'flip_night_mode_url': _level_url(level, not night_mode), }) return render(request, 'game/game.html', context_instance=context)
def play_level(request, level, from_editor=False): """ Loads a level for rendering in the game. **Context** ``RequestContext`` ``level`` Level that is about to be played. An instance of :model:`game.Level`. ``blocks`` Blocks that are available during the game. List of :model:`game.Block`. ``lesson`` Instruction shown at the load of the level. String from `game.messages`. ``hint`` Hint shown after a number of failed attempts. String from `game.messages`. **Template:** :template:`game/game.html` """ night_mode = False if not app_settings.NIGHT_MODE_FEATURE_ENABLED else 'night' in request.GET if not permissions.can_play_level( request.user, level, app_settings.EARLY_ACCESS_FUNCTION(request)): return renderError(request, messages.noPermissionTitle(), messages.notSharedLevel()) # Set default level description/hint lookups lesson = 'description_level_default' hint = 'hint_level_default' # If it's one of our levels, set level description/hint lookups # to point to what they should be if level.default: lesson = 'description_level' + str(level.name) hint = 'hint_level' + str(level.name) # Try to get the relevant message, and fall back on defaults try: lessonCall = getattr(messages, lesson) hintCall = getattr(messages, hint) except AttributeError: lessonCall = messages.description_level_default hintCall = messages.hint_level_default lesson = mark_safe(lessonCall()) hint = mark_safe(hintCall()) house = getDecorElement('house', level.theme).url cfc = getDecorElement('cfc', level.theme).url background = getDecorElement('tile1', level.theme).url character = level.character workspace = None python_workspace = None if not request.user.is_anonymous() and hasattr(request.user.userprofile, 'student'): student = request.user.userprofile.student attempt = Attempt.objects \ .filter(level=level, student=student, finish_time__isnull=True, night_mode=night_mode) \ .order_by('-start_time') \ .first() if not attempt: attempt = Attempt(level=level, student=student, score=None, night_mode=night_mode) fetch_workspace_from_last_attempt(attempt) attempt.save() else: attempt = close_and_reset(attempt) workspace = attempt.workspace python_workspace = attempt.python_workspace decorData = level_management.get_decor(level) character_url = character.top_down character_width = character.width character_height = character.height wreckage_url = 'van_wreckage.svg' if night_mode: block_data = level_management.get_night_blocks(level) night_mode_javascript = "true" lesson = messages.title_night_mode() model_solution = '[]' else: block_data = level_management.get_blocks(level) night_mode_javascript = "false" model_solution = level.model_solution return_view = 'level_editor' if from_editor else 'levels' context = RequestContext( request, { 'level': level, 'lesson': lesson, 'blocks': block_data, 'decor': decorData, 'character': character, 'background': background, 'house': house, 'cfc': cfc, 'hint': hint, 'workspace': workspace, 'python_workspace': python_workspace, 'return_url': reverse(return_view), 'character_url': character_url, 'character_width': character_width, 'character_height': character_height, 'wreckage_url': wreckage_url, 'night_mode': night_mode_javascript, 'night_mode_feature_enabled': str(app_settings.NIGHT_MODE_FEATURE_ENABLED).lower(), 'model_solution': model_solution, 'next_level_url': _next_level_url(level, night_mode), 'flip_night_mode_url': _level_url(level, not night_mode), }) return render(request, 'game/game.html', context_instance=context)
def play_anonymous_level(request, levelId, from_level_editor=True, random_level=False): night_mode = False if not app_settings.NIGHT_MODE_FEATURE_ENABLED else 'night' in request.GET level = Level.objects.filter(id=levelId) if not level.exists(): return redirect(reverse('level_editor'), permanent=True) level = level[:1].get() if not level.anonymous: return redirect(reverse('level_editor'), permanent=True) lesson = mark_safe(messages.description_level_default()) hint = mark_safe(messages.hint_level_default()) attempt = None house = getDecorElement('house', level.theme).url cfc = getDecorElement('cfc', level.theme).url background = getDecorElement('tile1', level.theme).url character = level.character character_url = character.top_down character_width = character.width character_height = character.height wreckage_url = 'van_wreckage.svg' decor_data = level_management.get_decor(level) if night_mode: block_data = level_management.get_night_blocks(level) night_mode = "true" lesson = messages.title_night_mode() model_solution = '[]' else: block_data = level_management.get_blocks(level) night_mode = "false" model_solution = level.model_solution return_view_name = 'level_editor' if from_level_editor else 'levels' context = RequestContext(request, { 'level': level, 'decor': decor_data, 'blocks': block_data, 'lesson': lesson, 'character': character, 'background': background, 'house': house, 'cfc': cfc, 'hint': hint, 'attempt': attempt, 'random_level': random_level, 'return_url': reverse(return_view_name), 'character_url': character_url, 'character_width': character_width, 'character_height': character_height, 'wreckage_url': wreckage_url, 'night_mode': night_mode, 'night_mode_feature_enabled': str(app_settings.NIGHT_MODE_FEATURE_ENABLED).lower(), 'model_solution': model_solution, }) level.delete() return render(request, 'game/game.html', context_instance=context)