forked from Thyss/pack1pick1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
starwarsdestiny.js
73 lines (71 loc) · 2.88 KB
/
starwarsdestiny.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
/**
* https://swdestinydb.com/api/
*
* Booster generation for Star Wars Destiny using the swdestinydb.com API
*/
var request = require('request');
const Discord = require("discord.js");
var utils = require('./utils.js');
var cache = require('memory-cache');
function createBooster(setData) {
var mythic = [];
var rare = [];
var uncommon = [];
var common = [];
for (card of setData) {
if (card.rarity_name == "Common") {
common.push(card);
} else if(card.rarity_name == "Uncommon") {
uncommon.push(card);
} else if (card.rarity_name == "Rare") {
rare.push(card);
} else if (card.rarity_name == "Legendary") {
mythic.push(card);
}
}
common = utils.shuffleArray(common);
uncommon = utils.shuffleArray(uncommon);
rare = utils.shuffleArray(rare);
mythic = utils.shuffleArray(mythic);
var booster = common.slice(0,3);
booster = booster.concat(uncommon.slice(0,1));
if (Math.floor(Math.random() * 7) == 0) {
booster = booster.concat(mythic.slice(0,1));
} else {
booster = booster.concat(rare.slice(0,1));
}
return booster;
}
module.exports = {
getSwdBooster: function getSwdBooster(setCode, message, client) {
if (cache.get("swd_" + setCode)) {
utils.log("swd_" + setCode + " was found in the cache");
var booster = createBooster(cache.get("swd_" + setCode));
var cardnames = [];
var cardimage = "";
for (card of booster) {
cardnames.push(card.name);
if(card.rarity_name == "Rare" || card.rarity_name == "Legendary") {
cardimage = card.imagesrc;
utils.setActivityCard(card.name, client);
}
}
message.channel.send(new Discord.RichEmbed().setDescription(cardnames).setTitle("Star Wars Destiny Booster").setFooter("Want visuals? http://swdestinydb.com").setImage(cardimage));
} else {
request('https://swdestinydb.com/api/public/cards/' + setCode.replace(/\s/g, ''), {json: true}, function (error, response, setData) {
cache.put("swd_" + setCode, setData);
utils.log("Adding set to cache with key: swd_" + setCode);
var booster = createBooster(setData);
var cardnames = [];
var cardimage = "";
for (card of booster) {
cardnames.push(card.name);
if(card.rarity_name == "Rare" || card.rarity_name == "Legendary") {
cardimage = card.imagesrc;
}
}
message.channel.send(new Discord.RichEmbed().setDescription(cardnames).setTitle("Star Wars Destiny Booster").setFooter("Want visuals? http://swdestinydb.com").setImage(cardimage));
});
}
}
};