Exemple #1
0
def bot(query=None):
    fields = None
    short = request.args.get('short')
    if short:
        fields = {'history': 0}

    if query is not None:
        _or = [{'username': query}]
        try:
            _or.append({'_id': ObjectId(query)})
        except InvalidId:
            pass

        bot = mongo.db.bots.find_one({'$or': _or}, fields=fields)
        if bot:
            bot['inventory'] = fill_items(bot['inventory'])
            bot['apps'] = APPS
            return bot
    else:
        bots = []
        for bot in mongo.db.bots.find({}, fields=fields):
            bot['item_count'] = sum(
                i['amount'] for i in bot.get('inventory', [])
                if i['app_id'] == '570'
            )
            if short:
                bot.pop('inventory', None)
            bots.append(bot)

        return bots
Exemple #2
0
def user_inventory(_id):
    if request.method == 'GET':
        inventory = (mongo.db.users.find_one(
            {'_id': _id}, {'_id': 0, 'inventory': 1}
        ) or {}).get('inventory', {})
        return fill_items(inventory)
    elif request.json:
        inventory = request.json.get('inventory', {})
        if inventory:
            items = []
            for app, ctx, item, desc in flatten_inventory(inventory):
                item.update({
                    'app_id': app,
                    'context_id': ctx,
                })
                items.append(map_item(item))

            mongo.db.users.update(
                {'_id': _id},
                {'$set': {
                    'inventory': items,
                    'times.inventory': time.time(),
                }},
            )
            insert_items(inventory, foreign=True)

        return {}
Exemple #3
0
def get_ledger(_id=None):
    ledger = mongo.db.ledger
    if _id:
        transaction = ledger.find_one({'_id': _id})
        transaction['items'] = fill_items(transaction.get('items', []))
        return transaction
    else:
        query = {}
        user = request.args.get('user')
        if user:
            query['user_id'] = ObjectId(user)
        return ledger.find(query)
Exemple #4
0
def find_user(_id=None, openid=None, steamid=None, fill=True, fields=None):
    user = None
    if _id is not None:
        try:
            user = mongo.db.users.find_one({'_id': ObjectId(_id)}, fields=fields)
        except bson.errors.InvalidId:
            pass
    elif steamid is not None:
        user = mongo.db.users.find_one({'profile.steamid': steamid}, fields=fields)
    else:
        openid = openid or request.json.get('openid')
        if openid is not None:
            user = mongo.db.users.find_one({'openid': openid}, fields=fields)

    if user is not None:
        times = user.get('times', {})
        profile = user.get('profile')
        now = time.time()

        user['credits'] = int(user.get('credits') or 0)

        # fill profile cache if older than a week or doesn't exist
        if not profile or times.get('profile', 0) < (now - 604800):
            steamid = user['openid'].rsplit('/', 1)[1]
            response = steam_api.ISteamUser.GetPlayerSummaries(steamids=steamid)
            if response:
                profile = response['response']['players']['player'][0]
                profile['timestamp'] = now

                mongo.db.users.update(
                    {'_id': user['_id']},
                    {'$set': {
                        'profile': profile,
                        'times.profile': now,
                    }},
                )
                user['profile'] = profile

        if profile:
            if not user.get('steamrep') or times.get('steamrep', 0) < (now - 3600):
                user['steamrep'] = steamrep(profile['steamid'])
                mongo.db.users.update(
                    {'_id': user['_id']},
                    {'$set': {
                        'steamrep': user['steamrep'],
                        'times.steamrep': now,
                    }},
                )

            if not user.get('games') or times.get('games', 0) < (now - 86400):
                try:
                    response = steam_api.IPlayerService.GetOwnedGames(steamid=steamid)
                except Exception:
                    response = None
                    mongo.db.users.update(
                        {'_id': user['_id']},
                        {'$set': {'times.games': now}},
                    )

                if response:
                    games = response.get('response', {}).get('games')
                    if games:
                        mongo.db.users.update(
                            {'_id': user['_id']},
                            {'$set': {
                                'games': games,
                                'times.games': now,
                            }},
                        )
                        user['games'] = games

        if fill:
            user['cart'] = fill_items(user.get('cart', []), preserve_price=False)
            user['items'] = fill_items(user.get('items', []))

        trading = user.get('trading')
        if trading:
            _id = user['_id']
            queue = mongo.db.queue.find_one({'user_id': _id})
            if queue:
                before = mongo.db.queue.find(
                    {'time': {'$lt': queue['time']}}
                ).count()
                queue['position'] = before + 1
                status = queue.get('status')
                if status == 'request':
                    status = 'Friend request sent.'
                elif status == 'friend':
                    status = 'Trade request sent.'
                elif status == 'trade':
                    status = 'Trade started.'
                elif status == 'ready':
                    status = 'Trade ready.'
                elif status in ('confirm', 'verify'):
                    status = 'Verifying trade.'

                if status:
                    queue['status'] = status
                user['queue'] = queue
            else:
                user['trade'] = mongo.db.trades.find_one(
                    {'user_id': _id, 'stage': 'bot'},
                    sort=[('time', -1)]
                )

    return user