Exemple #1
0
  /** keys are player IDs, values are the number of spells the player cast in the previous turn */
  public static final class Tracker
      extends org.rnd.jmagic.engine.Tracker<java.util.Map<Integer, Integer>> {
    private java.util.HashMap<Integer, Integer> thisTurn =
        new java.util.HashMap<Integer, Integer>();
    private java.util.HashMap<Integer, Integer> previousTurn =
        new java.util.HashMap<Integer, Integer>();
    private java.util.Map<Integer, Integer> unmodifiable =
        java.util.Collections.unmodifiableMap(this.previousTurn);

    @SuppressWarnings("unchecked")
    @Override
    public Tracker clone() {
      Tracker ret = (Tracker) super.clone();
      ret.thisTurn = (java.util.HashMap<Integer, Integer>) this.thisTurn.clone();
      ret.previousTurn = (java.util.HashMap<Integer, Integer>) this.previousTurn.clone();
      ret.unmodifiable = java.util.Collections.unmodifiableMap(ret.previousTurn);
      return ret;
    }

    @Override
    protected boolean match(GameState state, Event event) {
      if (event.type == EventType.CAST_SPELL_OR_ACTIVATE_ABILITY) {
        if (event.getResult(state).getOne(GameObject.class).isSpell()) return true;
      } else if (event.type == EventType.BEGIN_TURN) return true;
      return false;
    }

    @Override
    protected java.util.Map<Integer, Integer> getValueInternal() {
      return this.unmodifiable;
    }

    @Override
    protected void update(GameState state, Event event) {
      if (event.type == EventType.CAST_SPELL_OR_ACTIVATE_ABILITY) {
        Player caster =
            event
                .parameters
                .get(EventType.Parameter.PLAYER)
                .evaluate(state, null)
                .getOne(Player.class);
        if (this.thisTurn.containsKey(caster.ID))
          this.thisTurn.put(caster.ID, this.thisTurn.get(caster.ID) + 1);
        else this.thisTurn.put(caster.ID, 1);
      } else
      // it's a turn change
      {
        this.previousTurn.clear();
        this.previousTurn.putAll(this.thisTurn);
        this.thisTurn.clear();
      }
    }

    @Override
    protected void reset() {
      // This is an actively resetting tracker.
    }
  }
Exemple #2
0
 @SuppressWarnings("unchecked")
 @Override
 public Tracker clone() {
   Tracker ret = (Tracker) super.clone();
   ret.thisTurn = (java.util.HashMap<Integer, Integer>) this.thisTurn.clone();
   ret.previousTurn = (java.util.HashMap<Integer, Integer>) this.previousTurn.clone();
   ret.unmodifiable = java.util.Collections.unmodifiableMap(ret.previousTurn);
   return ret;
 }