2019-08-13 19:37:22 +03:00
|
|
|
var BasePlugin = require('./base.js')
|
2018-04-29 17:37:07 +03:00
|
|
|
|
|
|
|
class Dice extends BasePlugin {
|
2019-08-13 19:37:22 +03:00
|
|
|
constructor (config) {
|
|
|
|
super(config)
|
|
|
|
this.name = 'Dice'
|
2018-04-29 17:37:07 +03:00
|
|
|
}
|
|
|
|
|
2019-11-20 20:28:11 +02:00
|
|
|
help () {
|
|
|
|
// | Command | Description
|
|
|
|
return '.heita | .heitä 1d4 Heitä noppaa. Esim. 1d4'
|
|
|
|
}
|
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
test (input) {
|
|
|
|
if (input.match(/^\.heit(a|ä) ([0-9]+)d([0-9]+)( ([ \w])*)*$/i)) {
|
|
|
|
return true
|
2018-04-29 17:37:07 +03:00
|
|
|
}
|
2019-08-13 19:37:22 +03:00
|
|
|
if (input.match(/^\.heit(a|ä) voltti homo$/i)) {
|
2018-04-29 17:37:07 +03:00
|
|
|
return true
|
|
|
|
}
|
2019-08-13 19:37:22 +03:00
|
|
|
return false
|
2018-04-29 17:37:07 +03:00
|
|
|
}
|
2019-08-13 19:37:22 +03:00
|
|
|
getDiceString (numThrows, sides) {
|
|
|
|
var total = 0
|
|
|
|
var totalThrows = []
|
2018-04-29 17:37:07 +03:00
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
numThrows = Number(numThrows)
|
|
|
|
sides = Number(sides)
|
2018-04-29 17:37:07 +03:00
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
if (numThrows === 0 || sides === 0) {
|
|
|
|
return 'Tyhjää ei voi heittää'
|
2018-04-29 17:37:07 +03:00
|
|
|
}
|
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
if (numThrows > 100) {
|
|
|
|
return 'Ei jaksa...'
|
2018-04-29 17:37:07 +03:00
|
|
|
}
|
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
for (var a = 0; a < numThrows; a++) {
|
|
|
|
var t = Math.floor(Math.random() * sides) + 1
|
|
|
|
totalThrows.push(t)
|
|
|
|
total += t
|
2018-04-29 17:37:07 +03:00
|
|
|
}
|
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
var resultString = ''
|
|
|
|
totalThrows.forEach(function (value) {
|
|
|
|
resultString += value + ', '
|
|
|
|
})
|
2018-04-29 17:37:07 +03:00
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
resultString = resultString.substring(0, resultString.length - 2)
|
|
|
|
resultString += ' (yhteensä: ' + total + '. ' + (total / numThrows).toFixed(2) + ' per heitto)'
|
2018-04-29 17:37:07 +03:00
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
return resultString
|
2018-04-29 17:37:07 +03:00
|
|
|
}
|
2019-08-13 19:37:22 +03:00
|
|
|
result (input) {
|
2018-04-29 17:37:07 +03:00
|
|
|
// Different cases
|
|
|
|
let res = null
|
2019-08-13 19:37:22 +03:00
|
|
|
res = input.match(/^\.heit(a|ä) ([0-9]+)d([0-9]+)( ([ \w])*)*$/i)
|
|
|
|
if (res !== null) {
|
|
|
|
var th = this.getDiceString(res[2], res[3])
|
|
|
|
return 'Heitettiin: ' + th
|
2018-04-29 17:37:07 +03:00
|
|
|
}
|
|
|
|
if (input.match(/^\.heit(a|ä) voltti homo$/i)) {
|
2019-08-13 19:37:22 +03:00
|
|
|
return 'steam://run/1250'
|
2018-04-29 17:37:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
module.exports = Dice
|