Example #1
0
def lookup_single(query, amount=None, add=0):
    item = mongo.db.items.find_one(query)
    if not item:
        return None

    if amount is None:
        same = [i for i in (mongo.db.items.find({
            'app_id': item['app_id'],
            'context_id': item['context_id'],
            'name': item['name'],
        }) or ())]
        amount = sum([i.get('amount', 0) for i in same])

    amount += add

    if item.get('price'):
        item['price'] = prices.hot_price(
            item['app_id'], item['context_id'],
            item['name'], item['price'], amount_matters=False)

    overstock = (mongo.db.pricing.find_one({
        'app_id': item['app_id'],
        'context_id': item['context_id'],
        'name': item['name'],
    }, {'limit': 1}) or {}).get('limit', prices.STOCK_LIMIT)
    if overstock and amount > overstock:
        item['buy_price'] = 0
    elif 'buy_price' in item:
        # really should centralize this
        # instead of duplicating in admin
        item['buy_price'] = min(prices.hot_price(
            item['app_id'], item['context_id'],
            item['name'], item['buy_price'], amount
        ), int(math.floor(item['price'])))

    variants = mongo.db.items.find({
        'app_id': item['app_id'],
        'context_id': item['context_id'],
        'name': item['name'],
        'available': {'$gt': 0},
        '_id': {'$ne': item['_id']},
    }, {
        '_id': 1,
        'available': 1,
        'class_id': 1,
        'icon_url': 1,
        'instance_id': 1,
        'name': 1,
        'tags': 1,
        'type': 1,
        'price': 1,
        'buy_price': 1
    })

    item['limit'] = overstock
    item['variants'] = variants
    return item
Example #2
0
def enum_admin_items(pricing=False):
    items = api('/item', need_id=1, overstock=0)
    worth = 0
    total = 0
    if items:
        amounts = defaultdict(int)
        for item in items:
            amounts[item['name']] += item['amount']

        item_set = {}
        for item in items:
            name = item['name']
            if pricing and name in item_set:
                continue

            amount = amounts[name]
            price = item.get('price', 0)
            buy_price = item.get('buy_price', 0)

            item['hot_price'] = prices.hot_price(
                item['app_id'], item['context_id'],
                name, price, amount_matters=False,
            )
            item['hot_buy_price'] = min(
                prices.hot_price(
                    item['app_id'], item['context_id'],
                    name, buy_price, amount,
                ), int(math.floor(item['hot_price'] * 0.95))
            )
            item['hot_buy_percent'] = 100 * round(
                float(item['hot_buy_price']) / (item['hot_price'] or 1), 3)
            item['hotness'] = round(
                item['hot_price'] / (float(item['price']) or 1), 2)

            if pricing:
                item['amount'] = amount
                item_set[name] = item
                total += amount
                worth += item['hot_price'] * amount
            else:
                total += item['amount']
                worth += item['hot_price'] * item['amount']

        if pricing:
            items = sorted(item_set.values(),
                key=lambda i: (i.get('hotness'), i.get('name')))
        else:
            items.sort(key=lambda i: (i.get('amount'), i.get('name')))

        items = list(reversed(items))

    return worth, total, items