-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCortexPalGS.js
309 lines (283 loc) · 10.7 KB
/
CortexPalGS.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
const DICE_EXPRESSION = /(\d*(d|D))?(4|6|8|10|12)/;
const DIE_SIZES = [4, 6, 8, 10, 12];
const UNTYPED_STRESS = 'General';
const DIE_FACE_ERROR = '{0} is not a valid die size. You may only use dice with sizes of 4, 6, 8, 10, or 12.';
const DIE_STRING_ERROR = '{0} is not a valid die or dice.';
const DIE_EXCESS_ERROR = "You can't use that many dice.";
const DIE_MISSING_ERROR = 'There were no valid dice in that command.';
const LOW_NUMBER_ERROR = '{0} must be greater than zero.';
const BEST_OPTION = 'best';
function parseToDiceFromString(words) {
/* Examine the words of an input string, and keep those that are cortex dice notations. */
const dice = [];
for (const word of words.split(' ')) {
if (DICE_EXPRESSION.test(word)) {
let die = new Die(word);
//if the die size is in DIE_SIZES add it to the dice array
if (DIE_SIZES.includes(die.size)) {
dice.push(die);
}
}
}
return dice;
}
//array of int[] to dice
function parseToDiceFromNumArray(numArray) {
return numArray.flat().filter(die => parseInt(die) && DIE_SIZES.includes(die)).map(die => new Die(null, "D" + die, die, 1, []));
}
//examine input, if string parse to dice, if array of numbers, parse to dice
//if array is of type dice return the array immediately
function parseToDiceArray(input) {
if(Array.isArray(input) && input.every(die => die instanceof Die)){return input;}
if (typeof input === 'string') {
return parseToDiceFromString(input);
} else if (Array.isArray(input)) {
return parseToDiceFromNumArray(input);
}
return [];
}
class Die {
constructor(expression = null, name = null, size = 4, qty = 1, values = []) {
this.name = name;
this.size = size;
this.qty = qty;
this.values = values;
if (expression) {
if (!DICE_EXPRESSION.test(expression)) {
throw new Error(DIE_STRING_ERROR + ": " + expression);
}
let numbers = expression.toLowerCase().split('d');
if (numbers.length === 1) {
this.size = parseInt(numbers[0]);
} else {
if (numbers[0]) {
this.qty = parseInt(numbers[0]);
}
this.size = parseInt(numbers[1]);
}
}
}
roll() {
this.values = [];
for (let i = 0; i < this.qty; i++) {
this.values.push(Math.floor(Math.random() * this.size) + 1);
}
return this.values;
}
isMax() {
return this.size === 12;
}
output() {
return this.toString();
}
isRolled() {
return this.values.length > 0;
}
isBotch() {
return this.values.length > 0 && this.values.every(v => v === 1);
}
//eligible die are die results that are not 1
eligibleDice(hitchOn = 1) {
let eligible = [];
for (const v of this.values) {
if (v > hitchOn) {
eligible.push(new Die(null, "D" + this.size, this.size, 1, [v]));
}
}
return eligible;
}
toString() {
if (this.qty > 1) {
return `${this.qty}D${this.size}`;
} else {
return `D${this.size}`;
}
}
}
const D4 = new Die(null, 'D4', 4, 1);
class DicePool {
constructor(incoming_dice = []) {
if(incoming_dice) incoming_dice = parseToDiceArray(incoming_dice);
this.dice = [null, null, null, null, null];
if (incoming_dice.length > 0) {
this.add(incoming_dice);
}
}
add(dice) {
for (const die of dice) {
const index = DIE_SIZES.indexOf(die.size);
if (this.dice[index]) {
this.dice[index].qty += die.qty;
}
else {
this.dice[index] = die;
}
}
//return `${this.output()} (added ${list_of_dice(dice)})`;
}
isRolled() {
return this.dice.some(die => die && die.isRolled());
}
isBotch() { return this.dice.every(die => { return die && die.isBotch(); }) }
is_empty() {
return !this.dice.some(die => die !== null);
}
hitchCount(){
return this.dice.reduce((sum, die) => sum + (die ? die.values.filter(v => v <= 1).length : 0), 0);
}
//eligible dice are dice results that are not 1
eligibleDice(hitchOn = 1) {
if (!this.isRolled()) { this.rollDice(); }
let eligible = [];
for (const die of this.dice) {
if (die) {
eligible = eligible.concat(die.eligibleDice(hitchOn));
}
}
return eligible;
}
getBest(rolls = null, keep = 2, hitchOn = 1, displayHitches = false) {
if (!rolls) { rolls = this.eligibleDice(); displayHitches = true;}
let output = '';
if (this.isBotch()) {
output += '\nBotch!';
return output;
}
else if (rolls.length <= keep) {
output += '\n';
output += this.getOnlyTotal(rolls, keep, displayHitches);
}
else {
if(!displayHitches) output += '\n';
output += this.getBestTotal(rolls, keep, hitchOn);
output += '\n';
output += this.getBestEffect(rolls, keep, hitchOn, displayHitches);
}
return output;
}
getHtichDisplay(){
return `\nHitches: ` + this.hitchCount();
}
getOnlyTotal(rolls, keep = 2, hitchOn = 1, displayHitches = false) {
if (!rolls) { rolls = this.eligibleDice(hitchOn); displayHitches = true;}
let output = '';
var only_total_addition = rolls.map(d => d.values[0]).join(' + ');
var only_total_1 = rolls.slice(0, keep).reduce((sum, d) => sum + d.values[0], 0);
output += `Only Total: ${only_total_1} (${only_total_addition}) with Effect: D4`;
if (displayHitches) {
output += this.getHtichDisplay();
}
return output;
}
getBestTotal(rolls = null, keep = 2, hitchOn = 1, displayHitches = false) {
if (!rolls) { rolls = this.eligibleDice(hitchOn); displayHitches = true;}
let output = '';
//Find the Best Total, then the best remaining effect
var rollsSorted = rolls.sort((a, b) => b.values[0] - a.values[0]);
var best_total_dice = rollsSorted.slice(0, keep);
var best_effect_dice = rollsSorted.slice(keep, rollsSorted.length).sort((a, b) => b.size - a.size).slice(0);
best_effect_dice.push(D4);
var best_effect_1 = `D${best_effect_dice[0].size}`;
var best_total_addition = best_total_dice.map(d => d.values[0]).join(' + ');
var best_total_1 = best_total_dice.reduce((sum, d) => sum + d.values[0], 0);
output += `Best Total: ${best_total_1} (${best_total_addition}) with Effect: ${best_effect_1}`;
if (displayHitches) {
output += this.getHtichDisplay();
}
return output;
}
getBestEffect(rolls = null, keep = 2, hitchOn = 1, displayHitches = false) {
if (!rolls) { rolls = this.eligibleDice(hitchOn); displayHitches = true; }
let output = '';
//Find the Best Effect, Then the Best Total
//sort by size, then by value
rolls.sort((a, b) => {
if (a.size !== b.size) {
return b.size - a.size;
} else {
return b.values[0] - a.values[0];
}
});
var best_effect_2 = `D${rolls[0].size}`;
//slice off the best effect, then sort by value, slice off the best total
var best_total_dice_2 = rolls.slice(1).sort((a, b) => b.values[0] - a.values[0]).slice(0, keep);
var best_total_addition_2 = best_total_dice_2.map(d => d.values[0]).join(' + ');
var best_total_2 = best_total_dice_2.reduce((sum, d) => sum + d.values[0], 0);
output += `Best Effect: ${best_effect_2} with Total: ${best_total_2} (${best_total_addition_2})`;
if (displayHitches) {
output += this.getHtichDisplay();
}
return output;
}
//returns rolled Dice, sets internal roll values
rollDice() {
for (const die of this.dice) {
if (die) die.roll();
}
return this.dice;
}
results(dice = null, hitchOn = 1) {
if (!dice) { dice = this.dice; }
let output = '';
let separator = '';
for (const die of this.dice) {
if (die) {
output += `${separator}D${die.size} : `;
for (let i = 0; i < die.values.length; i++) {
let roll_str = die.values[i].toString();
if (die.values[i] <= hitchOn) {
roll_str = '(' + roll_str + ')';
}
output += `${roll_str} `;
}
separator = '\n';
}
}
return output;
}
roll(suggest_best = null, keep = 2, hitchOn = 1, displayHitches = false) {
this.rollDice();
let output = this.results(this.dice, hitchOn);
let rolls = this.eligibleDice(hitchOn);
if (suggest_best) {
if(displayHitches) output += `\n`;
output += this.getBest(rolls, keep, hitchOn, displayHitches);
}
else if (displayHitches) {
output += this.getHtichDisplay();
}
return output;
}
rollDie(size) {
const face = Math.floor(Math.random() * size) + 1;
return face;
}
output() {
if (this.is_empty()) {
return 'empty';
}
return list_of_dice(this.dice);
}
}
const list_of_dice = (dice) => dice.filter(Boolean).map(die => die.output()).join(', ');
/*
Derived from https://github.com/dbisdorf/cortex-discord-2
Licensed, same as original, under the
MIT License
Copyright (c) 2021 dbisdorf
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/