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

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

  var p = adventurejs.Parser.prototype;

  /**
   * Verify that the active verb accepts the sentence structure.
   * @memberOf adventurejs.Parser
   * @method adventurejs.Parser#verifySentenceStructure
   */
  p.verifySentenceStructure = function Parser_verifySentenceStructure() {
    var this_turn = this.input_history[0];
    this.game.log(
      "L1243",
      "log",
      "high",
      "verifySentenceStructure.js > " + this_turn.verified_sentence_structure,
      "Parser"
    );

    let verb_name = this_turn.getVerb(1);
    let verb = this.game.dictionary.verbs[verb_name];
    if (verb && verb.accepts_structures.length) {
      var valid = verb.accepts_structures;
      var structure = this_turn.verified_sentence_structure;
      var found = false;
      for (var j = 0; j < valid.length; j++) {
        if (valid[j] === structure) {
          found = true;
          break;
        }
      }
      if (!found) {
        let msg = "";
        this.game.debug(
          `D1199 | verifySentenceStructure.js | ${verb_name} doesn't accept structure > ${this_turn.verified_sentence_structure} `
        );

        // make a couple of guesses about valid sentence structures
        if (
          this_turn.verified_sentence_structure === "verb" &&
          verb.accepts_structures.indexOf("verb noun") > -1
        ) {
          msg += `What did $(we) want to ${this_turn.input}? `;
          this_turn.setSoftPrompt({
            index: 1,
            type: "noun",
            noun1: true,
            input_verb: this_turn.input_verb,
            verb_phrase: this_turn.verb_phrase,
            verb: this_turn.input_verb,
            structure: "verb noun",
          });
        } else if (
          this_turn.verified_sentence_structure === "verb" &&
          verb.accepts_structures.indexOf("verb preposition noun") > -1
        ) {
          msg += `What did $(we) want to ${this_turn.input}? `;
          this_turn.setSoftPrompt({
            index: 1,
            type: "noun",
            noun1: true,
            preposition1: true,
            input_verb: this_turn.input_verb,
            verb_phrase: this_turn.verb_phrase,
            verb: this_turn.input_verb,
            structure: "verb preposition noun",
          });
        } else if (
          this_turn.verified_sentence_structure === "verb noun" &&
          verb.accepts_structures.indexOf("verb noun noun") > -1
        ) {
          msg += `Where did $(we) want to ${this_turn.input}? `;
          this_turn.setSoftPrompt({
            index: 2,
            type: "noun",
            noun2: true,
            input_verb: this_turn.input_verb,
            verb_phrase: this_turn.verb_phrase,
            verb: this_turn.input_verb,
            structure: "verb noun noun",
          });
        } else if (
          this_turn.verified_sentence_structure === "verb noun" &&
          verb.accepts_structures.indexOf("verb noun preposition noun") > -1
        ) {
          msg += `Where did $(we) want to ${this_turn.input}? `;
          this_turn.setSoftPrompt({
            index: 2,
            type: "noun",
            noun2: true,
            preposition2: true,
            input_verb: this_turn.input_verb,
            verb_phrase: this_turn.verb_phrase,
            verb: this_turn.input_verb,
            structure: "verb noun preposition noun",
          });
        } else msg += this.getUnparsedMessage(this_turn.input);

        this.game.print(msg);
        return false;
      }
    }

    return true;
  };
})(); // verifySentenceStructure