-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
146 lines (127 loc) · 4.41 KB
/
server.js
File metadata and controls
146 lines (127 loc) · 4.41 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
const _ = require('underscore');
const bodyParser = require('body-parser');
const config = require('./config.json');
const express = require('express');
const fs = require('fs');
const path = require('path');
const mongoose = require('mongoose');
const request = require('request');
const session = require('express-session');
const winston = require('winston');
const championGGKey = config.championGGKey;
const riotAPIKey = config.riotAPIKey;
const app = express();
winston.level = config.env === 'prod' ? 'error' : 'debug';
mongoose.connect('mongodb://localhost/lolrandom');
app.use(express.static('./public'));
app.use(bodyParser.urlencoded({ extended: 'true' })); // parse application/x-www-form-urlencoded
app.use(bodyParser.json()); // parse application/json
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json
app.use(session({ cookie: { maxAge: 60000 }, secret: 'api', resave: false, saveUninitialized: false }));
// Champion Model in Mongo
const Champion = mongoose.model('Champion', {
tags: Array,
laneInfo: Array,
name: String,
});
function createLaneInfoObj(patchWin, patchPlay) {
winston.debug(patchWin, patchPlay);
return {
patchWin: patchWin[patchWin.length - 1] > patchWin[patchWin.length - 2] ? 'UP' : 'DOWN',
patchPlay: patchPlay[patchPlay.length - 1] > patchPlay[patchPlay.length - 2] ? 'UP' : 'DOWN',
};
}
// Populats more tags from champoin.gg's api
function getMoreChampionData(championName) {
const retObj = {
tags: [],
laneInfo: [],
};
return new Promise((resolve, reject) => {
request(`http://api.champion.gg/champion/${championName}?api_key=${championGGKey}`, (err, response, body) => {
if (err) {
winston.error(err);
reject(err);
}
const data = JSON.parse(body);
_.each(data, (element) => {
// dmgComposition
const damageType = element.dmgComposition.magicDmg > element.dmgComposition.physicalDmg ? 'Magic' : 'Physical';
if (!_.contains(retObj.tags, damageType)) {
retObj.tags.push(damageType);
}
// lanes
retObj.tags.push(element.role);
// laneInfo
const laneInfoObj = createLaneInfoObj(element.patchWin, element.patchPlay);
const obj = {};
obj[element.role] = laneInfoObj;
retObj.laneInfo.push(obj);
});
resolve(retObj);
});
});
}
function addToMongo(allTags, laneInformation, index) {
return Champion.create({
tags: allTags,
laneInfo: laneInformation,
name: index,
});
}
function getChampionImage(championName) {
return new Promise((resolve, reject) => {
const championNamePng = `${championName}.png`;
// Make patch number configurat
request(`http://ddragon.leagueoflegends.com/cdn/6.24.1/img/champion/${championNamePng}`)
.on('error', (err) => {
winston.error(err);
reject(err);
})
.pipe(fs.createWriteStream(`./public/images/${championNamePng}`));
resolve('saved');
});
}
function populateFromData(data) {
// ONLY ADDS RIGHT NOW
// needs to update existing ones
_.each(data, (element, index) => {
let allTags = [];
const getMoreChampionDataPromise = getMoreChampionData(index);
let addToMongoPromise;
let getChampionImagePromise;
getMoreChampionDataPromise.then((championData) => {
allTags = _.union(championData.tags, element.tags);
addToMongoPromise = addToMongo(allTags, data.laneInfo, index);
getChampionImagePromise = getChampionImage(index);
});
Promise.all([addToMongoPromise, getChampionImagePromise]).catch((err) => {
throw err;
});
});
winston.debug('added/updated');
}
// Populates the db by hitting riot's api.
app.get('/api/riotTags', (req, res) => {
// Version number in config
const championDataUrl = `https://na.api.pvp.net/api/lol/static-data/na/v1.2/champion?champData=tags&api_key=${riotAPIKey}`;
request(championDataUrl, (err, response, body) => {
if (err) throw err;
const data = JSON.parse(body);
populateFromData(data.data);
});
res.send('success');
});
app.post('/api/championsByRoles', (req, res) => {
Champion.find(
{ tags: { $all: req.body } },
(err, champs) => {
if (err) throw err;
res.send(champs);
});
});
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '/public/'));
});
app.listen(config.appPort);
winston.debug('App listening on port 8080');