create GameException to store a copy of the game that caused the exception, restore exception handling in doAction

master
melvinzhang 2015-02-16 20:38:16 +08:00
parent 75e40e2931
commit c7343229e6
4 changed files with 32 additions and 4 deletions

View File

@ -0,0 +1,18 @@
package magic.exception;
import magic.model.MagicGame;
@SuppressWarnings("serial")
public class GameException extends RuntimeException {
final MagicGame game;
public GameException(final Throwable cause, final MagicGame aGame) {
super(cause);
game = aGame;
}
public MagicGame getGame() {
return game;
}
}

View File

@ -1,5 +1,6 @@
package magic.exception;
@SuppressWarnings("serial")
public class UndoClickedException extends Exception {
private static final long serialVersionUID = 1L;
}

View File

@ -14,6 +14,7 @@ import magic.model.MagicPowerToughness;
import magic.model.action.MagicAction;
import magic.model.stack.MagicItemOnStack;
import magic.utility.MagicSystem;
import magic.exception.GameException;
public class ExceptionReport {
@ -37,7 +38,10 @@ public class ExceptionReport {
sb.append(MagicSystem.getRuntimeParameters());
sb.append("\n\n");
final MagicGame game = MagicGame.getInstance();
final MagicGame game = (ex instanceof GameException) ?
((GameException)ex).getGame() :
MagicGame.getInstance();
try {
//buildReport might throw an exception
if (game != null) {

View File

@ -42,6 +42,7 @@ import magic.model.trigger.MagicPermanentTriggerMap;
import magic.model.trigger.MagicTrigger;
import magic.model.trigger.MagicTriggerType;
import magic.model.trigger.MagicWhenOtherComesIntoPlayTrigger;
import magic.exception.GameException;
import java.util.ArrayList;
import java.util.Collection;
@ -508,7 +509,11 @@ public class MagicGame {
public void doAction(final MagicAction action) {
actions.add(action);
action.doAction(this);
try {
action.doAction(this);
} catch (Throwable ex) {
throw new GameException(ex, this);
}
//performing actions update the score
score += action.getScore(scorePlayer);
}
@ -593,7 +598,7 @@ public class MagicGame {
} catch (Throwable ex) {
//put action back so that it shows up in report
actions.addLast(action);
throw ex;
throw new GameException(ex, this);
}
} while (!(action instanceof MagicMarkerAction));
}