Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
// addWordsToLookup.js
(function () {
  /* global adventurejs A */

  var p = adventurejs.Game.prototype;

  /**
   * <strong>addWordsToLookup()</strong> takes words associated with
   * this asset and adds them to the global lookup table.
   * @method adventurejs.Game#addWordsToLookup
   * @memberOf adventurejs.Game
   * @param {Object} asset
   * @param {Array} words
   * @param {String} type
   */
  p.addWordsToLookup = function Game_addWordsToLookup(asset, words, type) {
    //console.log(`add asset.id:`, asset.id, `words:`, words, `type:`, type);
    if (!asset) return;
    if ("string" !== typeof words && !Array.isArray(words)) {
      console.warn(`addWordsToLookup received bad words:`, words);
      return;
    }
    if (!words.length) return;
    while (words.length > 0) {
      if (!words[0] || "string" !== typeof words[0] || " " === words[0]) {
        words.shift();
        continue;
      }

      if (words[0].includes(".")) {
        words.push(words[0].replace(/\./g, ""));
      }

      words[0] = words[0].trim().toLowerCase();

      if (!asset.keywords.includes(words[0])) {
        asset.keywords.push(words[0]);
      }

      // is this word not in our lookup yet?
      if (false === words[0] in this.game.world_lookup) {
        this.game.world_lookup[words[0]] = {};
        this.game.world_lookup[words[0]].IDs = [];

        // @TODO investigate this
        // first thing to send this word to lookup defines type
        // but may be that first thing is synonym eg "nut container"
        // while subsequent thing might be singular "nut"
        if (type) {
          this.game.world_lookup[words[0]].type = type;
        }
      }

      // is current object represented by this word yet?
      if (-1 === this.game.world_lookup[words[0]].IDs.indexOf(asset.id)) {
        this.game.world_lookup[words[0]].IDs.push(asset.id);
      }

      if (words[0].includes(".")) {
        this.addWordsToLookup(asset, [words[0].replace(".", "")], type);
      }

      words.shift();
    }

    return;
  };
})();