Some checks on 20Q

Fix a bug with babele on drop a compendium in 20q
This commit is contained in:
Vlyan
2020-12-30 16:12:45 +01:00
parent 7cab7de52b
commit 2074caa125
10 changed files with 101 additions and 59 deletions

View File

@@ -1,3 +1,4 @@
import { HelpersL5r5e } from "../helpers.js";
import { TwentyQuestions } from "./twenty-questions.js";
/**
@@ -12,14 +13,14 @@ export class TwentyQuestionsDialog extends FormApplication {
actor = null;
/**
* Errors object
* Errors
*/
errors = {};
errors = [];
/**
* Cache for items (techniques, adv...)
*/
cache = {};
cache = null;
/**
* Assign the default options
@@ -67,7 +68,15 @@ export class TwentyQuestionsDialog extends FormApplication {
this.actor = actor;
this.object = new TwentyQuestions(actor);
this.errors = this.object.validateForm();
this._constructCache();
}
/**
* Construct async cache here
* @override
*/
async _render(force = false, options = {}) {
await this._constructCache();
return super._render(force, options);
}
/**
@@ -105,7 +114,7 @@ export class TwentyQuestionsDialog extends FormApplication {
* @param options
* @return {Object}
*/
getData(options = null) {
async getData(options = null) {
return {
...super.getData(options),
ringsList: game.l5r5e.HelpersL5r5e.getRingsList(),
@@ -114,10 +123,8 @@ export class TwentyQuestionsDialog extends FormApplication {
techniquesList: CONFIG.l5r5e.techniques,
data: this.object.data,
cache: this.cache,
errors: Object.keys(this.errors)
.map((key) => `${game.i18n.localize("l5r5e.rings." + key)} (${this.errors[key]})`)
.join(", "), // TODO better msg :D
hasErrors: Object.keys(this.errors).length > 0,
errors: this.errors.join(", "),
hasErrors: this.errors.length > 0,
};
}
@@ -138,7 +145,7 @@ export class TwentyQuestionsDialog extends FormApplication {
const stepKey = $(event.currentTarget).parents(".tq-drag-n-drop").data("step");
const itemId = $(event.currentTarget).parents(".property").data("propertyId");
this._deleteOwnedItem(stepKey, itemId);
this.render(false);
this.submit();
});
// Submit button
@@ -176,10 +183,7 @@ export class TwentyQuestionsDialog extends FormApplication {
// Add the item (step and cache)
this._addOwnedItem(item, stepKey);
// TODO specific event (no added honor if tech selected etc)
// console.log(this.object.data, this.cache);
this.render(false);
this.submit();
} catch (err) {
console.warn(err);
}
@@ -194,14 +198,19 @@ export class TwentyQuestionsDialog extends FormApplication {
* @override
*/
async _updateObject(event, formData) {
// Check "Or" conditions
formData["step7.social_add_glory"] = formData["step7.skill"] === "none" ? 5 : 0;
formData["step8.social_add_honor"] = formData["step8.skill"] === "none" ? 10 : 0;
if (formData["step13.skill"] !== "none" && this.object.data.step13.advantage.length > 0) {
formData["step13.skill"] = "none";
}
// Update 20Q object data
this.object.updateFromForm(formData);
// Get errors if any
this.errors = this.object.validateForm();
// Only on close/submit
// if (event.type === "submit") {
// Store this form datas in actor
this.actor.data.data.twenty_questions = this.object.data;
this.actor.update({
@@ -209,16 +218,16 @@ export class TwentyQuestionsDialog extends FormApplication {
twenty_questions: this.object.data,
},
});
// }
this.render(false);
}
/**
* Construct the cache tree with Items full object
*/
_constructCache() {
async _constructCache() {
this.cache = {};
TwentyQuestions.itemsList.forEach((stepName) => {
for (const stepName of TwentyQuestions.itemsList) {
// Check if current step value is a array
let step = getProperty(this.object.data, stepName);
if (!step || !Array.isArray(step)) {
@@ -232,16 +241,19 @@ export class TwentyQuestionsDialog extends FormApplication {
// Get linked Item, and store it in cache (delete null value and old items)
const newStep = [];
step.forEach((id) => {
const item = game.items.get(id);
if (!id || !item) {
return;
for (const id of step) {
if (!id) {
continue;
}
const item = await HelpersL5r5e.getObjectGameOrPack(id, "Item");
if (!item) {
continue;
}
newStep.push(id);
getProperty(this.cache, stepName).push(item);
});
}
setProperty(this.object.data, stepName, newStep);
});
}
}
/**

View File

@@ -50,15 +50,15 @@ export class TwentyQuestions {
clan: "",
ring: "",
skill: "",
social_status: 0,
social_status: 30,
},
step2: {
family: "",
ring: "",
skill1: "",
skill2: "",
wealth: 0,
social_glory: 0,
wealth: 3,
social_glory: 39,
},
step3: {
school: "",
@@ -82,7 +82,7 @@ export class TwentyQuestions {
techniques: [],
school_ability: [],
equipment: [],
social_honor: 0,
social_honor: 30,
},
step4: {
stand_out: "",
@@ -97,12 +97,12 @@ export class TwentyQuestions {
step7: {
clan_relations: "",
skill: "",
social_add_glory: null,
social_add_glory: 0,
},
step8: {
bushido: "",
skill: "",
social_add_honor: null,
social_add_honor: 0,
},
step9: {
success: "",
@@ -213,7 +213,7 @@ export class TwentyQuestions {
const formData = this.data;
// Update the actor real datas
actorDatas.zeni = formData.step2.wealth;
actorDatas.zeni = Math.floor(formData.step2.wealth * 50);
actorDatas.identity = {
...actorDatas.identity,
clan: formData.step1.clan,
@@ -274,7 +274,7 @@ export class TwentyQuestions {
if (itemData.data?.bought_at_rank) {
itemData.data.bought_at_rank = 0;
}
actor.createEmbeddedEntity("OwnedItem", duplicate(itemData));
actor.createEmbeddedEntity("OwnedItem", itemData);
});
});
@@ -292,13 +292,22 @@ export class TwentyQuestions {
* Return a array of errors, empty array if no errors founds
*/
validateForm() {
// Rings & Skills
const errors = [];
// Rings & Skills, 3pt max for each
const rings = this._checkRingsOrSkills("ringList", 2); // ring start at 1
for (const key in rings) {
errors.push(`${game.i18n.localize("l5r5e.rings." + key)} (${rings[key]})`);
}
const skills = this._checkRingsOrSkills("skillList", 3); // skill start at 0
for (const key in skills) {
errors.push(
`${game.i18n.localize("l5r5e.skills." + CONFIG.l5r5e.skills.get(key) + "." + key)} (${skills[key]})`
);
}
// TODO Techniques / peculiarities
return { ...rings, ...skills };
return errors;
}
/**

View File

@@ -97,6 +97,12 @@ export class HelpersL5r5e {
// Unknown pack object, iterate all packs
for (const comp of game.packs) {
// TODO Bug with babele if "comp.getEntity(id)" return null...
const babeleFix = (await comp.getIndex()).some((e) => e._id === id);
if (!babeleFix) {
continue;
}
const data = await comp.getEntity(id);
if (data) {
return HelpersL5r5e.createItemFromCompendium(data);