Exemplo n.º 1
0
def bazaar(request, message='', error=''):
    player = request.user.get_profile() if request.user.is_authenticated(
    ) else None
    spells = Spell.objects.all().order_by('-available', 'level_required')

    # Disable exchange for real
    exchange_disabled = BoolSetting.get('disable-Bazaar-Exchange').get_value()
    try:
        rate = scoring.calculate('gold-points-rate', gold=1)['points']
        rate2 = round(1 /
                      scoring.calculate('points-gold-rate', points=1)['gold'])
    except InvalidFormula:
        rate, rate2 = 1, 1
    rate_text = _(
        'Rate: 1 gold = {rate} points, 1 gold = {rate2} points').format(
            rate=rate, rate2=rate2)

    cast_spells = PlayerSpellDue.objects.filter(source=player).all()
    unseen_count = cast_spells.filter(seen=False).count()

    # TODO: think of smth better
    cast_spells.update(seen=True)
    return render_to_response('magic/bazaar.html', {
        'spells': spells,
        'rate': rate,
        'rate_text': rate_text,
        'cast': cast_spells,
        'unseen_count': unseen_count,
        'theowner': player,
        'message': message,
        'exchange_disabled': exchange_disabled,
        'error': error
    },
                              context_instance=RequestContext(request))
Exemplo n.º 2
0
    def get_context_data(self, **kwargs):
        context = super(BazaarView, self).get_context_data(**kwargs)
        player = self.request.user.get_profile(
        ) if self.request.user.is_authenticated() else None
        spells = Spell.objects.all().order_by('-available', 'level_required')

        # Disable exchange for real
        exchange_disabled = BoolSetting.get(
            'setting-bazaar-exchange').get_value() is False
        try:
            rate = scoring.calculate('gold-points-rate', gold=1)['points']
            rate2 = round(
                1 / scoring.calculate('points-gold-rate', points=1)['gold'])
        except InvalidFormula:
            rate, rate2 = 1, 1
        rate_text = _(
            'Rate: 1 gold = {rate} points, {rate2} points = 1 gold').format(
                rate=rate, rate2=rate2)

        cast_spells = PlayerSpellDue.objects.filter(source=player).all()
        unseen_count = cast_spells.filter(seen=False).count()

        # TODO: think of smth better
        cast_spells.update(seen=True)

        context.update({
            'spells': spells,
            'rate': rate,
            'rate_text': rate_text,
            'cast': cast_spells,
            'unseen_count': unseen_count,
            'theowner': player,
            'exchange_disabled': exchange_disabled
        })
        return context
Exemplo n.º 3
0
    def get_context_data(self, **kwargs):
        context = super(BazaarView, self).get_context_data(**kwargs)
        player = self.request.user.get_profile() if self.request.user.is_authenticated() else None
        spells = Spell.objects.all().order_by('-available', 'level_required')

        # Disable exchange for real
        exchange_disabled = BoolSetting.get('disable-Bazaar-Exchange').get_value()
        try:
            rate = scoring.calculate('gold-points-rate', gold=1)['points']
            rate2 = round(1/scoring.calculate('points-gold-rate', points=1)['gold'])
        except InvalidFormula:
            rate, rate2 = 1, 1
        rate_text = _('Rate: 1 gold = {rate} points, {rate2} points = 1 gold').format(rate=rate,
                                                                                      rate2=rate2)

        cast_spells = PlayerSpellDue.objects.filter(source=player).all()
        unseen_count = cast_spells.filter(seen=False).count()

        # TODO: think of smth better
        cast_spells.update(seen=True)

        context.update({'spells': spells,
                        'rate': rate, 'rate_text': rate_text,
                        'cast': cast_spells,
                        'unseen_count': unseen_count,
                        'theowner': player,
                        'exchange_disabled': exchange_disabled})
        return context
