Added new calculation
This commit is contained in:
parent
2c2de58138
commit
48c2eaf556
17
bot.js
17
bot.js
@ -32,13 +32,16 @@ bot.on('message', function (user, userID, channelID, message, evt) {
|
|||||||
case 'hi':
|
case 'hi':
|
||||||
sendMessage(channelID, 'Hello, World!')
|
sendMessage(channelID, 'Hello, World!')
|
||||||
case 'roll':
|
case 'roll':
|
||||||
if (/([+\-*/]?\d{0,}d\d{1,})([+\-*/]\d{1,}){0,}/gi.test(message.substring(6).replace(/\s/g, ''))) {
|
if (/([+\-]?\d{0,}d\d{1,})([+\-*x/]\d{1,}){0,}/gi.test(message.substring(6).replace(/\s/g, ''))) {
|
||||||
sendMessage(channelID, diceRoller.roll(message.substring(6)))
|
sendMessage(
|
||||||
|
channelID,
|
||||||
|
message.substring(6) + ': ' + diceRoller.roll(message.substring(6))
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
sendMessage(channelID, "I don't recognize this: \""
|
sendMessage(channelID, "I don't recognize this: \""
|
||||||
+ message
|
+ message
|
||||||
.substring(6)
|
.substring(6)
|
||||||
.replace(/([+\-*/]?\d{0,}d\d{1,})([+\-*/]\d{1,}){0,}/gi, "")
|
.replace(/([+\-]?\d{0,}d\d{1,})([+\-*x/]\d{1,}){0,}/gi, "")
|
||||||
.toString()
|
.toString()
|
||||||
+ "\"");
|
+ "\"");
|
||||||
}
|
}
|
||||||
@ -59,11 +62,3 @@ function sendMessage(client, message) {
|
|||||||
message: message
|
message: message
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function roll(dice) {
|
|
||||||
return parseInt((Math.random() * dice) + 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
function getSum(total, num) {
|
|
||||||
return total + num
|
|
||||||
}
|
|
||||||
@ -1,23 +1,92 @@
|
|||||||
|
|
||||||
|
module.exports = {
|
||||||
function roll(req){
|
roll: function (req) {
|
||||||
if(typeof req !== "string"){
|
if (typeof req !== "string") {
|
||||||
return "Mollux! You don goofed!"
|
return "Mollux! You don goofed!"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Splits request into segments. Example "3d6+3+5d2" => ["3d6", "+3", "+5d2"]
|
// Splits request into segments. Example "3d6+3+5d2" => ["3d6", "+3", "+5d2"]
|
||||||
var segments = req.replace(/\s/g, '').match(/([+\-*/]?\d{0,}d\d{1,})|([+\-*/]\d{1,})/gi)
|
var segments = req.replace(/\s/g, '').match(/([+\-]?\d{0,}d\d{1,})|([+\-*/]\d{1,})/gi)
|
||||||
|
|
||||||
|
var message = '';
|
||||||
|
|
||||||
var i = -1
|
var i = -1
|
||||||
var results = []
|
var results = []
|
||||||
var curResult = 0
|
var curResult = 0
|
||||||
var curMod = 1
|
var curMod = 1
|
||||||
segments.forEach(e => {
|
segments.forEach(e => {
|
||||||
if (e.match(/([+\-*/]?\d{0,}d\d{1,})/gi)){ // xdy
|
if (e.match(/([+\-]?\d{0,}d\d{1,})/gi)) { // xdy
|
||||||
if (e.substring(0, 1)) curMod = -1
|
// if not first add previous result and resetting curResult
|
||||||
// TODO: Do stuff
|
if (i >= 0) {
|
||||||
} else if (e.match(/([+\-*/]\d{1,})/gi)){ // Modifier
|
curResult *= curMod
|
||||||
// TODO: Do stuff
|
results[i] = curResult
|
||||||
|
|
||||||
|
curResult = 0
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
|
||||||
|
// Set modifier for dice group
|
||||||
|
curMod = (e.substring(0, 1) === '-') ? -1 : 1
|
||||||
|
|
||||||
|
// dice[0] = number of rolls, dice[1] = sides of dice rolled
|
||||||
|
var dice = e.replace(/[+\-]?/, '').split(/d/i)
|
||||||
|
var rolls = []
|
||||||
|
for (var l = 0; l < parseInt(dice[0] === '' ? 1 : dice[0]); l++) {
|
||||||
|
rolls[l] = rollDice(dice[1])
|
||||||
|
curResult += rolls[l]
|
||||||
|
}
|
||||||
|
|
||||||
|
// add result to message
|
||||||
|
var sign = e.match(/[+\-]/)
|
||||||
|
message += ((sign === null) ? ' ' : ' ' + sign) + '[' + rolls.toString() + ']'
|
||||||
|
} else if (e.match(/([+\-*/]\d{1,})/gi)) { // Modifier
|
||||||
|
switch (e.substring(0, 1)) {
|
||||||
|
case '+':
|
||||||
|
curResult += parseInt(e.substring(1))
|
||||||
|
break;
|
||||||
|
case '-':
|
||||||
|
curResult -= parseInt(e.substring(1))
|
||||||
|
break;
|
||||||
|
case '*':
|
||||||
|
case 'x':
|
||||||
|
curResult *= parseInt(e.substring(1))
|
||||||
|
break;
|
||||||
|
case '/':
|
||||||
|
curResult /= parseInt(e.substring(1))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
message += e
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
curResult *= curMod
|
||||||
|
results[i] = curResult
|
||||||
|
|
||||||
|
|
||||||
|
// Adding the sum to the message
|
||||||
|
var sum = 0
|
||||||
|
if (results.length > 1){
|
||||||
|
sum = results.reduce(getSum)
|
||||||
|
} else if (results.length === 1){
|
||||||
|
sum = results[0]
|
||||||
|
}
|
||||||
|
message += ' = ' + sum
|
||||||
|
|
||||||
|
console.log(results.toString())
|
||||||
|
console.log(i)
|
||||||
|
console.log(sum)
|
||||||
|
|
||||||
|
return message
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Roll one dice, returns result
|
||||||
|
* @param {int} dice Amount of sides od dice rolled
|
||||||
|
*/
|
||||||
|
function rollDice(dice) {
|
||||||
|
return parseInt((Math.random() * dice) + 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSum(total, num) {
|
||||||
|
return total + num
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user