From dcda979aa91444a7a430b60851b3a3d0d0c2c68d Mon Sep 17 00:00:00 2001 From: Dan Van Atta Date: Sun, 21 Jul 2024 15:01:59 -0700 Subject: [PATCH] Bots: Replace chat upload to lobby with simple info logging (#12741) Commentary: - Drawback: bot chat history will no longer be readily available to moderators. That was useful when deciding "he-said-she-said" style disputes. - Benefits: fixes potential infinite loop that would crash the bot & simplifies. Fix details: - Any warn/error message is sent as a chat message. Any chat message in turn is sent to the lobby. If there is an error sending a message to the lobby, it can yield an error message, which in turn triggers a chat message, which in turn yields another upload to the lobby thereby starting an infinite cycle. If we do not send messages up to the lobby, then this is no longer a problem. Future plans: - Overall, ideally we can invest efforts in "network relay" rather than bot hosts, and remove all of the headless-game code entirely. --- .../strategy/engine/chat/HeadlessChat.java | 6 +- .../startup/mc/ServerChatUpload.java | 62 ------------------- .../framework/startup/mc/ServerModel.java | 11 ---- .../lobby/watcher/LobbyWatcherClient.java | 8 --- .../connections/GameToLobbyConnection.java | 5 -- 5 files changed, 5 insertions(+), 87 deletions(-) delete mode 100644 game-app/game-core/src/main/java/games/strategy/engine/framework/startup/mc/ServerChatUpload.java diff --git a/game-app/game-core/src/main/java/games/strategy/engine/chat/HeadlessChat.java b/game-app/game-core/src/main/java/games/strategy/engine/chat/HeadlessChat.java index 9c6c8ac531..80f14e9858 100644 --- a/game-app/game-core/src/main/java/games/strategy/engine/chat/HeadlessChat.java +++ b/game-app/game-core/src/main/java/games/strategy/engine/chat/HeadlessChat.java @@ -1,10 +1,12 @@ package games.strategy.engine.chat; import lombok.Getter; +import lombok.extern.slf4j.Slf4j; import org.triplea.domain.data.UserName; import org.triplea.game.chat.ChatModel; /** Headless version of ChatPanel. */ +@Slf4j public class HeadlessChat implements ChatMessageListener, ChatModel { @Getter(onMethod_ = @Override) private final Chat chat; @@ -17,7 +19,9 @@ public HeadlessChat(final Chat chat) { public void eventReceived(final String eventText) {} @Override - public void messageReceived(final UserName fromPlayer, final String chatMessage) {} + public void messageReceived(final UserName fromPlayer, final String chatMessage) { + log.info(String.format("Chat Message {Player: %s, Message: %s}", fromPlayer, chatMessage)); + } @Override public void slapped(final UserName from) {} diff --git a/game-app/game-core/src/main/java/games/strategy/engine/framework/startup/mc/ServerChatUpload.java b/game-app/game-core/src/main/java/games/strategy/engine/framework/startup/mc/ServerChatUpload.java deleted file mode 100644 index 8a27e45b28..0000000000 --- a/game-app/game-core/src/main/java/games/strategy/engine/framework/startup/mc/ServerChatUpload.java +++ /dev/null @@ -1,62 +0,0 @@ -package games.strategy.engine.framework.startup.mc; - -import games.strategy.engine.chat.ChatMessageListener; -import java.util.function.Supplier; -import javax.annotation.Nonnull; -import lombok.Builder; -import lombok.extern.slf4j.Slf4j; -import org.triplea.domain.data.UserName; -import org.triplea.http.client.lobby.game.lobby.watcher.ChatUploadParams; -import org.triplea.http.client.web.socket.client.connections.GameToLobbyConnection; -import org.triplea.java.concurrency.AsyncRunner; - -/** - * This module listens for chat messages and uploads them to lobby. The upload is done non-blocking - * on a background thread. - */ -@Builder -@Slf4j -class ServerChatUpload implements ChatMessageListener { - @Nonnull private final GameToLobbyConnection gameToLobbyConnection; - @Nonnull private final UserName hostName; - @Nonnull private final Supplier gameIdSupplier; - - @Override - public void messageReceived(final UserName fromPlayer, final String chatMessage) { - final String gameId = gameIdSupplier.get(); - if (gameId == null) { - // null gameId can mean we are in process of reconnecting to lobby. - return; - } - AsyncRunner.runAsync( - () -> - gameToLobbyConnection.sendChatMessageToLobby( - buildUploadParams(gameId, fromPlayer, chatMessage))) - .exceptionally( - e -> - // Handle this as an info level so we do not disturb the user with an error pop-up, - // we want this to be a silent failure. - log.info("Error sending chat message to lobby: " + e.getMessage())); - } - - private ChatUploadParams buildUploadParams( - final String gameId, final UserName fromPlayer, final String chatMessage) { - return ChatUploadParams.builder() - .fromPlayer(fromPlayer) - .chatMessage(chatMessage) - .gameId(gameId) - .build(); - } - - @Override - public void eventReceived(final String eventText) {} - - @Override - public void slapped(final UserName from) {} - - @Override - public void playerJoined(final String message) {} - - @Override - public void playerLeft(final String message) {} -} diff --git a/game-app/game-core/src/main/java/games/strategy/engine/framework/startup/mc/ServerModel.java b/game-app/game-core/src/main/java/games/strategy/engine/framework/startup/mc/ServerModel.java index 43928001c0..ba7b940052 100644 --- a/game-app/game-core/src/main/java/games/strategy/engine/framework/startup/mc/ServerModel.java +++ b/game-app/game-core/src/main/java/games/strategy/engine/framework/startup/mc/ServerModel.java @@ -264,17 +264,6 @@ private GameHostingResponse createServerMessenger(final ServerConnectionProps pr chatModel = launchAction.createChatModel(CHAT_NAME, messengers, ClientNetworkBridge.NO_OP_SENDER); - if (gameToLobbyConnection != null && lobbyWatcherThread != null) { - chatModel - .getChat() - .addChatListener( - ServerChatUpload.builder() - .gameToLobbyConnection(gameToLobbyConnection) - .hostName(messengers.getLocalNode().getPlayerName()) - .gameIdSupplier(() -> lobbyWatcherThread.getGameId().orElse(null)) - .build()); - } - serverMessenger.setAcceptNewConnections(true); gameDataChanged(); return gameHostingResponse; diff --git a/http-clients/lobby-client/src/main/java/org/triplea/http/client/lobby/game/lobby/watcher/LobbyWatcherClient.java b/http-clients/lobby-client/src/main/java/org/triplea/http/client/lobby/game/lobby/watcher/LobbyWatcherClient.java index 9a6de675dd..a29a6f00ee 100644 --- a/http-clients/lobby-client/src/main/java/org/triplea/http/client/lobby/game/lobby/watcher/LobbyWatcherClient.java +++ b/http-clients/lobby-client/src/main/java/org/triplea/http/client/lobby/game/lobby/watcher/LobbyWatcherClient.java @@ -20,7 +20,6 @@ public interface LobbyWatcherClient { String REMOVE_GAME_PATH = "/lobby/games/remove-game"; String PLAYER_JOINED_PATH = "/lobby/games/player-joined"; String PLAYER_LEFT_PATH = "/lobby/games/player-left"; - String UPLOAD_CHAT_PATH = "/lobby/chat/upload"; static LobbyWatcherClient newClient(final URI serverUri, final ApiKey apiKey) { return HttpClient.newClient( @@ -43,13 +42,6 @@ default void updateGame(final String gameId, final LobbyGame lobbyGame) { @RequestLine("POST " + LobbyWatcherClient.REMOVE_GAME_PATH) void removeGame(String gameId); - @RequestLine("POST " + LobbyWatcherClient.UPLOAD_CHAT_PATH) - String uploadChatMessage(ChatMessageUpload chatMessageUpload); - - default void uploadChatMessage(final ChatUploadParams uploadChatMessageParams) { - uploadChatMessage(uploadChatMessageParams.toChatMessageUpload()); - } - @RequestLine("POST " + LobbyWatcherClient.PLAYER_JOINED_PATH) String playerJoined(PlayerJoinedNotification playerJoinedNotification); diff --git a/http-clients/lobby-client/src/main/java/org/triplea/http/client/web/socket/client/connections/GameToLobbyConnection.java b/http-clients/lobby-client/src/main/java/org/triplea/http/client/web/socket/client/connections/GameToLobbyConnection.java index 782a5b3097..ab9e783f60 100644 --- a/http-clients/lobby-client/src/main/java/org/triplea/http/client/web/socket/client/connections/GameToLobbyConnection.java +++ b/http-clients/lobby-client/src/main/java/org/triplea/http/client/web/socket/client/connections/GameToLobbyConnection.java @@ -12,7 +12,6 @@ import org.triplea.http.client.LobbyHttpClientConfig; import org.triplea.http.client.lobby.HttpLobbyClient; import org.triplea.http.client.lobby.game.hosting.request.GameHostingResponse; -import org.triplea.http.client.lobby.game.lobby.watcher.ChatUploadParams; import org.triplea.http.client.lobby.game.lobby.watcher.GamePostingRequest; import org.triplea.http.client.lobby.game.lobby.watcher.GamePostingResponse; import org.triplea.http.client.lobby.game.lobby.watcher.LobbyWatcherClient; @@ -92,10 +91,6 @@ public void close() { webSocket.close(); } - public void sendChatMessageToLobby(final ChatUploadParams chatUploadParams) { - lobbyWatcherClient.uploadChatMessage(chatUploadParams); - } - public void playerJoined(final String gameId, final UserName playerName) { AsyncRunner.runAsync(() -> lobbyWatcherClient.playerJoined(gameId, playerName)) .exceptionally(e -> log.info("Failed to notify lobby a player connected", e));