Class WebSocketMessagesManager
WebSocketMessagesManager class is responsible for managing the sending and receiving of messages
over the WebSocket connection in the game.
It handles asynchronous communication by maintaining a thread-safe map of pending callbacks for message responses. This class provides methods for sending various types of remote messages, such as creating remote interaction chains, appending interactions, applying or aborting remote game state updates, and controlling game state transitions like starting a game or exiting to the main menu.
The class follows the singleton design pattern to ensure only one instance exists during the application's lifecycle.
It interacts with other components such as GameController, LevelManager, and StageManager to
coordinate remote operations and state updates.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidabortRemoteGSU(String remoteGameControllerInstanceId) Sends a request to abort remote game state updates (GSUs).voidappendRemoteInteraction(String remoteGameControllerInstanceId, CompletableFuture<Object> future, Interaction interaction, boolean suppressTranscendentFollowUp) Sends a request to append a remote interaction and registers a callback to handle the response.voidapplyRemoteGSUs(String remoteGameControllerInstanceId) Sends a request to apply remote game state updates (GSUs).voidCompletes the future associated with a given message.voidcreateRemoteInteractionChain(String remoteGameControllerInstanceId, CompletableFuture<Object> future) Sends a request to create a remote interaction chain and registers a callback to handle the response.static voidSends a message via the WebSocket connection to instruct the remote game controller to exit to the main menu.static WebSocketMessagesManagerRetrieves the singleton instance of the WebSocketMessagesManager.static voidhandleIncomingMessages(Object deserializedObject) Handles incoming messages from the WebSocket connection.static voidstartGame(int levelIndex) Sends a message via the WebSocket connection to start the game at the specified level.
-
Constructor Details
-
WebSocketMessagesManager
public WebSocketMessagesManager()
-
-
Method Details
-
getInstance
Retrieves the singleton instance of the WebSocketMessagesManager.If the instance is not yet initialized, it creates a new one.
- Returns:
- the singleton instance of the WebSocketMessagesManager.
-
createRemoteInteractionChain
public void createRemoteInteractionChain(String remoteGameControllerInstanceId, CompletableFuture<Object> future) Sends a request to create a remote interaction chain and registers a callback to handle the response.- Parameters:
remoteGameControllerInstanceId- the ID of the remote game controller instance.future- theCompletableFutureto complete when the response is received.
-
appendRemoteInteraction
public void appendRemoteInteraction(String remoteGameControllerInstanceId, CompletableFuture<Object> future, Interaction interaction, boolean suppressTranscendentFollowUp) Sends a request to append a remote interaction and registers a callback to handle the response.- Parameters:
remoteGameControllerInstanceId- the ID of the remote game controller instance.future- theCompletableFutureto complete when the response is received.interaction- The interaction to trigger.suppressTranscendentFollowUp- whether to suppress follow-up interactions.
-
applyRemoteGSUs
Sends a request to apply remote game state updates (GSUs).- Parameters:
remoteGameControllerInstanceId- the ID of the remote game controller instance.
-
abortRemoteGSU
Sends a request to abort remote game state updates (GSUs).- Parameters:
remoteGameControllerInstanceId- the ID of the remote game controller instance.
-
startGame
public static void startGame(int levelIndex) Sends a message via the WebSocket connection to start the game at the specified level.This method constructs a
Messages.startGameMessagewith the provided level index, serializes it to JSON, and sends it using theWebSocketClient. The level index should correspond to an entry in theLevelsenum, indicating which level to load.- Parameters:
levelIndex- the index of the level to start
-
exitToMainMenuMessage
public static void exitToMainMenuMessage()Sends a message via the WebSocket connection to instruct the remote game controller to exit to the main menu.This method constructs a
Messages.exitToMainMenuMessage, serializes it to JSON, and sends it using theWebSocketClient. When processed, this message should trigger a transition back to the main menu on the remote system. -
callback
Completes the future associated with a given message.- Parameters:
messageObj- theMessageobject to use for completing the future.
-
handleIncomingMessages
Handles incoming messages from the WebSocket connection.This method runs asynchronously and dispatches messages based on their type.
- Parameters:
deserializedObject- the deserialized message.
-