Skip to content

Commit

Permalink
feature: add hits and sum, fix modifier (#4)
Browse files Browse the repository at this point in the history
* fix: change how middleware is imported

* fix: change how the modifier works, returns a sum

* chore: update readme

* feature: add syntax for hits

* chore: bump version number
  • Loading branch information
djensen47 committed Jan 8, 2021
1 parent 9828100 commit 2f0cbee
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ Anywhere in a post type something like the following:
[dice 1d6]
```

or with a modifier
or with a modifier, which also returns a sum

```
[dice 1d6+2]
[dice 1d6+0]
```

You can also roll mulitple types of dice
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nodebb-plugin-dice-bot",
"version": "1.0.1",
"version": "1.1.0",
"repository": {
"type": "git",
"url": "https://github.com/djensen47/nodebb-plugin-dice-bot.git"
Expand All @@ -17,6 +17,6 @@
"author": "david@jensen47.com",
"license": "Apache-2.0",
"nbbpm": {
"compatibility": "^1.12.2"
"compatibility": "^1.16.0"
}
}
49 changes: 41 additions & 8 deletions src/dice.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ var TAG = '[plugins/dice-bot]';
var MINUTES = 60 * 1000;

var postReply = function postReply(tid, uid, content) {
// var uid = Dicebot._settings.diceBotUid;
winston.info(TAG + ' dicebot uid: ' + uid);
//winston.info(TAG + ' dicebot uid: ' + uid);
//TODO async.waterfall this
Topics.reply({
tid: tid,
Expand Down Expand Up @@ -39,7 +38,7 @@ var postReply = function postReply(tid, uid, content) {

var executeDice = function executeDice(diceTags) {
var results = [];
var diceRe = /(\d+)*d(\d+)([-\+]\d+){0,1}(([<>]=*)(\d+))*/;
var diceRe = /(\d+)*d(\d+)([-\+]\d+){0,1}([<>]=*)*(\d+)*/;

for(var i = 0; i < diceTags.length; i++) {
var cmds = diceTags[i].replace(/[\[\]]/g,'').split(' ').slice(1);
Expand All @@ -52,18 +51,45 @@ var executeDice = function executeDice(diceTags) {
var num = parseInt(params[1]);
var sides = parseInt(params[2]);
var modifier = parseInt(params[3]);
var comparison = params[4];
var target = parseInt(params[5]);

winston.info(`comparison: ${comparison}, target: ${target}`);

var result = { cmd: cmd, roll: roll(num, sides) };

if (modifier > 0) {
result.sum = modifier + result.roll.reduce((acc, curr) => acc + curr);
}

if (!!comparison && !!target) {
winston.info('get some hits!');
result.hits = result.roll.reduce((acc, curr) => {
switch(comparison) {
case '<':
return curr < target ? acc + 1 : acc;
case '<=':
return curr <= target ? acc + 1 : acc;
case '>':
return curr > target ? acc + 1 : acc;
case '>=':
return curr >= target ? acc + 1 : acc;
default:
return acc;
}
}, 0);
}

var result = { cmd: cmd, roll: roll(num, sides, modifier)};
results.push(result);
}
}
return results;
}

var roll = function roll(num, sides, modifier) {
var roll = function roll(num, sides) {
var results = [];
for (var i = 0; i < num; i++) {
results.push( Math.floor(Math.random() * sides + 1) + modifier );
results.push( Math.floor(Math.random() * sides + 1) );
}
return results;
};
Expand All @@ -73,7 +99,7 @@ var Dicebot = {
};

Dicebot.init = function(data, callback) {
var hostMiddleware = module.parent.require('./middleware');
var hostMiddleware = require.main.require('./src/middleware');
var controllers = require('./controllers');

data.router.get('/admin/plugins/dice-bot', hostMiddleware.admin.buildHeader, controllers.renderAdminPage);
Expand Down Expand Up @@ -115,7 +141,14 @@ Dicebot.postDice = function(data) {
var results = executeDice(matches);
var content = '';
for(var result of results) {
content += `**${result.cmd}:** ${result.roll.join(', ')}\n`;
content += `**${result.cmd}:** ${result.roll.join(', ')}`;
if (result.sum !== undefined) {
content += `; **Sum:** ${result.sum}`;
}
if (result.hits !== undefined) {
content += `; **Hits:** ${result.hits}`;
}
content += '\n';
}
postReply(tid, uid, content);
}
Expand Down

0 comments on commit 2f0cbee

Please sign in to comment.