cleanup remote client game state creation

Signed-off-by: Jamin W. Collins <jamin.collins@gmail.com>
This commit is contained in:
Jamin W. Collins
2018-02-18 19:50:03 -07:00
parent 8d2d66abb9
commit d086e4d692

View File

@@ -27,6 +27,8 @@ final class GameClientHandler extends GameProtocolHandler<IGuiGame> {
private final FGameClient client; private final FGameClient client;
private final IGuiGame gui; private final IGuiGame gui;
private Tracker tracker; private Tracker tracker;
private Match match;
private Game game;
/** /**
* Creates a client-side game handler. * Creates a client-side game handler.
@@ -36,6 +38,8 @@ final class GameClientHandler extends GameProtocolHandler<IGuiGame> {
this.client = client; this.client = client;
this.gui = client.getGui(); this.gui = client.getGui();
this.tracker = null; this.tracker = null;
this.match = null;
this.game = null;
} }
@Override @Override
@@ -58,11 +62,15 @@ final class GameClientHandler extends GameProtocolHandler<IGuiGame> {
protected void beforeCall(final ProtocolMethod protocolMethod, final Object[] args) { protected void beforeCall(final ProtocolMethod protocolMethod, final Object[] args) {
switch (protocolMethod) { switch (protocolMethod) {
case openView: case openView:
if (this.tracker == null) { // only need one **match**
int maxAttempts = 5; if (this.match == null) {
for (int numAttempts = 0; numAttempts < maxAttempts; numAttempts++) { this.match = createMatch();
try { }
// openView is called **once** per game, for now create a new Game instance each time
this.game = createGame();
// get a tracker
this.tracker = createTracker(); this.tracker = createTracker();
for (PlayerView myPlayer : (TrackableCollection<PlayerView>) args[0]) { for (PlayerView myPlayer : (TrackableCollection<PlayerView>) args[0]) {
@@ -74,16 +82,6 @@ final class GameClientHandler extends GameProtocolHandler<IGuiGame> {
final TrackableCollection<PlayerView> myPlayers = (TrackableCollection<PlayerView>) args[0]; final TrackableCollection<PlayerView> myPlayers = (TrackableCollection<PlayerView>) args[0];
client.setGameControllers(myPlayers); client.setGameControllers(myPlayers);
} catch (Exception e) {
System.err.println("Failed: attempt number: " + numAttempts + " - " + e.toString());
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
break; break;
default: default:
break; break;
@@ -108,19 +106,17 @@ final class GameClientHandler extends GameProtocolHandler<IGuiGame> {
} }
/** /**
* This method creates the necessary objects and state to retrieve a <b>Tracker</b> object. * This method retrieves enough of the existing (incomplete) game state to
* * recreate a new viable Match object
* Near as I can tell, that means that we need to create a <b>Match</b>.
* *
* Creating a <b>Match</b> requires that we have: * Creating a <b>Match</b> requires that we have:
* * <b>GameRules</b> * * <b>GameRules</b>
* * <b>RegisteredPlayers</b> * * <b>RegisteredPlayers</b>
* * Title * * Title
* *
* @return Tracker * @return Match
*/ */
private Tracker createTracker() { private Match createMatch() {
// retrieve what we can from the existing (but incomplete) state // retrieve what we can from the existing (but incomplete) state
final IGuiGame gui = client.getGui(); final IGuiGame gui = client.getGui();
GameView gameView = gui.getGameView(); GameView gameView = gui.getGameView();
@@ -134,12 +130,24 @@ final class GameClientHandler extends GameProtocolHandler<IGuiGame> {
// create a valid match object and game // create a valid match object and game
Match match = new Match(gameRules, registeredPlayers, title); 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 <b>Tracker</b> object.
*
* @return Tracker
*/
private Tracker createTracker() {
// replace the existing incomplete GameView with the newly created one // replace the existing incomplete GameView with the newly created one
gui.setGameView(null); gui.setGameView(null);
gui.setGameView(game.getView()); gui.setGameView(game.getView());
return gui.getGameView().getTracker(); return gui.getGameView().getTracker();
} }