Fixed missing watchers on game start, added error on missing watcher;

This commit is contained in:
Oleg Agafonov 2019-01-08 13:50:19 +04:00
parent 1d8fbb3304
commit b313ab3b7c
3 changed files with 50 additions and 55 deletions

View file

@ -1,14 +1,7 @@
package mage.view;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import mage.MageObject;
import mage.abilities.costs.Cost;
import mage.cards.Card;
@ -32,8 +25,10 @@ import mage.players.Player;
import mage.watchers.common.CastSpellLastTurnWatcher;
import org.apache.log4j.Logger;
import java.io.Serializable;
import java.util.*;
/**
*
* @author BetaSteward_at_googlemail.com
*/
public class GameView implements Serializable {
@ -90,7 +85,7 @@ public class GameView implements Serializable {
if (object != null) {
if (object instanceof Permanent) {
boolean controlled = ((Permanent) object).getControllerId().equals(createdForPlayerId);
stack.put(stackObject.getId(), new StackAbilityView(game, (StackAbility) stackObject, ((Permanent) object).getName(), new CardView(((Permanent) object), game, controlled, false, false)));
stack.put(stackObject.getId(), new StackAbilityView(game, (StackAbility) stackObject, object.getName(), new CardView(((Permanent) object), game, controlled, false, false)));
} else {
stack.put(stackObject.getId(), new StackAbilityView(game, (StackAbility) stackObject, card.getName(), new CardView(card, game, false, false, false)));
}
@ -109,14 +104,14 @@ public class GameView implements Serializable {
} else if (object instanceof Emblem) {
CardView cardView = new CardView(new EmblemView((Emblem) object));
// Card sourceCard = (Card) ((Emblem) object).getSourceObject();
((StackAbility) stackObject).setName(((Emblem) object).getName());
stackObject.setName(object.getName());
// ((StackAbility) stackObject).setExpansionSetCode(sourceCard.getExpansionSetCode());
stack.put(stackObject.getId(),
new StackAbilityView(game, (StackAbility) stackObject, object.getName(), cardView));
checkPaid(stackObject.getId(), ((StackAbility) stackObject));
} else if (object instanceof Plane) {
CardView cardView = new CardView(new PlaneView((Plane) object));
((StackAbility) stackObject).setName(((Plane) object).getName());
stackObject.setName(object.getName());
stack.put(stackObject.getId(),
new StackAbilityView(game, (StackAbility) stackObject, object.getName(), cardView));
checkPaid(stackObject.getId(), ((StackAbility) stackObject));
@ -131,7 +126,7 @@ public class GameView implements Serializable {
} else if (object instanceof StackAbility) {
StackAbility stackAbility = ((StackAbility) object);
stackAbility.newId();
stack.put(stackObject.getId(), new CardView(((StackAbility) stackObject)));
stack.put(stackObject.getId(), new CardView(stackObject));
checkPaid(stackObject.getId(), ((StackAbility) stackObject));
} else {
LOGGER.fatal("Object can't be cast to StackAbility: " + object.getName() + ' ' + object.toString() + ' ' + object.getClass().toString());
@ -182,7 +177,7 @@ public class GameView implements Serializable {
this.special = false;
}
CastSpellLastTurnWatcher watcher = (CastSpellLastTurnWatcher) game.getState().getWatchers().get(CastSpellLastTurnWatcher.class.getSimpleName());
CastSpellLastTurnWatcher watcher = game.getState().getWatcher(CastSpellLastTurnWatcher.class);
if (watcher != null) {
spellsCastCurrentTurn = watcher.getAmountOfSpellsAllPlayersCastOnCurrentTurn();
} else {

View file

@ -1,9 +1,5 @@
package mage.game;
import java.io.IOException;
import java.io.Serializable;
import java.util.*;
import java.util.Map.Entry;
import mage.MageException;
import mage.MageObject;
import mage.abilities.*;
@ -65,10 +61,14 @@ import mage.util.GameLog;
import mage.util.MessageToClient;
import mage.util.RandomUtil;
import mage.util.functions.ApplyToPermanent;
import mage.watchers.Watchers;
import mage.watchers.common.*;
import org.apache.log4j.Logger;
import java.io.IOException;
import java.io.Serializable;
import java.util.*;
import java.util.Map.Entry;
public abstract class GameImpl implements Game, Serializable {
private static final int ROLLBACK_TURNS_MAX = 4;
@ -150,6 +150,7 @@ public abstract class GameImpl implements Game, Serializable {
this.state = new GameState();
this.startLife = startLife;
this.executingRollback = false;
initGameDefaultWatchers();
}
public GameImpl(final GameImpl game) {
@ -259,6 +260,7 @@ public abstract class GameImpl implements Game, Serializable {
public void addPlayer(Player player, Deck deck) {
player.useDeck(deck, this);
state.addPlayer(player);
initPlayerDefaultWatchers(player.getId());
}
@Override
@ -504,7 +506,7 @@ public abstract class GameImpl implements Game, Serializable {
return Optional.empty();
}
// @Override
// @Override
// public Zone getZone(UUID objectId) {
// return state.getZone(objectId);
// }
@ -532,7 +534,7 @@ public abstract class GameImpl implements Game, Serializable {
}
}
// /**
// /**
// * Starts check if game is over or if playerId is given let the player
// * concede.
// *
@ -1012,20 +1014,6 @@ public abstract class GameImpl implements Game, Serializable {
}
getState().setChoosingPlayerId(null);
state.resetWatchers(); // watcher objects from cards are reused during match so reset all card watchers already added
// add default watchers
for (UUID playerId : state.getPlayerList(startingPlayerId)) {
getState().addWatcher(new PlayerDamagedBySourceWatcher(playerId));
getState().addWatcher(new BloodthirstWatcher(playerId));
}
getState().addWatcher(new MorbidWatcher());
getState().addWatcher(new CastSpellLastTurnWatcher());
getState().addWatcher(new CastSpellYourLastTurnWatcher());
getState().addWatcher(new PlayerLostLifeWatcher());
getState().addWatcher(new PlayerLostLifeNonCombatWatcher());
getState().addWatcher(new BlockedAttackerWatcher());
getState().addWatcher(new DamageDoneWatcher());
getState().addWatcher(new PlanarRollWatcher());
getState().addWatcher(new PlayersAttackedThisTurnWatcher());
//20100716 - 103.5
for (UUID playerId : state.getPlayerList(startingPlayerId)) {
@ -1074,6 +1062,24 @@ public abstract class GameImpl implements Game, Serializable {
}
}
private void initGameDefaultWatchers() {
getState().addWatcher(new MorbidWatcher());
getState().addWatcher(new CastSpellLastTurnWatcher());
getState().addWatcher(new CastSpellYourLastTurnWatcher());
getState().addWatcher(new PlayerLostLifeWatcher());
getState().addWatcher(new PlayerLostLifeNonCombatWatcher());
getState().addWatcher(new BlockedAttackerWatcher());
getState().addWatcher(new DamageDoneWatcher());
getState().addWatcher(new PlanarRollWatcher());
getState().addWatcher(new PlayersAttackedThisTurnWatcher());
}
private void initPlayerDefaultWatchers(UUID playerId) {
getState().addWatcher(new PlayerDamagedBySourceWatcher(playerId));
getState().addWatcher(new BloodthirstWatcher(playerId));
}
protected void sendStartMessage(Player choosingPlayer, Player startingPlayer) {
StringBuilder message = new StringBuilder();
if (choosingPlayer != null) {
@ -1205,7 +1211,7 @@ public abstract class GameImpl implements Game, Serializable {
fireInformEvent(new StringBuilder(player.getLogName())
.append(" mulligans")
.append(deduction == 0 ? " for free and draws " : " down to ")
.append(Integer.toString(numCards - deduction))
.append((numCards - deduction))
.append(numCards - deduction == 1 ? " card" : " cards").toString());
player.drawCards(numCards - deduction, this);
}
@ -1542,10 +1548,9 @@ public abstract class GameImpl implements Game, Serializable {
}
/**
*
* @param emblem
* @param sourceObject
* @param toPlayerId controller and owner of the emblem
* @param toPlayerId controller and owner of the emblem
*/
@Override
public void addEmblem(Emblem emblem, MageObject sourceObject, UUID toPlayerId) {
@ -1561,11 +1566,10 @@ public abstract class GameImpl implements Game, Serializable {
}
/**
*
* @param plane
* @param sourceObject
* @param toPlayerId controller and owner of the plane (may only be one per
* game..)
* @param toPlayerId controller and owner of the plane (may only be one per
* game..)
* @return boolean - whether the plane was added successfully or not
*/
@Override
@ -1794,7 +1798,7 @@ public abstract class GameImpl implements Game, Serializable {
break;
}
// triggered abilities that don't use the stack have to be executed first (e.g. Banisher Priest Return exiled creature
for (Iterator<TriggeredAbility> it = abilities.iterator(); it.hasNext();) {
for (Iterator<TriggeredAbility> it = abilities.iterator(); it.hasNext(); ) {
TriggeredAbility triggeredAbility = it.next();
if (!triggeredAbility.isUsesStack()) {
state.removeTriggeredAbility(triggeredAbility);
@ -1979,7 +1983,7 @@ public abstract class GameImpl implements Game, Serializable {
if ((ability instanceof SpellAbility)
&& SpellAbilityType.BASE_ALTERNATE == ((SpellAbility) ability).getSpellAbilityType()
&& !ability.getTargets().isEmpty()) {
spellAbility = (SpellAbility) ability;
spellAbility = ability;
break;
}
}
@ -2529,7 +2533,7 @@ public abstract class GameImpl implements Game, Serializable {
}
//20100423 - 800.4a
Set<Card> toOutside = new HashSet<>();
for (Iterator<Permanent> it = getBattlefield().getAllPermanents().iterator(); it.hasNext();) {
for (Iterator<Permanent> it = getBattlefield().getAllPermanents().iterator(); it.hasNext(); ) {
Permanent perm = it.next();
if (perm.isOwnedBy(playerId)) {
if (perm.getAttachedTo() != null) {
@ -2572,7 +2576,7 @@ public abstract class GameImpl implements Game, Serializable {
player.moveCards(toOutside, Zone.OUTSIDE, null, this);
// triggered abilities that don't use the stack have to be executed
List<TriggeredAbility> abilities = state.getTriggered(player.getId());
for (Iterator<TriggeredAbility> it = abilities.iterator(); it.hasNext();) {
for (Iterator<TriggeredAbility> it = abilities.iterator(); it.hasNext(); ) {
TriggeredAbility triggeredAbility = it.next();
if (!triggeredAbility.isUsesStack()) {
state.removeTriggeredAbility(triggeredAbility);
@ -2592,7 +2596,7 @@ public abstract class GameImpl implements Game, Serializable {
// Remove cards from the player in all exile zones
for (ExileZone exile : this.getExile().getExileZones()) {
for (Iterator<UUID> it = exile.iterator(); it.hasNext();) {
for (Iterator<UUID> it = exile.iterator(); it.hasNext(); ) {
Card card = this.getCard(it.next());
if (card != null && card.isOwnedBy(playerId)) {
it.remove();
@ -2602,7 +2606,7 @@ public abstract class GameImpl implements Game, Serializable {
//Remove all commander/emblems/plane the player controls
boolean addPlaneAgain = false;
for (Iterator<CommandObject> it = this.getState().getCommand().iterator(); it.hasNext();) {
for (Iterator<CommandObject> it = this.getState().getCommand().iterator(); it.hasNext(); ) {
CommandObject obj = it.next();
if (obj.isControlledBy(playerId)) {
if (obj instanceof Emblem) {
@ -2738,7 +2742,7 @@ public abstract class GameImpl implements Game, Serializable {
}
if (!game.isSimulation()) {
StringBuilder message = new StringBuilder(preventionSource.getLogName()).append(": Prevented ");
message.append(Integer.toString(result.getPreventedDamage())).append(" damage from ").append(damageSource.getLogName());
message.append(result.getPreventedDamage()).append(" damage from ").append(damageSource.getLogName());
if (!targetName.isEmpty()) {
message.append(" to ").append(targetName);
}
@ -2773,7 +2777,7 @@ public abstract class GameImpl implements Game, Serializable {
* Gets last known information about object in the zone. At the moment
* doesn't take into account zone (it is expected that it doesn't really
* matter, if not, then Map<UUID, Map<Zone, Card>> should be used instead).
*
* <p>
* Can return null.
*
* @param objectId
@ -3128,7 +3132,7 @@ public abstract class GameImpl implements Game, Serializable {
public void saveRollBackGameState() {
if (gameOptions.rollbackTurnsAllowed) {
int toDelete = getTurnNum() - ROLLBACK_TURNS_MAX;
if (toDelete > 0 && gameStatesRollBack.containsKey(toDelete)) {
if (toDelete > 0) {
gameStatesRollBack.remove(toDelete);
}
gameStatesRollBack.put(getTurnNum(), state.copy());
@ -3180,9 +3184,7 @@ public abstract class GameImpl implements Game, Serializable {
@Override
public void setEnterWithCounters(UUID sourceId, Counters counters) {
if (counters == null) {
if (enterWithCounters.containsKey(sourceId)) {
enterWithCounters.remove(sourceId);
}
enterWithCounters.remove(sourceId);
return;
}
enterWithCounters.put(sourceId, counters);

View file

@ -1,4 +1,3 @@
package mage.watchers;
import mage.game.Game;
@ -7,7 +6,6 @@ import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import java.util.HashMap;
import java.util.UUID;
/**
* @author BetaSteward_at_googlemail.com
@ -50,7 +48,7 @@ public class Watchers extends HashMap<String, Watcher> {
if (containsKey(key)) {
return super.get(key);
}
logger.info(key + " not found in watchers");
logger.error(key + " not found in watchers", new Throwable());
return null;
}
}