Пример #1
0
    def testBackpack(self):
        from db.models import User, Item, DiscuzMember
        from server.item import backpack, constants

        s = current_session()

        backpack.use(1, 'foo')

        with assert_raises(exceptions.ItemNotFound):
            backpack.use(1, 'foo')

        with assert_raises(exceptions.ItemNotFound):
            backpack.use(1, 'bar')

        backpack.add(1, 'foo')
        backpack.use(1, 'foo')

        backpack.add(1, 'bar')

        with assert_raises(exceptions.ItemNotUsable):
            backpack.use(1, 'bar')

        s.rollback()
        id = s.query(Item).filter(Item.owner_id == 1, Item.sku == 'bar').first().id
        backpack.drop(1, id)

        for i in xrange(constants.BACKPACK_SIZE):
            backpack.add(1, 'foo')

        with assert_raises(exceptions.BackpackFull):
            backpack.add(1, 'foo')

        eq_(len(backpack.list(1)), constants.BACKPACK_SIZE)

        s.rollback()
        id = s.query(Item).filter(Item.owner_id == 1).first().id

        backpack.drop(1, id)
        eq_(len(backpack.list(1)), constants.BACKPACK_SIZE - 1)

        with assert_raises(exceptions.ItemNotFound):
            backpack.drop(1, id)

        s.rollback()
        u = s.query(User).filter(User.id == 1).first()
        u.ppoint = 0

        dz_member = s.query(DiscuzMember).filter(DiscuzMember.uid == 1).first()
        dz_member.member_count.jiecao = 0
        s.commit()

        backpack.add(1, 'jiecao:1234')
        backpack.use(1, 'jiecao:1234')
        backpack.add(1, 'ppoint:1234')
        backpack.use(1, 'ppoint:1234')

        u = s.query(User).filter(User.id == 1).first()
        eq_(u.ppoint, 1234)
        dz_member = s.query(DiscuzMember).filter(DiscuzMember.uid == 1).first()
        eq_(dz_member.member_count.jiecao, 1234)
Пример #2
0
def draw(uid, currency):
    s = current_session()
    u = Account.find(uid)
    if not u:
        raise exceptions.UserNotFound

    helpers.require_free_backpack_slot(s, uid)

    if currency == 'ppoint':
        amount = constants.LOTTERY_PRICE
        Account.add_user_credit(u, [('ppoint', -amount)],
                                exceptions.InsufficientFunds)

    elif currency == 'jiecao':
        amount = constants.LOTTERY_JIECAO_PRICE
        Account.add_user_credit(u, [('jiecao', -amount)],
                                exceptions.InsufficientFunds)

    else:
        raise exceptions.InvalidCurrency

    reward = random.choice(constants.LOTTERY_REWARD_LIST)

    item = Item(owner_id=uid, sku=reward, status='backpack')
    s.add(item)
    s.flush()
    s.add(ItemActivity(
        uid=uid, action='lottery', item_id=item.id,
        extra=json.dumps({'currency': currency, 'amount': amount}),
        created=datetime.datetime.now(),
    ))
    return reward
Пример #3
0
def sell(uid, item_id, price):
    s = current_session()
    item = s.query(Item).filter(Item.id == item_id,
                                Item.owner_id == uid).first()
    if not item:
        raise exceptions.ItemNotFound

    existing = s.query(Exchange).filter(Exchange.seller_id == uid).count()
    if existing > constants.MAX_SELLING_ITEMS:
        raise exceptions.TooManySellingItems

    s.add(Exchange(
        seller_id=uid,
        item_id=item.id,
        price=price,
    ))

    s.add(
        ItemActivity(
            uid=uid,
            action='sell',
            item_id=item.id,
            extra=json.dumps({'price': price}),
            created=datetime.datetime.now(),
        ))

    item.status = 'exchange'
    item.owner_id = None
