-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdiceRoller.ts
182 lines (151 loc) · 4.59 KB
/
diceRoller.ts
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// This dice roller class uses the generated pegjs (https://github.com/pegjs/pegjs) file based from the grammar available at https://github.com/BTMorton/dice_roller
// You will need to download the repo and compile the grammar, then copy it into this folder to use
interface IDiceRoll {
roll: number;
value: number;
type: string;
valid: boolean;
success: boolean;
order: number;
dice: IDiceRoll[];
rolls: IDiceRoll[];
die: IDiceRoll;
ops: string[];
count: IDiceRoll;
}
export class DiceRoller {
private parser: any = require("./diceroll.js");
private stupidDiceInsults = [
"What, you think I'm stupid?",
"Oh, yeah, because that was gonna work.",
"Hey! Everybody! Look at Mr Clever over here!",
"Yawn.... how about -3?",
"you're: (a) = dick",
"Stop trying to break me. Please.",
"y u do dis",
"42",
"Nice try. Idiot.",
"(╯°□°)╯︵ ┻━┻",
];
public rollDice(expression: string): string {
try {
const roll: IDiceRoll = this.parser.parse(expression);
const render: string = expression.replace(/\*/g, "\\\*") + ": " + this.render(roll);
if (render.length > 2000) {
return expression.replace(/\*/g, "\\\*") + " = " + roll.value;
}
return render;
} catch (e) {
if (e.message.includes("Invalid reroll target")) {
return this.stupidDiceInsults[Math.floor(Math.random() * this.stupidDiceInsults.length)];
}
throw e;
}
}
private render(roll: IDiceRoll): string {
let render = "";
let type: string = roll.type;
if (type.startsWith("root")) {
type = type.slice(4);
}
switch (type) {
case "groupExpression":
case "diceExpression":
render = this.renderGroupExpr(roll);
break;
case "group":
render = this.renderGroup(roll);
break;
case "die":
render = this.renderDie(roll);
break;
case "expression":
render = this.renderExpression(roll);
break;
case "roll":
return this.renderRoll(roll);
case "fateroll":
return this.renderFateRoll(roll);
case "number":
return roll.value + "";
case "fate":
return "F";
default:
throw new Error("Unable to render");
}
if (!roll.valid) {
render = "~~" + render.replace(/~~/g, "") + "~~";
}
if (roll.type.startsWith("root")) {
return render;
} else {
return "(" + render + ")";
}
}
private renderGroup(group: IDiceRoll): string {
const replies: Array<string> = [];
for (let die of group.dice) {
replies.push(this.render(die));
}
return "{ " + replies.join(" + ") + " } = " + group.value;
}
private renderGroupExpr(group: IDiceRoll): string {
const replies: Array<string> = [];
for (let die of group.dice) {
replies.push(this.render(die));
}
return replies.length > 1 ? "(" + replies.join(" + ") + ") = " + group.value : replies[0];
}
private renderDie(die: IDiceRoll): string {
const replies: Array<string> = [];
for (let roll of die.rolls) {
replies.push(this.render(roll));
}
let reply: string = "(" + replies.join(", ") + ")";
if (!["number", "fate"].includes(die.die.type) || !["number", "fate"].includes(die.count.type)) {
reply += "[*Rolling: " + this.render(die.count) + "d" + this.render(die.die) + "*]";
}
reply += " = " + die.value;
return reply;
}
private renderExpression(expr: IDiceRoll): string {
if (expr.dice.length > 1) {
const expressions: Array<string> = [];
for (let i = 0; i < expr.dice.length - 1; i++) {
expressions.push(this.render(expr.dice[i]));
expressions.push(expr.ops[i]);
}
expressions.push(this.render(expr.dice.slice(-1)[0]));
expressions.push("=");
expressions.push(expr.value + "");
return expressions.join(" ");
} else if (expr.dice[0].type === "number") {
return expr.value + "";
} else {
return this.render(expr.dice[0]);
}
}
private renderRoll(roll: IDiceRoll): string {
if (!roll.valid) {
return "~~" + roll.roll + "~~";
} else if (roll.success && roll.value === 1) {
return "**" + roll.roll + "**";
} else if (roll.success && roll.value === -1) {
return "*" + roll.roll + "*";
} else {
return roll.roll + "";
}
}
private renderFateRoll(roll: IDiceRoll): string {
const rollValue: string = roll.roll === 0 ? "0" : roll.roll > 0 ? "+" : "-";
if (!roll.valid) {
return "~~" + rollValue + "~~";
} else if (roll.success && roll.value === 1) {
return "**" + rollValue + "**";
} else if (roll.success && roll.value === -1) {
return "*" + rollValue + "*";
} else {
return rollValue;
}
}
}