Skip to content

Commit

Permalink
Simplify: merge class 'LobbyClient' into 'LoginResult'
Browse files Browse the repository at this point in the history
This update replaces any usage of LobbyClient with LoginResult.
There is a lot of overlap between the two classes, for the most
part it is a 1:1 replacement.

LobbyClient itself has some issues, it's certainly more of a
result object and has a lot of overlap with another result object
named 'LoginResult'.
  • Loading branch information
DanVanAtta committed Dec 2, 2023
1 parent d9fa57d commit 39a00a2
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -99,43 +107,45 @@ public void windowClosing(final WindowEvent e) {
});
}

private List<Action> lobbyPlayerRightClickMenuActions(final ChatParticipant clickedOn) {
if (clickedOn.getUserName().equals(lobbyClient.getUserName())) {
private static List<Action> 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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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");
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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()),
Expand All @@ -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);
Expand All @@ -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();
}
Expand Down Expand Up @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()) {
Expand All @@ -29,39 +32,33 @@ 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(
"Open Toolbox",
'T',
() ->
ToolBoxWindow.showWindow(
lobbyFrame,
lobbyFrame
.getLobbyClient()
.getPlayerToLobbyConnection()
.getHttpModeratorToolboxClient()))
lobbyFrame, playerToLobbyConnection.getHttpModeratorToolboxClient()))
.build());
}

Expand Down

0 comments on commit 39a00a2

Please sign in to comment.