-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (55 loc) · 1.73 KB
/
index.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
const express = require('express');
const app = express()
const { jsonLoader } = require('./helpers/json');
const path = require('path');
const aliases = require('./helpers/alises');
const dataset = jsonLoader(path.resolve('./data/cards.json'));
app.get('/cards', (req, res) => {
res.json(dataset);
});
app.get('/cards/:name', (req, res) => {
const number = req.params.name.match(/[0-9]/g);
const query = req.params.name.replace(/[0-9]/g, '').trim();
var cards = dataset.filter(e => {
return e.name.toLowerCase() == query.toLowerCase();
});
if (cards.length == 0) {
cards = dataset.filter(e => {
return e.name.toLowerCase() == aliases[query.toLowerCase()].toLowerCase();
});
}
var card = number != null ? cards[number - 1] : cards[0];
if (!card) {
res.status(404).json({
message: 'Não encontrado'
});
}
res.json(card);
});
app.get('/lvlup/:name', (req, res) => {
var card = dataset.find(e => {
return e.name.toLowerCase() == req.params.name.toLowerCase() && e.supertype == 'Campeão';
});
if (!card) {
card = dataset.find(e => {
return e.name.toLowerCase() == aliases[req.params.name.toLowerCase()].toLowerCase();
});
}
if (!card) {
res.status(404).json({
message: 'Não encontrado'
});
}
res.json({
name: card.name,
levelUp: card.levelupDescriptionRaw
});
});
app.get('/random', (req, res) => {
const subdataset = dataset.filter(e => e.type != 'Habilidade');
const randomIndex = Math.floor(Math.random() * subdataset.length);
res.json(subdataset[randomIndex]);
});
app.listen(1337, () => {
console.log('API online!');
});