This repository has been archived by the owner on Jul 24, 2021. It is now read-only.
forked from 4m2c4bl3/nodebb-plugin-dice-and-numbers
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.js
75 lines (65 loc) · 2.89 KB
/
library.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"use strict";
const RandomNumber = require('random-number');
const Roll = require('roll');
const dice = new Roll();
const composerFormatting = (data, callback) => {
data.options.push({
name: 'diceroller',
className: 'fas fa-dice',
title: 'Dice roller/Number generator'
});
callback(null, data);
};
module.exports.composerFormatting = composerFormatting;
const parsePost = (data, callback) => {
if (data.postData.randomNumber) {
data.postData.content = `${data.postData.content} <br> <div class="random-results"><div class="individual">Generating ${data.postData.randomNumber.amount} random number${data.postData.randomNumber.amount > 1 ? 's' : ''} from ${data.postData.randomNumber.min} to ${data.postData.randomNumber.max}: [${data.postData.randomNumber.result}]</div> <div class="total">Total:${data.postData.randomNumber.total}</div></div>`;
}
if (data.postData.diceRoll) {
data.postData.content = `${data.postData.content} <br> <div class="dice-results"><div class="individual">Rolling ${data.postData.diceRoll.query} dice: [${data.postData.diceRoll.rolled.toString().replace(/,/g, ', ')}]</div> <div class="total">Total:${data.postData.diceRoll.result}</div></div>`;
}
callback(null, data);
};
module.exports.parsePost = parsePost;
const posts = require.main.require('./src/posts');
const buildComposer = async (data, callback) => {
var req = data.req;
if (req && req.query.pid) {
let postData;
await posts.getPostData(req.query.pid, (e, p) => {e ? console.log(e) : postData = p});
data.templateData = {...data.templateData, randomNumber: postData.randomNumber, diceRoll: postData.diceRoll};
}
callback(null, data);
}
module.exports.buildComposer = buildComposer;
var Numbers = {
random: function (amount, min, max) {
const numbers = [];
const settings = {min: parseInt(min), max: parseInt(max), integer: true}
for (var i = 0; i < amount; i++) {
numbers.push(RandomNumber(settings))
}
return numbers;
},
roll: function (query) {
const diceRoll = dice.roll(query);
return { query, rolled: diceRoll.rolled, result: diceRoll.result };
}
};
const generateNumbers = (data, callback) => {
if (data.data.diceRoll && data.post) {
data.post.diceRoll = Numbers.roll(data.data.diceRoll);
}
if (data.data.randomNumber && data.post) {
const randomNumber = {
amount: data.data.randomNumber[0],
min: data.data.randomNumber[1],
max: data.data.randomNumber[2]
};
const result = Numbers.random(randomNumber.amount, randomNumber.min, randomNumber.max);
const total = result.reduce((a, b) => a + b, 0);
data.post.randomNumber = { ...randomNumber, result: result.join(', '), total};
}
callback(null, data);
};
module.exports.generateNumbers = generateNumbers;