Example #1
0
def user_login(request):
    if request.user.is_authenticated:
        return HttpResponseRedirect(reverse('treasurehunt:question'))
    registered = False

    if request.method == 'POST':
        user_form = forms.UserForm(data=request.POST)
        if user_form.is_valid():
            passmain = user_form.cleaned_data['password']
            passverify = user_form.cleaned_data['confirm_password']
            if passmain == passverify:
                user = user_form.save()
                user.set_password(user.password)
                user.save()

                score = models.Score()
                score.user = user
                score.save()

                registered = True
            else:
                return HttpResponse("Password Don't Match")
        else:
            print(user_form.errors)
    else:
        user_form = forms.UserForm()

    return render(request, 'treasurehunt/signup.html', {
        'user_form': user_form,
        'registered': registered
    })
Example #2
0
def leaderboard(request):
    leader = models.Score.objects.all().filter(ban=False).order_by(
        '-score', 'timestamp')

    current_user = request.user

    rank = 0
    current_user1 = str(current_user)
    if (current_user1 == "AnonymousUser"):
        user_name = []
        i = 1

        for x in leader:

            user_name.append((i, x.user.username, x.score, x.timestamp))
            i += 1

        return render(request, 'treasurehunt/leaderboard.html',
                      {'user_name': user_name})
    else:
        try:
            sc = models.Score.objects.get(user__exact=current_user)

        except:
            score = models.Score()
            score.user = current_user
            score.save()
            sc = models.Score.objects.get(user__exact=current_user)
        user_name = []
        i = 1

        for x in leader:
            print(x.user.username, current_user)
            if (str(x.user.username) == current_user1):
                rank = i

            user_name.append((i, x.user.username, x.score, x.timestamp))
            i += 1

        return render(request, 'treasurehunt/leaderboard.html', {
            'user_name': user_name,
            'score': sc.score,
            'rank': rank
        })
Example #3
0
def index(request):
    score = 0
    current_user = request.user
    current_user1 = str(current_user)
    print()
    if (current_user1 == "AnonymousUser"):
        return render(request, 'treasurehunt/index.html')
    else:
        try:

            sc = models.Score.objects.get(user__exact=current_user)
            score = sc.score
        except:
            score = models.Score()
            score.user = current_user
            score.save()
            sc = models.Score.objects.get(user__exact=current_user)
            score = sc.score
        return render(request, 'treasurehunt/index.html', {'score': sc.score})
Example #4
0
def question(request):

    question_fixed = [
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12',
        '13', '14'
    ]

    current_user = request.user
    try:
        sc = models.Score.objects.get(user__exact=current_user)
    except:
        score = models.Score()
        score.user = current_user
        score.save()
        sc = models.Score.objects.get(user__exact=current_user)
    try:
        ans_fixed = models.AnswerChecker.objects.get(index__exact=sc.score)
    except:
        return render(request, 'treasurehunt/hunt_win.html',
                      {'score': sc.score})

    level = models.level.objects.get(l_number=sc.score + 1)
    if sc.ban == True:
        return render(request, 'treasurehunt/banned.html', {'score': sc.score})
    else:

        if int(sc.score) == 14:
            return render(request, 'treasurehunt/hunt_win.html',
                          {'score': sc.score})
        else:
            if request.method == 'POST':
                question_form = forms.Answer(data=request.POST)
                if question_form.is_valid():
                    ans = question_form.cleaned_data['answer']

                    if ans.lower() == ans_fixed.ans_value():
                        sc.score = sc.score + 1
                        sc.timestamp = datetime.datetime.now()
                        sc.save()
                        level.numuser = level.numuser + 1
                        level.accuracy = round(
                            level.numuser /
                            (float(level.numuser + level.wrong)), 2)
                        level.save()
                        anslog = 'Level: ' + str(
                            sc.score + 1) + ' user: '******' answer: ' + str(ans)
                        print(colored(anslog, 'green'))
                        return render(request,
                                      'treasurehunt/level_transition.html',
                                      {'score': sc.score})

                    else:
                        level.wrong = level.wrong + 1
                        level.save()
                        anslog = 'Level: ' + str(
                            sc.score + 1) + ' user: '******' answer: ' + str(ans)
                        print(colored(anslog, 'red'))
                        return render(request, 'treasurehunt/level_fail.html',
                                      {'score': sc.score})
                else:

                    return render(
                        request, 'treasurehunt/question.html', {
                            'question_form': question_form,
                            'score': sc.score,
                            'question_fixed': question_fixed[sc.score],
                            'level': level
                        })
            else:
                question_form = forms.Answer()

            return render(
                request, 'treasurehunt/question.html', {
                    'question_form': question_form,
                    'score': sc.score,
                    'question_fixed': question_fixed[sc.score],
                    'level': level
                })