-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd-roll.js
90 lines (85 loc) · 3.42 KB
/
cmd-roll.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const lib = require('./lib');
const { MAX_DICE, DICETYPE } = require('./constants');
const discordLib = require('./discord-lib');
const droll = require('droll');
const { createCanvas } = require('canvas')
const cmdPush = require('./cmd-push');
function validateAndRoll(diceType, diceCodeString, dice) {
if (lib.isNumeric(dice)) {
if (dice <= MAX_DICE) {
rolledResults = droll.roll(dice + 'd6');
//console.log("rolledResults: " + rolledResults);
if (rolledResults.total != undefined) {
return rolledResults.rolls;
}
} else {
throw ("Really? More than " + MAX_DICE + " " + diceType + " dice?");
}
}
throw ("[" + diceCodeString + "] is an invalid dice code!");
}
function getSkillDice(diceCodeString) {
return validateAndRoll(DICETYPE.TASK, diceCodeString, diceCodeString.split('+')[0]);
}
function getStressDice(diceCodeString) {
if (diceCodeString.split('+').length > 1) {
return validateAndRoll(DICETYPE.STRESS, diceCodeString, diceCodeString.split('+')[1]);
}
return [];
}
function createDiceCanvas(skillDiceArray, stressDiceArray) {
const canvas = createCanvas((skillDiceArray.length + stressDiceArray.length) * 64, 64);
const ctx = canvas.getContext('2d');
skillDiceArray.forEach((element, i) => {
//console.log("Drawing image, element: " + element + " index: " + i);
ctx.drawImage(TaskDiceImages[element - 1], i * 64, 0, 64, 64)
})
stressDiceArray.forEach((element, i) => {
//console.log("Drawing image, element: " + element + " index: " + i);
ctx.drawImage(StressDiceImages[element - 1], (skillDiceArray.length + i) * 64, 0, 64, 64)
})
return canvas;
}
exports.roll = function (discordMessage, args, task, stress) {
// !roll
// Roll a task. Whitespace is stripped
// E.g.
// !roll 4 -- Roll a task with four skill dice
// !roll 4+1 -- Roll a task with four skill dice and a stress die
// If 'task' or 'stress' are defined, the results get appended to
// those arrays (used for pushes).
if (args != undefined && args.length >= 1) {
var diceCodeString = args[0].trim().replace(' ', '');
//console.log(diceCodeString)
var skillDiceArray = [];
var stressDiceArray = [];
try {
skillDiceArray = getSkillDice(diceCodeString);
} catch (e) {
// Invalid dice code
//console.log(e);
discordLib.showError(discordMessage, e);
return;
}
try {
stressDiceArray = getStressDice(diceCodeString);
} catch (e) {
//console.log(e);
discordLib.showError(discordMessage, e);
return;
}
if (typeof task !== 'undefined') {
//console.log("Concat task " + task + " to roll " + skillDiceArray);
skillDiceArray = task.concat(skillDiceArray);
}
if (typeof stress !== 'undefined') {
//console.log("Concat stress " + stress + " to roll " + stressDiceArray);
stressDiceArray = stress.concat(stressDiceArray);
}
cmdPush.setRoll(discordMessage, skillDiceArray, stressDiceArray);
//console.log("skillDiceArray: " + skillDiceArray);
var canvas = createDiceCanvas(skillDiceArray, stressDiceArray);
//console.log(canvas);
discordLib.sendMessageEmbedWithCanvas(discordMessage, canvas);
}
}