Пример #4
0
def use(uid, item_sku_or_id, is_consume=False):
    s = current_session()

    if isinstance(item_sku_or_id, int):
        item = s.query(Item).filter(Item.owner_id == uid, Item.id == item_sku_or_id)
    else:
        item = s.query(Item).filter(Item.owner_id == uid, Item.sku == item_sku_or_id)

    item = item.first()

    if not item:
        raise exceptions.ItemNotFound

    s.add(ItemActivity(
        uid=uid, action='use', item_id=item.id,
        created=datetime.datetime.now(),
    ))

    if not is_consume:
        itemobj = GameItem.from_sku(item.sku)
        if not itemobj.usable:
            raise exceptions.ItemNotUsable

        itemobj.use(s, item.owner)

    item.status = 'used'
    item.owner_id = None
Пример #5
0
def use(uid, item_sku_or_id, is_consume=False):
    s = current_session()

    if isinstance(item_sku_or_id, int):
        item = s.query(Item).filter(Item.owner_id == uid,
                                    Item.id == item_sku_or_id)
    else:
        item = s.query(Item).filter(Item.owner_id == uid,
                                    Item.sku == item_sku_or_id)

    item = item.first()

    if not item:
        raise exceptions.ItemNotFound

    s.add(
        ItemActivity(
            uid=uid,
            action='use',
            item_id=item.id,
            created=datetime.datetime.now(),
        ))

    if not is_consume:
        itemobj = GameItem.from_sku(item.sku)
        if not itemobj.usable:
            raise exceptions.ItemNotUsable

        itemobj.use(s, item.owner)

    item.status = 'used'
    item.owner_id = None
Пример #6
0
    def testLottery(self):
        from db.models import User, DiscuzMember
        from server.item import constants, lottery

        lottery.draw(1, 'jiecao')
        lottery.draw(1, 'ppoint')
        lottery.draw(1, 'jiecao')
        lottery.draw(1, 'ppoint')
        lottery.draw(1, 'jiecao')
        lottery.draw(1, 'ppoint')

        s = current_session()
        u = s.query(User).filter(User.id == 1).first()
        eq_(u.ppoint, 1000 - constants.LOTTERY_PRICE * 3)
        if not options.freeplay:
            dz_member = s.query(DiscuzMember).filter(
                DiscuzMember.uid == 1).first()
            eq_(dz_member.member_count.jiecao,
                100000 - constants.LOTTERY_JIECAO_PRICE * 3)

            dz_member.member_count.jiecao = 0
        else:
            u.jiecao = 0

        s.commit()

        with assert_raises(exceptions.InsufficientFunds):
            lottery.draw(1, 'jiecao')
Пример #7
0
def cancel_sell(uid, entry_id):
    s = current_session()
    entry = s.query(Exchange).filter(
        Exchange.id == entry_id,
        Exchange.seller_id == uid,
    ).first()

    if not entry:
        raise exceptions.ItemNotFound

    helpers.require_free_backpack_slot(s, uid)

    item = entry.item
    item.owner_id = uid
    item.status = 'backpack'

    s.add(
        ItemActivity(
            uid=uid,
            action='cancel_sell',
            item_id=entry.item.id,
            extra=json.dumps({'price': entry.price}),
            created=datetime.datetime.now(),
        ))

    s.delete(entry)
Пример #8
0
    def testLottery(self):
        from db.models import User, DiscuzMember
        from server.item import constants, lottery

        lottery.draw(1, 'jiecao')
        lottery.draw(1, 'ppoint')
        lottery.draw(1, 'jiecao')
        lottery.draw(1, 'ppoint')
        lottery.draw(1, 'jiecao')
        lottery.draw(1, 'ppoint')

        s = current_session()
        u = s.query(User).filter(User.id == 1).first()
        eq_(u.ppoint, 1000 - constants.LOTTERY_PRICE * 3)
        if not options.freeplay:
            dz_member = s.query(DiscuzMember).filter(DiscuzMember.uid == 1).first()
            eq_(dz_member.member_count.jiecao, 100000 - constants.LOTTERY_JIECAO_PRICE * 3)

            dz_member.member_count.jiecao = 0
        else:
            u.jiecao = 0

        s.commit()

        with assert_raises(exceptions.InsufficientFunds):
            lottery.draw(1, 'jiecao')
