mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-19 20:28:00 +00:00
cleanup remote client game state creation
Signed-off-by: Jamin W. Collins <jamin.collins@gmail.com>
This commit is contained in:
@@ -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,32 +62,26 @@ 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 {
|
}
|
||||||
|
|
||||||
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<PlayerView>) args[0]) {
|
// get a tracker
|
||||||
if (myPlayer.getTracker() == null) {
|
this.tracker = createTracker();
|
||||||
myPlayer.setTracker(this.tracker);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
final TrackableCollection<PlayerView> myPlayers = (TrackableCollection<PlayerView>) args[0];
|
for (PlayerView myPlayer : (TrackableCollection<PlayerView>) args[0]) {
|
||||||
client.setGameControllers(myPlayers);
|
if (myPlayer.getTracker() == null) {
|
||||||
|
myPlayer.setTracker(this.tracker);
|
||||||
} catch (Exception e) {
|
|
||||||
System.err.println("Failed: attempt number: " + numAttempts + " - " + e.toString());
|
|
||||||
try {
|
|
||||||
Thread.sleep(100);
|
|
||||||
} catch (InterruptedException e1) {
|
|
||||||
e1.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final TrackableCollection<PlayerView> myPlayers = (TrackableCollection<PlayerView>) args[0];
|
||||||
|
client.setGameControllers(myPlayers);
|
||||||
|
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user