// 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)) {
this.game.log(
"L1038",
"error",
"high",
`[addWordsToLookup.js] addWordsToLookup() received bad words:\n${words}`,
"Parser"
);
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]].key = words[0];
this.game.world_lookup[words[0]].IDs = [];
this.game.world_lookup[words[0]].type = "";
}
// there are several options for type, but plural
// or group should take precedence because we use
// that to handle input like "take keys" without
// needing to disambiguate
if (type) {
if (!this.game.world_lookup[words[0]].type) {
// if it's unset, use this
this.game.world_lookup[words[0]].type = type;
} else if (type === "plural") {
// always set plural?
this.game.world_lookup[words[0]].type = type;
} else if (this.game.world_lookup[words[0]].type === "plural") {
// never override plural?
} else if (this.game.world_lookup[words[0]].type === "group") {
// never override group?
// or should plural override group?
}
}
// 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;
};
})();