Ejemplo n.º 1
0
  // TODO Refactor this with all the equipment tests.
  @Override
  public int passes(final Prerequisite prereq, final CharacterDisplay display, CDOMObject source)
      throws PrerequisiteException {
    final int number;
    try {
      number = Integer.parseInt(prereq.getOperand());
    } catch (NumberFormatException e) {
      throw new PrerequisiteException(
          LanguageBundle.getFormattedString(
              "PreItem.error.bad_operand", prereq.toString())); // $NON-NLS-1$
    }

    int runningTotal = 0;

    if (display.hasEquipment()) {
      // Work out exactlywhat we are going to test.
      final String aString = prereq.getKey();
      List<String> typeList = null;
      if (aString.startsWith(Constants.LST_TYPE_EQUAL)
          || aString.startsWith(Constants.LST_TYPE_DOT)) {
        String stripped = aString.substring(Constants.SUBSTRING_LENGTH_FIVE);
        typeList = CoreUtility.split(stripped, '.');
      }

      for (Equipment eq : display.getEquipmentSet()) {
        if (typeList != null) {
          // Check to see if the equipment matches
          // all of the types in the requested list;
          boolean bMatches = true;
          for (int i = 0, x = typeList.size(); i < x; ++i) {
            if (!eq.isType(typeList.get(i))) {
              bMatches = false;
              break;
            }
          }
          if (bMatches) {
            runningTotal++;
          }
        } else { // not a TYPE string
          final String eqName = eq.getName().toUpperCase();

          if (aString.indexOf('%') >= 0) {
            // handle wildcards (always assume
            // they end the line)
            final int percentPos = aString.indexOf('%');
            final String substring = aString.substring(0, percentPos).toUpperCase();
            if ((eqName.startsWith(substring))) {
              ++runningTotal;
              break;
            }
          } else if (eqName.equalsIgnoreCase(aString)) {
            // just a straight String compare
            ++runningTotal;
            break;
          }
        }
      }
    }

    runningTotal = prereq.getOperator().compare(runningTotal, number);
    return countedTotal(prereq, runningTotal);
  }