1
|
|
|
/* eslint-env node */ |
2
|
|
|
/*jslint node: true */ |
3
|
|
|
'use strict'; |
4
|
|
|
|
5
|
|
|
const jsonfile = require('jsonfile'); |
6
|
|
|
const template = require('lodash.template'); |
7
|
|
|
const crypto = require('crypto'); |
8
|
|
|
const fs = require('fs'); |
9
|
|
|
|
10
|
|
|
let upgrades = jsonfile.readFileSync('build/data/upgrades.json'); |
11
|
|
|
let exoticUpgrades = jsonfile.readFileSync('build/data/exotic_upgrades.json'); |
12
|
|
|
let darkUpgrades = jsonfile.readFileSync('build/data/dark_upgrades.json'); |
13
|
|
|
let generators = jsonfile.readFileSync('build/data/generators.json'); |
14
|
|
|
let upgradeComponent = fs.readFileSync('build/scripts/component/generators.js').toString(); |
15
|
|
|
let upgradeService = fs.readFileSync('build/scripts/services/upgrade.js').toString(); |
16
|
|
|
|
17
|
|
|
const FUNCTION_TEMPLATE = `this.<%= name %> = function (player, production, slot){ |
18
|
|
|
return <%= func %>; |
19
|
|
|
};`; |
20
|
|
|
|
21
|
|
|
const FIRE_ONCE_FUNCTION_TEMPLATE = `this.<%= name %> = function (player){ |
22
|
|
|
return <%= func %>; |
23
|
|
|
};`; |
24
|
|
|
|
25
|
|
|
let functionTemplate = template(FUNCTION_TEMPLATE); |
26
|
|
|
let fireOnceFunctionTemplate = template(FIRE_ONCE_FUNCTION_TEMPLATE); |
27
|
|
|
|
28
|
|
|
// add upgrades to generators |
29
|
|
|
for(let key in upgrades){ |
30
|
|
|
let upgrade = upgrades[key]; |
31
|
|
|
for(let generator of upgrade.tiers){ |
32
|
|
|
if(!generators[generator].upgrades){ |
33
|
|
|
generators[generator].upgrades = []; |
34
|
|
|
} |
35
|
|
|
generators[generator].upgrades.push(key); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
function generateFunctions(upgradesData, functionType, result, upgradeTemplate) { |
40
|
|
|
for(let i in upgradesData){ |
41
|
|
|
let upgrade = upgradesData[i]; |
42
|
|
|
//console.log(i+" "+functionType+ " "+upgrade[functionType]) |
43
|
|
|
if(!upgrade[functionType]){ |
44
|
|
|
continue; |
45
|
|
|
} |
46
|
|
|
if(upgrade[functionType].constructor === Array){ |
47
|
|
|
upgrade[functionType] = upgrade[functionType].join('\n'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
let name = '_'+crypto.createHash('md5').update(upgrade[functionType]).digest('hex'); |
51
|
|
|
result[name] = upgradeTemplate({ 'name': name, 'func': upgrade[functionType] }); |
52
|
|
|
// we overwrite progress with the name |
53
|
|
|
upgrade[functionType] = name; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
let functions = {}; |
58
|
|
|
let fireOncefunctions = {}; |
59
|
|
|
|
60
|
|
|
generateFunctions(upgrades, 'function', functions, functionTemplate); |
61
|
|
|
generateFunctions(upgrades, 'fire_once_function', fireOncefunctions, fireOnceFunctionTemplate); |
62
|
|
|
generateFunctions(exoticUpgrades, 'function', functions, functionTemplate); |
63
|
|
|
generateFunctions(exoticUpgrades, 'fire_once_function', fireOncefunctions, fireOnceFunctionTemplate); |
64
|
|
|
generateFunctions(darkUpgrades, 'function', functions, functionTemplate); |
65
|
|
|
generateFunctions(darkUpgrades, 'fire_once_function', fireOncefunctions, fireOnceFunctionTemplate); |
66
|
|
|
|
67
|
|
|
let concatFunctions = ''; |
68
|
|
|
for(let i in functions){ |
69
|
|
|
concatFunctions += functions[i]+'\n'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
let sourceTemplate = template(upgradeComponent); |
73
|
|
|
|
74
|
|
|
fs.writeFileSync('build/scripts/component/generators.js', sourceTemplate({'upgradeFunctions': concatFunctions})); |
75
|
|
|
|
76
|
|
|
concatFunctions = ''; |
77
|
|
|
for(let i in fireOncefunctions){ |
78
|
|
|
concatFunctions += fireOncefunctions[i]+'\n'; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
sourceTemplate = template(upgradeService); |
82
|
|
|
|
83
|
|
|
fs.writeFileSync('build/scripts/services/upgrade.js', sourceTemplate({'fireOnceFunctions': concatFunctions})); |
84
|
|
|
|
85
|
|
|
jsonfile.writeFileSync('build/data/upgrades.json', upgrades, { |
86
|
|
|
spaces: 2 |
87
|
|
|
}); |
88
|
|
|
jsonfile.writeFileSync('build/data/exotic_upgrades.json', exoticUpgrades, { |
89
|
|
|
spaces: 2 |
90
|
|
|
}); |
91
|
|
|
jsonfile.writeFileSync('build/data/dark_upgrades.json', darkUpgrades, { |
92
|
|
|
spaces: 2 |
93
|
|
|
}); |
94
|
|
|
jsonfile.writeFileSync('build/data/generators.json', generators, { |
95
|
|
|
spaces: 2 |
96
|
|
|
}); |
97
|
|
|
|