/**
   * Execute an action on this FSM, the action acts like a transition and will change its status.
   *
   * @param action the action to execute
   * @return a pair of public and private messages containing the result of the action
   */
  public ResultMsgPair executeAction(Action action) {

    if (state.isActionValid(action, game)) {

      // The next state of this FSM, if null the status should not change
      // (this keeps the FSM much more simple!)
      TurnState nextState = state.executeAction(action, game);

      if (nextState != null) {

        setState(nextState);
      }

      // Get the ResultMsgPair subsequently yo the execution of the action
      // (must be called only after execution!)
      return action.getResult();

    } else {

      // The action is NOT valid, inform the client and don't broadcast
      // any message. We inform that the problem is with the game rules
      // (invalid sequence of actions or action parameters not allowed).
      return new ResultMsgPair(new InvalidRequestMsg("Request against game rules" + "."), null);
    }
  }
Example #2
0
  /**
   * Returns an array of all general messages from the last adjudicated turn state.
   *
   * @param power the power to get results for
   * @param format one of the format constants
   * @return an array of <code>JdipResultAdapter</code>s
   */
  public JdipResult[] getAllResultsForPower(JdipPower power, int format) {
    OrderFormatOptions ofo = this.ceateOrderFormat(format);

    TurnState state = world.getPreviousTurnState(world.getLastTurnState());
    List allResults = state.getResultList();
    ArrayList generalResults = new ArrayList();
    Iterator it = allResults.iterator();

    while (it.hasNext()) {
      JdipResult r = new JdipResult((Result) it.next(), ofo);
      // find all resluts for given power
      if (!r.isGeneralResult() && r.getPower().equals(power.getPowerName())) {
        generalResults.add(r);
      }
    }

    return toJdipResultArray(generalResults.toArray());
  }
Example #3
0
  /**
   * Returns an array of all general messages from the last adjudicated turn state.
   *
   * @param format one of the format constants
   * @return an array of <code>JdipResultAdapter</code>s
   */
  public JdipResult[] getAllGeneralResults(int format) {
    OrderFormatOptions ofo = this.ceateOrderFormat(format);

    TurnState state = world.getPreviousTurnState(world.getLastTurnState());
    List allResults = state.getResultList();
    ArrayList generalResults = new ArrayList();
    Iterator it = allResults.iterator();

    while (it.hasNext()) {
      JdipResult r = new JdipResult((Result) it.next(), ofo);
      // find all general resluts
      if (r.isGeneralResult()) {
        generalResults.add(r);
      }
    }

    return toJdipResultArray(generalResults.toArray());
  }
  /**
   * Change the current state of the State machine.
   *
   * @param state the new state
   */
  public void setState(TurnState state) {

    LOG.log(Level.INFO, "State changed to: " + state.getClass().getSimpleName());
    this.state = state;
  }