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

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

  var p = adventurejs.Dictionary.prototype;

  /**
   * A lookup table for strings to describe ranges, such as temperature.
   * @var {Object} adventurejs.Dictionary#string_lookup
   * @default {}
   */
  p.string_lookup = {
    /**
     * Lookup table for prepositions that describe Aspects.
     * @var {Object} adventurejs.Dictionary#string_lookup.prepositions
     * @todo add ability to set new prepositions?
     * @todo revisit - is this needed? not currently using but can see case for 'attached to'
     */
    prepositions: {
      attached: "attached to",
      behind: "behind",
      in: "in",
      on: "on",
      over: "over",
      through: "through",
      under: "under",
      worn: "worn" /* experimental */,
    },

    /**
     * Lookup table for strings that describe the player's position.
     * @var {Object} adventurejs.Dictionary#string_lookup#positions
     */
    posture_positions: {
      default: "resting",
      resting: "resting",
      lying: "lying",
      sitting: "sitting",
      standing: "standing",
      hanging: "hanging",
      suspended: "suspended",
      floating: "floating",
      tied: "tied",
    },

    /**
     * Lookup table for strings that provide a gerund
     * to describe the player's current posture.
     * @var {Object} adventurejs.Dictionary#string_lookup#posture_gerunds
     */
    posture_gerunds: {
      climb: "climbing",
      cling: "clinging",
      crawl: "crawling",
      float: "floating",
      fly: "flying",
      hang: "hanging",
      hover: "hovering",
      kneel: "kneeling",
      lie: "lying",
      sit: "sitting",
      slither: "slithering",
      stand: "standing",
      swim: "swimming",
    },

    /**
     * Lookup table for strings that provide a gerund
     * to describe the player's movement.
     * @var {Object} adventurejs.Dictionary#string_lookup#movement_gerunds
     */
    movement_verbs: {
      climb: "climb",
      cling: "shinny",
      crawl: "crawl",
      float: "float",
      fly: "fly",
      grip: "grip",
      hang: "hang",
      hover: "hover",
      kneel: "crawl",
      lie: "belly crawl",
      sit: "scoot",
      slither: "slither",
      stand: "walk",
      swim: "swim",
    },

    /**
     * Lookup table for strings that describe how full of
     * {@link adventurejs.Substance|Substance} is a
     * {@link adventurejs.Vessel|Vessel}.
     * @var {Object} adventurejs.Dictionary#string_lookup#substance_percents
     */
    substance_percents: {
      0: "empty",
      "0.0": "empty",
      0.1: "slightly full",
      0.2: "about a quarter full",
      0.3: "about a third full",
      0.4: "almost half full",
      0.5: "half full",
      0.6: "slightly over half full",
      0.7: "about three quarters full",
      0.8: "mostly full",
      0.9: "about full",
      "1.0": "full",
      1: "full",
    },

    /**
     * Lookup table for strings that describe temperature of
     * {@link adventurejs.Substance|Substance} in a
     * {@link adventurejs.Vessel|Vessel}.
     * @var {Object} adventurejs.Dictionary#string_lookup#substance_temperatures
     */
    substance_temperatures: {
      0: "freezing",
      5: "near freezing",
      10: "chilled",
      14: "cold",
      17: "cool",
      20: "tepid",
      23: "lukewarm",
      40: "warm",
      60: "hot",
      85: "scalding",
      90: "near boiling",
      100: "boiling",
    },
  };

  /**
   *
   * @memberOf adventurejs.Dictionary
   * @method adventurejs.Dictionary#getStringLookup
   * @param {string} type The type to look up, ie "prepositions"
   * or "posture_gerunds".
   * @param {string} value The value to look up, ie "standing" or "0.1".
   * @returns {string}
   */
  p.getStringLookup = function Dictionary_getStringLookup(type, value) {
    var see = "See adventurejs.Dictionary.html#getStringLookup. ";
    if ("string" !== typeof type) {
      var msg =
        "Dictionary.getStringLookup received a non-string for type. " + see;
      this.game.log("L1312", "warn", "critical", msg, "dictionary");
      return "";
    }
    if ("string" !== typeof value) {
      var msg =
        "Dictionary.getStringLookup received a non-string for " +
        type +
        "value. " +
        see;
      this.game.log("L1313", "warn", "critical", msg, "dictionary");
      return "";
    }
    if ("undefined" === typeof this.string_lookup[type]) {
      var msg =
        "Dictionary.getStringLookup received a non-existent type: " +
        type +
        ". " +
        see;
      this.game.log("L1314", "warn", "critical", msg, "dictionary");
      return "";
    }
    if ("undefined" === typeof this.string_lookup[type][value]) {
      var msg =
        "Dictionary.getStringLookup received a non-existent value of " +
        type +
        ": " +
        value +
        ". " +
        see;
      this.game.log("L1315", "warn", "critical", msg, "dictionary");
      return "";
    }
    return this.string_lookup[type][value];
  };

  /**
   *
   * @memberOf adventurejs.Dictionary
   * @method adventurejs.Dictionary#setStringLookup
   * @param {string} type The type to set, ie "prepositions"
   * or "posture_gerunds".
   * @param {object} values An object containing values to set,
   * ie {"standing":"standing up"} or {"0":"empty","1":"full"}.
   * @returns {boolean}
   */
  p.setStringLookup = function Dictionary_setStringLookup(type, values) {
    var see = "See adventurejs.Dictionary.html#setStringLookup. ";
    if ("string" !== typeof type) {
      var msg =
        "Dictionary.setStringLookup received a non-string for type. " + see;
      this.game.log("L1316", "warn", "critical", msg, "dictionary");
      return false;
    }
    if ("object" !== typeof values) {
      var msg =
        "Dictionary.getStringLookup received a non-object for " +
        type +
        " values. " +
        see;
      this.game.log("L1317", "warn", "critical", msg, "dictionary");
      //console.warn( 'a' );
      return false;
    }
    if ("undefined" === typeof this.string_lookup[type]) {
      this.string_lookup[type] = {};
      var msg =
        "Dictionary.getStringLookup received a non-existent type: " +
        type +
        ". adventurejs will try to add this type. " +
        see;
      this.game.log("L1318", "warn", "critical", msg, "dictionary");
    }

    var valuekeys = Object.keys(values);
    for (var v = 0; v < valuekeys.length; v++) {
      var valuekey = valuekeys[v];
      this.string_lookup[type][valuekey] = values[valuekey];
    }

    msg =
      "Dictionary.setStringLookup: " + JSON.stringify(this.string_lookup[type]);
    this.game.log("L1319", "log", "high", msg, "dictionary");

    return true;
  };

  /*
// this sorts by value
var list = {"you": 100, "me": 75, "foo": 116, "bar": 15};
keysSorted = Object.keys(list).sort(function(a,b){return list[a]-list[b]})
console.log(keysSorted);     

// this sorts by key
var list = {"100": "you", "75":"me", "116":"foo", "15":"bar"};console.log("Object.keys(list): " + Object.keys(list));     
keysSorted = Object.keys(list).sort(function(a,b){return a-b})
console.log("keysSorted: " + keysSorted);     
*/

  /**
   *
   * @memberOf adventurejs.Dictionary
   * @method adventurejs.Dictionary#getStringLookupByRange
   * @param {string} type The type to look up, ie "prepositions"
   * or "posture_gerunds".
   * @param {string|number} value The numerical value to look up, ie "70" or "0.1".
   * @returns {string}
   */
  p.getStringLookupByRange = function Dictionary_getStringLookupByRange(
    type,
    value
  ) {
    var see = "See adventurejs.Dictionary.html#getStringLookupByRange. ";
    var str_return = "";
    var keysSorted;

    if ("string" !== typeof type) {
      var msg =
        "Dictionary.getStringLookupByRange received a non-string for type. " +
        see;
      this.game.log("L1320", "warn", "critical", msg, "dictionary");
    }
    if (isNaN(value)) {
      var msg =
        "Dictionary.getStringLookupByRange received a non-numerical value for " +
        type +
        " value. " +
        see;
      this.game.log("L1321", "warn", "critical", msg, "dictionary");
    }
    if ("undefined" === typeof this.string_lookup[type]) {
      var msg =
        "Dictionary.getStringLookup received a non-existent type: " +
        type +
        ". " +
        see;
      this.game.log("L1322", "warn", "critical", msg, "dictionary");
      return "";
    }

    keysSorted = Object.keys(this.string_lookup[type]).sort(function (a, b) {
      return a - b;
    });
    for (var i = 0; i < keysSorted.length; i++) {
      if (Number(value) >= keysSorted[i]) {
        str_return = this.string_lookup[type][keysSorted[i]];
      }
    }

    return str_return;
  };
})();