-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdata-storage.js
More file actions
94 lines (81 loc) · 2.93 KB
/
Copy pathdata-storage.js
File metadata and controls
94 lines (81 loc) · 2.93 KB
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
"use strict";
const debugEarmark = require('debug')('sc2:debug:earmark');
const { Race, Attribute } = require('../constants/enums');
const { AbilitiesByUnit } = require('../constants');
/**
* @returns {DataStorage & Map}
*/
function createDataManager() {
const data = new Map([
['earmarks', []],
['abilities', null],
['units', null],
['upgrades', null],
['buffs', null],
['effects', null],
]);
const StorageBlueprint = {
register(name, fn) {
this[name] = fn;
},
findUnitTypesWithAbility(abilityId) {
return Object.entries(AbilitiesByUnit)
.filter(([ignored, abilities]) => {
return abilities.some(ability => ability === abilityId);
})
.map(unitAbility => parseInt(unitAbility[0], 10));
},
getUnitTypeData(unitTypeId) {
/** @type {SC2APIProtocol.UnitTypeData} */
const unitData = this.get('units')[unitTypeId];
/**
* Fixes unit cost for zerg structures (removes the 'drone cost' inflation)
*/
if (unitData.race === Race.ZERG && unitData.attributes.includes(Attribute.STRUCTURE)) {
return {
...unitData,
mineralCost: unitData.mineralCost - 50,
};
} else {
return unitData;
}
},
getUpgradeData(upgradeId) {
return this.get('upgrades')[upgradeId];
},
getAbilityData(abilityId) {
return this.get('abilities')[abilityId];
},
getEffectData(effectId) {
return this.get('effects')[effectId];
},
mineralCost(unitTypeId) {
return this.getUnitTypeData(unitTypeId).mineralCost;
},
addEarmark(earmark) {
const earmarks = this.get('earmarks');
const exists = earmarks.find(em => em.name === earmark.name);
if (exists) {
return earmarks;
} else {
debugEarmark('New earmark:', earmark);
return this.set('earmarks', [ ...earmarks, earmark ]);
}
},
getEarmarkTotals(earmarkName) {
const total = this.get('earmarks').filter(em => em.name !== earmarkName).reduce((totals, em) => {
return { minerals: totals.minerals + em.minerals, vespene: totals.vespene + em.vespene };
}, { minerals: 0, vespene: 0});
return total;
},
settleEarmark(earmarkName) {
const newEarmarks = this.get('earmarks').filter(em => em.name !== earmarkName);
this.set('earmarks', newEarmarks);
return this.get('earmarks');
},
};
/** @type {DataStorage} */
const dataMap = Object.assign(data, StorageBlueprint);
return dataMap;
}
module.exports = createDataManager;