Beispiel #1
0
def bonus(request, player_id):
    player = get_object_or_404(Player, pk=player_id)

    if request.method == 'POST':
        form = BonusForm(request.POST)
        if form.is_valid():
            coin, amount = form.cleaned_data['coin'], form.cleaned_data['amount']
            formula = Formula.get('bonus-%s' % coin.name)
            if formula is None:
                messages.error(request, 'No such formula, bonus-%s' % coin.name)
            else:
                scoring.score(player, None, formula, external_id=request.user.get_profile().id, **{coin.name: amount})
                if form.cleaned_data['reason']:
                    add_activity(player, _('received {amount} {coin} bonus for {reason}'), amount=amount, coin=coin, reason=form.cleaned_data['reason'])
                messages.info(request, 'Successfully given bonus')
            return redirect('player_profile', id=player.id)
    else:
        form = BonusForm()

    bonuses = scoring.History.objects.filter(user=player, formula__name__startswith='bonus-').order_by('-timestamp')
    penalties = scoring.History.objects.filter(user=player, formula__name__startswith='penalty-').order_by('-timestamp')

    return render_to_response('cpanel/bonus.html', {'target_player': player, 'form': form, 'bonuses': bonuses, 'penalties': penalties},
        context_instance=RequestContext(request)
    )
Beispiel #2
0
 def _give_bonus(self, points, gold):
     if self.best_attempt is not None:
         if points > self.best_attempt.points:
             points = points - self.best_attempt.points
             gold = gold - self.best_attempt.gold
             scoring.score(self.user, None, "bonus-points", points=points)
             scoring.score(self.user, None, "bonus-gold", gold=gold)
             add_activity(
                 self.user,
                 _(
                     "received {points} points and "
                     "{gold} gold bonus"
                     " for beating his/her "
                     "highscore at quiz {quiz_name}"
                 ),
                 points=points,
                 gold=gold,
                 quiz_name=self.quiz.name,
             )
     else:
         scoring.score(self.user, None, "bonus-points", points=points)
         scoring.score(self.user, None, "bonus-gold", gold=gold)
         add_activity(
             self.user,
             _("received {points} points" " and {gold} gold bonus" " for submitting quiz {quiz_name}"),
             points=points,
             gold=gold,
             quiz_name=self.quiz.name,
         )
Beispiel #3
0
def karma_group_view(request, group):
    group_id = group
    group = get_object_or_404(PlayerGroup, pk=group)
    players = group.players.all()
    if request.method == 'POST':
        form = KarmaBonusForm(request.POST, players=players)
        if form.is_valid():
            formula = Formula.get('bonus-karma')
            if formula is None:
                messages.error(request, 'No such formula, bonus-karma')
            else:
                for player, entry in zip(players, form.fields):
                    # get amount of karma points for current player
                    karma_points = form.cleaned_data[entry]
                    # if karma points are zero then skip
                    if karma_points == 0:
                        continue
                    # compute formula and calculate amount of bonus given
                    amount = eval(formula.expression.format(**{'karma_points': karma_points}).split('=')[1])
                    # apply scoring
                    scoring.score(player, None, formula, external_id=request.user.get_profile().id, **{'karma_points': karma_points})
                    # add activity (parse formula expression to get the coin from formula)
                    add_activity(player, _(
                        'received {amount} {coin} bonus for {karma_points} Karma Points'),
                                amount=amount, coin=formula.expression.split('=')[0], karma_points=karma_points,
                                reason='Bonus for Karma')
                    messages.info(request, 'Successfully given bonus')
            return redirect('karma_group', **{'group': group_id})
    else:
        form = KarmaBonusForm(players=players)
    return render_to_response('cpanel/karma_group.html',
                              {'form':form, 'group':group},
                              context_instance=RequestContext(request))
Beispiel #4
0
 def _give_bonus(self, points, gold):
     if self.best_attempt is not None:
         if points > self.best_attempt.points:
             points = points - self.best_attempt.points
             gold = gold - self.best_attempt.gold
             scoring.score(self.user, None, 'bonus-points', points=points)
             scoring.score(self.user, None, 'bonus-gold', gold=gold)
             add_activity(self.user,
                          _('received {points} points and '
                            '{gold} gold bonus'
                            ' for beating his/her '
                            'highscore at quiz {quiz_name}'),
                          points=points,
                          gold=gold,
                          quiz_name=self.quiz.name)
     else:
         scoring.score(self.user, None, 'bonus-points', points=points)
         scoring.score(self.user, None, 'bonus-gold', gold=gold)
         add_activity(self.user,
                      _('received {points} points'
                        ' and {gold} gold bonus'
                        ' for submitting quiz {quiz_name}'),
                      points=points,
                      gold=gold,
                      quiz_name=self.quiz.name)
