mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-17 11:18:01 +00:00
- Move some files around in net code (plus some cleanup)
- Remove an unused method from IGuiGame
This commit is contained in:
1
.gitattributes
vendored
1
.gitattributes
vendored
@@ -17720,6 +17720,7 @@ forge-gui/src/main/java/forge/net/GameProtocol.java -text
|
||||
forge-gui/src/main/java/forge/net/GameProtocolHandler.java -text
|
||||
forge-gui/src/main/java/forge/net/GameProtocolSender.java -text
|
||||
forge-gui/src/main/java/forge/net/IRemote.java -text
|
||||
forge-gui/src/main/java/forge/net/ProtocolMethod.java -text
|
||||
forge-gui/src/main/java/forge/net/ReplyPool.java -text
|
||||
forge-gui/src/main/java/forge/net/client/ClientGameLobby.java -text
|
||||
forge-gui/src/main/java/forge/net/client/FGameClient.java -text
|
||||
|
||||
@@ -833,14 +833,6 @@ public final class CMatchUI
|
||||
return FOptionPane.showOptionDialog(message, title, icon == null ? null : FSkin.getImage(icon), options, defaultOption);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int showCardOptionDialog(final CardView card, final String message, final String title, final FSkinProp skinIcon, final String[] options, final int defaultOption) {
|
||||
if (card != null) {
|
||||
setCard(card);
|
||||
}
|
||||
return showOptionDialog(message, title, skinIcon, options, defaultOption);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String showInputDialog(final String message, final String title, final FSkinProp icon, final String initialInput, final String[] inputOptions) {
|
||||
return FOptionPane.showInputDialog(message, title, icon == null ? null : FSkin.getImage(icon), initialInput, inputOptions);
|
||||
|
||||
@@ -417,11 +417,6 @@ public class MatchController extends AbstractGuiGame {
|
||||
return SOptionPane.showOptionDialog(message, title, icon, options, defaultOption);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int showCardOptionDialog(final CardView card, final String message, final String title, final FSkinProp icon, final String[] options, final int defaultOption) {
|
||||
return FOptionPane.showCardOptionDialog(card, message, title, icon, options, defaultOption);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String showInputDialog(final String message, final String title, final FSkinProp icon, final String initialInput, final String[] inputOptions) {
|
||||
return SOptionPane.showInputDialog(message, title, icon, initialInput, inputOptions);
|
||||
|
||||
@@ -65,8 +65,6 @@ public interface IGuiGame {
|
||||
|
||||
int showOptionDialog(String message, String title, FSkinProp icon,
|
||||
String[] options, int defaultOption);
|
||||
int showCardOptionDialog(CardView card, String message, String title,
|
||||
FSkinProp icon, String[] options, int defaultOption);
|
||||
|
||||
String showInputDialog(String message, String title);
|
||||
String showInputDialog(String message, String title, FSkinProp icon);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package forge.net;
|
||||
|
||||
import forge.FThreads;
|
||||
import forge.net.GameProtocol.ProtocolMethod;
|
||||
import forge.net.event.GuiGameEvent;
|
||||
import forge.net.event.ReplyEvent;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
|
||||
@@ -2,7 +2,6 @@ package forge.net;
|
||||
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import forge.net.GameProtocol.ProtocolMethod;
|
||||
import forge.net.event.GuiGameEvent;
|
||||
|
||||
public final class GameProtocolSender {
|
||||
|
||||
167
forge-gui/src/main/java/forge/net/ProtocolMethod.java
Normal file
167
forge-gui/src/main/java/forge/net/ProtocolMethod.java
Normal file
@@ -0,0 +1,167 @@
|
||||
package forge.net;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
|
||||
import forge.assets.FSkinProp;
|
||||
import forge.deck.CardPool;
|
||||
import forge.game.GameEntityView;
|
||||
import forge.game.GameView;
|
||||
import forge.game.card.CardView;
|
||||
import forge.game.phase.PhaseType;
|
||||
import forge.game.player.DelayedReveal;
|
||||
import forge.game.player.PlayerView;
|
||||
import forge.game.spellability.SpellAbilityView;
|
||||
import forge.interfaces.IGameController;
|
||||
import forge.interfaces.IGuiGame;
|
||||
import forge.match.NextGameDecision;
|
||||
import forge.trackable.TrackableCollection;
|
||||
import forge.util.ITriggerEvent;
|
||||
import forge.util.ReflectionUtil;
|
||||
|
||||
/**
|
||||
* The methods that can be sent through this protocol.
|
||||
*/
|
||||
public enum ProtocolMethod {
|
||||
// Server -> Client
|
||||
setGameView (Mode.SERVER, Void.TYPE, GameView.class),
|
||||
openView (Mode.SERVER, Void.TYPE, TrackableCollection/*PlayerView*/.class),
|
||||
afterGameEnd (Mode.SERVER),
|
||||
showCombat (Mode.SERVER),
|
||||
showPromptMessage (Mode.SERVER, Void.TYPE, PlayerView.class, String.class),
|
||||
updateButtons (Mode.SERVER, Void.TYPE, PlayerView.class, String.class, String.class, Boolean.TYPE, Boolean.TYPE, Boolean.TYPE),
|
||||
flashIncorrectAction(Mode.SERVER),
|
||||
updatePhase (Mode.SERVER),
|
||||
updateTurn (Mode.SERVER, Void.TYPE, PlayerView.class),
|
||||
updatePlayerControl (Mode.SERVER),
|
||||
enableOverlay (Mode.SERVER),
|
||||
disableOverlay (Mode.SERVER),
|
||||
finishGame (Mode.SERVER),
|
||||
showManaPool (Mode.SERVER, Object.class, PlayerView.class),
|
||||
hideManaPool (Mode.SERVER, Void.TYPE, PlayerView.class),
|
||||
updateStack (Mode.SERVER),
|
||||
updateZones (Mode.SERVER, Void.TYPE, Iterable/*PlayerZoneUpdate*/.class),
|
||||
updateCards (Mode.SERVER, Void.TYPE, Iterable/*CardView*/.class),
|
||||
updateManaPool (Mode.SERVER, Void.TYPE, Iterable/*PlayerView*/.class),
|
||||
updateLives (Mode.SERVER, Void.TYPE, Iterable/*PlayerView*/.class),
|
||||
setPanelSelection (Mode.SERVER, Void.TYPE, CardView.class),
|
||||
getAbilityToPlay (Mode.SERVER, SpellAbilityView.class, CardView.class, List/*SpellAbilityView*/.class, ITriggerEvent.class),
|
||||
assignDamage (Mode.SERVER, Map.class, CardView.class, List/*CardView*/.class, Integer.TYPE, GameEntityView.class, Boolean.TYPE),
|
||||
message (Mode.SERVER, Void.TYPE, String.class, String.class),
|
||||
showErrorDialog (Mode.SERVER, Void.TYPE, String.class, String.class),
|
||||
showConfirmDialog (Mode.SERVER, Boolean.TYPE, String.class, String.class, String.class, String.class, Boolean.TYPE),
|
||||
showOptionDialog (Mode.SERVER, Integer.TYPE, String.class, String.class, FSkinProp.class, Array/*String*/.class, Integer.TYPE),
|
||||
showInputDialog (Mode.SERVER, String.class, String.class, String.class, FSkinProp.class, String.class, Array/*String*/.class),
|
||||
confirm (Mode.SERVER, Boolean.TYPE, CardView.class, String.class, Boolean.TYPE, Array/*String*/.class),
|
||||
getChoices (Mode.SERVER, List.class, String.class, Integer.TYPE, Integer.TYPE, Collection.class, Object.class, Function.class),
|
||||
order (Mode.SERVER, List.class, String.class, String.class, Integer.TYPE, Integer.TYPE, List.class, List.class, CardView.class, Boolean.TYPE),
|
||||
sideboard (Mode.SERVER, List.class, CardPool.class, CardPool.class),
|
||||
chooseSingleEntityForEffect(Mode.SERVER, GameEntityView.class, String.class, TrackableCollection.class, DelayedReveal.class, Boolean.TYPE),
|
||||
setCard (Mode.SERVER, Void.TYPE, CardView.class),
|
||||
// TODO case "setPlayerAvatar":
|
||||
openZones (Mode.SERVER, Boolean.TYPE, Collection/*ZoneType*/.class, Map/*PlayerView,Object*/.class),
|
||||
restoreOldZones (Mode.SERVER, Void.TYPE, Map/*PlayerView,Object*/.class),
|
||||
isUiSetToSkipPhase (Mode.SERVER, Boolean.TYPE, PlayerView.class, PhaseType.class),
|
||||
|
||||
// Client -> Server
|
||||
// Note: these should all return void, to avoid awkward situations in
|
||||
// which client and server wait for one another's response and block
|
||||
// the threads that're supposed to give that response
|
||||
useMana (Mode.CLIENT, Void.TYPE, Byte.TYPE),
|
||||
undoLastAction (Mode.CLIENT, Void.TYPE, Boolean.TYPE),
|
||||
selectPlayer (Mode.CLIENT, Void.TYPE, PlayerView.class, ITriggerEvent.class),
|
||||
selectCard (Mode.CLIENT, Void.TYPE, CardView.class, List.class, ITriggerEvent.class),
|
||||
selectButtonOk (Mode.CLIENT),
|
||||
selectButtonCancel (Mode.CLIENT),
|
||||
selectAbility (Mode.CLIENT, Void.TYPE, SpellAbilityView.class),
|
||||
passPriorityUntilEndOfTurn(Mode.CLIENT),
|
||||
passPriority (Mode.CLIENT),
|
||||
nextGameDecision (Mode.CLIENT, Void.TYPE, NextGameDecision.class),
|
||||
getActivateDescription (Mode.CLIENT, Void.TYPE, String.class, CardView.class),
|
||||
concede (Mode.CLIENT),
|
||||
alphaStrike (Mode.CLIENT),
|
||||
reorderHand (Mode.CLIENT, Void.TYPE, CardView.class, Integer.TYPE);
|
||||
|
||||
private enum Mode {
|
||||
SERVER(IGuiGame.class),
|
||||
CLIENT(IGameController.class);
|
||||
|
||||
private final Class<?> toInvoke;
|
||||
private Mode(final Class<?> toInvoke) {
|
||||
this.toInvoke = toInvoke;
|
||||
}
|
||||
};
|
||||
|
||||
private final ProtocolMethod.Mode mode;
|
||||
private final Class<?> returnType;
|
||||
private final Class<?>[] args;
|
||||
|
||||
private ProtocolMethod(final ProtocolMethod.Mode mode) {
|
||||
this(mode, Void.TYPE);
|
||||
}
|
||||
private ProtocolMethod(final ProtocolMethod.Mode mode, final Class<?> returnType) {
|
||||
this(mode, returnType, (Class<?>[]) null);
|
||||
}
|
||||
@SafeVarargs
|
||||
private ProtocolMethod(final ProtocolMethod.Mode mode, final Class<?> returnType, final Class<?> ... args) {
|
||||
this.mode = mode;
|
||||
this.returnType = returnType;
|
||||
this.args = args == null ? new Class<?>[] {} : args;
|
||||
}
|
||||
|
||||
public Method getMethod() {
|
||||
try {
|
||||
final Class<?> toCall = mode.toInvoke;
|
||||
final Method candidate = toCall.getMethod(name(), args);
|
||||
// Don't check Client return values for now as some use void
|
||||
// and a default return value, to improve performance
|
||||
if (mode == Mode.SERVER && !candidate.getReturnType().equals(returnType)) {
|
||||
throw new NoSuchMethodException(String.format("Wrong return type for method %s", name()));
|
||||
}
|
||||
return candidate;
|
||||
} catch (final NoSuchMethodException | SecurityException e) {
|
||||
System.err.println(String.format("Warning: class contains no accessible method named %s", name()));
|
||||
return getMethodNoArgs();
|
||||
}
|
||||
}
|
||||
|
||||
private Method getMethodNoArgs() {
|
||||
try {
|
||||
return mode.toInvoke.getMethod(name(), (Class<?>[]) null);
|
||||
} catch (final NoSuchMethodException | SecurityException e) {
|
||||
System.err.println(String.format("Warning: class contains no accessible arg-less method named %s", name()));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public Class<?> getReturnType() {
|
||||
return returnType;
|
||||
}
|
||||
public Class<?>[] getArgTypes() {
|
||||
return args;
|
||||
}
|
||||
|
||||
public void checkArgs(final Object[] args) {
|
||||
for (int iArg = 0; iArg < args.length; iArg++) {
|
||||
final Object arg = args[iArg];
|
||||
final Class<?> type = this.args[iArg];
|
||||
if (!ReflectionUtil.isInstance(arg, type)) {
|
||||
throw new InternalError(String.format("Protocol method %s: illegal argument (%d) of type %s, %s expected", name(), iArg, arg.getClass().getName(), type.getName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void checkReturnValue(final Object value) {
|
||||
if (returnType.equals(Void.TYPE)) {
|
||||
// If void is expected, any return value is fine
|
||||
return;
|
||||
}
|
||||
if (!ReflectionUtil.isInstance(value, returnType)) {
|
||||
throw new IllegalStateException(String.format("Protocol method %s: illegal return object type %s returned by client, expected %s", name(), value.getClass().getName(), getReturnType().getName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,9 +4,9 @@ import io.netty.channel.ChannelHandlerContext;
|
||||
import forge.game.player.PlayerView;
|
||||
import forge.interfaces.IGuiGame;
|
||||
import forge.model.FModel;
|
||||
import forge.net.GameProtocol.ProtocolMethod;
|
||||
import forge.net.GameProtocolHandler;
|
||||
import forge.net.IRemote;
|
||||
import forge.net.ProtocolMethod;
|
||||
import forge.net.ReplyPool;
|
||||
import forge.net.event.LoginEvent;
|
||||
import forge.properties.ForgePreferences.FPref;
|
||||
|
||||
@@ -8,8 +8,8 @@ import forge.game.spellability.SpellAbilityView;
|
||||
import forge.interfaces.IDevModeCheats;
|
||||
import forge.interfaces.IGameController;
|
||||
import forge.match.NextGameDecision;
|
||||
import forge.net.GameProtocol.ProtocolMethod;
|
||||
import forge.net.GameProtocolSender;
|
||||
import forge.net.ProtocolMethod;
|
||||
import forge.util.ITriggerEvent;
|
||||
|
||||
public class NetGameController implements IGameController {
|
||||
@@ -87,6 +87,7 @@ public class NetGameController implements IGameController {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getActivateDescription(final CardView card) {
|
||||
return sendAndWait(ProtocolMethod.getActivateDescription, card);
|
||||
}
|
||||
@@ -114,7 +115,7 @@ public class NetGameController implements IGameController {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reorderHand(CardView card, int index) {
|
||||
public void reorderHand(final CardView card, final int index) {
|
||||
send(ProtocolMethod.reorderHand, card, Integer.valueOf(index));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package forge.net.event;
|
||||
|
||||
import forge.net.GameProtocol.ProtocolMethod;
|
||||
import forge.net.ProtocolMethod;
|
||||
import forge.net.server.RemoteClient;
|
||||
|
||||
public final class GuiGameEvent implements IdentifiableNetEvent {
|
||||
|
||||
@@ -10,6 +10,7 @@ public class LogoutEvent implements NetEvent {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateForClient(final RemoteClient client) {
|
||||
}
|
||||
|
||||
|
||||
@@ -13,12 +13,15 @@ public final class MessageEvent implements NetEvent {
|
||||
this.source = source;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateForClient(final RemoteClient client) {
|
||||
}
|
||||
|
||||
public String getSource() {
|
||||
return source;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
@@ -47,6 +47,7 @@ public final class UpdateLobbyPlayerEvent implements NetEvent {
|
||||
this.aiOptions = aiOptions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateForClient(final RemoteClient client) {
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package forge.net.server;
|
||||
|
||||
import forge.GuiBase;
|
||||
import forge.game.GameView;
|
||||
import forge.interfaces.IGameController;
|
||||
import forge.interfaces.IGuiGame;
|
||||
import forge.interfaces.ILobbyListener;
|
||||
@@ -71,16 +70,13 @@ public final class FServerManager {
|
||||
RemoteClient getClient(final Channel ch) {
|
||||
return clients.get(ch);
|
||||
}
|
||||
GameView getGameView() {
|
||||
return localLobby.getGameView();
|
||||
}
|
||||
IGameController getController(final int index) {
|
||||
return localLobby.getController(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the singleton instance of {@link FServerManager}.
|
||||
*
|
||||
*
|
||||
* @return the singleton FServerManager.
|
||||
*/
|
||||
public static FServerManager getInstance() {
|
||||
@@ -94,10 +90,10 @@ public final class FServerManager {
|
||||
public void startServer(final int port) {
|
||||
try {
|
||||
final ServerBootstrap b = new ServerBootstrap()
|
||||
.group(bossGroup, workerGroup)
|
||||
.channel(NioServerSocketChannel.class)
|
||||
.handler(new LoggingHandler(LogLevel.INFO))
|
||||
.childHandler(new ChannelInitializer<SocketChannel>() {
|
||||
.group(bossGroup, workerGroup)
|
||||
.channel(NioServerSocketChannel.class)
|
||||
.handler(new LoggingHandler(LogLevel.INFO))
|
||||
.childHandler(new ChannelInitializer<SocketChannel>() {
|
||||
@Override public final void initChannel(final SocketChannel ch) {
|
||||
final ChannelPipeline p = ch.pipeline();
|
||||
p.addLast(
|
||||
@@ -109,7 +105,7 @@ public final class FServerManager {
|
||||
new DeregisterClientHandler(),
|
||||
new GameServerHandler());
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Bind and start to accept incoming connections.
|
||||
final ChannelFuture ch = b.bind(port).sync().channel().closeFuture();
|
||||
@@ -277,7 +273,7 @@ public final class FServerManager {
|
||||
|
||||
private class DeregisterClientHandler extends ChannelInboundHandlerAdapter {
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
public void channelInactive(final ChannelHandlerContext ctx) throws Exception {
|
||||
final RemoteClient client = clients.remove(ctx.channel());
|
||||
localLobby.disconnectPlayer(client.getIndex());
|
||||
broadcast(new LogoutEvent(client.getUsername()));
|
||||
|
||||
@@ -2,9 +2,9 @@ package forge.net.server;
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import forge.interfaces.IGameController;
|
||||
import forge.net.GameProtocol.ProtocolMethod;
|
||||
import forge.net.GameProtocolHandler;
|
||||
import forge.net.IRemote;
|
||||
import forge.net.ProtocolMethod;
|
||||
import forge.net.ReplyPool;
|
||||
|
||||
final class GameServerHandler extends GameProtocolHandler<IGameController> {
|
||||
|
||||
@@ -20,8 +20,8 @@ import forge.game.spellability.SpellAbilityView;
|
||||
import forge.game.zone.ZoneType;
|
||||
import forge.item.PaperCard;
|
||||
import forge.match.AbstractGuiGame;
|
||||
import forge.net.GameProtocol.ProtocolMethod;
|
||||
import forge.net.GameProtocolSender;
|
||||
import forge.net.ProtocolMethod;
|
||||
import forge.player.PlayerZoneUpdate;
|
||||
import forge.trackable.TrackableCollection;
|
||||
import forge.util.ITriggerEvent;
|
||||
@@ -193,11 +193,6 @@ public class NetGuiGame extends AbstractGuiGame {
|
||||
return sendAndWait(ProtocolMethod.showOptionDialog, message, title, icon, options, defaultOption);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int showCardOptionDialog(final CardView card, final String message, final String title, final FSkinProp icon, final String[] options, final int defaultOption) {
|
||||
return sendAndWait(ProtocolMethod.showCardOptionDialog, card, message, title, icon, options, defaultOption);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String showInputDialog(final String message, final String title, final FSkinProp icon, final String initialInput, final String[] inputOptions) {
|
||||
return sendAndWait(ProtocolMethod.showInputDialog, message, title, icon, initialInput, inputOptions);
|
||||
|
||||
Reference in New Issue
Block a user