forked from shawtan/lolchest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
194 lines (163 loc) · 5.4 KB
/
app.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
const DEBUG = false;
// Load environment variables
require('dotenv').config({silent: !DEBUG});
const port = process.env.PORT;
const express = require('express');
const app = express();
const path = require('path');
const request = require('./helpers/request');
const cache = require('./helpers/cache');
// Defined values
const rank = ['S+', 'S', 'S-', 'A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D', 'D-', 'No Rank'];
const CHAMPION_ROLES = require('./champion_roles.json');
const REGIONS = require('./regions.json');
const NUMBER_CHAMPS_TO_RECOMMEND = 6;
// Main page
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname + '/public/index.html'));
});
app.get('/riot.txt', function (req, res) {
res.sendFile(path.join(__dirname + '/riot.txt'));
});
// API queries
app.get('/info/:region/:username/:role*?', function (req, res) {
if (req.params.role === undefined) {
req.params.role = 'all';
}
req.params.region = REGIONS[req.params.region];
console.log('Info request for ' + req.params.username + ' in ' + req.params.region + ' for role ' + req.params.role); // eslint-disable-line no-console
getSummonerId(req.params)
.then(getChampionMastery)
.then(addChampionInfo)
.then(filterChampions)
.then(displayData)
.then(data => res.json(data))
.catch(err => {
console.log('Error:', err); // eslint-disable-line no-console
res.json('Internal error');
});
});
app.listen(port, function () {
DEBUG && console.log('App started at localhost:' + port); // eslint-disable-line no-console
});
/****** Preform additional quries to assemble information ******/
const getSummonerId = (params) => {
const summoner_name = params.username;
const region = params.region;
const url = `https://${region}.api.riotgames.com/lol/summoner/v4/summoners/by-name/` + encodeURIComponent(summoner_name);
return request(url).then(({id, name}) =>
({
...params,
summoner_id: id,
summoner_name: name
})
);
};
const getChampionMastery = (data) => {
const region = data.region;
const url = `https://${region}.api.riotgames.com/lol/champion-mastery/v4/champion-masteries/by-summoner/${data['summoner_id']}`;
return request(url).then(mastery_info =>
({
...data,
champions: mastery_info,
mastery_info
})
);
};
const addChampionInfo = (data) => {
const region = data.region;
return cache.getStaticData(region).then(response => {
const champlist = response.champions;
const champArray = [];
for (const championInfo in champlist) {
champArray.push(combineChampionInfo(champlist[championInfo], data.mastery_info));
}
data.champions = champArray;
data.champions.sort(compareChampion);
data.version = response.version;
return data;
});
};
const filterChampions = (data) => {
data.has_chest = data.champions.filter(championHasChest);
data.recommended = data.champions
.filter(championHasNoChest)
.filter(c => championRoleMatches(c, data.role))
.slice(0, NUMBER_CHAMPS_TO_RECOMMEND);
return data;
};
/**
Responds with the assembled information
**/
function displayData({
summoner_name,
summoner_id,
has_chest,
recommended,
version,
}) {
return {
summoner_name,
summoner_id,
has_chest,
recommended,
version,
};
}
/****** Helper Functions ******/
function combineChampionInfo({name, title, key, image, id}, championMasteryList) {
const championMastery = championMasteryList.find(function (c) {
return c.championId == key; // eslint-disable-line eqeqeq
});
const result = {
name,
title,
key,
'champion_points': 0,
image,
'has_chest': false,
id,
};
if (championMastery !== undefined) {
if (championMastery.highestGrade !== undefined) {
result.highest_grade = championMastery.highestGrade;
}
if (championMastery.championPoints !== undefined) {
result.champion_points = championMastery.championPoints;
}
if (championMastery.chestGranted !== undefined) {
result.has_chest = championMastery.chestGranted;
}
}
return result;
}
function championHasChest(championMastery) {
return championMastery.has_chest;
}
function championHasNoChest(championMastery) {
return !championHasChest(championMastery);
}
function championRoleMatches(champion, role) {
return role === 'all' || (role in CHAMPION_ROLES && CHAMPION_ROLES[role].indexOf(champion.key) !== -1);
}
function compareChampion(c1, c2) {
// return c2.champion_points - c1.champion_points;
const score1 = c1.highest_grade;
const score2 = c2.highest_grade;
if (score1 === null && score2 === null || rank.indexOf(score1) === rank.indexOf(score2)) {
// Compare mastery points
if (c1.champion_points === c2.champion_points) {
// Compare win rates
return 0;
} else {
return c2.champion_points - c1.champion_points;
}
} else if (score2 === null) {
return -1;
} else if (score1 === null) {
return 1;
} else {
return rank.indexOf(score1) - rank.indexOf(score2);
}
}