match itself no longer creates a separate thread, it is now caller's responsibility. Some callers (such as tests or simulations) may run their match in the same thread.

This commit is contained in:
Maxmtg
2014-01-24 06:32:45 +00:00
parent aa4f3de30c
commit 2ec4873552
4 changed files with 34 additions and 58 deletions

View File

@@ -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,13 +74,7 @@ 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() {
public void startGame(final Game game) {
prepareAllZones(game);
if (rules.useAnte()) { // Deciding which cards go to ante
Multimap<Player, Card> list = game.chooseCardsForAnte();
@@ -103,12 +95,6 @@ public class Match {
// will pull UI dialog, when the UI is listening
game.fireEvent(new GameEventGameFinished());
if (null != latch) {
latch.countDown();
}
}
});
}
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<PaperCard> getRemovedAnteCards(Deck toUse) {
final String keywordToRemove = "Remove CARDNAME from your deck before playing if you're not playing for ante.";
Set<PaperCard> myRemovedAnteCards = new HashSet<PaperCard>();

View File

@@ -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() {

View File

@@ -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);

View File

@@ -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<GameLogEntry> log = g1.getGameLog().getLogEntries(null);