Merge branch 'master' into 'master'

[Bug] - Allow games that throw exceptions or timer runs out to allow GameOutcomes to return with a NPE being thrown

See merge request core-developers/forge!3264
This commit is contained in:
Michael Kamensky
2020-10-14 04:21:48 +00:00
2 changed files with 66 additions and 75 deletions

View File

@@ -67,8 +67,13 @@ public final class GameOutcome implements Iterable<Entry<RegisteredPlayer, Playe
this.lostCards.addAll(cards);
}
public static AnteResult won(List<PaperCard> cards) { return new AnteResult(cards, true); }
public static AnteResult lost(List<PaperCard> cards) { return new AnteResult(cards, false); }
public static AnteResult won(List<PaperCard> cards) {
return new AnteResult(cards, true);
}
public static AnteResult lost(List<PaperCard> cards) {
return new AnteResult(cards, false);
}
}
private int lastTurnNumber = 0;
@@ -83,21 +88,22 @@ public final class GameOutcome implements Iterable<Entry<RegisteredPlayer, Playe
public GameOutcome(GameEndReason reason, final Iterable<Player> players) {
winCondition = reason;
calculateLifeDelta(players);
int winnersHealth = 0;
int opponentsHealth = 0;
for (final Player p : players) {
this.playerRating.put(p.getRegisteredPlayer(), p.getStats());
this.playerNames.put(p.getRegisteredPlayer(), p.getName());
if (p.getOutcome().hasWon() && winCondition == GameEndReason.AllOpposingTeamsLost) {
if (winCondition == GameEndReason.AllOpposingTeamsLost && p.getOutcome().hasWon()) {
// Only mark the WinningTeam when "Team mode" is on.
winningTeam = p.getTeam();
}
}
// Unable to calculate lifeDelta between a winning and losing player whe a draw is in place
if (winCondition == GameEndReason.Draw) return;
int winnersHealth = 0;
int opponentsHealth = 0;
for (final Player p : players) {
if (p.getTeam() == winningTeam) {
winnersHealth += p.getLife();
@@ -106,6 +112,7 @@ public final class GameOutcome implements Iterable<Entry<RegisteredPlayer, Playe
}
}
calculateLifeDelta(players);
lifeDelta = Math.max(0, winnersHealth - opponentsHealth);
}
@@ -116,8 +123,7 @@ public final class GameOutcome implements Iterable<Entry<RegisteredPlayer, Playe
for (Player p : players) {
if (p.getOutcome().hasWon()) {
winnersHealth += p.getLife();
}
else {
} else {
opponentsHealth += p.getLife();
}
}

View File

@@ -1,28 +1,27 @@
package forge.view;
import forge.LobbyPlayer;
import forge.deck.Deck;
import forge.deck.DeckGroup;
import forge.deck.io.DeckSerializer;
import forge.game.*;
import forge.game.player.RegisteredPlayer;
import forge.model.FModel;
import forge.player.GamePlayerUtil;
import forge.properties.ForgeConstants;
import forge.tournament.system.*;
import forge.util.Lang;
import forge.util.TextUtil;
import forge.util.WordUtil;
import forge.util.storage.IStorage;
import org.apache.commons.lang3.time.StopWatch;
import java.io.File;
import java.io.FilenameFilter;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import forge.LobbyPlayer;
import forge.deck.DeckGroup;
import forge.game.*;
import forge.properties.ForgeConstants;
import forge.tournament.system.*;
import forge.util.TextUtil;
import forge.util.WordUtil;
import forge.util.storage.IStorage;
import org.apache.commons.lang3.time.StopWatch;
import forge.deck.Deck;
import forge.deck.io.DeckSerializer;
import forge.game.player.RegisteredPlayer;
import forge.model.FModel;
import forge.player.GamePlayerUtil;
import forge.util.Lang;
public class SimulateMatch {
public static void simulate(String[] args) {
FModel.initialize(null, null);
@@ -49,11 +48,9 @@ public class SimulateMatch {
options = new ArrayList<>();
params.put(a.substring(1), options);
}
else if (options != null) {
} else if (options != null) {
options.add(a);
}
else {
} else {
System.err.println("Illegal parameter usage");
return;
}
@@ -159,38 +156,26 @@ public class SimulateMatch {
}
public static void simulateSingleMatch(final Match mc, int iGame, boolean outputGamelog) {
final StopWatch sw = new StopWatch();
sw.start();
final Game g1 = mc.createGame();
// will run match in the same thread
long startTime = System.currentTimeMillis();
try {
TimeLimitedCodeBlock.runWithTimeout(new Runnable() {
@Override
public void run() {
TimeLimitedCodeBlock.runWithTimeout(() -> {
mc.startGame(g1);
sw.stop();
}
}, 120, TimeUnit.SECONDS);
}
catch (TimeoutException e) {
} catch (TimeoutException e) {
System.out.println("Stopping slow match as draw");
g1.setGameOver(GameEndReason.Draw);
sw.stop();
}catch (Exception e){
} catch (Exception | StackOverflowError e) {
e.printStackTrace();
g1.setGameOver(GameEndReason.Draw);
sw.stop();
}catch(StackOverflowError e){
} finally {
g1.setGameOver(GameEndReason.Draw);
sw.stop();
}
List<GameLogEntry> log;
if (outputGamelog) {
log = g1.getGameLog().getLogEntries(null);
@@ -204,9 +189,9 @@ public class SimulateMatch {
// If both players life totals to 0 in a single turn, the game should end in a draw
if (g1.getOutcome().isDraw()) {
System.out.println(String.format("Game %d ended in a Draw! Took %d ms.", 1+iGame, sw.getTime()));
System.out.printf("\nGame Result: Game %d ended in a Draw! Took %d ms.%n", 1 + iGame, sw.getTime());
} else {
System.out.println(String.format("\nGame %d ended in %d ms. %s has won!\n", 1+iGame, sw.getTime(), g1.getOutcome().getWinningLobbyPlayer().getName()));
System.out.printf("\nGame Result: Game %d ended in %d ms. %s has won!\n%n", 1 + iGame, sw.getTime(), g1.getOutcome().getWinningLobbyPlayer().getName());
}
}