Exemplo n.º 1
0
    def login(client, data):
        user = user_controller.login(data)

        if user:
            user = to_dict_obj(user)
            print user
            u_id, w_id = user['id'], str(id(client))
            user_key = get_user_key(u_id)
            # record online user
            redis_user = redis_obj.db.hgetall(user_key)
            if not redis_user:
                redis_user = {
                    'u_id': u_id,  # user id
                    'w_id': w_id,  # websocket id
                    'r_id': -1,  # room
                    'p_id': 0, #0blue 1red
                    'status': UserStatus.free,
                }
            else:
                redis_user['w_id'] = w_id
            redis_obj.db.hmset(user_key, redis_user)
            # associated webSocket and user
            redis_obj.db.set(get_w_key(client), u_id)
            user["p_id"] = redis_user["p_id"]
            user["r_id"] = redis_user["r_id"]
            user['status'] = redis_user["status"]
        return user
Exemplo n.º 2
0
 def leave_room(self, data):
     user_id = redis_obj.db.get(get_w_key(self.client))
     room = RoomTool.init_room(user_id, self.client, BattleType.freestyle,
                               GameType.offline)
     Response("room.create", room).response(self.client)
     room_id, p_id = data.get('room_id', None), data.get('p_id', None)
     room = RoomTool.leave_room(self.client, room_id, p_id)
     Response('room.leave', data={}).response(self.client)
     Response('room.info', data=room).response(clients=self.clients)
Exemplo n.º 3
0
 def update_user(client, r_id=None, status=None, p_id=None):
     u_id = redis_obj.db.get(get_w_key(client))
     u_key = get_user_key(u_id)
     if r_id is not None:
         redis_obj.db.hset(u_key, 'r_id', r_id)
     if status is not None:
         redis_obj.db.hset(u_key, 'status', status)
     if p_id is not None:
         redis_obj.db.hset(u_key, 'p_id', p_id)
     return UsersTool.current_user(client)
Exemplo n.º 4
0
 def quit(client, clients):
     from tool.room_tool import RoomTool
     room = RoomTool.leave_room(client)
     user = UsersTool.current_user(client)
     w_key = get_w_key(client)
     u_id = redis_obj.db.get(w_key)
     u_key = get_user_key(u_id)
     if w_key is None or u_key is None:
         return None
     redis_obj.db.delete(w_key)
     redis_obj.delete(u_key)
     del clients[str(id(client))]
     return user, room
Exemplo n.º 5
0
 def leave_room(client, room_id=None):
     if room_id is None:
         user = UsersTool.current_user(client)
         room_id = int(user.get('r_id', -1))
         if room_id == -1:
             return None
     users = RoomTool.room_users(room_id)
     users.remove(redis_obj.db.get(get_w_key(client)))
     RoomTool.update_room(room_id, users=users)
     if not users:
         RoomTool.delete_room(room_id)
     UsersTool.update_user(client, r_id=-1, status=UserStatus.free)
     return RoomTool.room_by(room_id)
Exemplo n.º 6
0
 def create_room(self, data=None):
     user_id = redis_obj.db.get(get_w_key(self.client))
     room = RoomTool.init_room(user_id, self.client, BattleType.freestyle,
                               GameType.offline)
     Response("room.create", room).response(self.client)
Exemplo n.º 7
0
 def current_user(client):
     u_id = redis_obj.db.get(get_w_key(client))
    # user_info = db.query(UserModel).filter_by(id=u_id).first()
     user_info = None
     return user_info.to_dict_obj(user_info)
Exemplo n.º 8
0
 def is_login(client):
     result = True if redis_obj.db.get(get_w_key(client)) else False
     return result