367 lines
12 KiB
JavaScript
367 lines
12 KiB
JavaScript
|
import { TeDeumUtility } from "../common/tedeum-utility.js";
|
||
|
|
||
|
export class TeDeumCharacterCreator {
|
||
|
|
||
|
/*--------------------------------------------*/
|
||
|
async init() {
|
||
|
this.stages = {}
|
||
|
this.currentStage = "origineSociale"
|
||
|
this.sex = undefined
|
||
|
this.origineSociale = undefined
|
||
|
this.religion = undefined
|
||
|
this.caracBonus = {}
|
||
|
this.competenceBonus = {}
|
||
|
this.competences = TeDeumUtility.getCompetencesForDropDown()
|
||
|
|
||
|
for (let k in game.system.tedeum.config.caracteristiques) {
|
||
|
this.caracBonus[k] = { value: 0 }
|
||
|
}
|
||
|
|
||
|
for (let stage in game.system.tedeum.config.etapesEducation) {
|
||
|
this.stages[stage] = { selectedItem: null, items: [] }
|
||
|
}
|
||
|
console.log(this.stages, game.system.tedeum.etapesEducation)
|
||
|
|
||
|
const educations = await TeDeumUtility.loadCompendium("fvtt-te-deum.education")
|
||
|
for (let edu of educations) {
|
||
|
this.stages[edu.system.etape].items.push(edu)
|
||
|
}
|
||
|
|
||
|
this.processStage()
|
||
|
}
|
||
|
|
||
|
/*--------------------------------------------*/
|
||
|
increaseCompetence(compName) {
|
||
|
compName = compName.toLowerCase()
|
||
|
if (!this.competenceBonus[compName]) {
|
||
|
this.competenceBonus[compName] = { value: 1 }
|
||
|
} else {
|
||
|
this.competenceBonus[compName].value += 1
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/*--------------------------------------------*/
|
||
|
processReponses(question) {
|
||
|
let fullResponses = []
|
||
|
for (let key in question.reponses) {
|
||
|
let response = question.reponses[key]
|
||
|
fullResponses.push({ id: key, label: `${response.reponse} (${response.compName} +1)` })
|
||
|
}
|
||
|
return fullResponses
|
||
|
}
|
||
|
|
||
|
/*--------------------------------------------*/
|
||
|
processReponsesRadio(question) {
|
||
|
let fullResponses = {}
|
||
|
for (let key in question.reponses) {
|
||
|
let response = question.reponses[key]
|
||
|
fullResponses[key] = `${response.reponse} (${response.compName} +1)`
|
||
|
}
|
||
|
return fullResponses
|
||
|
}
|
||
|
|
||
|
/*--------------------------------------------*/
|
||
|
async askStageName(context) {
|
||
|
const content = await renderTemplate("systems/fvtt-te-deum/templates/dialogs/character-creator-select-stage-name.hbs", context)
|
||
|
const choiceResult = await foundry.applications.api.DialogV2.wait({
|
||
|
window: { title: context.title },
|
||
|
classes: ["fvtt-te-deum"],
|
||
|
content,
|
||
|
buttons: [
|
||
|
{
|
||
|
label: context.label,
|
||
|
callback: (event, button, dialog) => {
|
||
|
const output = Array.from(button.form.elements).reduce((obj, input) => {
|
||
|
if (input.name) obj[input.name] = input.value
|
||
|
return obj
|
||
|
}, {})
|
||
|
return output
|
||
|
},
|
||
|
},
|
||
|
],
|
||
|
actions: {
|
||
|
},
|
||
|
rejectClose: false, // Click on Close button will not launch an error
|
||
|
render: (event, dialog) => {
|
||
|
}
|
||
|
})
|
||
|
return choiceResult
|
||
|
}
|
||
|
|
||
|
/*--------------------------------------------*/
|
||
|
processCompetences(stage) {
|
||
|
for (let compKey in stage.system.competences) {
|
||
|
let comp = stage.system.competences[compKey]
|
||
|
if (comp.valid && comp.compName !== "") {
|
||
|
this.increaseCompetence(comp.compName)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/*--------------------------------------------*/
|
||
|
async askQuestionnaire(stage, context) {
|
||
|
for (let key in stage.system.questionnaire) {
|
||
|
let question = stage.system.questionnaire[key]
|
||
|
if (question.question === "") { break }
|
||
|
|
||
|
context.question = question.question
|
||
|
context.responses = this.processReponses(question)
|
||
|
context.responsesRadio = this.processReponsesRadio(question)
|
||
|
|
||
|
const content = await renderTemplate("systems/fvtt-te-deum/templates/dialogs/character-creator-select-questions.hbs", context)
|
||
|
const choiceResult = await foundry.applications.api.DialogV2.wait({
|
||
|
window: { title: context.title },
|
||
|
classes: ["fvtt-te-deum"],
|
||
|
content,
|
||
|
buttons: [
|
||
|
{
|
||
|
label: context.label,
|
||
|
callback: (event, button, dialog) => {
|
||
|
const output = Array.from(button.form.elements).reduce((obj, input) => {
|
||
|
if (input.name) obj[input.name] = input.value
|
||
|
return obj
|
||
|
}, {})
|
||
|
return output
|
||
|
},
|
||
|
},
|
||
|
],
|
||
|
actions: {
|
||
|
},
|
||
|
rejectClose: false, // Click on Close button will not launch an error
|
||
|
render: (event, dialog) => {
|
||
|
}
|
||
|
})
|
||
|
if (choiceResult == null) { return }
|
||
|
let selectedResponse = question.reponses[choiceResult.responseKey]
|
||
|
console.log(choiceResult, selectedResponse, question)
|
||
|
this.increaseCompetence(selectedResponse.compName)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/*------------- -------------------------------*/
|
||
|
async askCarac(stage, context) {
|
||
|
for (let i = 0; i < stage.system.nbChoixCarac; i++) {
|
||
|
context.caracList = []
|
||
|
for (let caracKey in stage.system.caracteristiques) {
|
||
|
let carac = stage.system.caracteristiques[caracKey]
|
||
|
context.caracList.push(game.system.tedeum.config.caracteristiques[carac.caracId])
|
||
|
}
|
||
|
context.competences = stage.system.competences
|
||
|
|
||
|
const content = await renderTemplate("systems/fvtt-te-deum/templates/dialogs/character-creator-select-carac.hbs", context)
|
||
|
const choiceResult = await foundry.applications.api.DialogV2.wait({
|
||
|
window: { title: context.title },
|
||
|
classes: ["fvtt-te-deum"],
|
||
|
content,
|
||
|
buttons: [
|
||
|
{
|
||
|
label: context.label,
|
||
|
callback: (event, button, dialog) => {
|
||
|
const output = Array.from(button.form.elements).reduce((obj, input) => {
|
||
|
if (input.name) obj[input.name] = input.value
|
||
|
return obj
|
||
|
}, {})
|
||
|
return output
|
||
|
},
|
||
|
},
|
||
|
],
|
||
|
actions: {
|
||
|
},
|
||
|
rejectClose: false, // Click on Close button will not launch an error
|
||
|
render: (event, dialog) => {
|
||
|
}
|
||
|
})
|
||
|
if (choiceResult == null) { return }
|
||
|
this.caracBonus[choiceResult.carac].value += 1
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/*--------------------------------------------*/
|
||
|
async renderOrigineSociale(stage) {
|
||
|
let context = {
|
||
|
title: "Création de personnage - Origine Sociale",
|
||
|
sexeChoice: { "homme": "Homme", "femme": "Femme" },
|
||
|
religionChoice: { "catholique": "Catholique", "protestante": "Protestante" },
|
||
|
origineChoice: game.system.tedeum.config.origineSociale
|
||
|
}
|
||
|
const content = await renderTemplate("systems/fvtt-te-deum/templates/dialogs/character-creator-origine.hbs", context)
|
||
|
|
||
|
const label = "Valider le choix de l'Origine Sociale"
|
||
|
const choiceResult = await foundry.applications.api.DialogV2.wait({
|
||
|
window: { title: context.title },
|
||
|
classes: ["fvtt-te-deum"],
|
||
|
content,
|
||
|
buttons: [
|
||
|
{
|
||
|
label: label,
|
||
|
callback: (event, button, dialog) => {
|
||
|
const output = Array.from(button.form.elements).reduce((obj, input) => {
|
||
|
if (input.name) obj[input.name] = input.value
|
||
|
return obj
|
||
|
}, {})
|
||
|
return output
|
||
|
},
|
||
|
},
|
||
|
],
|
||
|
actions: {
|
||
|
},
|
||
|
rejectClose: false, // Click on Close button will not launch an error
|
||
|
render: (event, dialog) => { }
|
||
|
})
|
||
|
|
||
|
if (choiceResult == null) { return }
|
||
|
|
||
|
console.log(choiceResult)
|
||
|
|
||
|
this.sexe = choiceResult.sexe
|
||
|
this.religion = choiceResult.religion
|
||
|
this.origineSociale = foundry.utils.duplicate(game.system.tedeum.config.origineSociale[choiceResult.origineSociale])
|
||
|
this.currentStage = "pouponniere"
|
||
|
|
||
|
}
|
||
|
|
||
|
/*--------------------------------------------*/
|
||
|
async renderPouponniere(stage) {
|
||
|
|
||
|
// Filter available pouponniere from origineSociale
|
||
|
let pouponniereItems = stage.items.filter(item => item.system.accessible[this.origineSociale.id].isaccessible)
|
||
|
|
||
|
let context = {
|
||
|
title: "Création de personnage - La Pouponnière",
|
||
|
label: "Valider le choix de la Pouponnière",
|
||
|
choices: pouponniereItems
|
||
|
}
|
||
|
let choiceResult = await this.askStageName(context)
|
||
|
if (choiceResult == null) { return }
|
||
|
|
||
|
this.pouponniere = foundry.utils.duplicate(stage.items.find(item => item.id === choiceResult.selectedItem))
|
||
|
TeDeumUtility.prepareEducationContent(this.pouponniere);
|
||
|
console.log(choiceResult, this.pouponniere)
|
||
|
|
||
|
context.label = "Valider l'augmentation de caracteristique"
|
||
|
await this.askCarac(this.pouponniere, context)
|
||
|
|
||
|
this.processCompetences(this.pouponniere)
|
||
|
|
||
|
context.title = "Création de personnage - La Pouponnière - Questions"
|
||
|
context.label = "Valider cette réponse"
|
||
|
await this.askQuestionnaire(this.pouponniere, context)
|
||
|
|
||
|
this.currentStage = "petitsgrimauds"
|
||
|
}
|
||
|
|
||
|
/*--------------------------------------------*/
|
||
|
async renderPetitsGrimauds(stage) {
|
||
|
// Filter available pouponniere from origineSociale
|
||
|
let grimaudsItems = stage.items.filter(item => item.system.accessible[this.origineSociale.id].isaccessible)
|
||
|
|
||
|
let context = {
|
||
|
title: "Création de personnage - Les Petits Grimauds",
|
||
|
label: "Valider le choix des Petits Grimauds",
|
||
|
choices: grimaudsItems
|
||
|
}
|
||
|
|
||
|
let choiceResult = await this.askStageName(context)
|
||
|
if (choiceResult == null) { return }
|
||
|
|
||
|
this.grimauds = foundry.utils.duplicate(stage.items.find(item => item.id === choiceResult.selectedItem))
|
||
|
TeDeumUtility.prepareEducationContent(this.grimauds);
|
||
|
console.log(choiceResult, this.grimauds)
|
||
|
|
||
|
context.label = "Valider l'augmentation de caracteristique"
|
||
|
await this.askCarac(this.grimauds, context)
|
||
|
|
||
|
this.processCompetences(this.grimauds)
|
||
|
|
||
|
context.title = "Création de personnage - Les Petits Grimauds - Questions"
|
||
|
context.label = "Valider cette réponse"
|
||
|
await this.askQuestionnaire(this.grimauds, context)
|
||
|
|
||
|
this.currentStage = "rosevie"
|
||
|
}
|
||
|
|
||
|
/*--------------------------------------------*/
|
||
|
async renderRosesDeLaVie(stage) {
|
||
|
// Filter available pouponniere from origineSociale
|
||
|
let rosesItems = stage.items.filter(item => item.system.accessible[this.origineSociale.id].isaccessible)
|
||
|
|
||
|
let context = {
|
||
|
title: "Création de personnage - Les Roses de la Vie",
|
||
|
label: "Valider le choix des Roses de la Vie",
|
||
|
choices: rosesItems
|
||
|
}
|
||
|
|
||
|
let choiceResult = await this.askStageName(context)
|
||
|
if (choiceResult == null) { return }
|
||
|
|
||
|
this.roses = foundry.utils.duplicate(stage.items.find(item => item.id === choiceResult.selectedItem))
|
||
|
TeDeumUtility.prepareEducationContent(this.roses);
|
||
|
console.log(choiceResult, this.roses)
|
||
|
|
||
|
context.label = "Valider l'augmentation de caracteristique"
|
||
|
await this.askCarac(this.roses, context)
|
||
|
|
||
|
this.processCompetences(this.roses)
|
||
|
|
||
|
context.title = "Création de personnage - Les Roses de la Vie - Questions"
|
||
|
context.label = "Valider cette réponse"
|
||
|
await this.askQuestionnaire(this.roses, context)
|
||
|
|
||
|
this.currentStage = "ageviril"
|
||
|
}
|
||
|
|
||
|
/*--------------------------------------------*/
|
||
|
async renderAgeViril(stage) {
|
||
|
// Filter available pouponniere from origineSociale
|
||
|
let ageVirilItems = stage.items.filter(item => item.system.accessible[this.origineSociale.id].isaccessible)
|
||
|
|
||
|
let context = {
|
||
|
title: "Création de personnage - L'Age Viril",
|
||
|
label: "Valider le choix de l'Age Viril",
|
||
|
choices: ageVirilItems
|
||
|
}
|
||
|
|
||
|
let choiceResult = await this.askStageName(context)
|
||
|
if (choiceResult == null) { return }
|
||
|
|
||
|
this.ageViril = foundry.utils.duplicate(stage.items.find(item => item.id === choiceResult.selectedItem))
|
||
|
TeDeumUtility.prepareEducationContent(this.ageViril);
|
||
|
console.log(choiceResult, this.ageViril)
|
||
|
|
||
|
context.label = "Valider l'augmentation de caracteristique"
|
||
|
await this.askCarac(this.ageViril, context)
|
||
|
|
||
|
this.processCompetences(this.ageViril)
|
||
|
|
||
|
this.currentStage = "finished"
|
||
|
}
|
||
|
|
||
|
/*--------------------------------------------*/
|
||
|
async processStage() {
|
||
|
while (this.currentStage !== "finished") {
|
||
|
let stage = this.stages[this.currentStage]
|
||
|
switch (this.currentStage) {
|
||
|
case "origineSociale":
|
||
|
await this.renderOrigineSociale(stage)
|
||
|
break
|
||
|
case "pouponniere":
|
||
|
await this.renderPouponniere(stage)
|
||
|
break
|
||
|
case "petitsgrimauds":
|
||
|
await this.renderPetitsGrimauds(stage)
|
||
|
break
|
||
|
case "rosevie":
|
||
|
await this.renderRosesDeLaVie(stage)
|
||
|
break
|
||
|
case "ageviril":
|
||
|
await this.renderAgeViril(stage)
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
|
||
|
console.log("Carac Bonus", this.caracBonus)
|
||
|
console.log("Competence Bonus", this.competenceBonus)
|
||
|
}
|
||
|
|
||
|
}
|