Implemented and cleaned up character schemas

This commit is contained in:
Thaum
2014-11-13 08:44:21 +00:00
parent d80fb19dfe
commit 7ab97a17cc
27 changed files with 403 additions and 259 deletions

View File

@@ -0,0 +1,44 @@
roll = function (n, d){
if(!isNaN(n)){
//first digit is a number
if(d === undefined){
d = n;
n = 1;
}
if(n > 500){
console.log(n + " > 500, cannot lift that many dice to roll them");
return;
}
var result = {sum: 0, rolls: []};
for (var i = 0; i < n; i++){
var roll = Math.floor(Random.fraction() * d + 1)
result.sum += roll;
result.rolls.push(roll);
}
return result;
}
console.log("rolling dice failed for inputs: ", n, d);
return {sum: 0, rolls: []};
}
rollDropLow = function(n, d, drop){
var r = roll(n,d)
r.rolls.sort(function(a, b){return a-b}); //sort ascending
r.rolls.splice(0, drop); //remove the lowest elements
r.sum = 0;
for (var i = 0, l = r.rolls.length; i , l ; i++){
sum += r.rolls[i];
}
return r;
}
rollDropHigh = function(n, d, drop){
var r = roll(n,d)
r.rolls.sort(function(a, b){return b-a}); //sort descending
r.rolls.splice(0, drop); //remove the highest elements
r.sum = 0;
for (var i = 0, l = r.rolls.length; i , l ; i++){
sum += r.rolls[i];
}
return r;
}

View File

@@ -0,0 +1,36 @@
evaluate = function(character, string){
string = string.replace(/\b[a-z]+\b/g, function(sub){
//skill mods
if(character.skills[sub]){
return +character.skillMod(character.skills[sub]);
}
//attributes
if(character.attributes[sub]){
return +character.attributeValue(character.attributes[sub]);
}
return sub;
});
try{
result = math.eval(string);
return result
} catch(e){
console.log(e)
return string;
}
}
evaluateString = function(character, string){
//define brackets as curly brackets around anything that isn't a curly bracket
var brackets = /\{[^\{\}]*\}/g;
var result = string.replace(brackets, function(exp){
var exp = exp.replace(/(\{|\})/g, "") //remove brackets
var span = jQuery('<span/>', {
title: exp,
text: evaluate(character, exp),
class: "calculatedValue"
});
return span.prop('outerHTML');
});
//this is going to return HTML, ensure it is santized!
return result;
}

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,49 @@
// turns dot notation strings into keys of root
// argument formats:
// 157, anything -> 157
// "some.number", object -> object.some.number
// "some.function", object -> object.some.function()
// "some.function arg1 arg2", object -> object.some.function(arg1, arg2)
pop = function(input, root){
if(typeof(input) === "string"){
//we need root for this part
if(root === undefined) return;
//this is a likely to fail if the string is malformed
try{
//split over spaces
var parts = input.split(" ");
//for each word
for (var i = 0; i < parts.length; i++){
//split over dots
var str = parts[i].split(".");
//start at root
parts[i] = root;
//for each word between dots
for (var j = 0; j < str.length; j++){
parts[i] = parts[i][str[j]];
}
}
//pull the first word out, might be a function
var func = parts.splice(0, 1)[0];
//if it's a function, apply the arguments to it
if(_.isFunction(func)) return +func.apply(root, parts);
//if it's a number, return it
if(!isNaN(func)) return +func;
} catch (err) {
//TODO pokemon catch statement is bad
//"gotta catch em all"
console.log(err);
return;
}
}
return +input;
}