-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmagicthegathering.js
340 lines (327 loc) · 17.6 KB
/
magicthegathering.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
/**
* Utilities class for methods related to Magic: The Gathering
*/
var utils = require('./utils.js');
var cache = require('memory-cache');
var request = require('request');
const Discord = require("discord.js");
const mainclass = require("./app.js");
//Global variables
var setTag;
var packvalue;
var foil;
function getOneRandomCard(setData) {
var shuffled = utils.shuffleArray(setData);
return shuffled[0];
}
module.exports = {
getPlanarCard: function(message) {
var planarchaos = "mtg_planarchaos";
if (cache.get(planarchaos)) {
var setData = cache.get(planarchaos);
var card = getOneRandomCard(setData);
message.channel.send(new Discord.RichEmbed().setTitle(card.name).setDescription(card.oracle_text).setImage(card.image_uris['normal']).setURL(card.scryfall_uri).setFooter("paypal.me/yunra"));
} else {
request('https://api.scryfall.com/cards/search?unique=cards&q=e%3Aopca+t%3Aplane', {json: true}, function (error, response, setData) {
cache.put(planarchaos, setData.data);
var card = getOneRandomCard(setData.data);
message.channel.send(new Discord.RichEmbed().setTitle(card.name).setDescription(card.oracle_text).setImage(card.image_uris['normal']).setURL(card.scryfall_uri).setFooter("paypal.me/yunra"));
});
}
},
momir: function(cmc, message) {
request('https://api.scryfall.com/cards/search?unique=cards&q=cmc:' + cmc + '+type:creature', {json: true}, function (error, response, setData) {
if (setData.status != 404) {
var card = getOneRandomCard(setData.data);
message.channel.send(new Discord.RichEmbed().setTitle("Momir: " + card.name).setImage(card.image_uris['normal']).setURL(card.scryfall_uri).setFooter("paypal.me/yunra"));
} else {
message.channel.send("Couldn't find any creature with Converted Mana Cost " + cmc);
}
});
},
rollForPlanes: function(message) {
if (Math.floor(Math.random() * 5) == 0) {
message.channel.send("You rolled {CHAOS} and should do what the Planar card tells you.");
} else if (Math.floor(Math.random() * 5) == 1) {
message.channel.send("You rolled to change the planes!");
this.getPlanarCard(message);
} else {
message.channel.send("You rolled a blank and nothing happens");
}
},
//Create a booster with a set amount of mythics, rares, uncommons and commons
createBooster: function(set, mythicrares = 1, uncommons = 3, commons = 10) {
var mythic = [];
var rare = [];
var uncommon = [];
var common = [];
for (card of set) {
utils.log(card.name)
if (card.rarity == "common") {
if (setTag == "m20" || setTag == "khm") {
common = utils.removeCardtypeFromList(common, "Land");
}
common.push(card);
} else if(card.rarity == "uncommon") {
uncommon.push(card);
} else if (card.rarity == "rare") {
rare.push(card);
} else if (card.rarity == "mythic") {
mythic.push(card);
}
}
common = utils.shuffleArray(common);
uncommon = utils.shuffleArray(uncommon);
rare = utils.shuffleArray(rare);
mythic = utils.shuffleArray(mythic);
var booster = [];
booster = common.slice(0,commons);
booster = booster.concat(uncommon.slice(0,uncommons));
if (Math.floor(Math.random() * 7) == 0) {
booster = booster.concat(mythic.slice(0,mythicrares));
} else {
booster = booster.concat(rare.slice(0,mythicrares));
}
//Replace a common with a special type of card
if (setTag == "dom") {
booster = this.replaceBasicWithCard(set, "Legendary", booster);
} if (setTag == "war") {
booster = this.replaceBasicWithCard(set, "Planeswalker", booster);
}
var cardnames = [];
packvalue = 0;
for (card of booster) {
cardnames.push(card.name);
if (parseInt(card.usd) > packvalue) {
packvalue = parseInt(card.usd);
}
}
// Replace basic land with foil
if (this.containsFoil(set[0].set)) {
cardnames = this.addFoilToCardnames(cardnames, set);
} else {
cardnames.push(module.exports.getBasicLand());
}
return cardnames;
},
getFoilCard: function(set) {
var mythic = [];
var rare = [];
var uncommon = [];
var common = [];
for (card of set) {
if (card.rarity == "common") {
common.push(card);
} else if(card.rarity == "uncommon") {
uncommon.push(card);
} else if (card.rarity == "rare") {
rare.push(card);
} else if (card.rarity == "mythic") {
mythic.push(card);
}
}
common = utils.shuffleArray(common);
uncommon = utils.shuffleArray(uncommon);
rare = utils.shuffleArray(rare);
mythic = utils.shuffleArray(mythic);
card = [];
randomNumber = Math.floor(Math.random() * 14);
if (randomNumber == 0) {
if (Math.floor(Math.random() * 7) == 0) {
card = card.concat(mythic.slice(0,1));
} else {
card = card.concat(rare.slice(0,1));
}
} else if (randomNumber <= 3 && randomNumber >= 1 ) {
var card = uncommon.slice(0,1);
} else {
var card = common.slice(0,1);
}
return card;
},
replaceBasicWithCard: function(set, cardtype, booster) {
var containsCorrectCard = false;
for (card of booster) {
if (card.type_line.includes(cardtype) && !containsCorrectCard) {
containsCorrectCard = true;
break;
}
}
if (!containsCorrectCard) {
var cardsOfType = [];
for (card of set) {
if (card.type_line.includes(cardtype)) {
cardsOfType.push(card);
}
}
var cardsOfCardtype = utils.shuffleArray(cardsOfType);
var replaced = false;
for (card of booster) {
const index = booster.indexOf(card.name);
if (!replaced) {
if(card.rarity === "common") {
booster.splice(booster, 1);
replaced = true;
break;
}
}
}
var cardOfType = cardsOfCardtype.slice(0,1);
booster = booster.concat(cardOfType);
return booster;
}
return booster;
},
containsFoil: function(setCode) {
if (setCode == "uma") {
return true;
} else {
return false;
}
},
addFoilToCardnames: function(cardnames, set) {
if (this.containsFoil(set[0].set)) {
foil = [];
foil = foil.concat(this.getFoilCard(set));
cardnames.push(foil[0].name);
foil = foil[0].name;
}
return cardnames;
},
//Get a land for the "landslot"
getBasicLand: function(amount = 1) {
var lands = ["Plains", "Island", "Swamp", "Mountain", "Forest"];
var ravnicaGuildGates = ["Boros Guildgate", "Dimir Guildgate", "Selesnya Guildgate", "Izzet Guildgate", "Golgari Guildgate"];
var ravnicaAllegianceGuildGates = ["Azorius Guildgate", "Gruul Guildgate", "Orzhov Guildgate", "Rakdos Guildgate", "Simic Guildgate"];
var coreSet2020Lands = ["Plains", "Island", "Swamp", "Mountain", "Forest", "Bloodfell Caves", "Blossoming Sands", "Dismal Backwater", "Evolving Wilds", "Jungle Hollow", "Rugged Highlands", "Scoured Barrens", "Swiftwater Cliffs", "Thornwood Falls", "Tranquil Cove", "Wind-Scarred Crag"];
var kaldheimLands = ["Plains", "Island", "Swamp", "Mountain", "Forest", "Snow-Covered Plains", "Snow-Covered Island", "Snow-Covered Swamp", "Snow-Covered Mountain", "Snow-Covered Forest", "Rimewood Falls", "Ice Tunnel", "Alpine Meadow", "Snowfield Sinkhole", "Woodland Chasm", "Sulfurous Mire", "Arctic Treeline", "Volatile Fjord", "Highland Forest", "Glacial Floodplain"];
if (setTag == "grn") {
lands = ravnicaGuildGates;
} if (setTag == "rna") {
lands = ravnicaAllegianceGuildGates;
} if (setTag == "m20") {
lands = coreSet2020Lands;
} if (setTag == "khm") {
lands = kaldheimLands;
}
var shuffledBasics = utils.shuffleArray(lands);
return shuffledBasics.slice(0, amount);
},
//Takes a set (3 letters) and an amount of cards in the booster, default 14
generateBoosterFromScryfall: function(client, message, set_code, amount = 14) {
setTag = set_code;
if (!cache.get(set_code)) {
message.channel.send("Hold on " + message.author.toString() + ", fetching set and generating booster");
}
request('https://api.scryfall.com/sets/' + set_code, {json: true}, function (error, response, setData) {
if(setData.card_count < 15) {
if(setData.card_count >= 1) {
request('https://api.scryfall.com/cards/' + set_code, {json: true}, function(error, response, body){
message.channel.send(setData.name + " only contains " + setData.card_count + " cards and can therefore not generate a booster. \nIt will release or was released " + setData.released_at);
message.channel.send(new Discord.RichEmbed().setTitle("Check out the set on Scryfall").setURL(setData.scryfall_uri).setFooter("paypal.me/yunra"));
utils.log("[DEBUG]" + message.author.id + " wanted a " + setData.name + "-booster. the set only contains " + setData.card_count + " cards and can't generate a booster.");
});
} else {
message.channel.send(setData.name + " only contains " + setData.card_count + " cards and can therefore not generate a booster. \nIt will release or was released " + setData.released_at);
}
} else {
if (cache.get(set_code)) {
var cached_set = cache.get(set_code);
utils.log(setData.name + " was found in the cache");
var cardnames = module.exports.createBooster(cached_set);
utils.log(cardnames);
var footer = "";
if (packvalue >= 10) {
footer = "A card in this pack is worth $" + packvalue + " - " + setData.name + " was released " + setData.released_at;
} else {
footer = setData.name + " was released " + setData.released_at;
}
message.channel.send(new Discord.RichEmbed().setDescription(cardnames).setTitle(setData.name).setURL(module.exports.createScryfallLink(cardnames, "rarity", setData.code)).setFooter(utils.setPatreonText(footer)));
} else {
var isBooster = "+is%3Abooster";
if(module.exports.isSetReleased(setData.released_at) == false) {
isBooster = "";
}
var scryfallSearchUri = "https://api.scryfall.com/cards/search?unique=cards&q=e%3A" + set_code + isBooster + "+-t%3Abasic+-t%3Agate";
request(scryfallSearchUri, {json: true}, function (error, response, body) {
var set = JSON.parse(JSON.stringify(body));
var next_page = "";
let cards = set.data;
if (typeof cards !== 'undefined' && cards) {
if (set.total_cards > 175) { //Scryfall returns 175 cards per request - https://scryfall.com/docs/api/cards/search
next_page = set.next_page.replace("\u0026", "");
request(next_page, {json: true}, function (error, response, body2) {
var moreinset = JSON.parse(JSON.stringify(body2));
cards = cards.concat(moreinset.data);
var cardnames = module.exports.createBooster(cards);
utils.setActivity(cardnames, client);
if (module.exports.isSetReleased(setData.released_at) == true) {
var footer = "";
if (packvalue >= 10) {
footer = "A card in this pack is worth $" + packvalue + " - " + setData.name + " was released " + setData.released_at;
} else {
footer = setData.name + " was released " + setData.released_at;
}
message.channel.send(new Discord.RichEmbed().setDescription(cardnames).setTitle(setData.name).setURL(module.exports.createScryfallLink(cardnames, "rarity", setData.code)).setFooter(utils.setPatreonText(footer)));
} else {
message.channel.send(new Discord.RichEmbed().setDescription("This set has not been released yet and for spoiler reasons you have to use the scryfall link to see the generated booster. The pack can contain any currently spoiled card, including promos and planeswalker deck cards.").setTitle(setData.name).setURL(module.exports.createScryfallLink(cardnames, "rarity", setData.code)).setFooter(utils.setPatreonText(setData.name + " will be released " + setData.released_at)));
}
utils.log(message.author.id + " generated a " + setData.name + "-booster");
});
} else {
var cardnames = module.exports.createBooster(cards);
utils.setActivity(cardnames, client);
if (module.exports.isSetReleased(setData.released_at) == true) {
var footer = "";
if (packvalue >= 10) {
footer = "A card in this pack is worth $" + packvalue + " - " + setData.name + " was released " + setData.released_at;
} else {
footer = setData.name + " was released " + setData.released_at;
}
message.channel.send(new Discord.RichEmbed().setDescription(cardnames).setTitle(setData.name).setURL(module.exports.createScryfallLink(cardnames, "rarity", setData.code)).setFooter(utils.setPatreonText(footer)));
} else {
message.channel.send(new Discord.RichEmbed().setDescription("This set has not been released yet and for spoiler reasons you have to use the scryfall link to see the generated booster. The pack can contain any currently spoiled card, including promos and planeswalker deck cards.").setTitle(setData.name).setURL(module.exports.createScryfallLink(cardnames, "rarity", setData.code)).setFooter(utils.setPatreonText(setData.name + " will be released " + setData.released_at)));
}
utils.log(message.author.id + " generated a " + setData.name + "-booster");
}
if (module.exports.isSetReleased(setData.released_at) == true) {
cache.put(set_code, cards);
utils.log("Adding set " + setData.name + " to cache with key: " + set_code);
}
} else if(body.status == 400) {
message.channel.send(set_code + " did not result in any hits.");
utils.log("[DEBUG] Could not find set with set code: " + set_code);
} else {
message.channel.send("Couldn't find any cards for a booster, " + setData.name + " might not have been released in boosters. Choose another set and try again or check out the set on scryfall.\n" + setData.scryfall_uri);
}
});
}
}
});
},
//Check if a set is released by comparing the releasedate with todays date
isSetReleased: function(releasedate) {
var today = new Date();
var released_at = new Date(releasedate);
var released = false;
if (today < released_at) {
release = false;
} else {
released = true;
}
return released;
},
//Create the scryfall link so you can view the cards easily
createScryfallLink: function(cardlist, order = "rarity", set) {
var scryfalllink = "https://scryfall.com/search?unique=cards&as=grid&order=" + order + "&q=!";
if (set) {
scryfalllink += cardlist.join('+e%3A' + set + '+or+!');
scryfalllink += '+e%3A' + set;
} else {
scryfalllink += cardlist.join('+or+!');
}
scryfalllink = scryfalllink.replace(/ /g, '-');
scryfalllink = scryfalllink.replace(/\s/g, '');
return scryfalllink;
}
}