Exemplo n.º 4
0
def bazaar(request, message='', error=''):
    player = request.user.get_profile() if request.user.is_authenticated() else None
    spells = Spell.objects.all().order_by('-available', 'level_required')

    # Disable exchange for real
    exchange_disabled = BoolSetting.get('disable-Bazaar-Exchange').get_value()
    try:
        rate = scoring.calculate('gold-points-rate', gold=1)['points']
        rate2 = round(1/scoring.calculate('points-gold-rate', points=1)['gold'])
    except InvalidFormula:
        rate, rate2 = 1, 1
    rate_text = _('Rate: 1 gold = {rate} points, 1 gold = {rate2} points').format(rate=rate, rate2=rate2)

    cast_spells = PlayerSpellDue.objects.filter(source=player).all()
    unseen_count = cast_spells.filter(seen=False).count()

    # TODO: think of smth better
    cast_spells.update(seen=True)
    return render_to_response('magic/bazaar.html', {'spells': spells,
                              'rate': rate, 'rate_text': rate_text,
                              'cast': cast_spells,
                              'unseen_count': unseen_count,
                              'theowner': player,
                              'message': message,
                              'exchange_disabled': exchange_disabled,
                              'error': error},
                              context_instance=RequestContext(request))
Exemplo n.º 5
0
    def testCalculate(self):
        formula = Formula.objects.create(id="_test_formula", formula="_test=5", owner=self.game)

        # Call by name
        ret = scoring.calculate("_test_formula")
        self.assertTrue(isinstance(ret, dict))

        # Call by objcet
        ret = scoring.calculate(formula)
        self.assertTrue(isinstance(ret, dict))
        self.assertEqual(ret["_test"], 5)

        formula2 = Formula.objects.create(id="_test_formula2", formula="_test=5*3", owner=self.game)

        ret = scoring.calculate(formula2)
        self.assertTrue(isinstance(ret, dict))
        self.assertEqual(ret["_test"], 15)

        # Multiple coins
        formula2.formula = "_test=5*3; points=4"

        ret = scoring.calculate(formula2)
        self.assertTrue(isinstance(ret, dict))
        self.assertEqual(ret["_test"], 15)
        self.assertEqual(ret["points"], 4)

        # Fail safe
        formula2.formula = "_test=5*cucu"
        try:
            ret = scoring.calculate(formula2)
            # no error? wtf
            self.assertFalse(true)
        except Exception as e:
            self.assertTrue(isinstance(e, FormulaParsingError))
Exemplo n.º 6
0
def bazaar_exchange(request):
    gold_rate = scoring.calculate('gold-points-rate', gold=1)['points']
    points_rate = scoring.calculate('points-gold-rate', points=1)['gold']

    player = request.user.get_profile()
    message, error = '', ''
    if BoolSetting.get('setting-bazaar-exchange').get_value() is False:
        error = _("Exchange is disabled")
    elif request.method == 'POST':
        try:
            points = float(request.POST.get('points', 0))
            gold = round(float(request.POST.get('gold', 0)))
        except:
            error = _('Invalid amounts')
        else:
            if points != 0:
                gold = points_rate * points
                if gold > 0:
                    if player.points < points:
                        error = _('Insufficient points')
                    else:
                        points = round(gold) / points_rate
                        scoring.score(player,
                                      None,
                                      'points-gold-rate',
                                      points=points)
                        message = _('Converted successfully')
                else:
                    error = _('Insufficient points')
            # other way around
            elif gold != 0:
                points = gold_rate * gold
                if player.coins['gold'] < gold:
                    error = _('Insufficient gold')
                else:
                    scoring.score(player, None, 'gold-points-rate', gold=gold)
                    message = _('Converted successfully')
            else:
                error = _('Unknown action')
    else:
        error = _('Expected post')

    if error:
        messages.error(request, error)
    if message:
        messages.success(request, message)

    return render_to_response('magic/bazaar_buy.html', {'tab': 'exchange'},
                              context_instance=RequestContext(request))
