mirror of https://gitgud.io/fatchan/jschan.git
Dice reroll fix See merge request fatchan/jschan!174merge-requests/208/head
commit
8aab859305
7 changed files with 71 additions and 24 deletions
@ -1,17 +1,40 @@ |
|||||||
'use strict'; |
'use strict'; |
||||||
|
|
||||||
module.exports = (match, numdice, numsides, operator, modifier) => { |
module.exports = { |
||||||
numdice = parseInt(numdice); |
|
||||||
numsides = parseInt(numsides); |
regexPrepare: /##(?<numdice>[1-9][0-9]{0,1})d(?<numsides>1[0-9]{1,8}|[2-9][0-9]{0,8})(?:(?<operator>[+-])(?<modifier>[1-9][0-9]{0,8}))?(?<value>=[1-9][0-9]{0,8})?/gmi, |
||||||
let sum = (Math.floor(Math.random() * numsides) + 1) * numdice; |
regexMarkdown: /##(?<numdice>[1-9][0-9]{0,1})d(?<numsides>1[0-9]{1,8}|[2-9][0-9]{0,8})(?:(?<operator>[+-])(?<modifier>[1-9][0-9]{0,8}))?=(?<value>[1-9][0-9]{0,8})/gmi, |
||||||
if (modifier && operator) { |
|
||||||
modifier = parseInt(modifier); |
prepare: (force) => (match, numdice, numsides, operator, modifier, value) => { |
||||||
//do i need to make sure it doesnt go negative or maybe give absolute value?
|
if (!force && value) { |
||||||
if (operator === '+') { |
return match; |
||||||
sum += modifier; |
|
||||||
} else { |
|
||||||
sum -= modifier; |
|
||||||
} |
} |
||||||
} |
|
||||||
return `<img src='/file/dice.png' height='16' width='16' /><span class='dice'>(${match}) Rolled ${numdice} dice with ${numsides} sides${modifier ? ' and modifier '+operator+modifier : '' } = ${sum}</span>`; |
numdice = parseInt(numdice); |
||||||
|
numsides = parseInt(numsides); |
||||||
|
let matchWithoutValue = match.replace(/=.*/, ''); |
||||||
|
|
||||||
|
let sum = 0; |
||||||
|
for (let i = 0; i < numdice; ++i) { |
||||||
|
sum += Math.floor(Math.random() * numsides) + 1; |
||||||
|
} |
||||||
|
if (modifier && operator) { |
||||||
|
modifier = parseInt(modifier); |
||||||
|
//do i need to make sure it doesnt go negative or maybe give absolute value?
|
||||||
|
if (operator === '+') { |
||||||
|
sum += modifier; |
||||||
|
} else { |
||||||
|
sum -= modifier; |
||||||
|
} |
||||||
|
} |
||||||
|
return `${matchWithoutValue}=${sum}${value ? value : ''}`; |
||||||
|
}, |
||||||
|
|
||||||
|
markdown: (match, numdice, numsides, operator, modifier, value) => { |
||||||
|
numdice = parseInt(numdice); |
||||||
|
numsides = parseInt(numsides); |
||||||
|
value = parseInt(value); |
||||||
|
let matchWithoutValue = match.replace(/=.*/, ''); |
||||||
|
return `<img src='/file/dice.png' height='16' width='16' /><span class='dice'>(${matchWithoutValue}) Rolled ${numdice} dice with ${numsides} sides${modifier ? ' and modifier '+operator+modifier : '' } = ${value}</span>`; |
||||||
|
}, |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue