diff --git a/game-app/game-headed/src/main/java/games/strategy/engine/framework/startup/ui/panels/main/HeadedServerSetupModel.java b/game-app/game-headed/src/main/java/games/strategy/engine/framework/startup/ui/panels/main/HeadedServerSetupModel.java index 9067892578..2051894536 100644 --- a/game-app/game-headed/src/main/java/games/strategy/engine/framework/startup/ui/panels/main/HeadedServerSetupModel.java +++ b/game-app/game-headed/src/main/java/games/strategy/engine/framework/startup/ui/panels/main/HeadedServerSetupModel.java @@ -17,7 +17,6 @@ import games.strategy.engine.framework.startup.ui.posted.game.pbem.PbemSetupPanel; import games.strategy.engine.framework.startup.ui.posted.game.pbf.PbfSetupPanel; import games.strategy.engine.framework.ui.MainFrame; -import games.strategy.engine.lobby.client.LobbyClient; import games.strategy.engine.lobby.client.login.LobbyLogin; import games.strategy.engine.lobby.client.login.LoginMode; import games.strategy.engine.lobby.client.login.LoginResult; @@ -133,8 +132,7 @@ public void login() { } private void showLobbyWindow(final LoginResult loginResult) { - final var lobbyClient = LobbyClient.newLobbyClient(loginResult); - final LobbyFrame lobbyFrame = new LobbyFrame(lobbyClient); + final LobbyFrame lobbyFrame = new LobbyFrame(loginResult); MainFrame.hide(); lobbyFrame.setVisible(true); } diff --git a/game-app/game-headed/src/main/java/games/strategy/engine/lobby/client/LobbyClient.java b/game-app/game-headed/src/main/java/games/strategy/engine/lobby/client/LobbyClient.java deleted file mode 100644 index a7dbab4eaa..0000000000 --- a/game-app/game-headed/src/main/java/games/strategy/engine/lobby/client/LobbyClient.java +++ /dev/null @@ -1,36 +0,0 @@ -package games.strategy.engine.lobby.client; - -import games.strategy.engine.lobby.client.login.LoginResult; -import games.strategy.triplea.settings.ClientSetting; -import javax.annotation.Nonnull; -import lombok.Builder; -import lombok.Getter; -import org.triplea.domain.data.UserName; -import org.triplea.http.client.web.socket.client.connections.PlayerToLobbyConnection; -import org.triplea.swing.SwingComponents; - -/** Provides information about a client connection to a lobby server. */ -@Getter -@Builder -public class LobbyClient { - @Nonnull private final PlayerToLobbyConnection playerToLobbyConnection; - @Nonnull private final UserName userName; - private final boolean anonymousLogin; - private final boolean moderator; - private final boolean passwordChangeRequired; - private final String lobbyMessage; - - public static LobbyClient newLobbyClient(final LoginResult loginResult) { - return LobbyClient.builder() - .playerToLobbyConnection( - new PlayerToLobbyConnection( - ClientSetting.lobbyUri.getValueOrThrow(), - loginResult.getApiKey(), - error -> SwingComponents.showError(null, "Error communicating with lobby", error))) - .anonymousLogin(loginResult.isAnonymousLogin()) - .moderator(loginResult.isModerator()) - .userName(loginResult.getUsername()) - .lobbyMessage(loginResult.getLoginMessage()) - .build(); - } -} diff --git a/game-app/game-headed/src/main/java/games/strategy/engine/lobby/client/ui/LobbyFrame.java b/game-app/game-headed/src/main/java/games/strategy/engine/lobby/client/ui/LobbyFrame.java index 5b77b768c4..dc70019c12 100644 --- a/game-app/game-headed/src/main/java/games/strategy/engine/lobby/client/ui/LobbyFrame.java +++ b/game-app/game-headed/src/main/java/games/strategy/engine/lobby/client/ui/LobbyFrame.java @@ -6,12 +6,13 @@ import games.strategy.engine.chat.ChatPlayerPanel; import games.strategy.engine.chat.ChatTransmitter; import games.strategy.engine.chat.LobbyChatTransmitter; -import games.strategy.engine.lobby.client.LobbyClient; +import games.strategy.engine.lobby.client.login.LoginResult; import games.strategy.engine.lobby.client.ui.action.BanPlayerModeratorAction; import games.strategy.engine.lobby.client.ui.action.DisconnectPlayerModeratorAction; import games.strategy.engine.lobby.client.ui.action.MutePlayerAction; import games.strategy.engine.lobby.client.ui.action.player.info.ShowPlayerInformationAction; import games.strategy.triplea.EngineImageLoader; +import games.strategy.triplea.settings.ClientSetting; import games.strategy.triplea.ui.QuitHandler; import games.strategy.triplea.ui.menubar.LobbyMenu; import java.awt.BorderLayout; @@ -26,6 +27,7 @@ import lombok.Getter; import org.triplea.domain.data.ChatParticipant; import org.triplea.game.client.HeadedGameRunner; +import org.triplea.http.client.web.socket.client.connections.PlayerToLobbyConnection; import org.triplea.sound.ClipPlayer; import org.triplea.swing.DialogBuilder; import org.triplea.swing.SwingComponents; @@ -34,33 +36,51 @@ public class LobbyFrame extends JFrame implements QuitHandler { private static final long serialVersionUID = -388371674076362572L; - @Getter private final LobbyClient lobbyClient; + @Getter private final LoginResult loginResult; private final ChatTransmitter chatTransmitter; private final LobbyGameTableModel tableModel; - public LobbyFrame(final LobbyClient lobbyClient) { + public LobbyFrame(final LoginResult loginResult) { super("TripleA Lobby"); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); SwingComponents.addWindowClosedListener(this, HeadedGameRunner::exitGameIfNoWindowsVisible); setIconImage(EngineImageLoader.loadFrameIcon()); - this.lobbyClient = lobbyClient; - setJMenuBar(new LobbyMenu(this)); - chatTransmitter = - new LobbyChatTransmitter( - lobbyClient.getPlayerToLobbyConnection(), lobbyClient.getUserName()); + this.loginResult = loginResult; + + PlayerToLobbyConnection playerToLobbyConnection = + new PlayerToLobbyConnection( + ClientSetting.lobbyUri.getValueOrThrow(), + loginResult.getApiKey(), + error -> SwingComponents.showError(null, "Error communicating with lobby", error)); + + setJMenuBar(new LobbyMenu(this, loginResult, playerToLobbyConnection)); + playerToLobbyConnection.addConnectionTerminatedListener( + reason -> { + DialogBuilder.builder() + .parent(this) + .title("Connection to Lobby Closed") + .errorMessage("Connection closed: " + reason) + .showDialog(); + shutdown(); + }); + playerToLobbyConnection.addConnectionClosedListener(this::shutdown); + + chatTransmitter = new LobbyChatTransmitter(playerToLobbyConnection, loginResult.getUsername()); final Chat chat = new Chat(chatTransmitter); final ChatMessagePanel chatMessagePanel = new ChatMessagePanel(chat, ChatSoundProfile.LOBBY, new ClipPlayer()); - Optional.ofNullable(lobbyClient.getLobbyMessage()) + Optional.ofNullable(loginResult.getLoginMessage()) .ifPresent(chatMessagePanel::addServerMessage); final ChatPlayerPanel chatPlayers = new ChatPlayerPanel(chat); chatPlayers.setPreferredSize(new Dimension(200, 600)); - chatPlayers.addActionFactory(this::lobbyPlayerRightClickMenuActions); + chatPlayers.addActionFactory( + clickedChatter -> + lobbyPlayerRightClickMenuActions( + this, loginResult, clickedChatter, playerToLobbyConnection)); - tableModel = - new LobbyGameTableModel( - lobbyClient.isModerator(), lobbyClient.getPlayerToLobbyConnection()); - final LobbyGamePanel gamePanel = new LobbyGamePanel(this, lobbyClient, tableModel); + tableModel = new LobbyGameTableModel(loginResult.isModerator(), playerToLobbyConnection); + final LobbyGamePanel gamePanel = + new LobbyGamePanel(this, loginResult, tableModel, playerToLobbyConnection); final JSplitPane leftSplit = new JSplitPane(); leftSplit.setOrientation(JSplitPane.VERTICAL_SPLIT); @@ -78,18 +98,6 @@ public LobbyFrame(final LobbyClient lobbyClient) { pack(); chatMessagePanel.requestFocusInWindow(); setLocationRelativeTo(null); - lobbyClient - .getPlayerToLobbyConnection() - .addConnectionTerminatedListener( - reason -> { - DialogBuilder.builder() - .parent(this) - .title("Connection to Lobby Closed") - .errorMessage("Connection closed: " + reason) - .showDialog(); - shutdown(); - }); - lobbyClient.getPlayerToLobbyConnection().addConnectionClosedListener(this::shutdown); addWindowListener( new WindowAdapter() { @Override @@ -99,43 +107,45 @@ public void windowClosing(final WindowEvent e) { }); } - private List lobbyPlayerRightClickMenuActions(final ChatParticipant clickedOn) { - if (clickedOn.getUserName().equals(lobbyClient.getUserName())) { + private static List lobbyPlayerRightClickMenuActions( + JFrame parentWindow, + LoginResult loginResult, + ChatParticipant clickedOn, + PlayerToLobbyConnection playerToLobbyConnection) { + if (clickedOn.getUserName().equals(loginResult.getUsername())) { return List.of(); } - final var playerToLobbyConnection = lobbyClient.getPlayerToLobbyConnection(); - final var showPlayerInformationAction = ShowPlayerInformationAction.builder() - .parent(this) + .parent(parentWindow) .playerChatId(clickedOn.getPlayerChatId()) .playerName(clickedOn.getUserName()) .playerToLobbyConnection(playerToLobbyConnection) .build() .toSwingAction(); - if (!lobbyClient.isModerator()) { + if (!loginResult.isModerator()) { return List.of(showPlayerInformationAction); } return List.of( showPlayerInformationAction, MutePlayerAction.builder() - .parent(this) + .parent(parentWindow) .playerChatId(clickedOn.getPlayerChatId()) .playerToLobbyConnection(playerToLobbyConnection) .playerName(clickedOn.getUserName().getValue()) .build() .toSwingAction(), DisconnectPlayerModeratorAction.builder() - .parent(this) + .parent(parentWindow) .playerToLobbyConnection(playerToLobbyConnection) .playerChatId(clickedOn.getPlayerChatId()) .userName(clickedOn.getUserName()) .build() .toSwingAction(), BanPlayerModeratorAction.builder() - .parent(this) + .parent(parentWindow) .playerToLobbyConnection(playerToLobbyConnection) .playerChatIdToBan(clickedOn.getPlayerChatId()) .playerName(clickedOn.getUserName().getValue()) diff --git a/game-app/game-headed/src/main/java/games/strategy/engine/lobby/client/ui/LobbyGamePanel.java b/game-app/game-headed/src/main/java/games/strategy/engine/lobby/client/ui/LobbyGamePanel.java index 9b48d25aa3..045a22ad0f 100644 --- a/game-app/game-headed/src/main/java/games/strategy/engine/lobby/client/ui/LobbyGamePanel.java +++ b/game-app/game-headed/src/main/java/games/strategy/engine/lobby/client/ui/LobbyGamePanel.java @@ -2,7 +2,7 @@ import games.strategy.engine.framework.GameProcess; import games.strategy.engine.framework.startup.ui.ServerOptions; -import games.strategy.engine.lobby.client.LobbyClient; +import games.strategy.engine.lobby.client.login.LoginResult; import games.strategy.engine.lobby.client.ui.action.FetchChatHistory; import games.strategy.engine.lobby.client.ui.action.ShowPlayersAction; import games.strategy.triplea.settings.ClientSetting; @@ -26,6 +26,7 @@ import javax.swing.UIManager; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableRowSorter; +import org.triplea.http.client.web.socket.client.connections.PlayerToLobbyConnection; import org.triplea.lobby.common.GameDescription; import org.triplea.swing.MouseListenerBuilder; import org.triplea.swing.SwingAction; @@ -35,16 +36,19 @@ class LobbyGamePanel extends JPanel { private final JFrame parent; private final JButton joinGameButton; private final LobbyGameTableModel gameTableModel; - private final LobbyClient lobbyClient; + private final LoginResult loginResult; private final JTable gameTable; + private final PlayerToLobbyConnection playerToLobbyConnection; LobbyGamePanel( final JFrame parent, - final LobbyClient lobbyClient, - final LobbyGameTableModel lobbyGameTableModel) { + final LoginResult loginResult, + final LobbyGameTableModel lobbyGameTableModel, + final PlayerToLobbyConnection playerToLobbyConnection) { this.parent = parent; - this.lobbyClient = lobbyClient; + this.loginResult = loginResult; this.gameTableModel = lobbyGameTableModel; + this.playerToLobbyConnection = playerToLobbyConnection; final JButton hostGameButton = new JButton("Host Game"); joinGameButton = new JButton("Join Game"); @@ -104,7 +108,7 @@ public Component prepareRenderer( .getColumnModel() .getColumn(gameTableModel.getColumnIndex(LobbyGameTableModel.Column.P)) .setPreferredWidth(12); - if (lobbyClient.isModerator()) { + if (loginResult.isModerator()) { gameTable .getColumnModel() .getColumn(gameTableModel.getColumnIndex(LobbyGameTableModel.Column.Started)) @@ -182,12 +186,12 @@ private void mouseOnGamesList(final MouseEvent mouseEvent) { () -> gameTableModel.getGameListingForRow( gameTable.convertRowIndexToModel(gameTable.getSelectedRow()))) - .playerToLobbyConnection(lobbyClient.getPlayerToLobbyConnection()) + .playerToLobbyConnection(playerToLobbyConnection) .build() .buildSwingAction()) .forEach(menu::add); - if (lobbyClient.isModerator()) { + if (loginResult.isModerator()) { menu.addSeparator(); List.of( SwingAction.of("Show Chat History", e -> showChatHistory()), @@ -209,13 +213,13 @@ private void joinGame() { // we sort the table, so get the correct index final GameDescription description = gameTableModel.get(gameTable.convertRowIndexToModel(selectedIndex)); - GameProcess.joinGame(description, lobbyClient.getUserName()); + GameProcess.joinGame(description, loginResult.getUsername()); } private void hostGame() { final ServerOptions options = new ServerOptions( - JOptionPane.getFrameForComponent(this), lobbyClient.getUserName(), 3300, true); + JOptionPane.getFrameForComponent(this), loginResult.getUsername(), 3300, true); options.setLocationRelativeTo(JOptionPane.getFrameForComponent(this)); options.setNameEditable(false); options.setVisible(true); @@ -240,7 +244,7 @@ private void showChatHistory() { .parentWindow(parent) .gameId(gameTableModel.getGameIdForRow(gameTable.convertRowIndexToModel(selectedIndex))) .gameHostName(gameTableModel.get(selectedIndex).getHostedBy().getName()) - .playerToLobbyConnection(lobbyClient.getPlayerToLobbyConnection()) + .playerToLobbyConnection(playerToLobbyConnection) .build() .fetchAndShowChatHistory(); } @@ -282,7 +286,7 @@ private void shutdown() { final String gameId = gameTableModel.getGameIdForRow(gameTable.convertRowIndexToModel(selectedIndex)); - lobbyClient.getPlayerToLobbyConnection().sendShutdownRequest(gameId); + playerToLobbyConnection.sendShutdownRequest(gameId); JOptionPane.showMessageDialog(null, "The game you selected was sent a shutdown signal"); } } diff --git a/game-app/game-headed/src/main/java/games/strategy/triplea/ui/menubar/LobbyMenu.java b/game-app/game-headed/src/main/java/games/strategy/triplea/ui/menubar/LobbyMenu.java index d34c7b3b2a..c2c752508b 100644 --- a/game-app/game-headed/src/main/java/games/strategy/triplea/ui/menubar/LobbyMenu.java +++ b/game-app/game-headed/src/main/java/games/strategy/triplea/ui/menubar/LobbyMenu.java @@ -3,12 +3,14 @@ import games.strategy.engine.framework.system.SystemProperties; import games.strategy.engine.lobby.client.login.ChangeEmailPanel; import games.strategy.engine.lobby.client.login.ChangePasswordPanel; +import games.strategy.engine.lobby.client.login.LoginResult; import games.strategy.engine.lobby.client.ui.LobbyFrame; import games.strategy.engine.lobby.moderator.toolbox.ToolBoxWindow; import games.strategy.triplea.UrlConstants; import games.strategy.triplea.settings.ClientSetting; import games.strategy.triplea.ui.MacOsIntegration; import javax.swing.JMenuBar; +import org.triplea.http.client.web.socket.client.connections.PlayerToLobbyConnection; import org.triplea.sound.SoundOptions; import org.triplea.swing.JMenuBuilder; import org.triplea.swing.JMenuItemCheckBoxBuilder; @@ -20,7 +22,8 @@ public final class LobbyMenu extends JMenuBar { private final LobbyFrame lobbyFrame; - public LobbyMenu(final LobbyFrame frame) { + public LobbyMenu( + LobbyFrame frame, LoginResult loginResult, PlayerToLobbyConnection playerToLobbyConnection) { lobbyFrame = frame; // file only has one value, and on mac it is in the apple menu if (!SystemProperties.isMac()) { @@ -29,27 +32,25 @@ public LobbyMenu(final LobbyFrame frame) { MacOsIntegration.setQuitHandler(lobbyFrame); } - if (!lobbyFrame.getLobbyClient().isAnonymousLogin()) { + if (!loginResult.isAnonymousLogin()) { add( new JMenuBuilder("Account", 'A') .addMenuItem( "Update Email", 'E', - () -> - ChangeEmailPanel.promptUserForNewEmail( - frame, frame.getLobbyClient().getPlayerToLobbyConnection())) + () -> ChangeEmailPanel.promptUserForNewEmail(frame, playerToLobbyConnection)) .addMenuItem( "Update Password", 'P', () -> ChangePasswordPanel.doPasswordChange( frame, - frame.getLobbyClient().getPlayerToLobbyConnection(), + playerToLobbyConnection, ChangePasswordPanel.AllowCancelMode.SHOW_CANCEL_BUTTON)) .build()); } - if (lobbyFrame.getLobbyClient().isModerator()) { + if (loginResult.isModerator()) { add( new JMenuBuilder("Admin", 'M') .addMenuItem( @@ -57,11 +58,7 @@ public LobbyMenu(final LobbyFrame frame) { 'T', () -> ToolBoxWindow.showWindow( - lobbyFrame, - lobbyFrame - .getLobbyClient() - .getPlayerToLobbyConnection() - .getHttpModeratorToolboxClient())) + lobbyFrame, playerToLobbyConnection.getHttpModeratorToolboxClient())) .build()); }