def parse_login_result_command(cmd_body): status_code = extract_kwargs(cmd_body, ('StatusCode', ), 'command.parse_login_result_command') message = extract_kwargs(cmd_body, ('Message', ), 'command.parse_login_result_command') ID = extract_kwargs(cmd_body, ('ID', ), 'command.parse_login_result_command') return status_code, message, ID
def handle_simple_forward(server, **kwargs): raw_message = extract_kwargs(kwargs, ('raw_message', ), 'common_handlers.handle_simple_forward') cur_gamer = extract_kwargs(kwargs, ('cur_gamer', ), 'common_handlers.handle_simple_forward') raw_cmd, raw_cmd_body = decode_msg(raw_message) for gamer in server.Gamers: # 检查gamer if gamer_filter is not None: if not gamer_filter(cur_gamer, gamer): continue # 直接转发之 gamer.send_cmd(command=raw_cmd, **raw_cmd_body)
def handle_gamer_login(server, **kwargs): gamer = extract_kwargs(kwargs, ('Gamer', 'gamer'), 'server.login_handler') # 首先检查玩家数量是否超过限制 with server.Gamers as gp: is_gamer_enough = gp.check_gamer_is_enough() if is_gamer_enough: fail_cmd = make_login_result_command(status_code=0, login_message='玩家数量超过限制', ID=-1) gamer.send_cmd(**fail_cmd) return username, passwd = parse_login_command(kwargs) passed, login_msg, gamer_id = server.GamerAccount.check_gamer_login( username, passwd) cmd = make_login_result_command(status_code=1 if passed else 0, login_message=login_msg, ID=gamer_id) logger.debug('handlers.handle_gamer_login', f'send msg: {cmd}') if passed: from server.gamer import Gamer # 逻辑内部import,防止循环import问题 new_gamer = Gamer(gamer, gamer_id, username) with server.Gamers as gp: # 首先发送登录结果信息 gamer.send_cmd(**cmd) gp.add_gamer(new_gamer, gamer_id, safe_call=True) # 然后发送所有玩家信息,更新玩家情况 gamers_info = gp.pack_all_gamers_info() server.send_all_cmd(**make_gamer_info_command(gamers_info)) # 登录成功后将flag标志为True告知外部可以退出登录循环 gamer.LoginFlag.write_val(True) logger.info('server.login', 'gamer {}:{} login'.format(gamer_id, username)) else: # 不通过也要发送登录结果 gamer.send_cmd(**cmd)
def handle_gamer_exit_login(server, **kwargs): gamer = extract_kwargs(kwargs, ('Gamer', 'gamer'), 'server.login_handler') gamer.LoginFlag.write_val(True) # 玩家主动退出登录操作也需要标志为True退出登录循环
def parse_inform_command(cmd_body): inform_msg = extract_kwargs(cmd_body, ('content', ), 'command.parse_inform_command') return inform_msg
def parse_gamer_info_command(cmd_body): gamers = extract_kwargs(cmd_body, ('gamers', ), 'command.parse_gamer_info_command') return gamers
def parse_chat_command(cmd_body): name = extract_kwargs(cmd_body, ('Name', ), 'command.parse_chat_message') cont = extract_kwargs(cmd_body, ('Content', ), 'command.parse_chat_message') ID = extract_kwargs(cmd_body, ('ID', ), 'command.parse_chat_message') return name, ID, cont
def parse_login_command(cmd_body): username = extract_kwargs(cmd_body, ('Username', ), 'command.parse_login_command') passwd = extract_kwargs(cmd_body, ('Password', ), 'command.parse_login_command') return username, passwd
def parse_setting_changed_command(cmd_body): settings = extract_kwargs(cmd_body, ('settings', ), 'command.parse_setting_changed_command') return settings
def parse_paint_point_command(cmd_body): points = extract_kwargs(cmd_body, ('points', ), 'command.parse_paint_point_command') return points
def parse_click_point_command(cmd_body): x = extract_kwargs(cmd_body, ('X', ), 'command.parse_click_point_command') y = extract_kwargs(cmd_body, ('Y', ), 'command.parse_click_point_command') return x, y
def parse_make_puzzle_command(cmd_body): answer = extract_kwargs(cmd_body, ('answer', ), 'command.parse_make_puzzle_command') hint = extract_kwargs(cmd_body, ('hint', ), 'command.parse_make_puzzle_command') return answer, hint
def parse_game_timer_event_command(cmd_body): sec = extract_kwargs(cmd_body, ('second', ), 'command.prase_game_timer_event_command') return sec