kulmapaikka-ircbot/plugins/dice.js

69 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

var Promise = require('promise');
var requestPromise = require('./helpers/requestPromise.js');
var BasePlugin = require('./base.js');
var logger = require('winston');
class Dice extends BasePlugin {
constructor(config) {
super(config);
this.name = 'Dice';
}
test(input) {
var res = null;
if (res = input.match(/^\.heit(a|ä) ([0-9]+)d([0-9]+)$/i)) {
return true;
}
if (res = input.match(/^\.heit(a|ä) voltti homo$/i)) {
return true
}
return false;
}
getDiceString(numThrows, sides) {
var total = 0;
var totalThrows = [];
numThrows = Number(numThrows);
sides = Number(sides);
if(numThrows === 0 || sides === 0) {
return 'Tyhjää ei voi heittää';
}
if(numThrows > 100) {
return 'Ei jaksa...';
}
for(var a = 0; a < numThrows; a++) {
var t = Math.floor(Math.random()*sides)+1;
totalThrows.push(t);
total += t;
}
var resultString = '';
totalThrows.forEach(function(value) {
resultString += value + ', ';
});
resultString = resultString.substring(0, resultString.length - 2);
resultString += ' (yhteensä: ' + total + '. '+ (total/numThrows).toFixed(2) + ' per heitto)';
return resultString;
}
result(input) {
// Different cases
let res = null
if (res = input.match(/^\.heit(a|ä) ([0-9]+)d([0-9]+)$/i)) {
var th = this.getDiceString(res[2], res[3]);
return 'Heitettiin: '+th;
}
if (input.match(/^\.heit(a|ä) voltti homo$/i)) {
return 'steam://run/1250';
}
}
}
module.exports = Dice;