mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-20 12:48:00 +00:00
Refactor net connection logic to be reusable by app
This commit is contained in:
@@ -18,6 +18,7 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import forge.Singletons;
|
||||
import forge.gui.framework.SDisplayUtil;
|
||||
import forge.model.FModel;
|
||||
import forge.net.IOnlineChatInterface;
|
||||
import forge.net.IRemote;
|
||||
import forge.net.event.MessageEvent;
|
||||
import forge.properties.ForgePreferences;
|
||||
@@ -34,7 +35,7 @@ import forge.view.FDialog;
|
||||
import forge.view.FFrame;
|
||||
|
||||
|
||||
public enum FNetOverlay {
|
||||
public enum FNetOverlay implements IOnlineChatInterface {
|
||||
SINGLETON_INSTANCE;
|
||||
|
||||
private static final String COORD_DELIM = ",";
|
||||
|
||||
@@ -35,8 +35,8 @@ import forge.deckchooser.FDeckChooser;
|
||||
import forge.game.GameType;
|
||||
import forge.game.card.CardView;
|
||||
import forge.gui.CardDetailPanel;
|
||||
import forge.interfaces.ILobbyView;
|
||||
import forge.interfaces.IPlayerChangeListener;
|
||||
import forge.interfaces.IUpdateable;
|
||||
import forge.item.PaperCard;
|
||||
import forge.match.GameLobby;
|
||||
import forge.match.LobbySlot;
|
||||
@@ -66,7 +66,7 @@ import forge.util.storage.IStorage;
|
||||
*
|
||||
* <br><br><i>(V at beginning of class name denotes a view class.)</i>
|
||||
*/
|
||||
public class VLobby implements IUpdateable {
|
||||
public class VLobby implements ILobbyView {
|
||||
|
||||
static final int MAX_PLAYERS = 8;
|
||||
private static final ForgePreferences prefs = FModel.getPreferences();
|
||||
|
||||
@@ -6,40 +6,18 @@ import java.util.List;
|
||||
import javax.swing.JMenu;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import forge.FThreads;
|
||||
import forge.GuiBase;
|
||||
import forge.assets.FSkinProp;
|
||||
import forge.gui.FNetOverlay;
|
||||
import forge.gui.SOverlayUtils;
|
||||
import forge.gui.framework.EDocID;
|
||||
import forge.gui.framework.ICDoc;
|
||||
import forge.interfaces.IGuiGame;
|
||||
import forge.interfaces.ILobbyListener;
|
||||
import forge.interfaces.IPlayerChangeListener;
|
||||
import forge.interfaces.IUpdateable;
|
||||
import forge.match.GameLobby.GameLobbyData;
|
||||
import forge.menus.IMenuProvider;
|
||||
import forge.menus.MenuUtil;
|
||||
import forge.model.FModel;
|
||||
import forge.net.IRemote;
|
||||
import forge.net.client.ClientGameLobby;
|
||||
import forge.net.client.FGameClient;
|
||||
import forge.net.event.IdentifiableNetEvent;
|
||||
import forge.net.event.MessageEvent;
|
||||
import forge.net.event.NetEvent;
|
||||
import forge.net.event.UpdateLobbyPlayerEvent;
|
||||
import forge.net.server.FServerManager;
|
||||
import forge.net.server.ServerGameLobby;
|
||||
import forge.player.GamePlayerUtil;
|
||||
import forge.properties.ForgePreferences.FPref;
|
||||
import forge.properties.ForgeProfileProperties;
|
||||
import forge.net.NetConnectUtil;
|
||||
import forge.screens.home.CHomeUI;
|
||||
import forge.screens.home.CLobby;
|
||||
import forge.screens.home.VLobby;
|
||||
import forge.screens.home.sanctioned.ConstructedGameMenu;
|
||||
import forge.util.gui.SOptionPane;
|
||||
|
||||
public enum CSubmenuOnlineLobby implements ICDoc, IMenuProvider {
|
||||
SINGLETON_INSTANCE;
|
||||
@@ -52,14 +30,9 @@ public enum CSubmenuOnlineLobby implements ICDoc, IMenuProvider {
|
||||
}
|
||||
|
||||
void connectToServer() {
|
||||
final String url = SOptionPane.showInputDialog("Enter URL of server to join. Leave blank to host your own server.", "Connect to Server");
|
||||
final String url = NetConnectUtil.getServerUrl();
|
||||
if (url == null) { return; }
|
||||
|
||||
//prompt user for player one name if needed
|
||||
if (StringUtils.isBlank(FModel.getPreferences().getPref(FPref.PLAYER_NAME))) {
|
||||
GamePlayerUtil.setPlayerName();
|
||||
}
|
||||
|
||||
FThreads.invokeInBackgroundThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
@@ -82,66 +55,13 @@ public enum CSubmenuOnlineLobby implements ICDoc, IMenuProvider {
|
||||
}
|
||||
});
|
||||
|
||||
final int port = ForgeProfileProperties.getServerPort();
|
||||
final FServerManager server = FServerManager.getInstance();
|
||||
final ServerGameLobby lobby = new ServerGameLobby();
|
||||
final VLobby view = VSubmenuOnlineLobby.SINGLETON_INSTANCE.setLobby(lobby);
|
||||
|
||||
server.startServer(port);
|
||||
server.setLobby(lobby);
|
||||
|
||||
lobby.setListener(new IUpdateable() {
|
||||
@Override
|
||||
public final void update(final boolean fullUpdate) {
|
||||
view.update(fullUpdate);
|
||||
server.updateLobbyState();
|
||||
}
|
||||
});
|
||||
view.setPlayerChangeListener(new IPlayerChangeListener() {
|
||||
@Override
|
||||
public final void update(final int index, final UpdateLobbyPlayerEvent event) {
|
||||
server.updateSlot(index, event);
|
||||
server.updateLobbyState();
|
||||
}
|
||||
});
|
||||
|
||||
server.setLobbyListener(new ILobbyListener() {
|
||||
@Override
|
||||
public final void update(final GameLobbyData state, final int slot) {
|
||||
// NO-OP, lobby connected directly
|
||||
}
|
||||
@Override
|
||||
public final void message(final String source, final String message) {
|
||||
FNetOverlay.SINGLETON_INSTANCE.addMessage(source, message);
|
||||
}
|
||||
@Override
|
||||
public final void close() {
|
||||
// NO-OP, server can't receive close message
|
||||
}
|
||||
});
|
||||
FNetOverlay.SINGLETON_INSTANCE.setGameClient(new IRemote() {
|
||||
@Override
|
||||
public final void send(final NetEvent event) {
|
||||
if (event instanceof MessageEvent) {
|
||||
final MessageEvent message = (MessageEvent) event;
|
||||
FNetOverlay.SINGLETON_INSTANCE.addMessage(message.getSource(), message.getMessage());
|
||||
server.broadcast(event);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public final Object sendAndWait(final IdentifiableNetEvent event) {
|
||||
send(event);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
view.update(true);
|
||||
final String result = NetConnectUtil.host(VSubmenuOnlineLobby.SINGLETON_INSTANCE, FNetOverlay.SINGLETON_INSTANCE);
|
||||
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
SOverlayUtils.hideOverlay();
|
||||
FNetOverlay.SINGLETON_INSTANCE.show(String.format("Hosting on port %d", port));
|
||||
FNetOverlay.SINGLETON_INSTANCE.show(result);
|
||||
if (CHomeUI.SINGLETON_INSTANCE.getCurrentDocID() == EDocID.HOME_NETWORK) {
|
||||
VSubmenuOnlineLobby.SINGLETON_INSTANCE.populate();
|
||||
}
|
||||
@@ -158,61 +78,13 @@ public enum CSubmenuOnlineLobby implements ICDoc, IMenuProvider {
|
||||
}
|
||||
});
|
||||
|
||||
final IGuiGame gui = GuiBase.getInterface().getNewGuiGame();
|
||||
final FGameClient client = new FGameClient(FModel.getPreferences().getPref(FPref.PLAYER_NAME), "0", gui);
|
||||
VSubmenuOnlineLobby.SINGLETON_INSTANCE.setClient(client);
|
||||
FNetOverlay.SINGLETON_INSTANCE.setGameClient(client);
|
||||
final ClientGameLobby lobby = new ClientGameLobby();
|
||||
final VLobby view = VSubmenuOnlineLobby.SINGLETON_INSTANCE.setLobby(lobby);
|
||||
lobby.setListener(view);
|
||||
client.addLobbyListener(new ILobbyListener() {
|
||||
@Override
|
||||
public final void message(final String source, final String message) {
|
||||
FNetOverlay.SINGLETON_INSTANCE.addMessage(source, message);
|
||||
}
|
||||
@Override
|
||||
public final void update(final GameLobbyData state, final int slot) {
|
||||
lobby.setLocalPlayer(slot);
|
||||
lobby.setData(state);
|
||||
}
|
||||
@Override
|
||||
public final void close() {
|
||||
SOptionPane.showMessageDialog("Connection to the host was interrupted.", "Error", FSkinProp.ICO_WARNING);
|
||||
VSubmenuOnlineLobby.SINGLETON_INSTANCE.setClient(null);
|
||||
}
|
||||
});
|
||||
view.setPlayerChangeListener(new IPlayerChangeListener() {
|
||||
@Override
|
||||
public final void update(final int index, final UpdateLobbyPlayerEvent event) {
|
||||
client.send(event);
|
||||
}
|
||||
});
|
||||
|
||||
final String hostname;
|
||||
int port0 = ForgeProfileProperties.getServerPort();
|
||||
|
||||
//see if port specified in URL
|
||||
int index = url.indexOf(':');
|
||||
if (index >= 0) {
|
||||
hostname = url.substring(0, index);
|
||||
String portStr = url.substring(index + 1);
|
||||
try {
|
||||
port0 = Integer.parseInt(portStr);
|
||||
}
|
||||
catch (Exception ex) {}
|
||||
}
|
||||
else {
|
||||
hostname = url;
|
||||
}
|
||||
final int port = port0;
|
||||
|
||||
client.connect(hostname, port);
|
||||
final String result = NetConnectUtil.join(url, VSubmenuOnlineLobby.SINGLETON_INSTANCE, FNetOverlay.SINGLETON_INSTANCE);
|
||||
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
SOverlayUtils.hideOverlay();
|
||||
FNetOverlay.SINGLETON_INSTANCE.show(String.format("Connected to %s:%d", hostname, port));
|
||||
FNetOverlay.SINGLETON_INSTANCE.show(result);
|
||||
if (CHomeUI.SINGLETON_INSTANCE.getCurrentDocID() == EDocID.HOME_NETWORK) {
|
||||
VSubmenuOnlineLobby.SINGLETON_INSTANCE.populate();
|
||||
}
|
||||
|
||||
@@ -15,7 +15,9 @@ import forge.gui.framework.DragTab;
|
||||
import forge.gui.framework.EDocID;
|
||||
import forge.gui.framework.FScreen;
|
||||
import forge.gui.framework.IVTopLevelUI;
|
||||
import forge.interfaces.ILobbyView;
|
||||
import forge.match.GameLobby;
|
||||
import forge.net.IOnlineLobby;
|
||||
import forge.net.client.FGameClient;
|
||||
import forge.net.server.FServerManager;
|
||||
import forge.screens.home.EMenuGroup;
|
||||
@@ -26,7 +28,7 @@ import forge.toolbox.FButton;
|
||||
import forge.toolbox.FSkin;
|
||||
import forge.util.gui.SOptionPane;
|
||||
|
||||
public enum VSubmenuOnlineLobby implements IVSubmenu<CSubmenuOnlineLobby>, IVTopLevelUI {
|
||||
public enum VSubmenuOnlineLobby implements IVSubmenu<CSubmenuOnlineLobby>, IOnlineLobby, IVTopLevelUI {
|
||||
SINGLETON_INSTANCE;
|
||||
|
||||
private DragCell parentCell;
|
||||
@@ -37,13 +39,13 @@ public enum VSubmenuOnlineLobby implements IVSubmenu<CSubmenuOnlineLobby>, IVTop
|
||||
private VSubmenuOnlineLobby() {
|
||||
}
|
||||
|
||||
VLobby setLobby(final GameLobby lobby) {
|
||||
public ILobbyView setLobby(final GameLobby lobby) {
|
||||
this.lobby = new VLobby(lobby);
|
||||
getLayoutControl().setLobby(this.lobby);
|
||||
return this.lobby;
|
||||
}
|
||||
|
||||
void setClient(final FGameClient client) {
|
||||
public void setClient(final FGameClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user