Пример #9
0
def list(uid):
    s = current_session()

    items = s.query(Item) \
        .filter(Item.owner_id == uid, Item.status == 'backpack') \
        .order_by(Item.id.desc()) \
        .all()
    items = [{'id': i.id, 'sku': i.sku} for i in items]
    return items
Пример #10
0
def list(uid):
    s = current_session()

    items = s.query(Item) \
        .filter(Item.owner_id == uid, Item.status == 'backpack') \
        .order_by(Item.id.desc()) \
        .all()
    items = [{'id': i.id, 'sku': i.sku} for i in items]
    return items
Пример #11
0
def list():
    s = current_session()
    l = s.query(Exchange).options(joinedload("item")).order_by(Exchange.id.desc())
    l = [
        {"id": i.id, "seller_id": i.seller_id, "item_id": i.item_id, "item_sku": i.item.sku, "price": i.price}
        for i in l
    ]

    return l
Пример #12
0
def should_have(uid, sku):
    s = current_session()

    n = s.query(Item) \
        .filter(Item.owner_id == uid,
                Item.status == 'backpack',
                Item.sku == sku) \
        .count()

    if not n:
        raise exceptions.ItemNotFound

    return n
Пример #13
0
def list():
    s = current_session()
    l = s.query(Exchange).options(joinedload('item')).order_by(
        Exchange.id.desc())
    l = [{
        'id': i.id,
        'seller_id': i.seller_id,
        'item_id': i.item_id,
        'item_sku': i.item.sku,
        'price': i.price
    } for i in l]

    return l
Пример #14
0
def should_have(uid, sku):
    s = current_session()

    n = s.query(Item) \
        .filter(Item.owner_id == uid,
                Item.status == 'backpack',
                Item.sku == sku) \
        .count()

    if not n:
        raise exceptions.ItemNotFound

    return n
Пример #15
0
def add(uid, item_sku, reason=None):
    s = current_session()
    helpers.require_free_backpack_slot(s, uid)

    item = Item(owner_id=uid, sku=item_sku, status='backpack')
    s.add(item)
    s.flush()

    s.add(ItemActivity(
        uid=uid, action='get', item_id=item.id,
        extra=reason and json.dumps(reason),
        created=datetime.datetime.now(),
    ))

    return item.id
Пример #16
0
    def testExchange(self):
        from db.models import Exchange, User, Item
        from server.item import exchange

        s = current_session()

        s.rollback()
        exchange.sell(uid=1, item_id=1, price=500)
        eq_(s.query(Item).filter(Item.owner_id != None).count(), 1)  # noqa
        e = s.query(Exchange).first()
        eq_(e.item.sku, 'foo')
        eid = e.id

        s.rollback()
        exchange.buy(uid=2, entry_id=eid)

        u1, u2 = s.query(User).order_by(User.id.asc()).all()
        eq_(u1.ppoint, 1500)
        eq_(u2.ppoint, 500)

        i1, i2 = s.query(Item).order_by(Item.id.asc()).all()
        eq_(i1.owner_id, 2)
        eq_(i2.owner_id, 2)

        with assert_raises(exceptions.UserNotFound):
            exchange.buy(uid=3, entry_id=3)

        with assert_raises(exceptions.ItemNotFound):
            exchange.buy(uid=1, entry_id=3)

        s.rollback()
        id = s.query(Item).first().id
        exchange.sell(uid=2, item_id=id, price=5000)
        s.rollback()
        id = s.query(Exchange).first().id

        with assert_raises(exceptions.InsufficientFunds):
            exchange.buy(uid=1, entry_id=id)

        exchange.cancel_sell(uid=2, entry_id=id)

        i1, i2 = s.query(Item).order_by(Item.id.asc()).all()
        eq_(i1.owner_id, 2)
        eq_(i2.owner_id, 2)

        exchange.list()
