Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
// validateAssetPrecursor.js

(function () {
  /*global adventurejs A*/

  var p = adventurejs.Game.prototype;

  /**
   * Validate unclassed precursor object prior to creating an asset.
   * Generic objects must have at minimum a valid class and a name.
   * Name is used to generate ID.
   * Exits are the exception, where ID is generated from location + direction.
   * @method adventurejs.Game#validateAssetPrecursor
   * @memberOf adventurejs.Game
   * @param {Object} child A generic object.
   * @returns {Object} A classed object.
   */
  p.validateAssetPrecursor = function Game_validateAssetPrecursor(child) {
    // has it got a class?
    if (typeof child.class === "undefined") {
      this.game.log("L1062", "error", "critical", [
        "Whatever this is, it needs a class definition.",
        child,
      ]);
      return false;
    }

    // all classes are initial uppercase
    // so let's ensure that is true
    //child.class = A.propercase( child.class );

    // is its class a valid class?
    if (typeof adventurejs[child.class] === "undefined") {
      this.game.log(
        "L1063",
        "error",
        "critical",
        "This object, " +
          child.name +
          ", has an unrecognized class, " +
          child.class +
          ". If you think this is in error, double check " +
          "that you spelled its class correctly. " +
          "Remember, class names are case sensitive."
      );
      return false;
    }

    // has it got a name?
    let nameless = ["Exit", "GlobalString"];
    if (
      typeof child.name === "undefined" &&
      nameless.indexOf(child.class) === -1
    ) {
      this.game.log("L1064", "error", "critical", [
        "This " + child.class + " has no name.",
        child,
      ]);
      return false;
    }

    // if it has a noun property, is the noun a string?
    if ("undefined" !== typeof child.noun && "string" !== typeof child.noun) {
      this.game.log(
        "L1065",
        "error",
        "critical",
        child.class +
          " " +
          child.name +
          "'s noun property is not a string, and it should be. "
      );
      return false;
    }

    // if it has a plural property, is the plural a string?
    if (
      "undefined" !== typeof child.plural &&
      "string" !== typeof child.plural
    ) {
      this.game.log(
        "L1066",
        "error",
        "critical",
        child.class +
          " " +
          child.name +
          "'s plural property is not a string, and it should be. "
      );
      return false;
    }

    // if it has a noun property, does it also have a plural prop?
    if (
      "undefined" !== typeof child.noun &&
      "string" === typeof child.noun &&
      "string" !== typeof child.plural
    ) {
      this.game.log(
        "L1067",
        "error",
        "critical",
        child.class +
          " " +
          child.name +
          "'s has a custom noun property but no plural property. "
      );
      return false;
    }

    // if it has a plural property, does it also have a noun prop?
    if (
      "undefined" !== typeof child.plural &&
      "string" === typeof child.plural &&
      "string" !== typeof child.noun
    ) {
      this.game.log(
        "L1068",
        "error",
        "critical",
        child.class +
          " " +
          child.name +
          "'s has a custom plural property but no noun property. "
      );
      return false;
    }

    // if it has custom noun/plural pairs written as a string
    // is the string formatted correctly?
    // and convert it into an array
    if (
      "undefined" !== typeof child.singlePluralPairs &&
      "string" === typeof child.singlePluralPairs
    ) {
      child.singlePluralPairs = child.singlePluralPairs.split(",");
      for (var i = 0; i < child.singlePluralPairs.length; i++) {
        child.singlePluralPairs[i] = child.singlePluralPairs[i].trim();
      }
      if (
        child.singlePluralPairs.length !==
        Math.floor(child.singlePluralPairs.length * 0.5 * 2)
      ) {
        this.game.log(
          "L1069",
          "error",
          "critical",
          child.class +
            " " +
            child.name +
            "'s singlePluralPairs property has an odd number of items. " +
            "It should have an even number of noun/plural pairs."
        );
        return false;
      }
      var tempArray = [];
      for (var i = 0; i < child.singlePluralPairs.length; i += 2) {
        tempArray.push([
          child.singlePluralPairs[i],
          child.singlePluralPairs[i + 1],
        ]);
      }
      child.singlePluralPairs = tempArray;
    }

    // if it has custom noun/plural pairs in an array,
    // is the array formatted correctly?
    if (
      "undefined" !== typeof child.singlePluralPairs &&
      Array.isArray(child.singlePluralPairs)
    ) {
      for (var i = 0; i < child.singlePluralPairs.length; i++) {
        if (
          false === Array.isArray(child.singlePluralPairs[i]) ||
          2 !== child.singlePluralPairs[i].length
        ) {
          this.game.log(
            "L1070",
            "error",
            "critical",
            child.class +
              " " +
              child.name +
              "'s singlePluralPairs property has an array item which isn't a pair. " +
              "It should have an even number of noun/plural pairs."
          );
          return false;
        }
      }
    }

    // SUCCESS!

    return child;
  };
})();