-
Notifications
You must be signed in to change notification settings - Fork 1
/
champions.js
108 lines (95 loc) · 2.9 KB
/
champions.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
const {get} = require('request-promise');
const {writeFile, existsSync, mkdirSync} = require('fs');
const {join} = require('path');
const log4js = require('log4js');
const logger = log4js.getLogger('champions');
logger.level = 'debug';
let locales = [
'cs_cz',
'de_de',
'default',
'el_gr',
'en_au',
'en_gb',
'es_ar',
'es_es',
'es_mx',
'fr_fr',
'hu_hu',
'it_it',
'ja_jp',
'ko_kr',
'pl_pl',
'pt_br',
'ro_ro',
'ru_ru',
'th_th',
'tr_tr',
'vn_vn',
'zh_cn',
'zh_tw',
];
/////////////////////////////////////////////
/// Here you can change file/folder names ///
let championsFileName = 'champions'; ///
let championsFolderName = 'champions'; ///
let idsFileName = 'ids'; ///
let idsFolderName = 'ids'; ///
/////////////////////////////////////////////
function generateJsonFiles() {
existsSync(championsFolderName) ?
logger.info(`${championsFolderName} exists continuing`) :
mkdirSync(championsFolderName);
existsSync(idsFolderName) ?
logger.info(`${idsFolderName} exists continuing`) :
mkdirSync(idsFolderName);
for (let i = 0; i < locales.length; i++) {
getChampionsJSON(locales[i].toLowerCase());
}
}
// Get and parse the data from cdragon
function getChampionsJSON(locale) {
get(`http://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/${locale}/v1/champion-summary.json`).
then(data => {
let json = JSON.parse(data);
let champions = new Array();
let key = new Array();
// Start from index 1 since index 0 isnt an actually champion its just None
for (let i = 1; i < json.length; i++) {
champions.push(json[i].name);
key.push(json[i].id);
}
makeChampionsJSON(locale, champions, key);
makeIdsJSON(locale, champions, key);
}).catch(err => {
logger.error(err);
});
}
// turn data into object then turn object into json string then write to file
function makeIdsJSON(locale, championArray, idArray) {
let ids = {};
for (let i = 0; i < championArray.length; i++) {
ids[championArray[i]] = idArray[i];
}
writeFile(join(idsFolderName, `${idsFileName}-${locale}.json`),
JSON.stringify(ids), err => {
if (err) {
logger.error(`Unable to write ${idsFileName}-${locale}.json`);
}
logger.info(`Created ${idsFileName}-${locale}.json`);
});
}
function makeChampionsJSON(locale, championArray, idArray) {
let champion = {};
for (let i = 0; i < championArray.length; i++) {
champion[idArray[i]] = championArray[i];
}
writeFile(join(championsFolderName, `${championsFileName}-${locale}.json`),
JSON.stringify(champion), err => {
if (err) {
logger.error(`Unable to write ${championsFileName}-${locale}.json`);
}
logger.info(`Created ${championsFileName}-${locale}.json`);
});
}
module.exports = {generateJsonFiles};