-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
filterModifiers.js
165 lines (148 loc) · 5.48 KB
/
filterModifiers.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* UTILITY
* Returns a string representation of friendlyTypename and friendlySubtypeName for an obj[]
* @param {object[]} arr array of objects
*/
function extractInfo(arr) {
return arr.map(e => `${e.friendlyTypeName} (${e.friendlySubtypeName})`);
}
/**
* Extracts basic character information
* @param {object} data Character JSON
* returns information about the classes this character chose, including
* - {string} name
* - {number} level
* - {boolean} isStartingClass
* - {object[]} modifiers (empty, will be filled later)
* }
*/
function getClassInfo(data) {
return data.classes.map(cls => {
return {
name:
cls.subclassDefinition && cls.subclassDefinition.name
? `${cls.definition.name} (${cls.subclassDefinition.name})`
: cls.definition.name,
level: cls.level,
isStartingClass: cls.isStartingClass,
modifiers: [],
};
});
}
/**
* Gets all class features up to a certain class level
* @param {obj} cls character.classes[] entry
* @param {*} classLevel level requirement up to which the class features should be extracted
*/
function getClassFeatures(cls, classLevel = 20) {
if (
cls.subclassDefinition
&& cls.subclassDefinition.classFeatures
&& Array.isArray(cls.subclassDefinition.classFeatures)
) {
return cls.classFeatures
.map(feature => feature.definition)
.concat(cls.subclassDefinition.classFeatures)
.filter(classFeature => classFeature.requiredLevel <= classLevel)
.sort((a, b) => a.requiredLevel - b.requiredLevel);
} else {
return cls.classFeatures
.map(feature => feature.definition)
.filter(classFeature => classFeature.requiredLevel <= classLevel)
.sort((a, b) => a.requiredLevel - b.requiredLevel);
}
}
/**
* Checks if a given class is the starting class of this character
* @param {object} data character data
* @param {string} className name of the class to check
* @returns {boolean} true of the class is a starting class, false otherwise
*/
function isStartingClass(data, className) {
return data.classes.find(cls => cls.definition.name === className && cls.isStartingClass);
}
/**
* Gets all class modifiers for a given character
* This filters out all modifiers that do not have an entry in the class features passed in
* For multiclassing characters, it checks if the given class is the starting class or a multiclass,
* then the `.availableToMulticlass` is queried if this modifier is enabled or not
* @param {obj} cls character.classes[] entry
* @param {*} classLevel level requirement up to which the class features should be extracted
*/
function getClassModifiers(data, classFeatures, isStartingClass = false) {
const modifiers = data.modifiers.class.filter(classModifier => {
// check the class from which this modifier came
const componentId = classModifier.componentId;
//const feature = classFeatures.find(feature => feature.id === componentId || chosenOptions.includes(feature.id));
const feature = classFeatures.find(feature => feature.id === componentId);
if (feature !== undefined) {
const isFeatureAvailable = classModifier.availableToMulticlass ? true : isStartingClass;
console.log(
`${isFeatureAvailable ? " [ AVAIL]" : " [UNAVAIL]"} Modifier found: ${classModifier.friendlyTypeName} (${
classModifier.friendlySubtypeName
})`
);
return isFeatureAvailable;
}
return false;
});
return modifiers;
}
function getClassOptionModifiers(data) {
const classFeatures = data.classes.map((cls) => {
return getClassFeatures(cls, cls.level);
}).flat();
const modifiers = data.modifiers.class.filter(classModifier => {
const componentId = classModifier.componentId;
const feature = classFeatures.find(feature => feature.id === componentId);
if (feature === undefined) {
console.log(
` [ EXTRA] Modifier found: ${classModifier.friendlyTypeName} (${
classModifier.friendlySubtypeName
})`
);
return true;
}
return false;
});
return modifiers;
}
/**
* Filters the modifiers with the utility functions above
* @param {object} data character data
* @returns {[object[]]} an array containing an array of filtered modifiers, grouped by class
*/
function filterModifiers(data, classInfo) {
// get the classFeatures for all classes
//const classInfo = getClassInfo(data);
data.classes.forEach((cls, index) => {
const features = getClassFeatures(cls, cls.level);
classInfo[index].modifiers = getClassModifiers(data, features, isStartingClass(data, cls.definition.name));
});
return classInfo;
}
/**
* =============================================================
* MAIN
* =============================================================
* Get the class information for this character
*/
function main(data) {
console.log("[ MODIFIERS ====================================================== ]");
const classInfo = getClassInfo(data.character);
const filteredClassInfo = filterModifiers(data.character, classInfo);
let classModifiers = getClassOptionModifiers(data.character, classInfo);
console.log("");
filteredClassInfo.forEach(cls => {
console.log(`${cls.isStartingClass ? "Starting Class" : "Multiclass"}: [lvl${cls.level}] ${cls.name} `);
console.log(
extractInfo(cls.modifiers)
.map(s => ` ${s}`)
.join("\n")
);
classModifiers = classModifiers.concat(cls.modifiers);
});
data.character.modifiers.class = classModifiers;
return data;
}
module.exports = main;