From d086e4d6926ef79aaf432859433bcbc04681e657 Mon Sep 17 00:00:00 2001 From: "Jamin W. Collins" Date: Sun, 18 Feb 2018 19:50:03 -0700 Subject: [PATCH] cleanup remote client game state creation Signed-off-by: Jamin W. Collins --- .../forge/net/client/GameClientHandler.java | 66 +++++++++++-------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/forge-gui/src/main/java/forge/net/client/GameClientHandler.java b/forge-gui/src/main/java/forge/net/client/GameClientHandler.java index f15a4576bc5..08955a4d2c1 100644 --- a/forge-gui/src/main/java/forge/net/client/GameClientHandler.java +++ b/forge-gui/src/main/java/forge/net/client/GameClientHandler.java @@ -27,6 +27,8 @@ final class GameClientHandler extends GameProtocolHandler { private final FGameClient client; private final IGuiGame gui; private Tracker tracker; + private Match match; + private Game game; /** * Creates a client-side game handler. @@ -36,6 +38,8 @@ final class GameClientHandler extends GameProtocolHandler { this.client = client; this.gui = client.getGui(); this.tracker = null; + this.match = null; + this.game = null; } @Override @@ -58,32 +62,26 @@ final class GameClientHandler extends GameProtocolHandler { protected void beforeCall(final ProtocolMethod protocolMethod, final Object[] args) { switch (protocolMethod) { case openView: - if (this.tracker == null) { - int maxAttempts = 5; - for (int numAttempts = 0; numAttempts < maxAttempts; numAttempts++) { - try { + // only need one **match** + if (this.match == null) { + this.match = createMatch(); + } - this.tracker = createTracker(); + // openView is called **once** per game, for now create a new Game instance each time + this.game = createGame(); - for (PlayerView myPlayer : (TrackableCollection) args[0]) { - if (myPlayer.getTracker() == null) { - myPlayer.setTracker(this.tracker); - } - } + // get a tracker + this.tracker = createTracker(); - final TrackableCollection myPlayers = (TrackableCollection) args[0]; - client.setGameControllers(myPlayers); - - } catch (Exception e) { - System.err.println("Failed: attempt number: " + numAttempts + " - " + e.toString()); - try { - Thread.sleep(100); - } catch (InterruptedException e1) { - e1.printStackTrace(); - } - } + for (PlayerView myPlayer : (TrackableCollection) args[0]) { + if (myPlayer.getTracker() == null) { + myPlayer.setTracker(this.tracker); } } + + final TrackableCollection myPlayers = (TrackableCollection) args[0]; + client.setGameControllers(myPlayers); + break; default: break; @@ -108,19 +106,17 @@ final class GameClientHandler extends GameProtocolHandler { } /** - * This method creates the necessary objects and state to retrieve a Tracker object. - * - * Near as I can tell, that means that we need to create a Match. + * This method retrieves enough of the existing (incomplete) game state to + * recreate a new viable Match object * * Creating a Match requires that we have: * * GameRules * * RegisteredPlayers * * Title * - * @return Tracker + * @return Match */ - private Tracker createTracker() { - + private Match createMatch() { // retrieve what we can from the existing (but incomplete) state final IGuiGame gui = client.getGui(); GameView gameView = gui.getGameView(); @@ -134,12 +130,24 @@ final class GameClientHandler extends GameProtocolHandler { // create a valid match object and game Match match = new Match(gameRules, registeredPlayers, title); - Game game = match.createGame(); + return match; + } + + private Game createGame() { + this.tracker = null; + return this.match.createGame(); + } + + /** + * Ensure the stored GameView is correct and retrieve a Tracker object. + * + * @return Tracker + */ + private Tracker createTracker() { // replace the existing incomplete GameView with the newly created one gui.setGameView(null); gui.setGameView(game.getView()); - return gui.getGameView().getTracker(); }