Exemplo n.º 7
0
def bazaar_exchange(request):
    gold_rate = scoring.calculate('gold-points-rate', gold=1)['points']
    points_rate = scoring.calculate('points-gold-rate', points=1)['gold']

    player = request.user.get_profile()
    message, error = '', ''
    if BoolSetting.get('disable-Bazaar-Exchange').get_value():
        error = _("Exchange is disabled")
    elif request.method == 'POST':
        try:
            points = float(request.POST.get('points', 0))
            gold = round(float(request.POST.get('gold', 0)))
        except:
            error = _('Invalid amounts')
        else:
            if points != 0:
                gold = points_rate * points
                if gold > 0:
                    if player.points < points:
                        error = _('Insufficient points')
                    else:
                        points = round(gold) / points_rate
                        scoring.score(player, None, 'points-gold-rate', points=points)
                        message = _('Converted successfully')
                else:
                    error = _('Insufficient points')
            # other way around
            elif gold != 0:
                points = gold_rate * gold
                if player.coins['gold'] < gold:
                    error = _('Insufficient gold')
                else:
                    scoring.score(player, None, 'gold-points-rate', gold=gold)
                    message = _('Converted successfully')
            else:
                error = _('Unknown action')
    else:
        error = _('Expected post')

    if error:
        messages.error(request, error)
    if message:
        messages.success(request, message)

    return render_to_response('magic/bazaar_buy.html',
                {'tab': 'exchange'},
                context_instance=RequestContext(request))
Exemplo n.º 8
0
def bazaar(request, message='', error=''):
    player = request.user.get_profile() if request.user.is_authenticated(
    ) else None
    spells = Spell.objects.all().order_by('-available', 'level_required')

    # Disable exchange for real
    exchange_disabled = BoolSetting.get('disable-Bazaar-Exchange').get_value()
    try:
        rate = scoring.calculate('gold-points-rate', gold=1)['points']
        rate2 = round(1 /
                      scoring.calculate('points-gold-rate', points=1)['gold'])
    except InvalidFormula:
        rate, rate2 = 1, 1
    rate_text = _(
        'Rate: 1 gold = {rate} points, 1 gold = {rate2} points').format(
            rate=rate, rate2=rate2)

    cast_spells = PlayerSpellDue.objects.filter(source=player).all()
    unseen_count = cast_spells.filter(seen=False).count()

    # TODO: think of smth better
    cast_spells.update(seen=True)

    # get all significant magic activity
    activity_list = Activity.objects.filter(
        Q(action='spell-buy') | Q(action='earned-ach')
        | Q(action__contains='gold') | Q(action='cast')).order_by('-timestamp')

    paginator = Paginator(activity_list, 40)
    try:
        activity = paginator.page(1)
    except (EmptyPage, InvalidPage):
        activity = paginator.page(paginator.num_pages)

    return render_to_response('magic/bazaar.html', {
        'spells': spells,
        'activity': activity,
        'rate': rate,
        'rate_text': rate_text,
        'cast': cast_spells,
        'unseen_count': unseen_count,
        'theowner': player,
        'message': message,
        'exchange_disabled': exchange_disabled,
        'error': error
    },
                              context_instance=RequestContext(request))
Exemplo n.º 9
0
def bazaar(request, message='', error=''):
    player = request.user.get_profile() if request.user.is_authenticated() else None
    spells = Spell.objects.all()

    rate = scoring.calculate('gold-points-rate', gold=1)['points']
    rate2 = round(1/scoring.calculate('points-gold-rate', points=1)['gold'])
    rate_text = _('Rate: 1 gold = {rate} points, 1 gold = {rate2} points').format(rate=rate, rate2=rate2)

    cast_spells = PlayerSpellDue.objects.filter(source=player).all()
    unseen_count = cast_spells.filter(seen=False).count()

    # TODO: think of smth better
    cast_spells.update(seen=True)
    return render_to_response('bazaar.html', {'spells': spells,
                              'rate': rate, 'rate_text': rate_text,
                              'cast': cast_spells,
                              'unseen_count': unseen_count,
                              'theowner': player,
                              'message': message,
                              'error': error},
                              context_instance=RequestContext(request))
