//convertTemperature.js
/*global adventurejs A*/
"use strict";
/**
* INCOMPLETE. A simple function to check whether a value is === false or null.
* @method adventurejs.Game#convertTemperature
* @memberOf adventurejs.Game
* @param {Number|String} temperature In Celsius.
* @param {String} parent_id The ID of the game object the temperature applies to.
* @returns {Boolean}
* @todo Finish writing this.
*/
adventurejs.convertTemperature = function Adventurejs_convertTemperature(temperature, parent_id) {
var newtemperature;
// if it's number-as-string, convert to number
// we default to celsius, so there y'go
if( false === isNaN( temperature ) )
{
newtemperature = Number( temperature );
}
// if it's a string, check for digits and suffix
else if( "string" === typeof temperature )
{
// strip out spaces
newtemperature = temperature.replace(/[ ]/g, "");
var digits = "";
var suffix = "";
// separate digits from suffix
// we're going to allow different forms of suffixes
// so we're going to check every character
for(var char in temperature)
{
var digit = ( false === isNaN(temperature[char]) || "." == temperature[char] );
if( digit )
{
// we only count digits until we start assembling a suffix
// if this is true, we've encountered a digit in what we
// think is the suffix and we don't know what to do with that
if( 0 > suffix.length ) break;
digits = digits + temperature[char];
continue;
}
suffix = suffix + temperature[char];
}
digits = Number( digits );
if( "f" === suffix.toLowerCase() ) {
// convert fahrenheit to celsius
newtemperature = ( digits - 32 ) * ( 5 / 9 );
}
// no need here for c to f, but it would be
// newtemperature = ( digits * ( 9 / 5 ) ) + 32
}
if( isNaN( newtemperature ) )
{
newtemperature = this.game.settings.room_temperature;
// let author know we don't know what to do with temperature
// and set newtemperature to 0 for a valid return
var msg = "";
if( "undefined" !== typeof parent_id ) msg += parent_id + "'s ";
else msg += "An object's ";
msg += "temperature was set to an invalid value. [MORE...] "
+ "\nconvertTemperature received a value of " + temperature + ". "
+ "\nAdventure.js defaults to temperature in celsius. "
+ "\nRecognized suffixes are: "
+ "\n - c (celsius)"
+ "\n - f (fahrenheit)";
this.game.log( "warn", "critical", msg , 'Utility' );
}
return newtemperature;
}