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

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

  var p = adventurejs.Dictionary.prototype;

  /**
   * A method to allow authors to add custom pronouns.
   * <h3 class="examples">Example:</h3>
   * <pre class="display"><code class="language-javascript">MyGame.dictionary.addPronouns({
   *   gender: "aile",
   *   singular: true,
   *   subjective_pronoun: "ai",
   *   objective_pronoun: "ain",
   *   possessive_determiner: "aire",
   *   possessive_pronoun: "aires",
   *   reflexive_pronoun: "aiself",
   * });
   * </code></pre>
   * @memberOf adventurejs.Dictionary
   * @method adventurejs.Dictionary#addPronouns
   * @param {Object} pronouns
   * @returns {Object}
   */
  p.addPronouns = function (pronouns) {
    this.game.log(
      "L1619",
      "log",
      "high",
      `Dictionary.addPronouns ${JSON.stringify(pronouns)}`,
      "dictionary"
    );

    let missing = [];
    let unknown = [];
    let invalid = [];
    const keys = Object.keys(pronouns);

    // verify required properties
    if (!keys.includes("gender")) missing.push("gender");
    if (!keys.includes("singular")) missing.push("singular");
    if (!keys.includes("subjective_pronoun"))
      missing.push("subjective_pronoun");
    if (!keys.includes("objective_pronoun")) missing.push("objective_pronoun");
    if (!keys.includes("possessive_determiner"))
      missing.push("possessive_determiner");
    if (!keys.includes("possessive_pronoun"))
      missing.push("possessive_pronoun");
    if (!keys.includes("reflexive_pronoun")) missing.push("reflexive_pronoun");
    if (missing.length) {
      this.game.log(
        "L1620",
        "warn",
        "high",
        `[addPronouns.js] addPronouns() was called with an incomplete list of pronouns: please supply ${String(missing)}.`,
        "Parser"
      );
      return null;
    }

    // for (let prop in foo){console.log(prop, foo[prop])}

    // look for unknown properties
    for (let prop in pronouns) {
      if (
        ![
          "gender",
          "singular",
          "subjective_pronoun",
          "objective_pronoun",
          "possessive_determiner",
          "possessive_pronoun",
          "reflexive_pronoun",
        ].includes(prop)
      ) {
        unknown.push(prop);
      }
      if (
        (prop === "singular" && "boolean" !== typeof pronouns[prop]) ||
        (prop !== "singular" &&
          ("string" !== typeof pronouns[prop] || !pronouns[prop]))
      ) {
        invalid.push(prop);
      }
    }
    if (unknown.length) {
      this.game.log(
        "L1621",
        "warn",
        "high",
        `[addPronouns.js] addPronouns() received invalid pronouns: ${String(unknown)}. Only singular, subjective_pronoun, objective_pronoun, possessive_determiner, possessive_pronoun, and reflexive_pronoun are necessary.`,
        "Parser"
      );
      return null;
    }
    if (invalid.length) {
      this.game.log(
        "L1622",
        "warn",
        "high",
        `[addPronouns.js] addPronouns() received invalid pronouns: ${String(invalid)}. Pronouns should be strings.`,
        "Parser"
      );
      return null;
    }

    // we've validated everything
    // now save to genders and inflections
    // also save a reference to added_genders
    // and added_inflections to help keep track

    this.game.dictionary.genders.push(pronouns.gender);
    this.game.dictionary.added_genders.push(pronouns.gender);

    this.game.dictionary.possessive_determiners.push(
      pronouns.possessive_determiner
    );
    this.game.dictionary.added_possessive_determiners[
      pronouns.possessive_determiner
    ] = pronouns.gender;

    this.game.dictionary.possessive_pronouns.push(pronouns.possessive_pronoun);
    this.game.dictionary.added_possessive_pronouns[
      pronouns.possessive_pronoun
    ] = pronouns.gender;

    this.game.dictionary.reflexive_pronouns.push(pronouns.reflexive_pronoun);
    this.game.dictionary.added_reflexive_pronouns[pronouns.reflexive_pronoun] =
      pronouns.gender;

    this.game.dictionary.objective_pronouns.push(pronouns.objective_pronoun);
    this.game.dictionary.added_objective_pronouns[pronouns.objective_pronoun] =
      pronouns.gender;

    this.game.dictionary.inflections[pronouns.gender] = {
      key: pronouns.gender,
      singular: pronouns.singular,
      subjective_pronoun: pronouns.subjective_pronoun,
      objective_pronoun: pronouns.objective_pronoun,
      possessive_determiner: pronouns.possessive_determiner,
      possessive_pronoun: pronouns.possessive_pronoun,
      reflexive_pronoun: pronouns.reflexive_pronoun,
      we: pronouns.subjective_pronoun,
      us: pronouns.objective_pronoun,
      our: pronouns.possessive_determiner,
      ours: pronouns.possessive_pronoun,
      ourself: pronouns.reflexive_pronoun,
      ourselves: pronouns.reflexive_pronoun,
      "we'd": `${pronouns.subjective_pronoun}'d`,
      "we've": `${pronouns.subjective_pronoun}'${pronouns.singular ? "s" : "ve"}`,
      "we'll": `${pronouns.subjective_pronoun}'ll`,
      "we're": `${pronouns.subjective_pronoun}'${pronouns.singular ? "s" : "re"}`,
      "don't": pronouns.singular ? "doesn't" : "don't",
      "haven't": pronouns.singular ? "hasn't" : "haven't",
      "weren't": pronouns.singular ? "wasn't" : "weren't",
      have: pronouns.singular ? "has" : "have",
      were: pronouns.singular ? "was" : "were",
      are: pronouns.singular ? "is" : "are",
      see: pronouns.singular ? "sees" : "see",
    };
    this.game.dictionary.added_inflections.push(pronouns.gender);

    return this.game.dictionary.inflections[pronouns.gender];
  };
})();