def __create_response(self): message = self.request.get("message") if message == "CONNECT": self.__player_name = self.request["name"] response = ProtoMessage.connection_ok_message(self.__spawn_pos) self.response_created = True return response elif message == "READY": response = ProtoMessage.start_message({ "object1": { "game_type": "/Some/gob_type/from/inventory", "pos": [23, 32], "heading": 54, "health": 100 }, "Player1": { "game_type": "/Ship", "pos": (0, 2), "heading": 0, "health": 58 } }) self.response_created = True return response elif message == "INPUT": response = self.__game_state_message self.response_created = True return response return None
def __create_response(self): message = self.request.get("message") if message == "CONNECT": self.__player_name = self.request["name"] response = ProtoMessage.init_message({ "object1": { "game_type": "/Some/gob_type/from/inventory", "pos": [23, 32], "heading": 54, "health": 100 }, "Player1": { "game_type": "/Ship", "pos": (0, 2), "heading": 0, "health": 58 } }) self.response_created = True return response elif message == "READY": response = ProtoMessage.start_message() self.response_created = True return response elif message == "INPUT": if self.__processed_count < 10: response = ProtoMessage.update_message({ "object1": { "id": 43, "pos": [230, 2], "heading": 5, "health": 30 }, "Player1": { "id": 23, "pos": (120, 2), "heading": 20, "health": 98 } }) else: response = ProtoMessage.stop_message() self.response_created = True return response return None
def connect(self, player_name): """Connects to the server.""" logging.debug("Connecting to {0}:{1}".format(self.__address[0], self.__address[1])) self.__socket.connect_ex(self.__address) self.__proto_message = ProtoMessage.connect_message(player_name) self.__selector.register(self.__socket, selectors.EVENT_WRITE)
def command_connection_ok(self, from_state, to_state, spawn_pos): """Executes the INIT command from the Server. Returns True if the state transition is successful.""" logging.debug( f"command_connection_ok(): {from_state} -> {to_state} spawn_pos = {spawn_pos}" ) self.__proto_message = ProtoMessage.ready_message({ "game_type": "/Ship", "pos": spawn_pos, "heading": 0, "health": 58 }) return True
def command_update(self, from_state, to_state): """Executes the START command from the Server. Returns True if the state transition is successful.""" logging.debug(f"command_update(): {from_state} -> {to_state}") self.__proto_message = ProtoMessage.input_message( ["FORWARD", "LEFT", "FIRE"]) return True
def command_init(self, from_state, to_state): """Executes the INIT command from the Server. Returns True if the state transition is successful.""" logging.debug(f"command_init(): {from_state} -> {to_state}") self.__proto_message = ProtoMessage.ready_message() return True
def command_update(self, from_state, to_state, objects): """Executes the UPDATE command from the Server. Returns True if the state transition is successful.""" logging.debug(f"command_update(): {from_state} -> {to_state}") self.__proto_message = ProtoMessage.input_message(self.__inputs) self.__game_state = objects return True
def set_inputs(self, inputs): """Sets the list of input actions collected during a frame. These are sent to the server.""" if self.__fsm.state == ClientState.PLAYING: self.__proto_message = ProtoMessage.input_message(inputs)
def propagate_game_state(self, game_state_dict): """Propagates the dictionary that represents game state to all the connected clients.""" message = ProtoMessage.update_message(game_state_dict) for _, client in self.__connected_clients.items(): client.set_game_state_message(message)