コード例 #1
0
ファイル: views.py プロジェクト: dobrypd/Numberlink
def boardlist(request, contest = 1):
    option = request.GET.has_key('options')
    if option:
        try:
            c = Contest.objects.get(id = contest)
        except Contest.DoesNotExist:
            raise Http404
        boardsnames = []
        for b in c.boards.all():
            boardsnames.append(str(b))
        return render_to_response('boardsoption.html', {'boardsnames': boardsnames})
    return render_to_response('board.html',
            {'title': 'Gra', 'contest':contest, 
            'contests':contests(request.user)}, context_instance=RequestContext(request)
        )
コード例 #2
0
ファイル: views.py プロジェクト: dobrypd/Numberlink
def highscores(request, contest):
    try:
        c = Contest.objects.get(id = contest)
    except Contest.DoesNotExist:
        raise Http404
    if request.method == 'POST':
        uname = request.user.username
        bname = request.POST['boardname']
        time = request.POST['time']
        try:
            u = User.objects.get(username=uname)
        except User.DoesNotExist:
            raise Http404
        try:
            b = Board.objects.get(name=bname)
        except Board.DoesNotExist:
            raise Http404
        time_sec = int(time)

        #sprawdz czy konkurs jest jeszcze wazny!
        if(c.expiredate < datetime.date.today()):
            return HttpResponse(simplejson.dumps(False), mimetype='application/json')

        #1sprawdz czy jest najlepszy
        try: 
            #niezmiennik (jest tylko jeden old (tylko tutaj go zmieniam))
            old = c.score.get(board = b)
        except Score.DoesNotExist:
            s = Score(user=u, board = b, time_s = time_sec, date = datetime.datetime.now())
            s.save()
            c.highscores.add(s)
            return HttpResponse(simplejson.dumps(True), mimetype='application/json')
        if (old.time_s > time_sec):
            old.delete()
            s = c.highscores(user = u, board = b, time_s = time_sec, date = datetime.datetime.now())
            s.save()
            c.highscores.add(s)
            return HttpResponse(simplejson.dumps(True), mimetype='application/json')
        else:
            return HttpResponse(simplejson.dumps(False), mimetype='application/json')
    else:   
        scores = []
        for s in  c.highscores.order_by('time_s'):
            scores.append((s.board.name, s.user, str(s.time_s) + 's'))

        return render_to_response('highscores.html', 
                {'title': 'Najlepsze wyniki', 'scores': scores, 'contests':contests(request.user)}, context_instance=RequestContext(request)
            )