-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrafiki.js
158 lines (131 loc) · 4.75 KB
/
rafiki.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
const
EntryPattern = /^([0-9]+)?\s*(.+)$/,
LorcanaApiFetchUrl = 'https://api.lorcana-api.com/cards/fetch',
Chunk = 19,
indexList = $('#index_list');
function resetIndexList() {
indexList.empty();
indexList.append('<div class="index_placeholder d-print-none"></div>');
}
$('#generate').on('click', function() {
const
names = [],
cardsByName = {},
requests = [],
lines = $('#deck_list').val().split('\n'),
errors = $('#errors'),
generate = $('#generate');
let deckSize = 0;
resetIndexList();
errors.empty();
generate.prop('disabled', !generate.prop('disabled'));
generate.text('Loading...');
for (const line of lines) {
if (line === '') {
continue;
}
const m = EntryPattern.exec(line);
if (m === null) {
errors.append(`<p class="error">error: invalid entry: ${line}</p>`);
generate.text('Generate');
generate.prop('disabled', !generate.prop('disabled'));
break;
}
let [_m, count, name] = m;
count = parseInt(count);
deckSize += count;
if (count > 4) {
errors.append(`<p class="warning">warning: 5+ copies: ${name}</p>`);
}
names.push(name);
}
if (deckSize === 0) {
errors.append(`<p class="warning">empty deck</p>`);
generate.text('Generate');
generate.prop('disabled', !generate.prop('disabled'));
return;
}
if (deckSize < 60) {
errors.append(`<p class="warning">warning: < 60 cards</p>`);
}
if (deckSize > 100) {
errors.append(`<p class="warning">warning: > 100 cards</p>`);
}
let u = new URL(LorcanaApiFetchUrl);
u.searchParams.append(
'search',
names.map(e => `name=${e}`).join(';|')
);
$.get(u, function(data, status) {
if (status < 200 || status > 299) {
console.log(`u: ${u}`);
errors.append(`<p class="error">error: received lorcana api status code: ${status}</p>`);
generate.text('Generate');
generate.prop('disabled', !generate.prop('disabled'));
return;
}
if (data.length === 0) {
console.log(`u: ${u}`);
errors.append(`<p class="error">error: no matching card names found</p>`);
generate.text('Generate');
generate.prop('disabled', !generate.prop('disabled'));
return;
}
if (!Array.isArray(data)) {
console.log(`data: ${JSON.stringify(data)}`);
errors.append('<p class="error">error: lorcana api unable to process query');
generate.text('Generate');
generate.prop('disabled', !generate.prop('disabled'));
return;
}
for (const d of data) {
cardsByName[d.Name] = {
inkable: d.Inkable,
color: d.Color.toLowerCase(),
cost: d.Cost,
name: d.Name,
};
};
const
cardsSorted = {},
entries = Object.entries(cardsByName);
entries.sort((a, b) => a[0].localeCompare(b[0]));
for (const [k, v] of entries) {
cardsSorted[k] = v;
}
let indices = Object.values(cardsSorted);
indices = [...Array(Math.ceil(indices.length/Chunk))].map(_ => indices.splice(0, Chunk));
let deckColorsUnique = {};
for (const card of Object.values(cardsByName)) {
deckColorsUnique[card.color] = true;
}
if (Object.keys(deckColorsUnique).length > 2) {
errors.append('<p class="warning">warning: 3+ colors</p>');
}
indexList.empty();
for (const index of indices) {
let indexDiv = '<div class="index">';
for (const card of index) {
let cardDiv = '<div class="tcg_card">';
cardDiv += `<img class="color" src="symbols/${card.color}.png" width="14" height="16" alt="${card.color}">`;
cardDiv += '<span class="cost ';
if (!card.inkable) {
cardDiv += 'un';
}
cardDiv += `inkable">${card.cost}</span>`;
cardDiv += `<span class="name">${card.name}</span>`;
cardDiv += '</div>';
indexDiv += cardDiv;
}
indexDiv += '</div>';
indexList.append(indexDiv);
}
generate.text('Generate');
generate.prop('disabled', !generate.prop('disabled'));
}).catch(e => {
console.log(`u: ${u}`);
errors.append(`<p class="error">error: network disruption</p>`);
generate.text('Generate');
generate.prop('disabled', !generate.prop('disabled'));
});
});