Exemple #1
0
 def on_start(self):
     """
     Set up the initial user. We only have access to the
     HTTP environment, so we use the session ID in the cookie
     and look up a user with it. If a valid user is found, we
     add them to the user set in redis, and broadcast their
     join event to everyone else.
     """
     try:
         cookie = Cookie(self.environ["HTTP_COOKIE"])
         session_key = cookie[settings.SESSION_COOKIE_NAME].value
         session = Session.objects.get(session_key=session_key)
         user_id = session.get_decoded().get(SESSION_KEY)
         user = User.objects.get(id=user_id)
     except (KeyError, ObjectDoesNotExist):
         self.user = None
     else:
         self.user = {
             "id": user.id,
             "name": user.username,
             "x": randint(780, 980),
             "y": randint(100, 300),
         }
         self.broadcast_event_not_me("join", self.user)
         redis.hset(USERS_KEY, self.user["id"], dumps(self.user))
     # Send the current set of users to the new socket.
     self.emit("users", [loads(u) for u in redis.hvals(USERS_KEY)])
     for game in registry.values():
         if game.players:
             self.emit("game_users", game.name, game.players.keys())
Exemple #2
0
 def on_start(self):
     """
     Set up the initial user. We only have access to the
     HTTP environment, so we use the session ID in the cookie
     and look up a user with it. If a valid user is found, we
     add them to the user set in redis, and broadcast their
     join event to everyone else.
     """
     try:
         cookie = Cookie(self.environ["HTTP_COOKIE"])
         session_key = cookie[settings.SESSION_COOKIE_NAME].value
         session = Session.objects.get(session_key=session_key)
         user_id = session.get_decoded().get(SESSION_KEY)
         user = User.objects.get(id=user_id)
     except (KeyError, ObjectDoesNotExist):
         self.user = None
     else:
         self.user = {
             "id": user.id,
             "name": user.username,
             "x": randint(780, 980),
             "y": randint(100, 300),
         }
         self.broadcast_event_not_me("join", self.user)
         redis.hset(USERS_KEY, self.user["id"], dumps(self.user))
     # Send the current set of users to the new socket.
     self.emit("users", [loads(u) for u in redis.hvals(USERS_KEY)])
     for game in registry.values():
         if game.players:
             self.emit("game_users", game.name, game.players.keys())