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.Random;
import java.util.Set; import java.util.Set;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.concurrent.CountDownLatch;
import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashMultimap; import com.google.common.collect.HashMultimap;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
@@ -76,39 +74,27 @@ public class Match {
/** /**
* TODO: Write javadoc for this method. * TODO: Write javadoc for this method.
*/ */
public void startGame(final Game game, final CountDownLatch latch) { public void startGame(final Game game) {
prepareAllZones(game);
if (rules.useAnte()) { // Deciding which cards go to ante
// This code could be run run from EDT. Multimap<Player, Card> list = game.chooseCardsForAnte();
game.getAction().invoke(new Runnable() { for (Entry<Player, Card> kv : list.entries()) {
@Override Player p = kv.getKey();
public void run() { game.getAction().moveTo(ZoneType.Ante, kv.getValue());
prepareAllZones(game); game.getGameLog().add(GameLogEntryType.ANTE, p + " anted " + kv.getValue());
if (rules.useAnte()) { // Deciding which cards go to ante
Multimap<Player, Card> list = game.chooseCardsForAnte();
for (Entry<Player, Card> 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();
}
} }
}); 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() { public void clearGamesPlayed() {
@@ -183,14 +169,6 @@ public class Match {
return players; return players;
} }
/**
* TODO: Write javadoc for this method.
* @return
*/
public static int getPoisonCountersAmountToLose() {
return 10;
}
private static Set<PaperCard> getRemovedAnteCards(Deck toUse) { private static Set<PaperCard> getRemovedAnteCards(Deck toUse) {
final String keywordToRemove = "Remove CARDNAME from your deck before playing if you're not playing for ante."; final String keywordToRemove = "Remove CARDNAME from your deck before playing if you're not playing for ante.";
Set<PaperCard> myRemovedAnteCards = new HashSet<PaperCard>(); Set<PaperCard> myRemovedAnteCards = new HashSet<PaperCard>();

View File

@@ -430,7 +430,7 @@ public enum FControl implements KeyEventDispatcher {
return inputQueue; return inputQueue;
} }
public final void startGameWithUi(Match match) { public final void startGameWithUi(final Match match) {
if (this.game != null) { if (this.game != null) {
this.setCurrentScreen(FScreen.MATCH_SCREEN); this.setCurrentScreen(FScreen.MATCH_SCREEN);
SOverlayUtils.hideOverlay(); 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 return; //TODO: See if it's possible to run multiple games at once without crashing
} }
setPlayerName(match.getPlayers()); setPlayerName(match.getPlayers());
Game newGame = match.createGame(); final Game newGame = match.createGame();
attachToGame(newGame); 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() { public final void endCurrentGame() {

View File

@@ -36,7 +36,6 @@ import javax.swing.border.Border;
import net.miginfocom.swing.MigLayout; import net.miginfocom.swing.MigLayout;
import forge.game.GameEntity; import forge.game.GameEntity;
import forge.game.Match;
import forge.game.card.Card; import forge.game.card.Card;
import forge.game.card.CounterType; import forge.game.card.CounterType;
import forge.game.player.Player; import forge.game.player.Player;
@@ -443,7 +442,7 @@ public class VAssignDamage {
if ( source == null ) { if ( source == null ) {
if ( defender instanceof Player ) { if ( defender instanceof Player ) {
Player p = (Player)defender; 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 } else if ( defender instanceof Card ) { // planeswalker
Card pw = (Card)defender; Card pw = (Card)defender;
lethalDamage = pw.getCounters(CounterType.LOYALTY); lethalDamage = pw.getCounters(CounterType.LOYALTY);

View File

@@ -4,8 +4,6 @@ import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.concurrent.CountDownLatch;
import org.apache.commons.lang3.time.StopWatch; import org.apache.commons.lang3.time.StopWatch;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
@@ -104,16 +102,9 @@ public enum FServer {
StopWatch sw = new StopWatch(); StopWatch sw = new StopWatch();
sw.start(); sw.start();
CountDownLatch cdl = new CountDownLatch(1);
Game g1 = mc.createGame(); Game g1 = mc.createGame();
mc.startGame(g1, cdl); // will run match in the same thread
try { mc.startGame(g1);
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();
}
sw.stop(); sw.stop();
List<GameLogEntry> log = g1.getGameLog().getLogEntries(null); List<GameLogEntry> log = g1.getGameLog().getLogEntries(null);