-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrandom.js
More file actions
113 lines (96 loc) · 2.9 KB
/
random.js
File metadata and controls
113 lines (96 loc) · 2.9 KB
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
import eventManager from 'src/utils/eventManager.js';
import { global } from 'src/utils/global.js';
import * as hover from 'src/utils/hover.js';
import shuffle from 'src/utils/shuffle.js';
import style from 'src/utils/style.js';
import rand from 'src/utils/rand.js';
import * as deckLoader from 'src/utils/loadDeck.js';
import { getTranslationArray } from 'src/base/underscript/translation';
const limits = {
BASE: 3,
COMMON: 3,
RARE: 3,
EPIC: 2,
LEGENDARY: 1,
DETERMINATION: 1,
};
function buildPool(shinyFilter) {
const cards = [{ id: 0, shiny: false, rarity: '' }];
cards.shift();
const collection = global('deckCollections')[global('soul')];
collection.forEach((card = { id: 0, quantity: 0, shiny: false }) => {
if (shinyFilter !== undefined && card.shiny !== shinyFilter) return;
for (let i = 0; i < card.quantity; i++) {
cards.push(card);
}
});
shuffle(cards);
return cards;
}
function buildArtifacts() {
const current = [...global('decksArtifacts')[global('soul')]];
const available = [...global('userArtifacts')].filter((a) => !current.includes(a));
if (current.length === 0) {
current.push(random(available));
}
if (current.length < 2 && !current[0].legendary) {
const commons = available.filter((a) => !a.legendary && a !== current[0]);
current.push(random(commons));
}
return current;
}
function fillDeck(e) {
const cDeck = global('decks')[global('soul')];
const cArts = global('decksArtifacts')[global('soul')];
const artifacts = buildArtifacts();
if (cDeck.length === 25 && cArts.length === artifacts.length) return;
const counts = new Map();
const cards = [];
let dtFlag = false;
function addCard(card) {
const amt = counts.get(card.id) || 0;
if (amt === limits[card.rarity]) return;
if (card.rarity === 'DETERMINATION') {
if (dtFlag) return;
dtFlag = true;
}
cards.push(card);
counts.set(card.id, amt + 1);
}
cDeck.forEach(addCard);
// Fill deck with cards
const pool = buildPool(getMode(e));
while (cards.length < 25 && pool.length) {
addCard(pool.shift());
}
// Load deck
deckLoader.load({
cards,
artifacts,
});
}
eventManager.on(':preload:Decks', () => {
style.add('.btn { padding: 5px 6px; }');
const button = $('<button>');
const inner = $('<span>');
inner.addClass('glyphicon glyphicon-random');
button.addClass('btn btn-sm btn-primary');
button.append(inner);
button.click(fillDeck);
eventManager.on('underscript:ready', () => {
button.hover(hover.show(
getTranslationArray('underscript.general.deck.fill')
.join('<br>'),
), hover.hide);
});
const clearDeck = $('#yourCardList > button:last');
clearDeck.after(' ', button);
});
function getMode({ ctrlKey = false, shiftKey = false }) {
if (ctrlKey) return true;
if (shiftKey) return false;
return undefined;
}
function random(array = []) {
return array[rand(array.length)];
}