-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokens.js
More file actions
244 lines (213 loc) · 6.34 KB
/
Copy pathtokens.js
File metadata and controls
244 lines (213 loc) · 6.34 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
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
var tokenSource = "https://raw.githubusercontent.com/Cockatrice/Magic-Token/master/tokens.xml";
var tokensLoaded = false;
var tokens = {};
var transformSource = "https://api.scryfall.com/cards/search?order=cmc&q=is%3Aflip+or+is%3Adfc+or+is%3Aadventure";
var transformLoaded = false;
var transform = {};
function isValidLine(line)
{
if(line.length==0) return false;
if(line.match(/^#/gm)) return false;
return true;
}
function clearLine(line)
{
line = line.replace(/[0-9]/g, "");
line = line.replace(/^\d+x/gm, ""); // Removes "2x"
line = line.replace(/\(.*\)/gm, ""); // Removes "(SET")"
line = line.replace(/\*\w+\*/gm, ""); // Removes "*F*"
line = line.replace(/^\s+/gm, ""); // Trims leading space
line = line.replace(/\s+$/gm, ""); // Trims trailing space
return line;
}
function loadTokens(callback)
{
console.log("LoadTokens called");
if(tokensLoaded)
{
callback();
}
else
{
$.get(tokenSource, null, function(data)
{
console.log("Data Load");
var xml = $.parseXML(data);
var cards = xml.getElementsByTagName("card");
for(var i=0;i<cards.length;i++)
{
var token = { fullname: "", name: "", color: "", description: "", pt: "", cards: [] };
for(var j=0;j<cards[i].childNodes.length;j++)
{
var nodeName = cards[i].childNodes[j].localName;
if(nodeName=="name")
{
var cleanName = cards[i].childNodes[j].textContent;
cleanName = cleanName.replace(/\s+$/gm, ""); // Cockatrice has strange trailing spaces in token names
cleanName = cleanName.replace(/\(token\)/gm, ""); // Cockatrice has strange trailing spaces in token names
if(cleanName.match(/\(emblem\)/gm))
{
cleanName = cleanName.replace(/\(emblem\)/gm, "");
cleanName = "Emblem " + cleanName;
}
token.name = cleanName;
}
if(nodeName=="prop")
{
for(var k=0;k<cards[i].childNodes[j].childNodes.length;k++)
{
var propNodeName = cards[i].childNodes[j].childNodes[k].localName;
if(propNodeName=="colors")
{
if(cards[i].childNodes[j].childNodes[k].textContent.match(/W/gm))
{
if(token.color!="") token.color = token.color + "/";
token.color = token.color + "White";
}
if(cards[i].childNodes[j].childNodes[k].textContent.match(/U/gm))
{
if(token.color!="") token.color = token.color + "/";
token.color = token.color + "Blue";
}
if(cards[i].childNodes[j].childNodes[k].textContent.match(/B/gm))
{
if(token.color!="") token.color = token.color + "/";
token.color = token.color + "Black";
}
if(cards[i].childNodes[j].childNodes[k].textContent.match(/R/gm))
{
if(token.color!="") token.color = token.color + "/";
token.color = token.color + "Red";
}
if(cards[i].childNodes[j].childNodes[k].textContent.match(/G/gm))
{
if(token.color!="") token.color = token.color + "/";
token.color = token.color + "Green";
}
if(token.color=="") token.color = "Colorless";
token.color = token.color;
}
if(propNodeName=="pt")
{
token.pt = cards[i].childNodes[j].childNodes[k].textContent;
}
}
}
if(nodeName=="text")
{
var cleanDescription = cards[i].childNodes[j].textContent;
cleanDescription = cleanDescription.replace(/\)$/gm, "");
cleanDescription = cleanDescription.replace(/^\(/gm, "");
cleanDescription = cleanDescription.replace(/\r/gm, "");
cleanDescription = cleanDescription.replace(/\n/gm, " ");
token.description = cleanDescription;
}
if(nodeName=="reverse-related")
{
token.cards.push(cards[i].childNodes[j].textContent);
if(cards[i].childNodes[j].textContent.includes(" // "))
{
var splitCards = cards[i].childNodes[j].textContent.split(" // ");
for(var k=0;k<splitCards.length;k++)
{
token.cards.push(splitCards[k]);
}
}
}
}
if(token.color=="") token.color = "Colorless";
token.fullname = token.color + " " + token.name;
if(token.pt!="") token.fullname = token.fullname + " " + token.pt;
if(token.description!="" && !token.name.match(/^Emblem/gm)) token.fullname = token.fullname + " (" + token.description + ")";
if(!token.name.match(/^\(/gm))
{
for(var j=0;j<token.cards.length;j++)
{
if(!tokens.hasOwnProperty(token.cards[j]))
{
tokens[token.cards[j]] = [];
}
tokens[token.cards[j]].push(token.fullname);
}
}
}
tokensLoaded = true;
callback();
});
}
}
function loadTransform(url, callback)
{
console.log("LoadTransform called");
if(transformLoaded)
{
callback();
}
else
{ $.get(url, null, function(data)
{
for(var i=0;i<data.data.length;i++)
{
transform[data.data[i].card_faces[0].name] = data.data[i].card_faces[1].name;
}
if(data.has_more)
{
loadTransform(data.next_page, callback);
}
else
{
transformLoaded = true;
callback();
}
});
}
}
function generate()
{
$(".outputtokens").val("Loading...");
loadTokens(function()
{
loadTransform(transformSource, generateTokens);
});
}
function generateTokens()
{
console.log("Generate called");
var input = $(".inputtokens").val();
var output = [];
var lines = input.match(/[^\r\n]+/g);
var validLines = [];
if(lines==null)
{
$(".outputtokens").val("Card list is empty");
return;
}
for(var i=0;i<lines.length;i++)
{
if(isValidLine(lines[i]))
{
var line = clearLine(lines[i]);
validLines.push(line);
if(transform.hasOwnProperty(line)) validLines.push(transform[line]);
}
}
for(var i=0;i<validLines.length;i++)
{
if(tokens.hasOwnProperty(validLines[i]))
{
for(var j=0;j<tokens[validLines[i]].length;j++)
{
output.push(tokens[validLines[i]][j]);
}
}
}
var output = Array.from(new Set(output)); // Filter unique items
if(output.length==0)
{
$(".outputtokens").val("No tokens found for the specified card list");
}
else
{
$("outputtokens").val(output.join("\n"));
}
}