// findCharacterComma.js
(function () {
/*global adventurejs A*/
var p = adventurejs.Parser.prototype;
/**
* By default, player is the target character.
* In the case of an NPC command such as
* "Claude, take the shovel" we want to strip
* the character name from the sentence and save
* a record of the target character back to the Input.
* @memberOf adventurejs.Parser
* @method adventurejs.Parser#findCharacterComma
* @param {String} input Player input.
* @returns {String}
* @TODO this needs to consider names with spaces in them
* or serialized names
*/
p.findCharacterComma = function Parser_findCharacterComma(input) {
var this_turn = this.input_history[0];
var player = this.game.getPlayer();
// if input starts with "[string], "
// see if string resolves to a character
// if it does, set character as target
// const input = "Paul, go north";
const pattern = /^(\w+),\s/;
const match = input.match(pattern);
function isPresentAndCharacter() {}
if (match) {
const name = match[1]; // Captures the name before the comma
const remaining = input.replace(pattern, ""); // Removes "Name, " from the input
const parsed_noun = this.parseNoun(name);
let char;
let chars = [];
let msg = "";
let debug = "";
switch (parsed_noun.matches.qualified.length) {
case 0:
// no asset found
// console.warn("no assets found");
msg = `$(We) $(don't) know of anyone named ${name}. `;
debug = "not found";
break;
case 1:
// found an asset
// console.warn("singular asset found", parsed_noun.matches.qualified);
char = this.game.getAsset(parsed_noun.matches.qualified[0]);
if (!char) {
msg = `$(We) $(don't) know of anyone named ${name}. `;
debug = "not found";
break;
} else if (!player.knowsAbout(char)) {
msg = `$(We) $(don't) know of anyone named ${name}. `;
debug = "not known";
break;
} else if (!char.hasClass("Character")) {
msg = char.is.present
? `${char.Articlename} ignores $(us). `
: `$(We) $(don't) see any ${name} here. `;
debug = "not a character";
break;
} else {
if (!char.is.present) {
msg = `$(We) $(don't) see ${char.articlename} here. `;
debug = "not present";
break;
}
}
break;
default:
// found multiple assets
// console.warn("multiple assets found", parsed_noun.matches.qualified);
// can we find a singular character?
for (let i = 0; i < parsed_noun.matches.qualified.length; i++) {
let asset = this.game.getAsset(parsed_noun.matches.qualified[i]);
if (asset.hasClass("Character") && asset.is.present) {
chars.push(asset);
}
}
switch (chars.length) {
case 0:
msg = `No one named ${name} is present. `;
debug = `not found`;
break;
case 1:
char = chars[0];
break;
default:
msg = `$(We'll) have to be more specific. `;
debug = `not unique`;
break;
}
break;
}
if (msg) {
// any msg means we failed to identify a target character
this.game.debug(`D1178 | findCharacterComma.js | ${name} is ${debug}`);
this.game.print(msg);
return false;
}
if (char) {
this_turn.setCharacter(char);
return remaining;
}
}
return input;
};
})();