GloryofWarfare.java :  » Game » magicwars » mw » server » card » gold » rw » Java Open Source

Java Open Source » Game » magicwars 
magicwars » mw » server » card » gold » rw » GloryofWarfare.java
package mw.server.card.gold.rw;

import java.util.Observable;
import java.util.Observer;

import mw.server.list.CardList;
import mw.server.model.Card;
import mw.server.model.effect.GlobalEffect;
import mw.mtgforge.Command;
import mw.server.GameManager;

@SuppressWarnings("serial")
public class GloryofWarfare {

    public static Card getCard(final GameManager game, final Card card) {

        final GlobalEffect gloryEffect = new GlobalEffect() {

            public void applyEffect() {

                CardList creatures = game.getBattlefield().getPermanentList(card.getControllerID());
                creatures = creatures.getType("Creature");
                
                int cid = card.getControllerID();
                int turnOwnerId = game.getManager().getTurnOwnerID();

                /**
                 * Apply for each creature
                 */
                for (int j = 0; j < creatures.size(); j++) {
                    applyForNewPermanent(creatures.get(j), cid == turnOwnerId);
                }
            }

            private void applyForNewPermanent(Card c, boolean isYourTurn) {
              if (c.isCreature() && c.getControllerID() == card.getControllerID()) {
                if (isYourTurn) {
                  c.addAttack(2);
                  c.addAspect("touchedByGloryofWarfare_Attack" + card.getTableID());
                } else {
                  c.addDefense(2);
                  c.addAspect("touchedByGloryofWarfare_Defense" + card.getTableID());
                }
              }
            }
            
            public void applyForNewPermanent(Card c) {
                if (c.isCreature() && c.getControllerID() == card.getControllerID()) {
                  
                    int cid = card.getControllerID();
                    int turnOwnerId = game.getManager().getTurnOwnerID();
                    
                if (cid == turnOwnerId) {
                  c.addAttack(2);
                  c.addAspect("touchedByGloryofWarfare_Attack" + card.getTableID());
                } else {
                  c.addDefense(2);
                  c.addAspect("touchedByGloryofWarfare_Defense" + card.getTableID());
                }
                }
            }
            
            public void discardFromPermanent(Card c) {
                if (c.isCreature() && c.getControllerID() == card.getControllerID()) {
                  if (c.hasAspect("touchedByGloryofWarfare_Attack" + card.getTableID())) {
                    c.subAttack(2);  
                    c.removeAspect("touchedByGloryofWarfare_Attack" + card.getTableID());
                  }
                  if (c.hasAspect("touchedByGloryofWarfare_Defense" + card.getTableID())) {
                    c.subDefense(2);
                    c.removeAspect("touchedByGloryofWarfare_Defense" + card.getTableID());
                  }
                }
            }

            public void discardEffect() {

                CardList creatures = game.getBattlefield().getPermanentList(card.getControllerID());
                creatures = creatures.getType("Creature");

                /**
                 * Discard from each creature
                 */
                for (int j = 0; j < creatures.size(); j++) {
                    discardFromPermanent(creatures.get(j));
                }
            }
            
      public Card getEffectOwnerCard() {
        return card;
      }
        };

        final Observer changeTurnObserver = new Observer() {

      public void update(Observable o, Object arg) {
        int cid = card.getControllerID();
        
        CardList creatures = game.getBattlefield().getPermanentList(cid);
        creatures = creatures.getType("Creature");
      
        if (creatures.size() > 0) {
          for (Card c : creatures) {
            gloryEffect.discardFromPermanent(c);
            gloryEffect.applyForNewPermanent(c);
          }
          game.getManager().requestTableUpdate();
        }
      }
        };
        
        final Command leavePlayCommand = new Command() {
            public void execute() {
                game.getManager().removeGlobalEffect(gloryEffect);
                game.getEventManager().getChangeTurnEvent().deleteObserver(changeTurnObserver);
            }
        };
        card.setLeavesTheBattlefieldCommand(leavePlayCommand);
        
        final Command comesIntoPlayCommand = new Command() {
            public void execute() {
                game.getManager().addGlobalEffect(gloryEffect);
                game.getEventManager().getChangeTurnEvent().addObserver(changeTurnObserver);
            }
        };
        card.setEntersTheBattlefieldCommand(comesIntoPlayCommand);

        return card;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.