Exemple #1
0
def play(request):
    params = request.GET.keys()
    game = Game(params)

    # Process the initial submission from the homepage
    if request.method == 'GET':
        # Get a random conjugation for the user, in the form of the list given
        # by this function.
        request.session['current_verb'] = game.get_conjugation()

        # Extract the parameter list from the outputted conjugation
        request.session['html_params'] = params_to_html(
            request.session['current_verb'][2])

        character = "/static/resources/ganbaru.png"
    # Process the user's conjugation attempts
    if request.is_ajax and request.method == 'POST':
        attempt = request.POST.get('attempt')

        if attempt in request.session['current_verb'][3]:
            request.session['current_verb'] = game.get_conjugation()
            request.session['html_params'] = params_to_html(
                request.session['current_verb'][2])
            character = "/static/resources/ganbaru.png"

        else:
            # print(request.session['current_verb'])
            character = "/static/resources/incorrect.png"

    # Send the proper parameters for the current conjugation to the template
    context = {
        'verb': verb_string_to_html(request.session['current_verb'][0]),
        'translation': request.session['current_verb'][1],
        'construction': request.session['html_params'][0],
        'polarity': request.session['html_params'][1],
        'tense': request.session['html_params'][2],
        'form': request.session['html_params'][3],
        'character': character
    }

    # Render the HTML template index.html with the data in the context variable
    return render(request, 'play.html', context=context)