Exemplo n.º 10
0
    def testCalculate(self):
        formula = Formula.objects.create(id='_test_formula',
                                         formula='_test=5',
                                         owner=self.game)

        # Call by name
        ret = scoring.calculate('_test_formula')
        self.assertTrue(isinstance(ret, dict))

        # Call by objcet
        ret = scoring.calculate(formula)
        self.assertTrue(isinstance(ret, dict))
        self.assertEqual(ret['_test'], 5)

        formula2 = Formula.objects.create(id='_test_formula2',
                                          formula='_test=5*3',
                                          owner=self.game)

        ret = scoring.calculate(formula2)
        self.assertTrue(isinstance(ret, dict))
        self.assertEqual(ret['_test'], 15)

        # Multiple coins
        formula2.formula = '_test=5*3; points=4'

        ret = scoring.calculate(formula2)
        self.assertTrue(isinstance(ret, dict))
        self.assertEqual(ret['_test'], 15)
        self.assertEqual(ret['points'], 4)

        # Fail safe
        formula2.formula = '_test=5*cucu'
        try:
            ret = scoring.calculate(formula2)
            # no error? wtf
            self.assertFalse(True)
        except Exception as e:
            self.assertTrue(isinstance(e, FormulaParsingError))
Exemplo n.º 11
0
def bazaar(request, message='', error=''):
    player = request.user.get_profile() if request.user.is_authenticated() else None
    spells = Spell.objects.all().order_by('-available', 'level_required')

    # Disable exchange for real
    exchange_disabled = BoolSetting.get('disable-Bazaar-Exchange').get_value()
    try:
        rate = scoring.calculate('gold-points-rate', gold=1)['points']
        rate2 = round(1/scoring.calculate('points-gold-rate', points=1)['gold'])
    except InvalidFormula:
        rate, rate2 = 1, 1
    rate_text = _('Rate: 1 gold = {rate} points, 1 gold = {rate2} points').format(rate=rate, rate2=rate2)

    cast_spells = PlayerSpellDue.objects.filter(source=player).all()
    unseen_count = cast_spells.filter(seen=False).count()

    # TODO: think of smth better
    cast_spells.update(seen=True)

    # get all significant magic activity
    activity_list = Activity.objects.filter(Q(action='spell-buy') | Q(action='earned-ach') | Q(action__contains='gold') | Q(action='cast')).order_by('-timestamp')

    paginator = Paginator(activity_list, 40)
    try:
        activity = paginator.page(1)
    except (EmptyPage, InvalidPage):
        activity = paginator.page(paginator.num_pages)

    return render_to_response('magic/bazaar.html', {'spells': spells,
                              'activity': activity,
                              'rate': rate, 'rate_text': rate_text,
                              'cast': cast_spells,
                              'unseen_count': unseen_count,
                              'theowner': player,
                              'message': message,
                              'exchange_disabled': exchange_disabled,
                              'error': error},
                              context_instance=RequestContext(request))
Exemplo n.º 12
0
    def testCalculate(self):
        formula = Formula.add('_test_formula',
                              expression='_test=5', owner=self.game)

        # Call by name
        ret = scoring.calculate('_test_formula')
        self.assertTrue(isinstance(ret, dict))

        # Call by object
        ret = scoring.calculate(formula)
        self.assertTrue(isinstance(ret, dict))
        self.assertEqual(ret['_test'], 5)

        formula2 = Formula.add('_test_formula2',
                               expression='_test=5*3', owner=self.game)

        ret = scoring.calculate(formula2)
        self.assertTrue(isinstance(ret, dict))
        self.assertEqual(ret['_test'], 15)

        # Multiple coins
        formula2.expression = '_test=5*3; points=4'

        ret = scoring.calculate(formula2)
        self.assertTrue(isinstance(ret, dict))
        self.assertEqual(ret['_test'], 15)
        self.assertEqual(ret['points'], 4)

        # Fail safe
        formula2.expression = '_test=5*cucu'
        try:
            ret = scoring.calculate(formula2)
            # no error? wtf
            self.assertFalse(True)
        except Exception as e:
            self.assertTrue(isinstance(e, FormulaParsingError))