-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAttributeFactory.ts
141 lines (123 loc) · 2.83 KB
/
AttributeFactory.ts
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { Attribute, AttributeFormula, IAttributeDef } from './Attribute';
type OmitFormula = Omit<IAttributeDef, 'formula'>;
export interface AttributeFactoryAttribute extends OmitFormula {
formula: AttributeFormula | null;
}
/**
* @property {Map} attributes
*/
export class AttributeFactory {
/** @property {Map} attributes */
attributes: Map<string, AttributeFactoryAttribute>;
constructor() {
this.attributes = new Map();
}
/**
* @param {string} name
* @param {number} base
* @param {AttributeFormula} formula
*/
add(
name: string,
base: number,
formula: AttributeFormula | null = null,
metadata = {}
) {
if (formula && !(formula instanceof AttributeFormula)) {
throw new TypeError('Formula not instance of AttributeFormula');
}
this.attributes.set(name, {
name,
base,
formula,
metadata,
});
}
/**
* @see Map#has
*/
has(name: string) {
return this.attributes.has(name);
}
/**
* Get a attribute definition. Use `create` if you want an instance of a attribute
* @param {string} name
* @return {object}
*/
get(name: string) {
return this.attributes.get(name);
}
/**
* @param {string} name
* @param {number} base
* @param {number} delta
* @return {Attribute}
*/
create(name: string, base: number | null = null, delta = 0) {
const def = this.attributes.get(name);
if (!def) {
throw new RangeError(`No attribute definition found for [${name}]`);
}
return new Attribute(
name,
base || def.base,
delta,
def.formula,
def.metadata || {}
);
}
/**
* Make sure there are no circular dependencies between attributes
* @throws Error
*/
validateAttributes() {
const references: Record<string, string[]> = {};
[...this.attributes].reduce((acc, [attrName, { formula }]) => {
if (!formula) {
return acc;
}
acc[attrName] = formula.requires;
return acc;
}, references);
for (const attrName in references) {
const check = this.checkReferences(attrName, references);
if (Array.isArray(check)) {
const path = check.concat(attrName).join(' -> ');
throw new Error(
`Attribute formula for [${attrName}] has circular dependency [${path}]`
);
}
}
}
/**
* @private
* @param {string} attr attribute name to check for circular ref
* @param {Object.<string, Array<string>>} references
* @param {Array<string>} stack
* @return bool
*/
private checkReferences(
attr: string,
references: Record<string, string[]>,
stack: string[] = []
): boolean | string[] {
if (stack.includes(attr)) {
return stack;
}
const requires = references[attr];
if (!requires || !requires.length) {
return true;
}
for (const reqAttr of requires) {
const check = this.checkReferences(
reqAttr,
references,
stack.concat(attr)
);
if (Array.isArray(check)) {
return check;
}
}
return true;
}
}