var Chance = require('chance'); module.exports = function(config) { var chance = new Chance(); var method = function() { var result = ''; Character = function() { this.gender = null; this.name = null; this.class = null; this.alignment = null; this.stats = []; this.statSum = -1; }; Character.prototype.create = function() { this.gender = chance.gender(); this.name = chance.first({gender: this.gender}); // Throw stats for(var i = 0; i < 6; i++) { var throws = [chance.d6(), chance.d6(), chance.d6(), chance.d6()].sort(); throws.splice(0, 1); var stat = throws.reduce(function(a, b) { return a + b; }, 0); this.stats.push(stat); } this.statSum = this.stats.reduce(function(a, b) { return a + b; }, 0); // Select race var races = { 'Human': '', 'Dwarf': 'Fighter', 'Elf': 'Wizard', 'Gnome': 'Bard', 'Half-Elf': '', 'Half-Orc': 'Barbarian', 'Halfling': 'Rogue', }; this.race = Object.keys(races)[chance.natural({min: 0, max: Object.keys(races).length-1})]; // Select alignment var alignmentY = ['Good', 'Neutral', 'Evil'].splice(chance.natural({min: 0, max: 2}), 1); var alignmentX = ['Lawful', 'Neutral', 'Chaotic'].splice(chance.natural({min: 0, max: 2}), 1); this.alignment = (alignmentY[0] === 'Neutral' && alignmentX[0] === 'Neutral') ? 'True Neutral' : alignmentX[0] + ' ' + alignmentY[0]; // Select class var classes = { 'Barbarian' : '', 'Bard': '', 'Cleric': '', 'Druid': '', 'Fighter': '', 'Monk': '', 'Paladin': '', 'Ranger': '', 'Rogue': '', 'Sorcerer': '', } this.class = Object.keys(classes)[chance.natural({min: 0, max: Object.keys(classes).length-1})]; // Select age var roll = function(times, func) { var sum = 0; for(var i = 0; i < times; i++) { sum += chance[func](); } return sum; } var ages = { 'Human': { start: 15, classes: [roll(1, 'd4'), roll(1, 'd6'), roll(2, 'd6')] }, 'Dwarf': { start: 40, classes: [roll(3, 'd6'), roll(5, 'd6'), roll(6, 'd6')] }, 'Elf': { start: 110, classes: [roll(4, 'd6'), roll(6, 'd6'), roll(10, 'd6')] }, 'Gnome': { start: 40, classes: [roll(4, 'd6'), roll(6, 'd6'), roll(9, 'd6')] }, 'Half-Elf': { start: 20, classes: [roll(1, 'd6'), roll(2, 'd6'), roll(3, 'd6')] }, 'Half-Orc': { start: 14, classes: [roll(1, 'd4'), roll(1, 'd6'), roll(2, 'd6')] }, 'Halfling': { start: 20, classes: [roll(2, 'd4'), roll(3, 'd6'), roll(4, 'd6')] }, }; var ageClasses = { 'Barbarian': 0, 'Rogue': 0, 'Sorcerer': 0, 'Bard': 1, 'Fighter': 1, 'Paladin': 1, 'Ranger': 1, 'Cleric': 2, 'Druid': 2, 'Monk': 2, 'Wizard': 2, }; this.age = ages[this.race].start + ages[this.race].classes[ageClasses[this.class]]; }; Character.prototype.toString = function() { var str = this.age + " years old " + this.alignment + " " + this.gender + " " + this.race + " " + this.class + " named " + this.name; str += " with stats "; this.stats.forEach(function(val) { str += val + " "; }); str += " ("+this.statSum+")"; return str; }; var newChar = new Character(); newChar.create(); result = newChar.toString(); return result; }; return method; }; // export