Esempio n. 1
0
def prizes(req):
    if not req.user.is_authenticated:
        raise Http404()
    if req.POST.get('action') == 'purchase':
        try:
            prize = Prize.objects.get(id=req.POST.get('prize'))
            if prize.stock <= 0:
                raise ValueError("Not enough stock")
            if prize.cost > req.user.bonuses:
                raise ValueError("Too expensive")
            purchase = Purchase()
            purchase.user = req.user
            purchase.prize = prize
            purchase.code = ''.join(
                random.choice("ABCDEFGHKMNPQRSTUVWXYZ23456789")
                for _ in range(8))
            purchase.status = Purchase.PAYED
            purchase.save()
            transaction = Transaction()
            transaction.sender = req.user
            transaction.amount = prize.cost
            transaction.save()
            req.user.bonuses -= prize.cost
            req.user.save()
            prize.stock -= 1
            prize.save()
            return redirect('/')
        except:
            raise Http404()
    return render(req,
                  'prizes.html',
                  context=dict(prizes=Prize.objects.filter(
                      stock__gt=0).order_by('cost').all()))
Esempio n. 2
0
def autoreward_lesson(lesson):
    if not lesson.contest_id:
        return
    upsolving = is_upsolving(lesson.contest_id)
    rules = [Rule(upsolving, rule) for rule in lesson.autorewards.split("\n")]
    runs = run_contests_cmd(lesson.contest_id, "dump-filtered-runs",
                            "status==OK", "0", "-1").split('\n')
    for s in runs:
        if not s:
            continue
        info = s.split(';')
        user_id, prob = int(info[10]), info[18]
        if not Autoreward.objects.filter(
                user__id=user_id, prob=prob, lesson=lesson).exists():
            score = 0
            for rule in rules:
                if rule.appliable(prob):
                    score += rule.score
                    if not rule.add:
                        break
            r = Autoreward()
            r.user = EjudgeAuthBackend().get_user(user_id)
            r.prob = prob
            r.lesson = lesson
            r.save()
            if score > 0:
                r.user.bonuses += score
                r.user.save()
                lesson.bank.bonuses -= score
                tr = Transaction()
                tr.sender = lesson.bank
                tr.receiver = r.user
                tr.amount = score
                tr.save()
    lesson.bank.save()
Esempio n. 3
0
def give_bonuses(modeladmin, request, queryset):
    bonuses = int(request.POST['bonuses'])
    for user in queryset:
        user.bonuses += bonuses
        user.save()
        request.user.bonuses -= bonuses
        tr = Transaction()
        tr.sender = request.user
        tr.receiver = user
        tr.amount = bonuses
        tr.save()
    request.user.save()
Esempio n. 4
0
def casino(req, group_id):
    session = False
    try:
        group = Group.objects.get(id=group_id)
        if CasinoSession.objects.filter(user=req.user, group=group).exists():
            session = True
        if not group.casino_contest:
            raise ValueError
        if not req.user.is_authenticated:
            raise ValueError
        if not User.objects.filter(id=req.user.id,
                                   groups__id=group.id).exists():
            raise ValueError
    except:
        raise Http404()
    if req.POST.get('action') == 'start' and not session:
        try:
            bet = int(req.POST.get('bet'))
            if bet < 0:
                raise ValueError
            if bet > req.user.bonuses:
                raise ValueError
            sess = CasinoSession()
            sess.group = group
            sess.user = req.user
            sess.bet = bet
            sess.save()
            sess.start_contest()
            transaction = Transaction()
            transaction.sender = req.user
            transaction.amount = bet
            transaction.save()
            req.user.bonuses -= bet
            req.user.save()
            return redirect('/casino/%d/play' % group.id)
        except:
            raise Http404()

    return render(req,
                  'casino.html',
                  context=dict(group=group, session=session))