Пример #17
0
    def testExchange(self):
        from db.models import Exchange, User, Item
        from server.item import exchange

        s = current_session()

        s.rollback()
        exchange.sell(uid=1, item_id=1, price=500)
        eq_(s.query(Item).filter(Item.owner_id != None).count(), 1)  # noqa
        e = s.query(Exchange).first()
        eq_(e.item.sku, 'foo')
        eid = e.id

        s.rollback()
        exchange.buy(uid=2, entry_id=eid)

        u1, u2 = s.query(User).order_by(User.id.asc()).all()
        eq_(u1.ppoint, 1500)
        eq_(u2.ppoint, 500)

        i1, i2 = s.query(Item).order_by(Item.id.asc()).all()
        eq_(i1.owner_id, 2)
        eq_(i2.owner_id, 2)

        with assert_raises(exceptions.UserNotFound):
            exchange.buy(uid=3, entry_id=3)

        with assert_raises(exceptions.ItemNotFound):
            exchange.buy(uid=1, entry_id=3)

        s.rollback()
        id = s.query(Item).first().id
        exchange.sell(uid=2, item_id=id, price=5000)
        s.rollback()
        id = s.query(Exchange).first().id

        with assert_raises(exceptions.InsufficientFunds):
            exchange.buy(uid=1, entry_id=id)

        exchange.cancel_sell(uid=2, entry_id=id)

        i1, i2 = s.query(Item).order_by(Item.id.asc()).all()
        eq_(i1.owner_id, 2)
        eq_(i2.owner_id, 2)

        exchange.list()
Пример #18
0
def add(uid, item_sku, reason=None):
    s = current_session()
    helpers.require_free_backpack_slot(s, uid)

    item = Item(owner_id=uid, sku=item_sku, status='backpack')
    s.add(item)
    s.flush()

    s.add(
        ItemActivity(
            uid=uid,
            action='get',
            item_id=item.id,
            extra=reason and json.dumps(reason),
            created=datetime.datetime.now(),
        ))

    return item.id
Пример #19
0
def sell(uid, item_id, price):
    s = current_session()
    item = s.query(Item).filter(Item.id == item_id, Item.owner_id == uid).first()
    if not item:
        raise exceptions.ItemNotFound

    existing = s.query(Exchange).filter(Exchange.seller_id == uid).count()
    if existing > constants.MAX_SELLING_ITEMS:
        raise exceptions.TooManySellingItems

    s.add(Exchange(seller_id=uid, item_id=item.id, price=price))

    s.add(
        ItemActivity(
            uid=uid, action="sell", item_id=item.id, extra=json.dumps({"price": price}), created=datetime.datetime.now()
        )
    )

    item.status = "exchange"
    item.owner_id = None
Пример #20
0
def drop(uid, item_id):
    s = current_session()

    item = s.query(Item) \
        .filter(Item.id == item_id) \
        .filter(Item.owner_id == uid) \
        .filter(Item.status == 'backpack') \
        .first()

    if not item:
        raise exceptions.ItemNotFound

    item.owner_id = None
    item.status = 'dropped'

    s.add(ItemActivity(
        uid=uid, action='drop', item_id=item.id,
        created=datetime.datetime.now(),
    ))

    return item.id
Пример #21
0
def buy(uid, entry_id):
    s = current_session()

    u = Account.find(uid)
    if not u:
        raise exceptions.UserNotFound

    entry = s.query(Exchange).filter(Exchange.id == entry_id).first()
    if not entry:
        raise exceptions.ItemNotFound

    seller = entry.seller

    if u.ppoint < entry.price:
        raise exceptions.InsufficientFunds

    helpers.require_free_backpack_slot(s, uid)

    u.ppoint -= entry.price
    seller.ppoint += entry.price

    item = entry.item

    s.add(
        ItemActivity(
            uid=uid,
            action='buy',
            item_id=entry.item.id,
            extra=json.dumps({
                'seller': seller.id,
                'price': entry.price
            }),
            created=datetime.datetime.now(),
        ))

    item.owner_id = u.id
    item.status = 'backpack'

    s.delete(entry)
