Exemplo n.º 1
0
    def _deduct_currency_from_user(self, store, user, amount, callback):
        store.deduct_balance(user.operant_id(), self, amount, callback)

    def award(self, store, user, amount=1, callback=None):
        """Awards the passed in amount of this currency"""
        def _cb(n):
            store.track_event("currency.awarded." + self.currency_id,
                              user.operant_id(), dict(amount=amount))
            callback(n)

        self._add_currency_to_user(store, user, amount, _cb)

    def deduct_balance(self, store, user, amount=1, callback=None):
        """Deducts the passed in amount of this currency from the player"""
        def _cb(n):
            store.track_event("currency.deducted." + self.currency_id,
                              user.operant_id(), dict(amount=amount))
            callback(n)

        self._deduct_currency_from_user(store, user, amount, _cb)

    def get_balance(self, store, user, callback=None):
        """Gets the users balance in the passed  in currency"""
        store.get_balance(user.operant_id(), self, callback)


Currencies = Registry("currency", "currency_id")
Currencies.set_str_handler(Currency)
get = Currencies.get
register = Currencies.register
Exemplo n.º 2
0
    def _add_currency_to_user(self, store, user, amount, callback):
        store.add_balance(user.operant_id(), self, amount, callback)

    def _deduct_currency_from_user(self, store, user, amount, callback):
        store.deduct_balance(user.operant_id(), self, amount, callback)

    def award(self, store, user, amount=1, callback=None):
        """Awards the passed in amount of this currency"""
        def _cb(n):
            store.track_event("currency.awarded." + self.currency_id,
                              user.operant_id(), dict(amount=amount))
            callback(n)
        self._add_currency_to_user(store, user, amount, _cb)

    def deduct_balance(self, store, user, amount=1, callback=None):
        """Deducts the passed in amount of this currency from the player"""
        def _cb(n):
            store.track_event("currency.deducted." + self.currency_id,
                              user.operant_id(), dict(amount=amount))
            callback(n)
        self._deduct_currency_from_user(store, user, amount, _cb)

    def get_balance(self, store, user, callback=None):
        """Gets the users balance in the passed  in currency"""
        store.get_balance(user.operant_id(), self, callback)

Currencies = Registry("currency", "currency_id")
Currencies.set_str_handler(Currency)
get = Currencies.get
register = Currencies.register
Exemplo n.º 3
0
    def on_awarded_points(self, user, now, amount):
        """ Called when a player has been awarded points of this kind.

        Override this method to implement custom logic like badges
        awarded every etc
        """

    def _add_points_to_user(self, store, user, amount, callback):
        def _cb(now):
            self.on_awarded_points(user, now, amount)
            callback(now)
        store.add_points(user.operant_id(), self, amount, _cb)

    def award(self, store, user, amount=1, callback=None):
        """Awards a number of points to a player"""
        def _cb(n):
            store.track_event("point.awarded." + self.points_id,
                              user.operant_id(), dict(amount=amount))
            callback(n)
        self._add_points_to_user(store, user, amount, _cb)

    def get_count(self, store, user, callback=None):
        """Gets the number of points a player has"""
        store.get_points(user.operant_id(), self, callback)

PointSystems = Registry("point system", "points_id")
PointSystems.set_str_handler(PointSystem)

get = PointSystems.get
register = PointSystems.register
Exemplo n.º 4
0
        return True

    def _check_preconditions(self, user):
        return self._precondition(user)

    def _add_badge_to_user(self, store, user, callback):

        def cb(success):
            if success:
                store.track_event("badge.awarded." + self.badge_id,
                                  user.operant_id())
                callback(self)
            else:
                callback(False)
        user_id = user.operant_id()
        store.add_badge(user_id, self, cb)

    def award(self, store, user, callback):
        """Awards a badge to a user"""
        if self._check_preconditions(user):
            self._add_badge_to_user(store, user, callback)
        else:
            callback(False)


Badges = Registry("badge", "badge_id")
Badges.set_str_handler(BadgePrototype)

get_badge = Badges.get
register_badge = Badges.register
Exemplo n.º 5
0
        Override this method to implement custom logic like badges
        awarded every etc
        """

    def _add_points_to_user(self, store, user, amount, callback):
        def _cb(now):
            self.on_awarded_points(user, now, amount)
            callback(now)

        store.add_points(user.operant_id(), self, amount, _cb)

    def award(self, store, user, amount=1, callback=None):
        """Awards a number of points to a player"""
        def _cb(n):
            store.track_event("point.awarded." + self.points_id,
                              user.operant_id(), dict(amount=amount))
            callback(n)

        self._add_points_to_user(store, user, amount, _cb)

    def get_count(self, store, user, callback=None):
        """Gets the number of points a player has"""
        store.get_points(user.operant_id(), self, callback)


PointSystems = Registry("point system", "points_id")
PointSystems.set_str_handler(PointSystem)

get = PointSystems.get
register = PointSystems.register
Exemplo n.º 6
0
        return True

    def _check_preconditions(self, user):
        return self._precondition(user)

    def _add_badge_to_user(self, store, user, callback):
        def cb(success):
            if success:
                store.track_event("badge.awarded." + self.badge_id,
                                  user.operant_id())
                callback(self)
            else:
                callback(False)

        user_id = user.operant_id()
        store.add_badge(user_id, self, cb)

    def award(self, store, user, callback):
        """Awards a badge to a user"""
        if self._check_preconditions(user):
            self._add_badge_to_user(store, user, callback)
        else:
            callback(False)


Badges = Registry("badge", "badge_id")
Badges.set_str_handler(BadgePrototype)

get_badge = Badges.get
register_badge = Badges.register