diff --git a/system/scripts/actors/twenty-questions-dialog.js b/system/scripts/actors/twenty-questions-dialog.js index 40d13ae..6ec5a9a 100644 --- a/system/scripts/actors/twenty-questions-dialog.js +++ b/system/scripts/actors/twenty-questions-dialog.js @@ -1,4 +1,4 @@ -import { L5R5E } from "../config.js"; +import { TwentyQuestions } from "./twenty-questions.js"; /** * L5R Twenty Questions form @@ -11,10 +11,7 @@ export class TwentyQuestionsDialog extends FormApplication { */ actor = null; - /** - * Current form datas - */ - datas = {}; + errors = []; /** * Assign the default options @@ -38,7 +35,7 @@ export class TwentyQuestionsDialog extends FormApplication { constructor(options = null, actor = null) { super(options); this.actor = actor; - this.datas = this._initFormDatas(actor); + this.object = new TwentyQuestions(actor); } /** @@ -82,7 +79,7 @@ export class TwentyQuestionsDialog extends FormApplication { ringsList: game.l5r5e.HelpersL5r5e.getRingsList(), skillsList: game.l5r5e.HelpersL5r5e.getSkillsList(true), techniquesList: CONFIG.l5r5e.techniques, - datas: this.datas, + data: this.object.data, }; } @@ -111,7 +108,28 @@ export class TwentyQuestionsDialog extends FormApplication { activateListeners(html) { super.activateListeners(html); - // html.find('input[name="approach"]').on("click", async (event) => {}); + // *** Everything below here is only needed if the sheet is editable *** + if (!this.options.editable) { + return; + } + + // Check rings total + html.find(".ring-select").on("change", async (event) => { + const sum = this._summarySelects(html, ".ring-select"); + // sum = Map(4) {"void" => 2, "water" => 1, "fire" => 1, "earth" => 1} + console.log(sum); + }); + + // Check skills total + html.find(".skill-select").on("change", async (event) => { + const sum = this._summarySelects(html, ".skill-select"); + console.log(sum); + }); + + // Submit button + html.find("#generate").on("click", async (event) => { + this.submit(); + }); } /** @@ -159,123 +177,30 @@ export class TwentyQuestionsDialog extends FormApplication { * @override */ async _updateObject(event, formData) { - // this.actor - const actorDatas = this.actor.data.data; - //this.actor.data.twenty_questions = formData; // TODO a tester - - actorDatas.name = (formData.step2_family + " " + formData.step19_firstname).trim(); - actorDatas.zeni = formData.step2_wealth; - actorDatas.identity = { - ...actorDatas.identity, - clan: formData.step1_clan, - family: formData.step2_family, - school: formData.step3_school, - roles: formData.step3_roles, - }; - - actorDatas.social = { - ...actorDatas.social, - status: formData.step1_social_status, - glory: formData.step2_social_glory, - honor: formData.step3_social_honor, - giri: formData.step5_social_giri, - ninjo: formData.step6_social_ninjo, - }; - - actorDatas.techniques = { - kata: !!formData.step3_technique_kata, - kiho: formData.step3_technique_kiho, - invocation: !!formData.step3_technique_invocation, - ritual: !!formData.step3_technique_ritual, - shuji: !!formData.step3_technique_shuji, - maho: !!formData.step3_technique_maho, - ninjutsu: !!formData.step3_technique_ninjutsu, - }; - - // actorDatas = formData.step3_techniques; - // actorDatas = formData.step3_school_ability; - // actorDatas = formData.step3_equipment; - // actorDatas = formData.step4_stand_out; - // actorDatas = formData.step7_clan_relations; - // actorDatas = formData.step7_social_add_glory; - // actorDatas = formData.step8_bushido; - // actorDatas = formData.step8_social_add_honor; - // actorDatas = formData.step9_success; - // actorDatas = formData.step9_distinction; - // actorDatas = formData.step10_difficulty; - // actorDatas = formData.step10_adversity; - // actorDatas = formData.step11_calms; - // actorDatas = formData.step11_passion; - // actorDatas = formData.step12_worries; - // actorDatas = formData.step12_anxiety; - // actorDatas = formData.step13_most_learn; - // actorDatas = formData.step13_disadvantage; - // actorDatas = formData.step13_advantage; - // actorDatas = formData.step14_first_sight; - // actorDatas = formData.step14_special_features; - // actorDatas = formData.step15_stress; - // actorDatas = formData.step16_relations; - // actorDatas = formData.step16_item; - // actorDatas = formData.step17_parents_pov; - // actorDatas = formData.step18_heritage_name; - // actorDatas = formData.step18_heritage_1; - // actorDatas = formData.step18_heritage_2; - // actorDatas = formData.step20_death; - - const rings = this._filterRingOrSkills(formData.rings); - const skills = this._filterRingOrSkills(formData.skills); - - console.log(actorDatas); - - // TODO - console.log(rings, skills, formData, actorDatas, this.actor); - - // return this.close(); - } - - _filterRingOrSkills(obj) { - return obj - .filter((e) => e !== "none") - .reduce((acc, id) => { - if (!acc.has(id)) { - acc.set(id, 0); - } - acc.set(id, acc.get(id) + 1); - return acc; - }, new Map()); + this.object.updateFromForm(formData); + this.object.toActor(this.actor); + return this.close(); } /** - * Initialize form array + * Return a map of skill/ring with count * @private */ - _initFormDatas(actor) { - const actorDatas = actor.data.data; - - // already 20q struct ? - if (actorDatas.twenty_questions?.step1_clan) { - return actorDatas.twenty_questions; - } - - // If not fill some values - return { - step1_clan: actorDatas.identity.clan, - step1_social_status: actorDatas.social.status, - step2_family: actorDatas.identity.family, - step2_social_glory: actorDatas.social.glory, - step3_school: actorDatas.identity.school, - step3_roles: actorDatas.identity.roles, - step3_technique_kata: actorDatas.techniques.kata, - step3_technique_kiho: actorDatas.techniques.kiho, - step3_technique_invocation: actorDatas.techniques.invocation, - step3_technique_ritual: actorDatas.techniques.ritual, - step3_technique_shuji: actorDatas.techniques.shuji, - step3_technique_maho: actorDatas.techniques.maho, - step3_technique_ninjutsu: actorDatas.techniques.ninjutsu, - step3_social_honor: actorDatas.social.honor, - step5_social_giri: actorDatas.social.giri, - step6_social_ninjo: actorDatas.social.ninjo, - step19_firstname: actor.data.name.replace(/^(?:\w+\s+)?(.+)$/gi, "$1") || "", - }; + _summarySelects(html, selector) { + return html + .find(selector) + .get() + .reduce((acc, curr) => { + curr = curr.value; + if (curr === "none") { + return acc; + } + let val = acc.get(curr); + if (!val) { + val = 0; + } + acc.set(curr, val + 1); + return acc; + }, new Map()); } } diff --git a/system/scripts/actors/twenty-questions.js b/system/scripts/actors/twenty-questions.js new file mode 100644 index 0000000..beab9e5 --- /dev/null +++ b/system/scripts/actors/twenty-questions.js @@ -0,0 +1,289 @@ +/** + * L5R Twenty Questions Base object + */ +export class TwentyQuestions { + /** + * Shortcut for all rings Id in data + */ + static ringList = ["step1.ring", "step2.ring", "step3.ring1", "step3.ring2", "step4.ring"]; + + /** + * Shortcut for all skills Id in data + */ + static skillList = [ + "step1.skill", + "step2.skill1", + "step2.skill2", + "step3.skill1", + "step3.skill2", + "step3.skill3", + "step3.skill4", + "step3.skill5", + "step7.skill", + "step8.skill", + "step13.skill", + "step17.skill", + ]; + + /** + * Steps datas + */ + data = { + step1: { + clan: "", + ring: "", + skill: "", + social_status: 0, + }, + step2: { + family: "", + ring: "", + skill1: "", + skill2: "", + wealth: 0, + social_glory: 0, + }, + step3: { + school: "", + roles: "", + ring1: "", + ring2: "", + skill1: "", + skill2: "", + skill3: "", + skill4: "", + skill5: "", + allowed_techniques: { + kata: false, + kiho: false, + invocation: false, + ritual: false, + shuji: false, + maho: false, + ninjutsu: false, + }, + techniques: "", + school_ability: "", + equipment: "", + social_honor: 0, + }, + step4: { + stand_out: "", + ring: "", + }, + step5: { + social_giri: "", + }, + step6: { + social_ninjo: "", + }, + step7: { + clan_relations: "", + skill: "", + social_add_glory: null, + }, + step8: { + bushido: "", + skill: "", + social_add_honor: null, + }, + step9: { + success: "", + distinction: "", + }, + step10: { + difficulty: "", + adversity: "", + }, + step11: { + calms: "", + passion: "", + }, + step12: { + worries: "", + anxiety: "", + }, + step13: { + most_learn: "", + skill: "", + advantage: "", + disadvantage: "", + }, + step14: { + first_sight: "", + special_features: "", + }, + step15: { + stress: "", + }, + step16: { + relations: "", + item: "", + }, + step17: { + parents_pov: "", + skill: "", + }, + step18: { + heritage_name: "", + heritage_1: null, + heritage_2: null, + }, + step19: { + firstname: "", + }, + step20: { + death: "", + }, + }; + + /** + * Create + */ + constructor(actor = null) { + if (actor instanceof Actor) { + this.fromActor(actor); + } + } + + /** + * Update object with form data + */ + updateFromForm(formData) { + this.data = { + ...this.data, + ...expandObject(formData), + }; + } + + /** + * Initialize data from a actor + */ + fromActor(actor) { + const actorDatas = actor.data.data; + + // already 20q struct ? + if (actorDatas.twenty_questions?.step1?.clan) { + this.data = { + ...this.data, + ...actorDatas.twenty_questions, + }; + return; + } + + // If not fill some values + this.data.step1.clan = actorDatas.identity.clan; + this.data.step1.social_status = actorDatas.social.status; + this.data.step2.family = actorDatas.identity.family; + this.data.step2.social_glory = actorDatas.social.glory; + this.data.step3.school = actorDatas.identity.school; + this.data.step3.roles = actorDatas.identity.roles; + this.data.step3.allowed_techniques.kata = actorDatas.techniques.kata; + this.data.step3.allowed_techniques.kiho = actorDatas.techniques.kiho; + this.data.step3.allowed_techniques.invocation = actorDatas.techniques.invocation; + this.data.step3.allowed_techniques.ritual = actorDatas.techniques.ritual; + this.data.step3.allowed_techniques.shuji = actorDatas.techniques.shuji; + this.data.step3.allowed_techniques.maho = actorDatas.techniques.maho; + this.data.step3.allowed_techniques.ninjutsu = actorDatas.techniques.ninjutsu; + this.data.step3.social_honor = actorDatas.social.honor; + this.data.step5.social_giri = actorDatas.social.giri; + this.data.step6.social_ninjo = actorDatas.social.ninjo; + this.data.step19.firstname = actor.data.name.replace(/^(?:\w+\s+)?(.+)$/gi, "$1") || ""; + } + + /** + * Fill a actor data from this object + */ + toActor(actor) { + const actorDatas = actor.data.data; + const formData = this.data; + + // Store this form datas + // TODO maybe elsewhere than in the actor ? + actorDatas.twenty_questions = this.data; + + // Update the actor real datas + actorDatas.zeni = formData.step2.wealth; + actorDatas.identity = { + ...actorDatas.identity, + clan: formData.step1.clan, + family: formData.step2.family, + school: formData.step3.school, + roles: formData.step3.roles, + }; + + actorDatas.social = { + ...actorDatas.social, + status: formData.step1.social_status, + glory: formData.step2.social_glory, + honor: formData.step3.social_honor, + giri: formData.step5.social_giri, + ninjo: formData.step6.social_ninjo, + }; + + actorDatas.techniques = { + ...actorDatas.techniques, + kata: !!formData.step3.allowed_techniques.kata, + kiho: !!formData.step3.allowed_techniques.kiho, + invocation: !!formData.step3.allowed_techniques.invocation, + ritual: !!formData.step3.allowed_techniques.ritual, + shuji: !!formData.step3.allowed_techniques.shuji, + maho: !!formData.step3.allowed_techniques.maho, + ninjutsu: !!formData.step3.allowed_techniques.ninjutsu, + }; + + // Rings - Reset to 1, and apply modifiers + CONFIG.l5r5e.stances.forEach((ring) => (actorDatas.rings[ring] = 1)); + TwentyQuestions.ringList.forEach((formName) => { + const ring = getProperty(this.data, formName); + actorDatas.rings[ring] = actorDatas.rings[ring] + 1; + }); + + // Skills - Reset to 0, and apply modifiers + Array.from(CONFIG.l5r5e.skills).forEach(([skillId, skillCat]) => { + actorDatas.skills[skillCat][skillId] = 0; + }); + TwentyQuestions.skillList.forEach((formName) => { + const skillId = getProperty(this.data, formName); + const skillCat = CONFIG.l5r5e.skills.get(skillId); + actorDatas.skills[skillCat][skillId] = actorDatas.skills[skillCat][skillId] + 1; + }); + + // TODO Items references + + // Update actor + actor.update({ + name: (formData.step2.family + " " + formData.step19.firstname).trim(), + data: actorDatas, + }); // , { diff: true } + + // TODO Tmp + console.log(actor); + } + + // checkRings() { + // const rings = {}; + // const exceed = {}; + // CONFIG.l5r5e.stances.forEach(ring => rings[ring] = 1); + // TwentyQuestions.ringList.forEach((formName) => { + // const ring = getProperty(this.data, formName); + // rings[ring] = rings[ring] + 1; + // + // console.log(rings); + // + // if (rings[ring] > 3) { + // exceed[ring] = ring; + // } + // }); + // return exceed; + // } + // + // checkSkills() { + // const skills = {}; + // TwentyQuestions.skillList.forEach((formName) => { + // const skillId = getProperty(this.data, formName); + // const skillCat = CONFIG.l5r5e.skills.get(skillId); + // actorDatas.skills[skillCat][skillId] = actorDatas.skills[skillCat][skillId] + 1; + // }); + // } +} diff --git a/system/templates/actors/twenty-questions-dialog.html b/system/templates/actors/twenty-questions-dialog.html index 973155f..a930833 100644 --- a/system/templates/actors/twenty-questions-dialog.html +++ b/system/templates/actors/twenty-questions-dialog.html @@ -11,17 +11,19 @@
@@ -29,7 +31,8 @@
@@ -45,7 +49,7 @@
@@ -53,24 +57,27 @@
- + {{#select data.step2.skill2}} {{#each skillsList as |skills catId|}} @@ -90,13 +99,14 @@ {{/each}} {{/each}} + {{/select}}
@@ -104,7 +114,7 @@
@@ -117,35 +127,40 @@
- + {{#select data.step3.ring2}} {{#each ringsList as |obj|}} {{/each}} + {{/select}}
- + {{#select data.step3.skill2}} {{#each skillsList as |skills catId|}} @@ -165,8 +182,10 @@ {{/each}} {{/each}} + {{/select}} - + {{#select data.step3.skill3}} {{#each skillsList as |skills catId|}} @@ -175,8 +194,10 @@ {{/each}} {{/each}} + {{/select}} - + {{#select data.step3.skill4}} {{#each skillsList as |skills catId|}} @@ -185,8 +206,10 @@ {{/each}} {{/each}} + {{/select}} - + {{#select data.step3.skill5}} {{#each skillsList as |skills catId|}} @@ -195,6 +218,7 @@ {{/each}} {{/each}} + {{/select}}
@@ -204,7 +228,7 @@ {{localizeTechnique 'title'}} {{#each techniquesList as |technique|}} {{/each}} @@ -213,14 +237,14 @@
drop item here - {{datas.step3_techniques}} + {{data.step3.techniques}}
@@ -228,21 +252,23 @@
+
+
@@ -250,16 +276,18 @@
@@ -271,7 +299,7 @@
@@ -279,19 +307,20 @@
@@ -307,7 +337,7 @@
@@ -315,13 +345,14 @@
@@ -337,7 +369,7 @@
@@ -348,61 +380,62 @@
@@ -418,14 +452,14 @@
@@ -436,29 +470,30 @@
@@ -468,12 +503,13 @@
@@ -520,12 +557,12 @@
-
-