forked from leek-wars/leek-wars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_data.js
57 lines (55 loc) · 2.4 KB
/
generate_data.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
const fs = require('fs')
const request = require('request-promise')
const util = require('util')
const values = [
['chips', 'chips', 'chip/get-all', '{[key: string]: ChipTemplate}'],
['constants', 'constants', 'constant/get-all', 'Constant[]'],
['functions', 'functions', 'function/get-all', 'LSFunction[]'],
['hats', 'hats', 'hat/get-all', '{[key: string]: HatTemplate}'],
['weapons', 'weapons', 'weapon/get-all', '{[key: string]: WeaponTemplate}'],
['pomps', 'pomps', 'pomp/get-all', '{[key: string]: PompTemplate}'],
['potions', 'potions', 'potion/get-all', '{[key: string]: PotionTemplate}'],
['hat_templates', 'hat_templates', 'hat/get-templates', '{[key: string]: {id: number, item: number}}'],
['chip_templates', 'chip_templates', 'chip/get-templates', '{[key: string]: {id: number, item: number}}'],
['summon_templates', "summon_templates", 'summon/get-templates', '{[key: string]: SummonTemplate}'],
['trophies', 'trophies', 'trophy/get-all'],
['trophy_categories', 'trophy_categories', 'trophy/get-categories'],
['items', null, 'item/get-all', '{[key: string]: ItemTemplate}'],
]
const promises = []
for (const value of values) {
// const host = 'http://localhost:8500/'
const host = 'https://leekwars.com/'
const p = request(host + 'api/' + value[2])
promises.push(p.then((data) => {
const json = JSON.parse(data)
console.log('received', value[0])
return "const " + value[0].toUpperCase()
+ (value[3] ? ': ' + value[3] : '')
+ " = " + util.inspect(value[1] ? json[value[1]] : json, {depth: Infinity, breakLength: Infinity, maxArrayLength: Infinity})
+ "\nexport { " + value[0].toUpperCase() + " }"
}).catch((err) => {
console.log("ERROR request failed for", value[0])
process.exit()
}))
}
Promise.all(promises).then((result) => {
let data = `/** This file is auto-generated from script/generate_data.js **/
/* tslint:disable */
import { ChipTemplate } from '@/model/chip'
import { Constant } from '@/model/constant'
import { LSFunction } from '@/model/function'
import { HatTemplate } from '@/model/hat'
import { ItemTemplate } from '@/model/item'
import { PompTemplate } from '@/model/pomp'
import { PotionTemplate } from '@/model/potion'
import { SummonTemplate } from '@/model/summon'
import { WeaponTemplate } from '@/model/weapon'
\n`
for (const v of result) {
data += v + "\n\n"
}
const file = 'src/model/data.ts'
fs.writeFileSync(file, data)
console.log(file + ' created successfully!')
})