Beispiel #5
0
def karma_group_view(request, id):
    group = get_object_or_404(PlayerGroup, pk=id)
    players = group.players.all()

    if request.method == 'POST':
        form = KarmaBonusForm(request.POST, players=players)

        if form.is_valid():
            formula = Formula.get('bonus-karma')

            if formula is None:
                messages.error(request, 'No such formula, bonus-karma')
            else:
                for player, entry in zip(players, form.fields):
                    # Get amount of karma points for current player
                    karma_points = form.cleaned_data[entry]

                    # Skip if there are no karma points
                    if karma_points == 0:
                        continue

                    # Compute formula and calculate amount of bonus given
                    amount = eval(
                        formula.expression.format(**{
                            'karma_points': karma_points
                        }).split('=')[1])

                    # Apply scoring
                    scoring.score(player,
                                  None,
                                  formula,
                                  external_id=request.user.get_profile().id,
                                  **{'karma_points': karma_points})

                    # Add activity (parse formula expression to get the coin from formula)
                    add_activity(
                        player,
                        _('received {amount} {coin} bonus for {karma_points} Karma Points'
                          ),
                        amount=amount,
                        coin=formula.expression.split('=')[0],
                        karma_points=karma_points,
                        reason='Bonus for Karma')

                    messages.info(request, 'Successfully given bonus')

            return redirect('karma_group', id=id)
    else:
        form = KarmaBonusForm(players=players)

    return render_to_response('cpanel/karma_group.html', {
        'form': form,
        'group': group
    },
                              context_instance=RequestContext(request))
Beispiel #6
0
 def _give_bonus(self, points, gold):
     if self.best_attempt is not None:
         if points > self.best_attempt.points:
             points = points - self.best_attempt.points
             gold = gold - self.best_attempt.gold
             scoring.score(self.user, None, 'bonus-points', points=points)
             scoring.score(self.user, None, 'bonus-gold', gold=gold)
             add_activity(self.user, _('received {points} points and {gold} gold bonus'
                                       ' for beating his/her highscore at quiz {quiz_name}'),
                          points=points, gold=gold, quiz_name=self.quiz.name)
     else:
         scoring.score(self.user, None, 'bonus-points', points=points)
         scoring.score(self.user, None, 'bonus-gold', gold=gold)
         add_activity(self.user, _('received {points} points and {gold} gold bonus'
                                   ' for submitting quiz {quiz_name}'),
                      points=points, gold=gold, quiz_name=self.quiz.name)
Beispiel #7
0
def bonus(request, player_id):
    player = get_object_or_404(Player, pk=player_id)

    if request.method == 'POST':
        form = BonusForm(request.POST)
        if form.is_valid():
            coin, amount = form.cleaned_data['coin'], form.cleaned_data[
                'amount']
            formula = Formula.get('bonus-%s' % coin.name)
            if formula is None:
                messages.error(request,
                               'No such formula, bonus-%s' % coin.name)
            else:
                scoring.score(player,
                              None,
                              formula,
                              external_id=request.user.get_profile().id,
                              **{coin.name: amount})
                if form.cleaned_data['reason']:
                    add_activity(
                        player,
                        _('received {amount} {coin} bonus for {reason}'),
                        amount=amount,
                        coin=coin,
                        reason=form.cleaned_data['reason'])
                messages.info(request, 'Successfully given bonus')
            return redirect('details_player', pk=player.id)
    else:
        form = BonusForm()

    bonuses = scoring.History.objects.filter(
        user=player, formula__name__startswith='bonus-').order_by('-timestamp')
    penalties = scoring.History.objects.filter(
        user=player,
        formula__name__startswith='penalty-').order_by('-timestamp')

    return render_to_response('cpanel/bonus.html', {
        'target_player': player,
        'form': form,
        'bonuses': bonuses,
        'penalties': penalties
    },
                              context_instance=RequestContext(request))