Exemplo n.º 1
0
    def apply_diff(self, diff, insert=False):
        for item in diff.get('added', []):
            self.add(item)
            if insert:
                app, ctx = item['appid'], item['ctxid']
                insert_item(app, ctx, item)

        for item in diff.get('removed', []):
            self.remove(item)
            if insert:
                item['amount'] = -item['amount']
                app, ctx = item['appid'], item['ctxid']
                insert_item(app, ctx, item)
Exemplo n.º 2
0
def bot_inventory(username):
    bots = mongo.db.bots
    bot = bots.find_one(
        {'username': username},
        {'inventory': 1, 'trader': 1},
    )
    if not bot:
        return {'error': 'could not find bot %s' % username}

    if request.json:
        data = request.json

        bots.update({'_id': bot['_id']}, {'$set': {'times.inventory': time.time()}})

        counts = defaultdict(int)
        inventory = Inventory(bot)
        for app, ctx, item, desc in merge_inventory(data['inventory']):
            key = (item['classid'], item['instanceid'])
            counts[key] += 1
            item = inventory.add(item, sync=True)
            if bot.get('trader'):
                insert_item(app, ctx, item, desc)

        check = defaultdict(int)
        for item in bot['inventory']:
            key = item['class_id'], item['instance_id']
            check[key] += item['amount']

        # subtract items that were completely removed
        for item in bot['inventory']:
            key = item['class_id'], item['instance_id']
            if counts[key] == 0:
                diff = min(item['amount'], check[key])
                app, ctx = item['app_id'], item['context_id']
                inventory.remove(item)
                if bot.get('trader'):
                    item['amount'] = -diff
                    insert_item(app, ctx, item)
                counts[key] -= diff

    return {}