Reimplemented CastSpellLastTurnWatcher. Possible fix for Issue 337. (but still doesn't work correctly because of wrong behavior in game state copying).

This commit is contained in:
magenoxx 2011-10-26 14:22:54 +04:00
parent 21f9633486
commit 8a74175d21
3 changed files with 39 additions and 9 deletions

View file

@ -46,6 +46,13 @@ public class NoSpellsWereCastLastTurnCondition implements Condition {
@Override @Override
public boolean apply(Game game, Ability source) { public boolean apply(Game game, Ability source) {
CastSpellLastTurnWatcher watcher = (CastSpellLastTurnWatcher) game.getState().getWatchers().get("CastSpellLastTurnWatcher"); CastSpellLastTurnWatcher watcher = (CastSpellLastTurnWatcher) game.getState().getWatchers().get("CastSpellLastTurnWatcher");
return watcher.getAmountOfSpellsCastOnPrevTurn() == 0; // if any player cast spell, return false
for (Integer count : watcher.getAmountOfSpellsCastOnPrevTurn().values()) {
if (count > 0) {
return false;
}
}
// no one cast spell this turn
return true;
} }
} }

View file

@ -46,6 +46,13 @@ public class TwoOrMoreSpellsWereCastLastTurnCondition implements Condition {
@Override @Override
public boolean apply(Game game, Ability source) { public boolean apply(Game game, Ability source) {
CastSpellLastTurnWatcher watcher = (CastSpellLastTurnWatcher) game.getState().getWatchers().get("CastSpellLastTurnWatcher"); CastSpellLastTurnWatcher watcher = (CastSpellLastTurnWatcher) game.getState().getWatchers().get("CastSpellLastTurnWatcher");
return watcher.getAmountOfSpellsCastOnPrevTurn() >= 2; // if any player cast more than two spells, return true
for (Integer count : watcher.getAmountOfSpellsCastOnPrevTurn().values()) {
if (count >= 2) {
return true;
}
}
// no one cast two or more spells this turn
return false;
} }
} }

View file

@ -33,14 +33,18 @@ import mage.game.Game;
import mage.game.events.GameEvent; import mage.game.events.GameEvent;
import mage.watchers.WatcherImpl; import mage.watchers.WatcherImpl;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/** /**
* *
* @author BetaSteward_at_googlemail.com * @author nantuko, BetaSteward_at_googlemail.com
*/ */
public class CastSpellLastTurnWatcher extends WatcherImpl<CastSpellLastTurnWatcher> { public class CastSpellLastTurnWatcher extends WatcherImpl<CastSpellLastTurnWatcher> {
private int amountOfSpellsCastOnPrevTurn; private Map<UUID, Integer> amountOfSpellsCastOnPrevTurn = new HashMap<UUID, Integer>();
private int amountOfSpellsCastOnCurrentTurn; private Map<UUID, Integer> amountOfSpellsCastOnCurrentTurn = new HashMap<UUID, Integer>();
public CastSpellLastTurnWatcher() { public CastSpellLastTurnWatcher() {
super("CastSpellLastTurnWatcher", WatcherScope.GAME); super("CastSpellLastTurnWatcher", WatcherScope.GAME);
@ -55,17 +59,29 @@ public class CastSpellLastTurnWatcher extends WatcherImpl<CastSpellLastTurnWatch
@Override @Override
public void watch(GameEvent event, Game game) { public void watch(GameEvent event, Game game) {
if (event.getType() == GameEvent.EventType.SPELL_CAST) { if (event.getType() == GameEvent.EventType.SPELL_CAST) {
amountOfSpellsCastOnCurrentTurn++; UUID playerId = event.getPlayerId();
if (playerId != null) {
Integer amount = amountOfSpellsCastOnCurrentTurn.get(playerId);
if (amount == null) {
amount = Integer.valueOf(1);
} else {
amount = Integer.valueOf(amount+1);
}
amountOfSpellsCastOnCurrentTurn.put(playerId, amount);
//Card card = game.getCard(event.getSourceId());
//System.out.println("CAST: " + card.getName());
}
} }
} }
@Override @Override
public void reset() { public void reset() {
amountOfSpellsCastOnPrevTurn = amountOfSpellsCastOnCurrentTurn; amountOfSpellsCastOnPrevTurn.clear();
amountOfSpellsCastOnCurrentTurn = 0; amountOfSpellsCastOnPrevTurn.putAll(amountOfSpellsCastOnCurrentTurn);
amountOfSpellsCastOnCurrentTurn.clear();
} }
public int getAmountOfSpellsCastOnPrevTurn() { public Map<UUID, Integer> getAmountOfSpellsCastOnPrevTurn() {
return amountOfSpellsCastOnPrevTurn; return amountOfSpellsCastOnPrevTurn;
} }