Esempio n. 1
0
def socket_ping(data):
    # We pull the user id ourselves to avoid excessive DB queries
    if request.cookies.get("sid"):
        s = Session.find(request.cookies.get("sid"))
        if s:
            redis.set("user:%s:ping" % s['user'], time.time())
            if 'lobby' in data:
                redis.set("user:%s:lobby:%s:ping" % (s['user'], data['lobby']), time.time())
Esempio n. 2
0
def socket_connect():
    """
    Flask has this awesome little thing called the request context, which
    is the reason things like "request" and "g" work the way they do. Sure,
    it's a ton of magic, but it's usefull. Unless you use websockets and need
    an ongoing loop. This function HAS to exit for socketio to parse NEW
    emissions from the client, so we start a greenlet attached too the socket
    (when the socket dies, the greenlet will too), and pass it the WS/user.
    """
    beforeRequest()
    ns = request.namespace.socket['/api/poll']
    ns.spawn(socket_loop, (request.namespace.socket['/api/poll'], g.user))
    if g.user: redis.set("user:%s:ping" % g.user.id, time.time())
Esempio n. 3
0
    def save(self, response):
        if not self._changed:
            log.debug('not saving session; it remains unchanged')
            return

        # TODO: pipeline/setex
        self._id = self._id or str(uuid4())
        ttl = int(redis.ttl("session:%s" % self._id) or SESSION_TTL)

        # TODO: domain?
        response.set_cookie("s", self._id, expires=(time.time() + ttl))

        log.debug('saving session %s', self._id)
        redis.set("session:%s" % self._id, json.dumps(self._data))
        redis.expire("session:%s" % self._id, ttl if ttl != -1 else SESSION_TTL)
        return True
Esempio n. 4
0
def handle_inventory_jobs(job):
    inv_key = "inv:%s" % job["steamid"]

    if redis.exists(inv_key):
        result = {"success": True, "inventory": json.loads(redis.get(inv_key)), "type": "inventory"}
        log.debug("inventory job hit cache")
    else:
        try:
            log.debug("inventory job missed cache")
            inv = steam.market(730).get_inventory(job["steamid"])
            redis.set(inv_key, json.dumps(inv))
            result = {"success": True, "inventory": inv, "type": "inventory"}
        except:
            log.exception("Failed to process job %s")
            result = {"success": False, "inventory": {}, "type": "inventory"}

    WebPush(job["user"]).send(result)
Esempio n. 5
0
def handle_inventory_job(job):
    inv_key = 'inv:%s' % job['steamid']

    if redis.exists(inv_key):
        inv = json.loads(redis.get(inv_key))
        pending = get_pending_items(job['user'])

        inv = filter(lambda i: (False and pending.remove(i['id'])) if i['id'] in pending else True, inv)

        result = {"success": True, "inventory": inv, "type": "inventory"}
        return WebPush(job['user']).send(result)

    try:
        inv = steam.market(730).get_inventory(job["steamid"])
        inv = process_inventory(inv)
        redis.set(inv_key, json.dumps(inv))
        result = {"success": True, "inventory": inv, "type": "inventory"}
    except SteamAPIError:
        log.exception("Failed to load inventory:")
        result = {"success": False, "inventory": {}, "type": "inventory"}

    WebPush(job['user']).send(result)