Пример #22
0
def buy(uid, entry_id):
    s = current_session()

    u = Account.find(uid)
    if not u:
        raise exceptions.UserNotFound

    entry = s.query(Exchange).filter(Exchange.id == entry_id).first()
    if not entry:
        raise exceptions.ItemNotFound

    seller = entry.seller

    if u.ppoint < entry.price:
        raise exceptions.InsufficientFunds

    helpers.require_free_backpack_slot(s, uid)

    u.ppoint -= entry.price
    seller.ppoint += entry.price

    item = entry.item

    s.add(
        ItemActivity(
            uid=uid,
            action="buy",
            item_id=entry.item.id,
            extra=json.dumps({"seller": seller.id, "price": entry.price}),
            created=datetime.datetime.now(),
        )
    )

    item.owner_id = u.id
    item.status = "backpack"

    s.delete(entry)
Пример #23
0
def drop(uid, item_id):
    s = current_session()

    item = s.query(Item) \
        .filter(Item.id == item_id) \
        .filter(Item.owner_id == uid) \
        .filter(Item.status == 'backpack') \
        .first()

    if not item:
        raise exceptions.ItemNotFound

    item.owner_id = None
    item.status = 'dropped'

    s.add(
        ItemActivity(
            uid=uid,
            action='drop',
            item_id=item.id,
            created=datetime.datetime.now(),
        ))

    return item.id
Пример #24
0
def cancel_sell(uid, entry_id):
    s = current_session()
    entry = s.query(Exchange).filter(Exchange.id == entry_id, Exchange.seller_id == uid).first()

    if not entry:
        raise exceptions.ItemNotFound

    helpers.require_free_backpack_slot(s, uid)

    item = entry.item
    item.owner_id = uid
    item.status = "backpack"

    s.add(
        ItemActivity(
            uid=uid,
            action="cancel_sell",
            item_id=entry.item.id,
            extra=json.dumps({"price": entry.price}),
            created=datetime.datetime.now(),
        )
    )

    s.delete(entry)
Пример #25
0
    def testBackpack(self):
        from db.models import User, Item, DiscuzMember
        from server.item import backpack, constants

        s = current_session()

        backpack.use(1, 'foo')

        with assert_raises(exceptions.ItemNotFound):
            backpack.use(1, 'foo')

        with assert_raises(exceptions.ItemNotFound):
            backpack.use(1, 'bar')

        backpack.add(1, 'foo')
        backpack.use(1, 'foo')

        backpack.add(1, 'bar')

        with assert_raises(exceptions.ItemNotUsable):
            backpack.use(1, 'bar')

        s.rollback()
        id = s.query(Item).filter(Item.owner_id == 1,
                                  Item.sku == 'bar').first().id
        backpack.drop(1, id)

        for i in xrange(constants.BACKPACK_SIZE):
            backpack.add(1, 'foo')

        with assert_raises(exceptions.BackpackFull):
            backpack.add(1, 'foo')

        eq_(len(backpack.list(1)), constants.BACKPACK_SIZE)

        s.rollback()
        id = s.query(Item).filter(Item.owner_id == 1).first().id

        backpack.drop(1, id)
        eq_(len(backpack.list(1)), constants.BACKPACK_SIZE - 1)

        with assert_raises(exceptions.ItemNotFound):
            backpack.drop(1, id)

        s.rollback()
        u = s.query(User).filter(User.id == 1).first()
        u.ppoint = 0
        if not options.freeplay:
            dz_member = s.query(DiscuzMember).filter(
                DiscuzMember.uid == 1).first()
            dz_member.member_count.jiecao = 0
        else:
            u.jiecao = 0
        s.commit()

        backpack.add(1, 'jiecao:1234')
        backpack.use(1, 'jiecao:1234')
        backpack.add(1, 'ppoint:1234')
        backpack.use(1, 'ppoint:1234')

        u = s.query(User).filter(User.id == 1).first()
        eq_(u.ppoint, 1234)
        if not options.freeplay:
            dz_member = s.query(DiscuzMember).filter(
                DiscuzMember.uid == 1).first()
            eq_(dz_member.member_count.jiecao, 1234)