refactor of watchers to use map functions

This commit is contained in:
ingmargoudt 2017-02-15 23:19:40 +01:00
parent fbbfc6c611
commit 4aa51210f9
18 changed files with 69 additions and 158 deletions

View file

@ -31,17 +31,16 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import mage.constants.WatcherScope;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.watchers.Watcher;
/**
*
* @author jeffwadsworth
*
* Amount of damage received by a player this turn
*
* <p>
* Amount of damage received by a player this turn
*/
public class AmountOfDamageAPlayerReceivedThisTurnWatcher extends Watcher {
@ -63,23 +62,14 @@ public class AmountOfDamageAPlayerReceivedThisTurnWatcher extends Watcher {
if (event.getType() == GameEvent.EventType.DAMAGED_PLAYER) {
UUID playerId = event.getTargetId();
if (playerId != null) {
Integer amount = amountOfDamageReceivedThisTurn.get(playerId);
if (amount == null) {
amount = event.getAmount();
} else {
amount = amount + event.getAmount();
}
amountOfDamageReceivedThisTurn.put(playerId, amount);
amountOfDamageReceivedThisTurn.putIfAbsent(playerId, 0);
amountOfDamageReceivedThisTurn.compute(playerId, (k, v) -> v + event.getAmount());
}
}
}
public int getAmountOfDamageReceivedThisTurn(UUID playerId) {
Integer amount = amountOfDamageReceivedThisTurn.get(playerId);
if (amount != null) {
return amount;
}
return 0;
return amountOfDamageReceivedThisTurn.getOrDefault(playerId, 0);
}
@Override

View file

@ -38,7 +38,6 @@ import java.util.Map;
import java.util.UUID;
/**
*
* @author Quercitron
* @author LevelX2
* @author jeffwadsworth
@ -61,11 +60,8 @@ public class CardsAmountDrawnThisTurnWatcher extends Watcher {
@Override
public void watch(GameEvent event, Game game) {
if (event.getType() == GameEvent.EventType.DREW_CARD) {
if (amountOfCardsDrawnThisTurn.containsKey(event.getPlayerId())) {
amountOfCardsDrawnThisTurn.put(event.getPlayerId(), amountOfCardsDrawnThisTurn.get(event.getPlayerId()) + 1);
} else {
amountOfCardsDrawnThisTurn.put(event.getPlayerId(), 1);
}
amountOfCardsDrawnThisTurn.putIfAbsent(event.getPlayerId(), 0);
amountOfCardsDrawnThisTurn.compute(event.getPlayerId(), (k, amount) -> amount += 1);
}
}
@ -90,11 +86,7 @@ public class CardsAmountDrawnThisTurnWatcher extends Watcher {
}
public int getAmountCardsDrawn(UUID playerId) {
Integer amount = amountOfCardsDrawnThisTurn.get(playerId);
if (amount != null) {
return amount;
}
return 0;
return amountOfCardsDrawnThisTurn.getOrDefault(playerId,0);
}
@Override

View file

@ -32,6 +32,7 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import mage.constants.PhaseStep;
import mage.constants.WatcherScope;
import mage.game.Game;
@ -39,11 +40,9 @@ import mage.game.events.GameEvent;
import mage.watchers.Watcher;
/**
*
* @author LevelX2
*
* Counts cards drawn during draw step
*
* <p>
* Counts cards drawn during draw step
*/
public class CardsDrawnDuringDrawStepWatcher extends Watcher {
@ -65,26 +64,18 @@ public class CardsDrawnDuringDrawStepWatcher extends Watcher {
public void watch(GameEvent event, Game game) {
if (event.getType() == GameEvent.EventType.DREW_CARD
&& game.getPhase() != null
&& game.getPhase().getStep().getType().equals(PhaseStep.DRAW)) {
&& game.getPhase().getStep().getType() == PhaseStep.DRAW) {
UUID playerId = event.getPlayerId();
if (playerId != null) {
Integer amount = amountOfCardsDrawnThisTurn.get(playerId);
if (amount == null) {
amount = 1;
} else {
amount++;
}
amountOfCardsDrawnThisTurn.put(playerId, amount);
amountOfCardsDrawnThisTurn.putIfAbsent(playerId, 0);
amountOfCardsDrawnThisTurn.compute(playerId, (k, amount) -> amount + 1);
}
}
}
public int getAmountCardsDrawn(UUID playerId) {
Integer amount = amountOfCardsDrawnThisTurn.get(playerId);
if (amount != null) {
return amount;
}
return 0;
return amountOfCardsDrawnThisTurn.getOrDefault(playerId, 0);
}
@Override

View file

@ -74,13 +74,9 @@ public class CardsPutIntoGraveyardWatcher extends Watcher {
if (event.getType() == GameEvent.EventType.ZONE_CHANGE && ((ZoneChangeEvent) event).getToZone() == Zone.GRAVEYARD) {
UUID playerId = event.getPlayerId();
if (playerId != null && game.getCard(event.getTargetId()) != null) {
Integer amount = amountOfCardsThisTurn.get(playerId);
if (amount == null) {
amount = 1;
} else {
++amount;
}
amountOfCardsThisTurn.put(playerId, amount);
amountOfCardsThisTurn.putIfAbsent(playerId, 0);
amountOfCardsThisTurn.compute(playerId, (k, amount) -> amount += 1);
if (((ZoneChangeEvent) event).getFromZone() == Zone.BATTLEFIELD) {
cardsPutToGraveyardFromBattlefield.add(new MageObjectReference(event.getTargetId(), game));
}
@ -89,11 +85,7 @@ public class CardsPutIntoGraveyardWatcher extends Watcher {
}
public int getAmountCardsPutToGraveyard(UUID playerId) {
Integer amount = amountOfCardsThisTurn.get(playerId);
if (amount != null) {
return amount;
}
return 0;
return amountOfCardsThisTurn.getOrDefault(playerId, 0);
}
public Set<MageObjectReference> getCardsPutToGraveyardFromBattlefield() {

View file

@ -4,6 +4,7 @@ import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import mage.constants.WatcherScope;
import mage.constants.Zone;
import mage.game.Game;
@ -37,7 +38,7 @@ public class CastFromHandWatcher extends Watcher {
spellsCastFromHand.clear();
step = null;
}
if (event.getType() == GameEvent.EventType.SPELL_CAST && event.getZone().equals(Zone.HAND)) {
if (event.getType() == GameEvent.EventType.SPELL_CAST && event.getZone() == Zone.HAND) {
if (step == null) {
step = game.getTurn().getStep();
}

View file

@ -27,20 +27,16 @@
*/
package mage.watchers.common;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import mage.MageObjectReference;
import mage.constants.WatcherScope;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.watchers.Watcher;
import java.util.*;
import java.util.Map.Entry;
/**
*
* @author nantuko, BetaSteward_at_googlemail.com
*/
public class CastSpellLastTurnWatcher extends Watcher {
@ -70,13 +66,9 @@ public class CastSpellLastTurnWatcher extends Watcher {
spellsCastThisTurnInOrder.add(new MageObjectReference(event.getTargetId(), game));
UUID playerId = event.getPlayerId();
if (playerId != null) {
Integer amount = amountOfSpellsCastOnCurrentTurn.get(playerId);
if (amount == null) {
amount = 1;
} else {
amount = amount + 1;
}
amountOfSpellsCastOnCurrentTurn.put(playerId, amount);
amountOfSpellsCastOnCurrentTurn.putIfAbsent(playerId, 0);
amountOfSpellsCastOnCurrentTurn.compute(playerId, (k, a) -> a + 1);
}
}
}
@ -98,20 +90,11 @@ public class CastSpellLastTurnWatcher extends Watcher {
}
public int getAmountOfSpellsAllPlayersCastOnCurrentTurn() {
int totalAmount = 0;
for (Integer amount : amountOfSpellsCastOnCurrentTurn.values()) {
totalAmount += amount;
}
return totalAmount;
return amountOfSpellsCastOnCurrentTurn.values().stream().mapToInt(Integer::intValue).sum();
}
public int getAmountOfSpellsPlayerCastOnCurrentTurn(UUID playerId) {
Integer value = amountOfSpellsCastOnCurrentTurn.get(playerId);
if (value != null) {
return value;
} else {
return 0;
}
return amountOfSpellsCastOnCurrentTurn.getOrDefault(playerId, 0);
}
public int getSpellOrder(MageObjectReference spell, Game game) {

View file

@ -77,10 +77,7 @@ public class CommanderInfoWatcher extends Watcher {
DamagedPlayerEvent damageEvent = (DamagedPlayerEvent) event;
if (damageEvent.isCombatDamage()) {
UUID playerUUID = event.getTargetId();
Integer damage = damageToPlayer.get(playerUUID);
if (damage == null) {
damage = 0;
}
Integer damage = damageToPlayer.getOrDefault(playerUUID,0);
damage += damageEvent.getAmount();
damageToPlayer.put(playerUUID, damage);
Player player = game.getPlayer(playerUUID);

View file

@ -81,10 +81,7 @@ public class CreaturesDiedWatcher extends Watcher {
}
public int getAmountOfCreaturesDiesThisTurn(UUID playerId) {
if (amountOfCreaturesThatDiedByController.containsKey(playerId)) {
return amountOfCreaturesThatDiedByController.get(playerId);
}
return 0;
return amountOfCreaturesThatDiedByController.getOrDefault(playerId, 0);
}
@Override

View file

@ -5,17 +5,17 @@
*/
package mage.watchers.common;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import mage.MageObjectReference;
import mage.constants.WatcherScope;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.watchers.Watcher;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
*
* @author LevelX2
*/
public class DamageDoneWatcher extends Watcher {
@ -50,12 +50,12 @@ public class DamageDoneWatcher extends Watcher {
case DAMAGED_PLANESWALKER:
case DAMAGED_PLAYER: {
MageObjectReference damageSourceRef = new MageObjectReference(event.getSourceId(), game);
int damageDone = damagingObjects.containsKey(damageSourceRef) ? damagingObjects.get(damageSourceRef) : 0;
damagingObjects.put(damageSourceRef, damageDone + event.getAmount());
damagingObjects.putIfAbsent(damageSourceRef, 0);
damagingObjects.compute(damageSourceRef, (k, damage) -> damage + event.getAmount());
MageObjectReference damageTargetRef = new MageObjectReference(event.getTargetId(), game);
int damageReceived = damagedObjects.containsKey(damageTargetRef) ? damagedObjects.get(damageTargetRef) : 0;
damagedObjects.put(damageTargetRef, damageReceived + event.getAmount());
damagedObjects.putIfAbsent(damageTargetRef, 0);
damagedObjects.compute(damageTargetRef, (k, damage) -> damage + event.getAmount());
}
}
}
@ -69,12 +69,12 @@ public class DamageDoneWatcher extends Watcher {
public int damageDoneBy(UUID objectId, int zoneChangeCounter, Game game) {
MageObjectReference mor = new MageObjectReference(objectId, zoneChangeCounter, game);
return damagingObjects.containsKey(mor) ? damagingObjects.get(mor) : 0;
return damagingObjects.getOrDefault(mor, 0);
}
public int damageDoneTo(UUID objectId, int zoneChangeCounter, Game game) {
MageObjectReference mor = new MageObjectReference(objectId, zoneChangeCounter, game);
return damagedObjects.containsKey(mor) ? damagedObjects.get(mor) : 0;
return damagedObjects.getOrDefault(mor, 0);
}
public boolean isDamaged(UUID objectId, int zoneChangeCounter, Game game) {

View file

@ -30,6 +30,7 @@ package mage.watchers.common;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import mage.MageObject;
import mage.MageObjectReference;
import mage.constants.WatcherScope;
@ -40,7 +41,6 @@ import mage.game.permanent.Permanent;
import mage.watchers.Watcher;
/**
*
* @author BetaSteward_at_googlemail.com
*/
public class DamagedByWatcher extends Watcher {
@ -72,12 +72,11 @@ public class DamagedByWatcher extends Watcher {
@Override
public void watch(GameEvent event, Game game) {
boolean eventHasAppropriateType = (event.getType() == EventType.DAMAGED_CREATURE) ||
(watchPlaneswalkers && event.getType() == EventType.DAMAGED_PLANESWALKER);
(watchPlaneswalkers && event.getType() == EventType.DAMAGED_PLANESWALKER);
if (eventHasAppropriateType && sourceId.equals(event.getSourceId())) {
MageObjectReference mor = new MageObjectReference(event.getTargetId(), game);
if (!damagedBySource.contains(mor)) {
damagedBySource.add(mor);
}
damagedBySource.add(mor);
}
}

View file

@ -3,6 +3,7 @@ package mage.watchers.common;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import mage.constants.WatcherScope;
import mage.game.Game;
import mage.game.events.GameEvent;
@ -12,7 +13,6 @@ import mage.watchers.Watcher;
/**
* @author jeffwadsworth
*
*/
public class FirstSpellCastThisTurnWatcher extends Watcher {
@ -36,9 +36,9 @@ public class FirstSpellCastThisTurnWatcher extends Watcher {
case CAST_SPELL:
Spell spell = (Spell) game.getObject(event.getTargetId());
if (spell != null && !playerFirstSpellCast.containsKey(spell.getControllerId())) {
if (event.getType().equals(EventType.SPELL_CAST)) {
if (event.getType() == EventType.SPELL_CAST) {
playerFirstSpellCast.put(spell.getControllerId(), spell.getId());
} else if (event.getType().equals(EventType.CAST_SPELL)) {
} else if (event.getType() == EventType.CAST_SPELL) {
playerFirstCastSpell.put(spell.getControllerId(), spell.getId());
}
}
@ -58,10 +58,6 @@ public class FirstSpellCastThisTurnWatcher extends Watcher {
}
public UUID getIdOfFirstCastSpell(UUID playerId) {
if (playerFirstSpellCast.get(playerId) == null) {
return playerFirstCastSpell.get(playerId);
} else {
return playerFirstSpellCast.get(playerId);
}
return playerFirstSpellCast.getOrDefault(playerId, playerFirstSpellCast.get(playerId));
}
}

View file

@ -39,7 +39,6 @@ import mage.watchers.Watcher;
/**
* Watcher saves the mana that was spent to cast a spell
*
*
* @author LevelX2
*/
public class ManaSpentToCastWatcher extends Watcher {
@ -64,7 +63,7 @@ public class ManaSpentToCastWatcher extends Watcher {
}
}
if (event.getType() == GameEvent.EventType.ZONE_CHANGE && this.getSourceId().equals(event.getSourceId())) {
if (((ZoneChangeEvent) event).getFromZone().equals(Zone.BATTLEFIELD)) {
if (((ZoneChangeEvent) event).getFromZone() == Zone.BATTLEFIELD) {
payment = null;
}
}

View file

@ -31,6 +31,7 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import mage.abilities.Ability;
import mage.abilities.keyword.MiracleAbility;
import mage.cards.Card;
@ -74,14 +75,9 @@ public class MiracleWatcher extends Watcher {
if (game.getPhase() != null && event.getType() == GameEvent.EventType.DREW_CARD) {
UUID playerId = event.getPlayerId();
if (playerId != null) {
Integer amount = amountOfCardsDrawnThisTurn.get(playerId);
if (amount == null) {
amount = 1;
} else {
amount++;
}
amountOfCardsDrawnThisTurn.put(playerId, amount);
if (amount == 1) {
amountOfCardsDrawnThisTurn.putIfAbsent(playerId, 0);
amountOfCardsDrawnThisTurn.compute(playerId, (p, amount) -> amount + 1);
if (amountOfCardsDrawnThisTurn.get(playerId) == 1) {
checkMiracleAbility(event, game);
}
}

View file

@ -66,8 +66,8 @@ public class PlayerAttackedWatcher extends Watcher {
@Override
public void watch(GameEvent event, Game game) {
if (event.getType() == GameEvent.EventType.ATTACKER_DECLARED) {
int numberAttackers = playerAttacked.containsKey(event.getPlayerId()) ? playerAttacked.get(event.getPlayerId()) : 0;
playerAttacked.put(event.getPlayerId(), numberAttackers + 1);
playerAttacked.putIfAbsent(event.getPlayerId(), 0);
playerAttacked.compute(event.getPlayerId(), (p,amount)->amount + 1);
}
}
@ -78,6 +78,6 @@ public class PlayerAttackedWatcher extends Watcher {
}
public int getNumberOfAttackersCurrentTurn(UUID playerId) {
return playerAttacked.containsKey(playerId) ? playerAttacked.get(playerId) : 0;
return playerAttacked.getOrDefault(playerId, 0);
}
}

View file

@ -65,23 +65,14 @@ public class PlayerGainedLifeWatcher extends Watcher {
if (event.getType() == GameEvent.EventType.GAINED_LIFE) {
UUID playerId = event.getPlayerId();
if (playerId != null) {
Integer amount = amountOfLifeGainedThisTurn.get(playerId);
if (amount == null) {
amount = event.getAmount();
} else {
amount = amount + event.getAmount();
}
amountOfLifeGainedThisTurn.put(playerId, amount);
amountOfLifeGainedThisTurn.putIfAbsent(playerId, 0);
amountOfLifeGainedThisTurn.compute(playerId, (p, amount) -> amount +event.getAmount());
}
}
}
public int getLiveGained(UUID playerId) {
Integer amount = amountOfLifeGainedThisTurn.get(playerId);
if (amount != null) {
return amount;
}
return 0;
return amountOfLifeGainedThisTurn.getOrDefault(playerId, 0);
}
@Override

View file

@ -75,19 +75,11 @@ public class PlayerLostLifeWatcher extends Watcher {
}
public int getLiveLost(UUID playerId) {
Integer amount = amountOfLifeLostThisTurn.get(playerId);
if (amount != null) {
return amount;
}
return 0;
return amountOfLifeLostThisTurn.getOrDefault(playerId, 0);
}
public int getLiveLostLastTurn(UUID playerId) {
Integer amount = amountOfLifeLostLastTurn.get(playerId);
if (amount != null) {
return amount;
}
return 0;
return amountOfLifeLostLastTurn.getOrDefault(playerId, 0);
}
@Override

View file

@ -80,10 +80,8 @@ public class ProwlWatcher extends Watcher {
if (creature.getAbilities().containsKey(ChangelingAbility.getInstance().getId()) || creature.getSubtype(game).contains(ChangelingAbility.ALL_CREATURE_TYPE)) {
allSubtypes.add(creature.getControllerId());
} else {
Set<String> subtypes = damagingSubtypes.get(creature.getControllerId());
if (subtypes == null) {
subtypes = new LinkedHashSet<>();
}
Set<String> subtypes = damagingSubtypes.getOrDefault(creature.getControllerId(), new LinkedHashSet<>());
subtypes.addAll(creature.getSubtype(game));
damagingSubtypes.put(creature.getControllerId(), subtypes);
}

View file

@ -33,9 +33,7 @@ import mage.game.events.GameEvent;
import mage.game.events.GameEvent.EventType;
import mage.watchers.Watcher;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.*;
/**
* Watcher stores which sources did damage to anything.
@ -44,7 +42,7 @@ import java.util.UUID;
*/
public class SourceDidDamageWatcher extends Watcher {
public final List<UUID> damageSources = new ArrayList<>();
public final Set<UUID> damageSources = new HashSet<>();
public SourceDidDamageWatcher() {
super("SourceDidDamageWatcher", WatcherScope.GAME);
@ -65,9 +63,8 @@ public class SourceDidDamageWatcher extends Watcher {
if (event.getType() == EventType.DAMAGED_CREATURE
|| event.getType() == EventType.DAMAGED_PLANESWALKER
|| event.getType() == EventType.DAMAGED_PLAYER) {
if (!damageSources.contains(event.getSourceId())) {
damageSources.add(event.getSourceId());
}
}
}