diff --git a/forge-game/src/main/java/forge/game/Match.java b/forge-game/src/main/java/forge/game/Match.java index 36ebc48d28a..000113cf9b6 100644 --- a/forge-game/src/main/java/forge/game/Match.java +++ b/forge-game/src/main/java/forge/game/Match.java @@ -8,8 +8,6 @@ import java.util.List; import java.util.Random; import java.util.Set; import java.util.Map.Entry; -import java.util.concurrent.CountDownLatch; - import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.HashMultimap; import com.google.common.collect.Iterables; @@ -76,39 +74,27 @@ public class Match { /** * TODO: Write javadoc for this method. */ - public void startGame(final Game game, final CountDownLatch latch) { - - - // This code could be run run from EDT. - game.getAction().invoke(new Runnable() { - @Override - public void run() { - prepareAllZones(game); - if (rules.useAnte()) { // Deciding which cards go to ante - Multimap list = game.chooseCardsForAnte(); - for (Entry kv : list.entries()) { - Player p = kv.getKey(); - game.getAction().moveTo(ZoneType.Ante, kv.getValue()); - game.getGameLog().add(GameLogEntryType.ANTE, p + " anted " + kv.getValue()); - } - game.fireEvent(new GameEventAnteCardsSelected(list)); - } - - GameOutcome lastOutcome = gamesPlayed.isEmpty() ? null : gamesPlayed.get(gamesPlayed.size() - 1); - game.getAction().startGame(lastOutcome); - - if (rules.useAnte()) { - executeAnte(game); - } - - // will pull UI dialog, when the UI is listening - game.fireEvent(new GameEventGameFinished()); - - if (null != latch) { - latch.countDown(); - } + public void startGame(final Game game) { + prepareAllZones(game); + if (rules.useAnte()) { // Deciding which cards go to ante + Multimap list = game.chooseCardsForAnte(); + for (Entry kv : list.entries()) { + Player p = kv.getKey(); + game.getAction().moveTo(ZoneType.Ante, kv.getValue()); + game.getGameLog().add(GameLogEntryType.ANTE, p + " anted " + kv.getValue()); } - }); + game.fireEvent(new GameEventAnteCardsSelected(list)); + } + + GameOutcome lastOutcome = gamesPlayed.isEmpty() ? null : gamesPlayed.get(gamesPlayed.size() - 1); + game.getAction().startGame(lastOutcome); + + if (rules.useAnte()) { + executeAnte(game); + } + + // will pull UI dialog, when the UI is listening + game.fireEvent(new GameEventGameFinished()); } public void clearGamesPlayed() { @@ -183,14 +169,6 @@ public class Match { return players; } - /** - * TODO: Write javadoc for this method. - * @return - */ - public static int getPoisonCountersAmountToLose() { - return 10; - } - private static Set getRemovedAnteCards(Deck toUse) { final String keywordToRemove = "Remove CARDNAME from your deck before playing if you're not playing for ante."; Set myRemovedAnteCards = new HashSet(); diff --git a/forge-gui/src/main/java/forge/control/FControl.java b/forge-gui/src/main/java/forge/control/FControl.java index e5c8d0aab22..717ef80ca7c 100644 --- a/forge-gui/src/main/java/forge/control/FControl.java +++ b/forge-gui/src/main/java/forge/control/FControl.java @@ -430,7 +430,7 @@ public enum FControl implements KeyEventDispatcher { return inputQueue; } - public final void startGameWithUi(Match match) { + public final void startGameWithUi(final Match match) { if (this.game != null) { this.setCurrentScreen(FScreen.MATCH_SCREEN); SOverlayUtils.hideOverlay(); @@ -438,9 +438,17 @@ public enum FControl implements KeyEventDispatcher { return; //TODO: See if it's possible to run multiple games at once without crashing } setPlayerName(match.getPlayers()); - Game newGame = match.createGame(); + final Game newGame = match.createGame(); attachToGame(newGame); - match.startGame(newGame, null); + + // It's important to run match in a different thread to allow GUI inputs to be invoked from inside game. + // Game is set on pause while gui player takes decisions + game.getAction().invoke(new Runnable() { + @Override + public void run() { + match.startGame(newGame); + } + }); } public final void endCurrentGame() { diff --git a/forge-gui/src/main/java/forge/gui/match/VAssignDamage.java b/forge-gui/src/main/java/forge/gui/match/VAssignDamage.java index 2a61ce1e97b..01d6230d910 100644 --- a/forge-gui/src/main/java/forge/gui/match/VAssignDamage.java +++ b/forge-gui/src/main/java/forge/gui/match/VAssignDamage.java @@ -36,7 +36,6 @@ import javax.swing.border.Border; import net.miginfocom.swing.MigLayout; import forge.game.GameEntity; -import forge.game.Match; import forge.game.card.Card; import forge.game.card.CounterType; import forge.game.player.Player; @@ -443,7 +442,7 @@ public class VAssignDamage { if ( source == null ) { if ( defender instanceof Player ) { Player p = (Player)defender; - lethalDamage = attackerHasInfect ? Match.getPoisonCountersAmountToLose() - p.getPoisonCounters() : p.getLife(); + lethalDamage = attackerHasInfect ? p.getGame().getRules().getPoisonCountersToLose() - p.getPoisonCounters() : p.getLife(); } else if ( defender instanceof Card ) { // planeswalker Card pw = (Card)defender; lethalDamage = pw.getCounters(CounterType.LOYALTY); diff --git a/forge-gui/src/main/java/forge/net/FServer.java b/forge-gui/src/main/java/forge/net/FServer.java index a83b2a098ca..5e6364186db 100644 --- a/forge-gui/src/main/java/forge/net/FServer.java +++ b/forge-gui/src/main/java/forge/net/FServer.java @@ -4,8 +4,6 @@ import java.io.File; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.concurrent.CountDownLatch; - import org.apache.commons.lang3.time.StopWatch; import com.google.common.base.Supplier; @@ -104,16 +102,9 @@ public enum FServer { StopWatch sw = new StopWatch(); sw.start(); - CountDownLatch cdl = new CountDownLatch(1); - Game g1 = mc.createGame(); - mc.startGame(g1, cdl); - try { - cdl.await(); // wait until game ends (in other thread) - } catch (InterruptedException e) { - // TODO Auto-generated catch block ignores the exception, but sends it to System.err and probably forge.log. - e.printStackTrace(); - } + // will run match in the same thread + mc.startGame(g1); sw.stop(); List log = g1.getGameLog().getLogEntries(null);