def handle_common_chat(server, **kwargs): name, ID, content = parse_chat_command(kwargs) if name is None or content is None: logger.warning('common_handlers.handle_common_chat', f'no name or content extracted: {name},{content}') return server.send_all_cmd(**make_chat_command(ID, name, content))
def handle_gamer_info(engine: ClientEngine, signals: ClientSignal, **kwargs): gamers = parse_gamer_info_command(kwargs) if gamers is None: logger.warning('client.handle_gamer_info' 'gamers were not extracted from command body') return engine.update_gamers(gamers)
def handle_common_chat(engine: ClientEngine, signals: ClientSignal, **kwargs): name, ID, content = parse_chat_command(kwargs) if name is None or content is None: logger.warning('client.handle_common_chat', f'no name or content extracted: {name},{content}') return engine.add_chat_message(name, content)
def get_handler(state, cmd, supress_log=False): logger.debug('server.handlers', 'getting handler of {}'.format(cmd)) try: handler = __handler_switch[state][cmd] return handler except KeyError: if not supress_log: logger.warning( 'server.handlers.get_handler', 'can not get handler for {c} in {s}'.format(c=cmd, s=state)) return handle_none except Exception as e: if not supress_log: logger.error('server.handlers.get_handler', 'unknown err: {}'.format(e)) return handle_none
def login(self): try: usrName, password = self.getUsrAndPsw() logger.info('LoginPanel.login', 'username: {}, psw: {}'.format(usrName, password)) self.send_cmd(**make_login_command(usrName, password)) # self.send_cmd(command='Login', Username=usrName, Password=password) # 收取服务器的回复 while True: cmd, body = self.recv_cmd() if cmd == CMD_LOGIN_RESULT: break else: logger.warning( 'LoginPanel.login', 'recv {} cmd with {}, skip'.format(cmd, body)) time.sleep(1) logger.info('LoginPanel.login', 'login success: {}'.format(body)) # login_status_code = body['LoginStateCode'] # login_info = body['LoginMessage'] login_status_code, login_info, ID = parse_login_result_command( body) self.ClientId = ID if login_status_code == 1: self.close() logger.info('LoginPanel.login', 'activating...') self.Activator(self.Socket, self.ClientId, self.UsrName.text()) else: widgets.QMessageBox.warning(self, '登陆失败', login_info) # self.UsrName.clear() self.Psw.clear() except Exception as e: logger.error('LoginPanel.login', 'Fail to login, err: {}'.format(e)) raise e
def accept_and_monitor_host(self): ''' 使用欢迎套接字来接受玩家连接,同时监听Host的开始游戏命令来终止 接受更多的玩家连接 :return: 玩家连接的套接字和地址,如果游戏开始则返回None ''' # 内层循环不断在监听主机命令和监听玩家连接之间循环 while True: try: # print('accepting...')\ con_socket, addr = self.Socket.accept() print('new gamer connected...') # 接受到新玩家以后,本轮循环不再监听主机命令 return con_socket, addr # 每3秒就切换到监听主机命令 except socket.timeout: if self.CntedUsrNumber != 0: self.UsrSocket[0].settimeout( config.server.HostCommandInterval) try: host_cmd, host_cmd_vals = self.recv_cmd(0, decode=True) # 如果主机发出开始游戏命令,则返回None代表不再接受玩家连接 if host_cmd == 'BeginGame': logger.info('server.accept', 'Game has begun!') self.send_all_cmd(command='BeginGame') # for i, s_ in enumerate(self.UsrSocket): # self.send_cmd(i, command='BeginGame') return None, None else: logger.warning( 'server.accept', f'Not accepted command type during login waiting: {host_cmd}, ignore' ) except socket.timeout: pass