/
vdl.js
384 lines (343 loc) · 12.7 KB
/
vdl.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// js ajax functions
function getOutput(postdata) {
getRequest(
'save-to-server.php', // URL for the PHP file
postdata, // our POST data
drawOutput, // handle successful request
drawError // handle error
);
return false;
}
// handles drawing an error message
function drawError() {
const container = document.getElementById('output');
container.innerHTML = 'Bummer: there was an error!';
}
// handles the response, adds the html
function drawOutput(responseText) {
// var container = document.getElementById('output');
// container.innerHTML = responseText;
}
// helper function for cross-browser request object
function getRequest(url, postdata, success, error) {
let req = false;
try {
// most browsers
req = new XMLHttpRequest();
} catch (e) {
// IE old versions
try {
req = new ActiveXObject('Msxml2.XMLHTTP');
} catch (e) {
// try an older version
try {
req = new ActiveXObject('Microsoft.XMLHTTP');
} catch (e) {
return false;
}
}
}
if (!req) return false;
if (typeof success !== 'function') success = function () {};
if (typeof error !== 'function') error = function () {};
req.onreadystatechange = function () {
if (req.readyState == 4) {
console.log('saving done');
vdl.renderDeck();
return req.status === 200 ? success(req.responseText) : error(req.status);
}
};
req.open('POST', url, true);
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
req.send(postdata);
// req.send(null);
return req;
}
// test if a file exists
function UrlExists(url) {
var http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send();
return http.status!=404;
}
const mtg = require('mtgsdk');
// mtg.card.all({ name: 'ether hub|dismember'})
// .on('data', card => {
// console.log(card.name);
// console.log(card);
// }
// );
// removes duplicates from an array
function unique(arr) {
const seen = {};
return arr.filter(item => seen.hasOwnProperty(item) ? false : (seen[item] = true));
}
var vdl = {
state: {
cardNames: [],
deck: [],
deckName: '',
queryList: '',
},
// get user input from form
getUserInput() {
},
// parse userinput decklist into a queryList that the mtgsdk can accept
parseDecklist(deckLines) {
let queryListArr = [];
const deck = [];
var queryName = '';
let splitPartner = '';
let isSplit = false;
for (let i = 0; i < deckLines.length; i++) {
queryName = '';
splitPartner = '';
isSplit = false;
const number = deckLines[i].substr(0, deckLines[i].indexOf(' ')); // 4
// check if there's a number. treat lines without a number as a divider
const hasNumber = !isNaN(parseInt(number));
if (hasNumber) {
var name = deckLines[i].replace(/\s\s+/g, ' ');
name = name.substr(name.indexOf(' ') + 1); // Lighting Bolt
queryName = name.toLowerCase();
// if (name.indexOf(',') > -1) {
// var queryName = name.replace(',', '%2C');
// }
// handle "aether" and split cards
if (name.indexOf('aether') > -1) {
var queryName = name.replace('aether', 'ether');
}
// handle split cards
if (name.indexOf('//') > -1) {
isSplit = true;
var queryName = name.substr(0, name.indexOf('//')).trim().toLowerCase();
}
queryListArr.push(queryName);
} else {
var name = deckLines[i];
}
deck.push({
slot: i,
name,
queryName,
quantity: number,
isSplit,
splitPartner,
isDivider: !hasNumber,
attributes: {}
});
}
vdl.state.deck = deck;
queryListArr = unique(queryListArr);
vdl.state.queryList = queryListArr;
console.log('finished building query list');
console.log(vdl.state);
// call requestCards here
vdl.updateLocalStorage(vdl.state);
vdl.requestCards(vdl.state.queryList);
},
// make the API call
requestCards(queryListArr) {
const queryListString = queryListArr.join('|').toLowerCase();
const cardData = [];
const emitter = mtg.card.all({ name: queryListString });
emitter.on('data', (card) => {
cardData.push(card);
});
emitter.on('end', (finish) => {
for (let i = 0; i < cardData.length; i++) {
const thisCard = cardData[i];
const thisCardName = thisCard.name.toLowerCase();
for (let j = 0; j < vdl.state.deck.length; j++) {
// if (thisCardName === vdl.state.deck[j].name.toLowerCase()){
// vdl.state.deck[j].attributes = thisCard;
// }
// do fuzzy matching to account for split cards
// this could cause problems if a split card name is contined w/in another card name
// think fire//ice and fireball
if (vdl.state.deck[j].name.toLowerCase().indexOf(thisCardName) > -1) {
vdl.state.deck[j].attributes = thisCard;
}
}
}
console.log(cardData);
// call the saveCardImages function
vdl.saveCardImages();
});
},
// save card images to the server
saveCardImages() {
document.querySelector('.loading__message').innerText = 'Downloading Card Images';
console.log('saveCardImages called');
console.log(vdl.state);
let queryStringURLs = 'cardurls=';
let queryStringNames = 'cardnames=';
const deck = vdl.state.deck;
console.log(deck);
for (let i = 0; i < deck.length; i++) {
if (deck[i].isDivider != true) {
var cardname = deck[i].attributes.name.replace(',', '');
queryStringURLs = `${queryStringURLs + deck[i].attributes.imageUrl.replace('&type=card', '%26type=card')},`;
console.log(queryStringURLs);
queryStringNames = `${queryStringNames + encodeURI(cardname)},`;
console.log(queryStringNames);
// queryStringURLs = `${queryStringURLs + deck[i].attributes.imageUrl.replace('&type=card', '%26type=card')},`;
// console.log(queryStringURLs);
// queryStringNames = `${queryStringNames + encodeURI(deck[i].attributes.name)},`;
// console.log(queryStringNames);
}
}
const queryString = `${queryStringURLs.replace(/,\s*$/, '')}&${queryStringNames.replace(/,\s*$/, '')}`;
console.log(`query string is ${queryString}`);
getOutput(queryString);
},
// update the state, also add the state obj to localStorage
updateState(cardNames, deck, deckName, queryList) {
if (cardNames) {
this.state.cardNames = cardNames;
}
if (deck) {
this.state.deck = deck;
}
if (deckName) {
this.state.deckName = deckName;
}
if (queryList) {
this.state.queryList = queryList;
}
console.log('updating state:');
console.log(vdl.state);
vdl.updateLocalStorage(vdl.state);
},
updateLocalStorage(state) {
localStorage.setItem('visualDecklistState', JSON.stringify(state));
},
clearState() {
this.state = {
cardNames: [],
deck: [],
deckName: '',
queryList: '',
};
this.updateLocalStorage(this.state);
},
renderDeck() {
//re-enable the button and remove loading classes
document.getElementById('button').disabled = false;
document.body.classList.remove('loading-active');
document.querySelector('.loading').classList.remove('active');
// display the contents of .col-right
document.querySelector('.col-right').classList.add('active');
const deck = vdl.state.deck;
const visualDeckList = document.getElementById('visualDeckList');
visualDeckList.innerHTML = '';
const deckNameTag = document.createElement('div');
deckNameTag.className = 'deck-name';
deckNameTag.innerHTML = `<div>${vdl.state.deckName}</div>`;
visualDeckList.appendChild(deckNameTag);
for (let i = 0; i < deck.length; i++) {
// create a row representing a deckslot
const row = document.createElement('div');
row.className = `card cardslot${i}`;
// add card darken filter
const cardDarkenTag = document.createElement('div');
cardDarkenTag.className = 'card-darken';
if (deck[i].isDivider) {
// cardFlexTag & leftTagDiv is duplicated in either side of this if statement
// these elements have to be appeneded to the DOM AFTER the cardBgTag div to
// to make the screenshot work properly
// add flex container
var cardFlexTag = document.createElement('div');
cardFlexTag.className = 'card-flex';
// add container for quantity and name
var leftDivTag = document.createElement('div');
cardFlexTag.appendChild(leftDivTag);
row.className = `${row.className} divider`;
} else {
// add card bg
const cardBgTag = document.createElement('div');
cardBgTag.className = 'card-bg';
row.appendChild(cardBgTag);
// add flex container
var cardFlexTag = document.createElement('div');
cardFlexTag.className = 'card-flex';
// add container for quantity and name
var leftDivTag = document.createElement('div');
cardFlexTag.appendChild(leftDivTag);
if (deck[i].isSplit) {
// cardBgTag.setAttribute('style', 'background-image:url("' + deck[i].attributes.imageUrl + '"), url("' + deck[i].attributes.imageUrl + '")');
cardBgTag.setAttribute('style', `background-image:url("img/${encodeURIComponent(deck[i].attributes.name)}.jpg"), url("img/${encodeURIComponent(deck[i].attributes.name)}.jpg")`);
row.className = `${row.className} split-card`;
} else {
// cardBgTag.setAttribute('style', 'background-image:url("' + deck[i].attributes.imageUrl + '")');
cardBgTag.setAttribute('style', `background-image:url("img/${encodeURIComponent(deck[i].attributes.name.replace(',', ''))}.jpg")`);
}
// add the quantity
const quantityTag = document.createElement('span');
quantityTag.className = 'card-quantity';
quantityTag.innerHTML = `${deck[i].quantity} `;
leftDivTag.appendChild(quantityTag);
// add the mana cost
const manaCostTag = document.createElement('div');
manaCostTag.className = 'mana-cost';
const thisManaCost = deck[i].attributes.manaCost;
if (typeof thisManaCost !== 'undefined') {
thisManaCostHTML = thisManaCost.toLowerCase();
thisManaCostHTML = thisManaCostHTML.replace(/{/g, '<span class="mana m-');
thisManaCostHTML = thisManaCostHTML.replace(/\//g, '');
thisManaCostHTML = thisManaCostHTML.replace(/}/g, '"></span>');
console.log(thisManaCostHTML);
manaCostTag.innerHTML = thisManaCostHTML;
}
cardFlexTag.appendChild(manaCostTag);
}
row.appendChild(cardDarkenTag);
row.appendChild(cardFlexTag);
// add the name
const cardNameTag = document.createElement('span');
cardNameTag.className = 'card-name';
console.log(deck[i].name);
console.log(deck[i].name.length);
let nameHTML = deck[i].name;
if (deck[i].name.length > 17) {
nameHTML = `<small>${deck[i].name}</small>`;
}
console.log(`nameHTML is ${nameHTML}`);
// cardNameTag.innerHTML = deck[i].name;
cardNameTag.innerHTML = nameHTML;
leftDivTag.appendChild(cardNameTag);
visualDeckList.appendChild(row);
}
const builtWithTag = document.createElement('div');
builtWithTag.className = 'built-with';
builtWithTag.innerHTML = 'VisualDecklist.com';
visualDeckList.appendChild(builtWithTag);
ga('send', 'event', 'DeckListBuilt');
return false;
},
init() {
document.getElementById('button').addEventListener('click', () => {
// get inputs
const deckName = document.getElementById('deckName').value;
const deckListTxt = document.getElementById('decklist').value
let deckLines = deckListTxt.split('\n');
if (deckListTxt.length > 0) {
// strip empty elements from array. works because empty strings are falsey
deckLines = deckLines.filter(Boolean);
this.disabled = true;
// activate loading animation
document.body.classList.add('loading-active');
document.querySelector('.loading').classList.add('active');
document.querySelector('.loading__message').innerText = 'Fetching Decklist Data';
// clear the state from the previous decklist, if there was one
vdl.clearState();
// cardNames, deck, deckName, queryList
vdl.updateState('', vdl.state.deck, deckName, vdl.state.queryList);
vdl.parseDecklist(deckLines);
} else {
alert("enter a decklist first");
}
}, false);
},
};
vdl.init();