def chess_win(client, clients, i, j, army_type): room_id = UserTool.current_user(client)['r_id'] room_clients = RoomTool.room_clients(clients, room_id) other_client = filter(lambda u: id(u) != id(client), room_clients) Packet(header='chess.down', data={ 'i': i, 'j': j, 'army_type': army_type }, msg=None).write_message_to(clients=other_client) Packet(header='chess.over', data=None, msg=None).write_message_to(clients=room_clients) army = u'白色方' if army_type == 'white' else u'黑色方' gm_say(u'%s获胜啦 5s后游戏结束' % army, room_clients) def game_over(): room = RoomTool.game_over(room_clients, room_id) time.sleep(5) Packet(header='rooms.info', data=room, msg=None).write_message_to(clients=clients) p = threading.Thread(target=game_over) p.start()
def leave(self, data): room_id, p_id = data.get('room_id', None), data.get('p_id', None) room = RoomTool.leave_room(self.client, room_id, p_id) Packet(header='rooms.leave', data=None, msg=None).write_message_to(self.client) Packet(header='rooms.info', data=room, msg=None).write_message_to(clients=self.clients)
def register(self, data): username, password, role_name, icon = data.get( 'username', None), data.get('password', None), data.get( 'rolename', None), data.get('icon', None).encode("ascii") log(data) reason = UserTool.register(username, password, role_name, icon) result, msg = reason['result'], reason['msg'] Packet(header='register.success', data=None, msg=msg).write_message_to( self.client) if result else Packet( header='register.error', data=None, msg=msg).write_message_to( self.client)
def begin(room, clients): r_id = int(room['r_id']) room_clients = RoomTool.room_clients(clients, r_id) games = ChessHandler.begin_game(room_clients, r_id, True) room, current_user, begin_client = games['room'], games['user'], games[ 'begin_client'] Packet(header='rooms.begin', data=room, msg=None).write_message_to(clients=clients) gm_say(u'游戏开始啦, 由黑色方 %s 先出手' % current_user['rolename'], room_clients) if begin_client: Packet(header='rooms.turned', data=room, msg=None).write_message_to(begin_client)
def draw_clear(client, clients): other_clients = filter( lambda u: id(u) != id(client), RoomTool.room_clients(clients, UserTool.current_user(client)['r_id'])) Packet(header='draw.clear', data=None, msg=None).write_message_to(clients=other_clients)
def next_begin(client, clients): r_id = UserTool.current_user(client)['r_id'] room_clients = RoomTool.room_clients(clients, r_id) games = ChessHandler.begin_game(room_clients, r_id) room, current_user, army, begin_client = games['room'], games[ 'user'], games['army'], games['begin_client'] # RoomTool.update_room(r_id, cantip=False) Packet(header='rooms.next', data=room, msg=None).write_message_to(clients=clients) gm_say(u'接下去由%s %s 出手' % (army, current_user['rolename']), room_clients) if begin_client is not None: Packet(header='rooms.turned', data=None, msg=None).write_message_to(begin_client)
def login(self, data): username = data.get('username', None) password = data.get('password', None) results = UserTool.login(self.client, self.clients, username, password) current_users, user = UserTool.current_users(self.client, self.clients.values()), UserTool.current_user(self.client) result, msg = results['result'], results['msg'] if result: # init room Packet(header='login.success', data=user, msg=msg).write_message_to(self.client) Packet(header='user.enter', data=user, msg=None).write_message_to(clients=self.clients) if len(current_users) > 0: Packet(header='user.all', data=current_users, msg=None).write_message_to(self.client) Packet(header='hall.init', data=RoomTool.rooms(), msg=None).write_message_to(self.client) else: Packet(header='login.error', data=None, msg=msg).write_message_to(self.client)
def judge(client, clients, topic, user, message): if DrawHandler.judge_draw_result(topic, message): # self.next_begin() Packet(header='rooms.willnext', data=10, msg=None).write_message_to(clients=clients) gm_say(u'%s 猜对啦, 时间调整为10秒' % user['rolename'], clients) else: say(client, message, clients, 'rooms.message')
def change_position(self, data): room_id, op_id, p_id = data.get('room_id', None), data.get('op_id', None), data.get('p_id', None) try: op_id, p_id = int(op_id), int(p_id) room = RoomTool.change_position(self.client, room_id, op_id, p_id) if room is not None: Packet(header='rooms.info', data=room, msg=None).write_message_to(clients=self.clients) except TypeError as e: log(e)
def enter_room(self, data): room_id, p_id = data.get('room_id', None), data.get('p_id', None) # 1 get room room = RoomTool.enter_room(self.client, room_id, p_id) room_id = room["r_id"] Packet(header='rooms.info', data=room, msg=None).write_message_to(clients=self.clients) Packet(header='rooms.enter', data=room_id, msg=None).write_message_to(self.client) if room['game_type'] == 'draw': gm_say('欢迎来到聚会玩, 该房间将开启的游戏是 - 你画我猜', client=self.client) elif room['game_type'] == 'chess': gm_say('欢迎来到聚会玩, 该房间将开启的游戏是 - 五子棋', client=self.client) gm_say('游戏规则 - 上方为白色方 下方为黑色方, 黑色方先出棋, 黑色方选手下完轮到白色方, 如此交替进行', client=self.client) else: gm_say('欢迎来到聚会玩, 该房间将开启的游戏是 - UNO棋牌', client=self.client) gm_say('游戏规则 - ')
def draw_start(data, client, clients): color, weight = data.get('color', None), data.get('weight', None) other_clients = filter( lambda u: id(u) != id(client), RoomTool.room_clients(clients, UserTool.current_user(client)['r_id'])) Packet(header='draw.begin', data={'color': color, 'weight': weight}, msg=None). \ write_message_to(clients=other_clients)
def next_begin(client, clients): r_id = UserTool.current_user(client)['r_id'] room_clients = RoomTool.room_clients(clients, r_id) games = DrawHandler.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) if over: Packet(header='rooms.info', data=room, msg=None).write_message_to(clients=clients) gm_say('本局游戏结束啦', room_clients) else: Packet(header='rooms.next', data=room, msg=None).write_message_to(clients=clients) gm_say(u'接下去由 %s 作画' % current_user['rolename'], room_clients) DrawHandler.send_tips(current_user, other_clients)
def begin(room, clients): r_id = int(room['r_id']) room_clients = RoomTool.room_clients(clients, r_id) room_user = DrawHandler.begin_game(room_clients, r_id, True) room, current_user, other_clients = room_user['room'], room_user[ 'user'], room_user['other_clients'] Packet(header='rooms.begin', data=room, msg=None).write_message_to(clients=clients) gm_say(u'游戏开始啦, 由 %s 先开始' % current_user['rolename'], room_clients) DrawHandler.send_tips(current_user, other_clients)
def draw_move(data, client, clients): x, y, width, height, r_id = (data.get('x', None), data.get('y', None), data.get('width', None), data.get('height', None), data.get('room_id', None)) other_clients = filter(lambda u: id(u) != id(client), RoomTool.room_clients(clients, r_id)) x_r, y_r = float(x) / width, float(y) / height Packet(header='draw.move', data={ 'x': x_r, 'y': y_r }, msg=None).write_message_to(clients=other_clients)
def toggle(self): result = RoomTool.toggle(self.client) room, is_begin = result['room'], result['begin'] room_type = room['game_type'] Packet(header='rooms.info', data=room, msg=None).write_message_to(clients=self.clients) if is_begin: if room_type == 'draw': self.draw_handler.begin(room, self.clients) elif room_type == 'chess': self.chess_handler.begin(room, self.clients)
def chess_down(client, clients, i, j, army_type): other_client = filter( lambda u: id(u) != id(client), RoomTool.room_clients(clients, UserTool.current_user(client)['r_id'])) Packet(header='chess.down', data={ 'i': i, 'j': j, 'army_type': army_type }, msg=None).write_message_to(clients=other_client) ChessHandler.next_begin(client, clients)
def game_over(): room = RoomTool.game_over(room_clients, room_id) time.sleep(5) Packet(header='rooms.info', data=room, msg=None).write_message_to(clients=clients)