def on_send_message_request(server, request, connection_handler, game): """ Manage SND request :param server: server which receives the request :param request: request to manage :param connection_handler: connection handler from which the request was sent :param game: the game :return: the list of responses """ _, _, token, power_name = utils.get_user_connection( server.users, game, connection_handler) message = ' '.join([ str( tokens.Token(from_bytes=(request.message_bytes[i], request.message_bytes[i + 1]))) for i in range(0, len(request.message_bytes), 2) ]) for recipient_power_name in request.powers: game_message = Message(sender=power_name, recipient=recipient_power_name, phase=game.get_current_phase(), message=message) send_game_message_request = internal_requests.SendGameMessage( power_name=power_name, message=game_message, game_role=power_name, phase=game.get_current_phase(), game_id=game.game_id, token=token) yield internal_request_managers.handle_request( server, send_game_message_request, connection_handler) return [responses.YES(bytes(request))]
def on_go_flag_request(server, request, connection_handler, game): """ Manage GOF request :param server: server which receives the request :param request: request to manage :param connection_handler: connection handler from which the request was sent :param game: the game :return: the list of responses """ _, _, token, power_name = utils.get_user_connection( server.users, game, connection_handler) set_wait_flag_request = internal_requests.SetWaitFlag( power_name=power_name, wait=False, game_id=request.game_id, game_role=power_name, phase=game.get_current_phase(), token=token) yield internal_request_managers.handle_request(server, set_wait_flag_request, connection_handler) if not game.get_power(power_name).order_is_set: set_orders_request = internal_requests.SetOrders( power_name=power_name, orders=[], game_id=request.game_id, game_role=power_name, phase=game.get_current_phase(), token=token) yield internal_request_managers.handle_request(server, set_orders_request, connection_handler) return [responses.YES(bytes(request))]
def test_yes(): """ Tests the YES response """ daide_str = 'YES ( TME ( #60 ) )' request_daide_str = 'TME ( #60 )' response = responses.YES(request_bytes=str_to_bytes(request_daide_str)) assert isinstance(response, responses.YES), 'Expected a YES response' assert bytes(response) == str_to_bytes(daide_str)
def on_i_am_request(server, request, connection_handler, game): """ Manage IAM request :param server: server which receives the request :param request: request to manage :param connection_handler: connection handler from which the request was sent :param game: the game :return: the list of responses """ power_name, passcode = request.power_name, request.passcode # find user username = None for user in server.users.values(): if not isinstance(user, DaideUser): continue is_passcode_valid = bool(user.passcode == passcode) if is_passcode_valid and game.is_controlled_by(power_name, user.username): username = user.username break if username is None: return [responses.REJ(bytes(request))] try: server.assert_token(connection_handler.token, connection_handler) except exceptions.TokenException: connection_handler.token = None if not connection_handler.token: sign_in_request = internal_requests.SignIn(username=username, password='******', create_user=False) try: token_response = yield internal_request_managers.handle_request( server, sign_in_request, connection_handler) connection_handler.token = token_response.data except exceptions.UserException: return [responses.REJ(bytes(request))] join_game_request = internal_requests.JoinGame( game_id=game.game_id, power_name=power_name, registration_password=None, token=connection_handler.token) yield internal_request_managers.handle_request(server, join_game_request, connection_handler) return [responses.YES(bytes(request))]
def on_name_request(server, request, connection_handler, game): """ Manage NME request :param server: server which receives the request :param request: request to manage :param connection_handler: connection handler from which the request was sent :param game: the game :return: the list of responses """ username = connection_handler.get_name_variant() + request.client_name try: server.assert_token(connection_handler.token, connection_handler) except exceptions.TokenException: connection_handler.token = None if not connection_handler.token: user_exists = server.users.has_username(username) sign_in_request = internal_requests.SignIn(username=username, password='******', create_user=not user_exists) try: token_response = yield internal_request_managers.handle_request( server, sign_in_request, connection_handler) connection_handler.token = token_response.data if not isinstance(server.users.get_user(username), DaideUser): daide_user = DaideUser( passcode=random.randint(1, 8191), client_name=request.client_name, client_version=request.client_version, **server.users.get_user(username).to_dict()) server.users.replace_user(username, daide_user) server.save_data() except exceptions.UserException: return [responses.REJ(bytes(request))] # find next available power power_name = [ power_name for power_name, power in game.powers.items() if not power.is_controlled() ] if not power_name: return [responses.REJ(bytes(request))] return [responses.YES(bytes(request)), responses.MAP(game.map.name)]
def on_draw_request(server, request, connection_handler, game): """ Manage DRW request :param server: server which receives the request :param request: request to manage :param connection_handler: connection handler from which the request was sent :param game: the game :return: the list of responses """ _, _, token, power_name = utils.get_user_connection( server.users, game, connection_handler) vote_request = internal_requests.Vote(power_name=power_name, vote=strings.YES, game_role=power_name, phase=game.get_current_phase(), game_id=game.game_id, token=token) yield internal_request_managers.handle_request(server, vote_request, connection_handler) return [responses.YES(bytes(request))]
def on_not_request(server, request, connection_handler, game): """ Manage NOT request :param server: server which receives the request :param request: request to manage :param connection_handler: connection handler from which the request was sent :param game: the game :return: the list of responses """ _, _, token, power_name = utils.get_user_connection( server.users, game, connection_handler) response = None not_request = request.request # Cancelling orders if isinstance(not_request, requests.SUB): if not_request.orders: # cancel one order pass else: clear_orders_request = internal_requests.ClearOrders( power_name=power_name, game_id=game.game_id, game_role=power_name, phase=game.get_current_phase(), token=token) yield internal_request_managers.handle_request( server, clear_orders_request, connection_handler) response = responses.YES(bytes(request)) # Cancel wait flag elif isinstance(not_request, requests.GOF): set_wait_flag_request = internal_requests.SetWaitFlag( power_name=power_name, wait=True, game_id=game.game_id, game_role=power_name, phase=game.get_current_phase(), token=token) yield internal_request_managers.handle_request(server, set_wait_flag_request, connection_handler) response = responses.YES(bytes(request)) # Cancel get deadline request elif isinstance(not_request, requests.TME): response = responses.REJ(bytes(request)) # Cancel vote elif isinstance(not_request, requests.DRW): vote_request = internal_requests.Vote(power_name=power_name, vote=strings.NEUTRAL, game_role=power_name, phase=game.get_current_phase(), game_id=game.game_id, token=token) yield internal_request_managers.handle_request(server, vote_request, connection_handler) response = responses.YES(bytes(request)) # Returning response return [response if response else responses.REJ(bytes(request))]