Refactor the core zone change code to use a common code path.

This commit is contained in:
Samuel Sandeen 2016-09-07 23:31:26 -04:00
parent b87f91fd97
commit c33a731a4a
9 changed files with 539 additions and 570 deletions

View file

@ -55,108 +55,4 @@ public class PermanentMeld extends PermanentCard {
return this.getCard().getConvertedManaCost();
}
}
@Override
public boolean moveToZone(Zone toZone, UUID sourceId, Game game, boolean flag, ArrayList<UUID> appliedEffects) {
ZoneChangeEvent event = new ZoneChangeEvent(this.getId(), sourceId, this.getOwnerId(), Zone.BATTLEFIELD, toZone, appliedEffects);
if (!game.replaceEvent(event)) {
Player controller = game.getPlayer(this.getControllerId());
if (controller != null) {
controller.removeFromBattlefield(this, game);
updateZoneChangeCounter(game);
MeldCard meldCard = (MeldCard) this.getCard();
Card topHalfCard = meldCard.getTopHalfCard();
Card bottomHalfCard = meldCard.getBottomHalfCard();
switch (event.getToZone()) {
case GRAVEYARD:
game.getPlayer(this.getOwnerId()).putInGraveyard(topHalfCard, game);
game.getPlayer(this.getOwnerId()).putInGraveyard(bottomHalfCard, game);
break;
case HAND:
game.getPlayer(this.getOwnerId()).getHand().add(topHalfCard);
game.getPlayer(this.getOwnerId()).getHand().add(bottomHalfCard);
break;
case EXILED:
game.getExile().getPermanentExile().add(topHalfCard);
game.getExile().getPermanentExile().add(bottomHalfCard);
break;
case LIBRARY:
CardsImpl cardsToMove = new CardsImpl();
cardsToMove.add(topHalfCard);
cardsToMove.add(bottomHalfCard);
if (flag) {
controller.putCardsOnTopOfLibrary(cardsToMove, game, null, true);
} else {
controller.putCardsOnBottomOfLibrary(cardsToMove, game, null, true);
}
break;
default:
return false;
}
meldCard.setMelded(false);
game.setZone(topHalfCard.getId(), event.getToZone());
game.setZone(bottomHalfCard.getId(), event.getToZone());
meldCard.setTopLastZoneChangeCounter(topHalfCard.getZoneChangeCounter(game));
meldCard.setBottomLastZoneChangeCounter(bottomHalfCard.getZoneChangeCounter(game));
game.addSimultaneousEvent(event);
return true;
}
}
return false;
}
@Override
public boolean moveToExile(UUID exileId, String name, UUID sourceId, Game game, ArrayList<UUID> appliedEffects) {
ZoneChangeEvent event = new ZoneChangeEvent(this.getId(), sourceId, this.getOwnerId(), Zone.BATTLEFIELD, Zone.EXILED, appliedEffects);
if (!game.replaceEvent(event)) {
Player controller = game.getPlayer(this.getControllerId());
if (controller != null) {
controller.removeFromBattlefield(this, game);
updateZoneChangeCounter(game);
MeldCard meldCard = (MeldCard) this.getCard();
Card topHalfCard = meldCard.getTopHalfCard();
Card bottomHalfCard = meldCard.getBottomHalfCard();
switch (event.getToZone()) {
case GRAVEYARD:
game.getPlayer(this.getOwnerId()).putInGraveyard(topHalfCard, game);
game.getPlayer(this.getOwnerId()).putInGraveyard(bottomHalfCard, game);
break;
case HAND:
game.getPlayer(this.getOwnerId()).getHand().add(topHalfCard);
game.getPlayer(this.getOwnerId()).getHand().add(bottomHalfCard);
break;
case EXILED:
if (exileId == null) {
game.getExile().getPermanentExile().add(topHalfCard);
game.getExile().getPermanentExile().add(bottomHalfCard);
} else {
game.getExile().createZone(exileId, name).add(topHalfCard);
game.getExile().getExileZone(exileId).add(bottomHalfCard);
}
break;
case LIBRARY:
CardsImpl cardsToMove = new CardsImpl();
cardsToMove.add(topHalfCard);
cardsToMove.add(bottomHalfCard);
if (event.getFlag()) {
controller.putCardsOnTopOfLibrary(cardsToMove, game, null, true);
} else {
controller.putCardsOnBottomOfLibrary(cardsToMove, game, null, true);
}
break;
default:
return false;
}
meldCard.setMelded(false);
game.setZone(meldCard.getId(), Zone.OUTSIDE);
game.setZone(topHalfCard.getId(), event.getToZone());
game.setZone(bottomHalfCard.getId(), event.getToZone());
meldCard.setTopLastZoneChangeCounter(topHalfCard.getZoneChangeCounter(game));
meldCard.setBottomLastZoneChangeCounter(bottomHalfCard.getZoneChangeCounter(game));
game.addSimultaneousEvent(event);
return true;
}
}
return false;
}
}