-
Notifications
You must be signed in to change notification settings - Fork 1
/
sets.js
198 lines (177 loc) · 6.07 KB
/
sets.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
const fs = require('fs');
const express = require('express');
var router = express.Router();
const SETS = ["7", "6", "6.5"]
/**
* Creates error JSON to return back to the user
* @param errorCode: the code/status of the error
* @param errorType: the type of error that occured
* @param errorMessage: the error message
**/
function CreateErrorJson(errorCode, errorType, errorMessage) {
return {
"status": errorCode,
"type": errorType,
"message": errorMessage,
}
}
SETS.forEach((set) => {
let setName = set.replace('.', '-');
let championData = fs.readFileSync(`./sets/set${set}/champions.json`);
let champions = JSON.parse(championData);
let itemData = fs.readFileSync(`./sets/set${set}/items.json`);
let items = JSON.parse(itemData);
let traitData = fs.readFileSync(`./sets/set${set}/traits.json`);
let traits = JSON.parse(traitData);
// Champions
router.get(`/set${set}/champions`, function(req, res) {
if (req.query.name) {
for (let champion of champions) {
let currChampion = champion["name"].toLowerCase();
if (currChampion === req.query.name.toLowerCase()) {
return res.json(champion);
}
}
return res.status(404).json(CreateErrorJson(404, "champion", `${req.query.name} is not the name of any champion`));
}
let retChamps = [...champions];
if (req.query.cost) {
let cost = req.query.cost.split(' ').map(Number);
retChamps = retChamps.filter(champion => {
return cost.includes(champion["cost"]);
});
}
if (req.query.traits) {
let traits = req.query.traits.split(' ').map(trait => { return trait.toLowerCase(); });
for (let trait of traits) {
retChamps = retChamps.filter(champion => {
let lowerTraits = champion["traits"].map(trait => { return trait.toLowerCase(); });
if (lowerTraits.includes(`set${setName}_${trait}`)) {
return true;
}
return false;
});
}
}
return res.json(retChamps);
});
// Traits
router.get(`/set${set}/traits`, function(req, res) {
if (req.query.name) {
for (let trait of traits) {
let name = req.query.name.toLowerCase();
if (trait["name"].toLowerCase() === name) {
return res.json(trait);
}
}
return res.status(404).json(CreateErrorJson(404, "trait", `${req.query.name} is not the name of any trait`));
}
let retTraits = [...traits];
if (req.query.type) {
let types = req.query.type.split(' ').map(type => { return type.toLowerCase(); });
retTraits = retTraits.filter(trait => {
for (let queryType of types) {
if (trait["type"].toLowerCase() === queryType) {
return true;
}
}
return false;
});
}
return res.json(retTraits)
});
// Items
router.get(`/set${set}/items`, function(req, res) {
if (req.query.id) {
for (let item of items) {
if (item["id"] === parseInt(req.query.id)) {
return res.json(item);
}
}
return res.status(404).json(CreateErrorJson(404, "item", `${req.query.id} is not the id of any item`));
}
if (req.query.name) {
for (let item of items) {
if (item["name"].toLowerCase() === req.query.name.toLowerCase()) {
return res.json(item);
}
}
return res.status(404).json(CreateErrorJson(404, "item", `${req.query.name} is not the name of any item`));
}
let retItems = [...items];
if (req.query.attr) {
let attributes = req.query.attr.split(' ');
if (typeof(attributes) === String) {
attributes = [attributes];
}
for (var i = 0; i < attributes.length; i++) {
switch (attributes[i].toLowerCase()) {
case "unique":
attributes[i] = "isUnique";
break;
case "elusive":
attributes[i] = "isElusive";
break;
case "artifact":
attributes[i] = "isArtifact";
break;
case "radiant":
attributes[i] = "isRadiant";
break;
default:
attributes[i] = "";
break;
}
}
retItems = retItems.filter(item => {
for (let attribute of attributes) {
try {
if (item[attribute] === true) {
return true;
}
}
catch {
continue;
}
}
return false;
});
}
return res.json(retItems);
});
// Images
router.get(`/set${set}/imgs`, function(req, res) {
if (req.query.champion) {
for (let champion of champions) {
let currChampion = champion["name"].toLowerCase();
if (currChampion == req.query.champion.toLowerCase()) {
return res.sendFile(`set${set}/champions/${champion["id"]}.png`, {"root": './sets/'});
}
}
return res.status(404).json(CreateErrorJson(404, "champion", `${req.query.champion} is not the name of any champion`));
}
if (req.query.item) {
for (let item of items) {
let currItem = item;
if (currItem["id"] === Number(req.query.item)) {
return res.sendFile(`set${set}/items/${item["id"]}.png`, {"root": './sets/'});
}
else if (currItem["name"].toLowerCase() === req.query.item.toLowerCase()) {
return res.sendFile(`set${set}/items/${item["id"]}.png`, {"root": './sets/'});
}
}
return res.status(404).json(CreateErrorJson(404, "item", `${req.query.item} is not the id or name of any item`));
}
if (req.query.trait) {
for (let trait of traits) {
let currTrait = trait;
if (currTrait["name"].toLowerCase() === req.query.trait.toLowerCase()) {
return res.sendFile(`set${set}/traits/${trait["name"]}.svg`, {"root": './sets/'});
}
}
return res.status(404).json(CreateErrorJson(404, "trait", `${req.query.trait} is not the name of any trait`));
}
return res.status(404).json(CreateErrorJson(404, "images", `No champion, trait, or item specified`));
});
});
module.exports = { router, CreateErrorJson };