def quit(self, client): if not UsersTool.is_login(client): return user, room = UsersTool.quit(client, self.clients) if user: Response('user.leave', data=user).response(clients=self.clients) if room: Response('room.info', data=room).response(clients=self.clients)
def response(self, client=None, clients=None): message = {"data": self.data, "msg": self._msg, "ret": self._ret, "header": self.header} if client: client.write_message(message) elif clients: if isinstance(clients, dict): for c in clients.values(): if UsersTool.is_login(c): c.write_message(message) elif isinstance(clients, list): for c in clients: if UsersTool.is_login(c): c.write_message(message)
def enter_room(client, room_id): code, msg = RoomTool.check_room_status(room_id) if code != 0: return code, msg users = RoomTool.room_users(room_id) UsersTool.update_user(client, r_id=room_id, status=UserStatus.waiting) for i in range(0, len(users)): if not users[i]: user = UsersTool.update_user(client) users[i] = user break room = RoomTool.update_room(room_id, users=users) return room
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)
def start(data, client, clients): other_clients = filter( lambda u: id(u) != id(client), RoomTool.room_clients(clients, UsersTool.current_user(client)['r_id'])) data = {} Response('offline.start', data).response(clients=other_clients)
def next_begin(client, clients): r_id = UsersTool.current_user(client)['r_id'] room_clients = RoomTool.room_clients(clients, r_id) games = BattleHandler.begin_game(room_clients, r_id) room, current_user, over, other_clients = games['room'], games[ 'user'], games['over'], games['other_clients'] RoomTool.update_room(r_id, cantip=False)
def begin(room, clients): r_id = int(room['r_id']) room_clients = RoomTool.room_clients(clients, r_id) room_user = BattleHandler.begin_game(room_clients, r_id) for client in clients: if not client: continue user = UsersTool.update_user(client, status=UserStatus.playing) room, current_user, other_clients = room_user['room'], room_user[ 'user'], room_user['other_clients'] Response('rooms.begin', room).response(clients=clients) gm_say(u'游戏开始啦', room_clients)
def current_topic(r_id): room = RoomTool.room_by(r_id) if room['status'] != RoomStatus.playing: return None users = UsersTool.room_users(r_id) for user in users: if user: if user.get('status', None) == Status.action: return user['topic'] return None
def say(client, message, clients, header): user = UsersTool.current_user(client) user['message'] = message Response(header=header, data=user).response(clients=clients)
def say_room(self, data): message, r_id = data.get('message', None), data.get('room_id', None) clients = RoomTool.room_clients(self.clients, r_id) user = UsersTool.current_user(self.client) say(self.client, message, clients, 'rooms.message')
def login(self, data): data = UsersTool.login(client=self.client, data=data) Response('user.login', data=data).response(self.client)
def change_position(client, room_id, p_id): users = RoomTool.room_users(room_id) user = UsersTool.update_user(client, p_id=p_id) room = RoomTool.update_room(room_id, users=users) return room
def game_over(clients, room_id): users = UsersTool.room_users(get_room_key(room_id)) for client in clients: if client: UsersTool.update_user(client, status=UserStatus.free) return RoomTool.delete_room(room_id)