Skip to content

Commit

Permalink
made classes respect the supported sources
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Mauro committed May 29, 2021
1 parent 9d52e4e commit a65bc6f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Currently support
- items and items base
- spells
- races
- classes (does not allow selection of sources)
- classes

### .ENV file

Expand Down
64 changes: 47 additions & 17 deletions classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,42 +31,66 @@ const main = async () => {
const subclassFeatures = responseClass.data.subclassFeature || [];

const newClassItems = classItems.map((classItem) => {
// don't process classes from sources not supported
if (!classSources.includes(classItem.source)) return null;
// merge class features into one structure
const newClassFeatures = classItem?.classFeatures.map((feature) => {
if (typeof feature === "string") {
const values = feature.split("|");
let values = feature.split("|");
values[2] = _.isEmpty(values[2]) ? "PHB" : values[2];
values[4] = _.isEmpty(values[4]) ? values[2] : values[4];
// don't process class features from sources not supported
if (
!classSources.includes(values[2]) ||
!classSources.includes(values[4])
)
return null;
return {
gainSubclassFeature: false,
..._.find(classFeatures, {
name: values[0],
className: values[1],
classSource: _.isEmpty(values[2]) ? "PHB" : values[2],
classSource: values[2],
level: parseInt(values[3]),
}),
};
} else {
const values = feature.classFeature.split("|");
let values = feature.classFeature.split("|");
values[2] = _.isEmpty(values[2]) ? "PHB" : values[2];
values[4] = _.isEmpty(values[4]) ? values[2] : values[4];
// don't process class features from sources not supported
if (
!classSources.includes(values[2]) ||
!classSources.includes(values[4])
)
return null;
return {
gainSubclassFeature: feature.gainSubclassFeature,
..._.find(classFeatures, {
name: values[0],
className: values[1],
classSource: _.isEmpty(values[2]) ? "PHB" : values[2],
classSource: values[2],
level: parseInt(values[3]),
}),
};
}
});
// merge subclass features into one structure
const newSubclass = classItem?.subclasses?.map((subclass) => {
// don't process classes from sources not supported
if (!classSources.includes(subclass.source)) return null;
// now loop through subclassFeatures array
const newFeatures = subclass?.subclassFeatures?.map((feature) => {
const values = feature.split("|");
let values = feature.split("|");
values[4] = _.isEmpty(values[4]) ? "PHB" : values[4];
console.log(values);
// don't process class features from sources not supported
if (!classSources.includes(values[4])) return null;
return {
..._.find(subclassFeatures, {
name: values[0],
className: values[1],
subclassSource: _.isEmpty(values[4]) ? "PHB" : values[4],
subclassSource: values[4],
subclassShortName: values[3],
level: parseInt(values[5]),
}),
Expand All @@ -78,22 +102,28 @@ const main = async () => {
};
});
return {
id: `${classItem.name.replace(/\W/g, "")}-${classItem.source}-${
classItem.page
id: `${classItem.name.replace(/\W/g, "")}-${classItem?.source}-${
classItem?.page
}`.toLowerCase(),
...classItem,
classFeatures: !_.isEmpty(newClassFeatures) ? newClassFeatures : [],
subclasses: !_.isEmpty(newSubclass) ? newSubclass : [],
classFeatures: !_.isEmpty(newClassFeatures)
? newClassFeatures.filter((o) => o)
: [],
subclasses: !_.isEmpty(newSubclass)
? newSubclass.filter((o) => o)
: [],
};
});
await Promise.all(
newClassItems.map(async (item) => {
console.log(item.id);
const query = { id: item.id };
const update = { $set: item };
const options = { upsert: true };
await dbo.collection("classes").updateOne(query, update, options);
})
newClassItems
.filter((o) => o)
.map(async (item) => {
console.log(item.id);
const query = { id: item.id };
const update = { $set: item };
const options = { upsert: true };
await dbo.collection("classes").updateOne(query, update, options);
})
);
})
);
Expand Down

0 comments on commit a65bc6f

Please sign in to comment.