new getCharacter2 plugin

This commit is contained in:
lanxu 2017-03-12 00:06:38 +02:00
parent 4babc165f8
commit a6ad04074c
5 changed files with 125 additions and 4 deletions

View File

@ -14,6 +14,8 @@ Irkkibotti kulmapaikkalaisille. Sisältää seuraavat huimat ominaisuudet:
* Hakee youtubesta hakusanalla
- .luohahmo
* Luo D&D-hahmon
- .luohahmo2
* Luo D&D-hahmon
##Automaattiset toiminnot

6
app.js
View File

@ -32,6 +32,7 @@ var googletin = require('./googleapi.js')(config);
var owmapi = require('./owmapi')(config);
var getTemperatureString = require('./plugins/getWeather')(config);
var getDnDCharacter = require('./plugins/getCharacter')();
var getDnDCharacter2 = require('./plugins/getCharacter2')();
var getDiceString = require('./plugins/getDice')();
var getUrlString = require('./plugins/getUrl')();
var getTweet = require('./plugins/getTweet')(config);
@ -114,6 +115,11 @@ var handleMessage = function(from, to, message) {
client.say(to, ''+character);
}
// Create Character
if (res = message.match(/^\.luohahmo2$/i)) {
var character = getDnDCharacter2();
client.say(to, ''+character);
}
// Instant Answer
if (res = message.match(/^\.wtf (.+)$/i)) {
getInstantAnswer(res[1]).then(function(answer) {

View File

@ -76,7 +76,12 @@ module.exports = function(config) {
//console.log(resp.items[0].snippet.channelTitle);
//console.log(resp, videoId);
if(resp.items.length <= 0) {
if(resp === null || resp.items === null) {
reject('No results (null)');
return;
}
if(resp.items.length <= 0) {
reject('No results');
return;
}

View File

@ -28,7 +28,8 @@
"irc": "latest",
"googleapis": "latest",
"node-ddg-api": "latest",
"twitter": "latest"
"twitter": "latest",
"chance": "=1.0.6"
},
"devDependencies": {
"nodemon": "^1.2.1"

107
plugins/getCharacter2.js Normal file
View File

@ -0,0 +1,107 @@
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