Class WebSocketMessagesManager

java.lang.Object
tech.underoaks.coldcase.remote.WebSocketMessagesManager

public class WebSocketMessagesManager extends Object
The 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 Details

    • WebSocketMessagesManager

      public WebSocketMessagesManager()
  • Method Details

    • getInstance

      public static WebSocketMessagesManager 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 - the CompletableFuture to 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 - the CompletableFuture to complete when the response is received.
      interaction - The interaction to trigger.
      suppressTranscendentFollowUp - whether to suppress follow-up interactions.
    • applyRemoteGSUs

      public void applyRemoteGSUs(String remoteGameControllerInstanceId)
      Sends a request to apply remote game state updates (GSUs).
      Parameters:
      remoteGameControllerInstanceId - the ID of the remote game controller instance.
    • abortRemoteGSU

      public void abortRemoteGSU(String remoteGameControllerInstanceId)
      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.startGameMessage with the provided level index, serializes it to JSON, and sends it using the WebSocketClient. The level index should correspond to an entry in the Levels enum, 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 the WebSocketClient. When processed, this message should trigger a transition back to the main menu on the remote system.

    • callback

      public void callback(Message messageObj)
      Completes the future associated with a given message.
      Parameters:
      messageObj - the Message object to use for completing the future.
    • handleIncomingMessages

      public static void handleIncomingMessages(Object deserializedObject)
      Handles incoming messages from the WebSocket connection.

      This method runs asynchronously and dispatches messages based on their type.

      Parameters:
      deserializedObject - the deserialized message.