Esempio n. 1
0
def close_and_reset(attempt):
    attempt.finish_time = timezone.now()
    attempt.save()
    new_attempt = Attempt(level=attempt.level,
                          student=attempt.student,
                          score=None,
                          night_mode=attempt.night_mode,
                          workspace=attempt.workspace,
                          python_workspace=attempt.python_workspace)
    new_attempt.save()
    return new_attempt
Esempio n. 2
0
def close_and_reset(attempt):
    attempt.finish_time = timezone.now()
    attempt.save()
    new_attempt = Attempt(level=attempt.level,
                          student=attempt.student,
                          score=None,
                          night_mode=attempt.night_mode,
                          workspace=attempt.workspace,
                          python_workspace=attempt.python_workspace)
    new_attempt.save()
    return new_attempt
Esempio n. 3
0
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)
Esempio n. 4
0
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 = get_decor_element("house", level.theme).url
    cfc = get_decor_element("cfc", level.theme).url
    background = get_decor_element("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

    decor_data = cached_level_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 = cached_level_blocks(level)
        night_mode_javascript = "false"
        model_solution = level.model_solution

    return_view = "level_editor" if from_editor else "levels"

    return render(
        request,
        "game/game.html",
        context={
            "level":
            level,
            "lesson":
            lesson,
            "blocks":
            block_data,
            "decor":
            decor_data,
            "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),
        },
    )
Esempio n. 5
0
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 = get_decor_element('house', level.theme).url
    cfc = get_decor_element('cfc', level.theme).url
    background = get_decor_element('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

    decor_data = cached_level_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 = cached_level_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':
            decor_data,
            '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)
Esempio n. 6
0
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 = get_decor_element("house", level.theme).url
    cfc = get_decor_element("cfc", level.theme).url
    background = get_decor_element("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

    decor_data = cached_level_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 = cached_level_blocks(level)
        night_mode_javascript = "false"
        model_solution = level.model_solution

    return_view = "level_editor" if from_editor else "levels"

    return render(
        request,
        "game/game.html",
        context={
            "level": level,
            "lesson": lesson,
            "blocks": block_data,
            "decor": decor_data,
            "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),
        },
    )