Completed
Push — master ( 990788...87d271 )
by Andres
49s
created

ions   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
nc 2
dl 0
loc 9
rs 9.6666
nop 1
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
const args = process.argv.slice(2);
11
12
let achievements = jsonfile.readFileSync(args[0]+'/data/achievements.json');
13
let unlocks = jsonfile.readFileSync(args[0]+'/data/unlocks.json');
14
let achievementService = fs.readFileSync(args[0]+'/scripts/component/achievements.js').toString();
15
16
const FUNCTION_TEMPLATE = `this.<%= name %> = function (player){
17
  return <%= progress %>;
18
};`;
19
20
let functionTemplate = template(FUNCTION_TEMPLATE);
21
22
// convert conditions to progress, to normalise the implementation
23
function conditionToProgress(source){
24
  for(let i in source){
25
  let achievement = source[i];
26
    if(typeof achievement.condition === 'undefined') {
27
      continue;
28
    }
29
    if(achievement.condition.constructor === Array){
30
      achievement.condition = achievement.condition.join('\n');
31
    }
32
    achievement.progress = '('+achievement.condition+') ? 1 : 0';
33
    achievement.goals = [1];
34
    delete achievement.condition;
35
  }
36
}
37
function createFunctions(source){
38
  for(let i in source){
39
    let achievement = source[i];
40
    let name = '_'+crypto.createHash('md5').update(achievement.progress).digest('hex');
41
    functions[name] = functionTemplate({ 'name': name, 'progress': achievement.progress });
42
    // we overwrite progress with the name
43
    source[i].progress = name;
44
  }
45
}
46
47
let functions = {};
48
conditionToProgress(achievements);
49
conditionToProgress(unlocks);
50
createFunctions(achievements);
51
createFunctions(unlocks);
52
53
let concatFunctions = '';
54
for(let i in functions){
55
  concatFunctions += functions[i]+'\n';
56
}
57
58
let serviceTemplate = template(achievementService);
59
60
fs.writeFileSync(args[0]+'/scripts/component/achievements.js', serviceTemplate({'functions': concatFunctions}));
61
62
jsonfile.writeFileSync(args[0] + '/data/achievements.json', achievements, {
63
  spaces: 2
64
});
65
jsonfile.writeFileSync(args[0] + '/data/unlocks.json', unlocks, {
66
  spaces: 2
67
});
68