First proper parsing without much "nice-ity".

This commit is contained in:
Oliver Rümpelein 2021-01-10 21:03:11 +01:00
parent a19a996d1d
commit 516c04c8de
2 changed files with 63 additions and 26 deletions

View file

@ -27,7 +27,6 @@ class CheckFactory {
) { ) {
this.checkOptions = new DefaultCheckOptions().mergeWith(passedOptions); this.checkOptions = new DefaultCheckOptions().mergeWith(passedOptions);
} }
private checkOptions: DS4CheckFactoryOptions; private checkOptions: DS4CheckFactoryOptions;
async execute(): Promise<void> { async execute(): Promise<void> {
@ -42,13 +41,14 @@ class CheckFactory {
const roll = new rollCls(formula); const roll = new rollCls(formula);
const rollModeTemplate = this.checkOptions.rollMode; const rollModeTemplate = this.checkOptions.rollMode;
console.log(rollModeTemplate);
return roll.toMessage({}, { rollMode: rollModeTemplate, create: true }); return roll.toMessage({}, { rollMode: rollModeTemplate, create: true });
} }
// Term generators // Term generators
createTargetValueTerm(): string { createTargetValueTerm(): string {
if (this.checkTargetValue != null) { if (this.checkTargetValue != null) {
return "v" + this.checkTargetValue; return "v" + (this.checkTargetValue + this.gmModifier);
} else { } else {
return null; return null;
} }
@ -80,8 +80,15 @@ export async function createCheckRoll(targetValue: number, options: Partial<DS4C
// Ask for additional required data; // Ask for additional required data;
const gmModifierData = await askGmModifier(targetValue, options); const gmModifierData = await askGmModifier(targetValue, options);
const newOptions: Partial<DS4CheckFactoryOptions> = {
maxCritSuccess: gmModifierData.maxCritSuccess ?? options.maxCritSuccess ?? undefined,
minCritFailure: gmModifierData.minCritFailure ?? options.minCritFailure ?? undefined,
useSlayingDice: gmModifierData.useSlayingDice ?? options.useSlayingDice ?? undefined,
rollMode: gmModifierData.rollMode ?? options.rollMode ?? undefined,
};
// Create Factory // Create Factory
const cf = new CheckFactory(targetValue, gmModifierData.gmModifier, options); const cf = new CheckFactory(gmModifierData.checkTargetValue, gmModifierData.gmModifier, newOptions);
// Possibly additional processing // Possibly additional processing
@ -115,30 +122,60 @@ async function askGmModifier(
const renderedHtml = await renderTemplate(usedTemplate, templateData); const renderedHtml = await renderTemplate(usedTemplate, templateData);
// TODO: Localize // TODO: Localize
const dialogData: DialogData = { const dialogPromise = new Promise<HTMLFormElement>((resolve) => {
title: title ?? "Roll Options", new Dialog(
close: (html) => null, {
content: renderedHtml, title: title ?? "Roll Options",
buttons: { close: (html: JQuery) => resolve(null),
ok: { content: renderedHtml,
label: "OK", buttons: {
callback: (html) => null, ok: {
label: "OK",
callback: (html: HTMLElement | JQuery) => {
if (!("jquery" in html)) {
throw new Error("Internal Type Error");
} else {
const innerForm = html[0].querySelector("form");
resolve(innerForm);
}
},
},
cancel: {
label: "Cancel",
callback: (html: JQuery) => resolve(null),
},
},
default: "ok",
}, },
cancel: { {},
label: "Cancel", ).render(true);
callback: (html) => null, });
}, const dialogForm = await dialogPromise;
}, return parseDialogFormData(dialogForm, targetValue);
default: "ok",
};
const dialog = new Dialog(dialogData, {}).render(true);
return { gmModifier: 0 };
} }
function parseDialogFormData(formData: HTMLFormElement, targetValue: number): GmModifierData {
const parsedData = {
checkTargetValue: parseInt(formData["ctv"]?.value) ?? targetValue,
gmModifier: parseInt(formData["gmmod"]?.value) ?? 0,
maxCritSuccess: parseInt(formData["maxcoup"]?.value) ?? defaultCheckOptions.maxCritSuccess,
minCritFailure: parseInt(formData["minfumble"]?.value) ?? defaultCheckOptions.minCritFailure,
useSlayingDice: false,
rollMode: formData["visibility"]?.value ?? defaultCheckOptions.rollMode,
};
console.log("Data", parsedData);
return parsedData;
}
// TODO: Remove unnecessary data step by step
interface GmModifierData { interface GmModifierData {
checkTargetValue: number;
gmModifier: number; gmModifier: number;
maxCritSuccess: number;
minCritFailure: number;
// TODO: In final version from system settings
useSlayingDice: boolean;
rollMode: DS4RollMode;
} }
export interface DS4CheckFactoryOptions { export interface DS4CheckFactoryOptions {

View file

@ -1,12 +1,12 @@
<form class="{{cssClass}} grid"> <form class="{{cssClass}} grid">
<label for="ctv">Check Target Value</label> <label for="ctv">Check Target Value</label>
<input id="ctv" type="number" name="ctv" value="{{checkTargetValue}}" /> <input id="ctv" data-type="Number" type="number" name="ctv" value="{{checkTargetValue}}" />
<label for="gmmod">Game Master Modifier</label> <label for="gmmod">Game Master Modifier</label>
<input id="gmmod" type="number" name="gmmod" value="0" /> <input id="gmmod" data-type="Number" type="number" name="gmmod" value="0" />
<label for="maxcoup">Coup to</label> <label for="maxcoup">Coup to</label>
<input id="maxcoup" type="number" name="maxcoup" value="{{maxCritSuccess}}" /> <input id="maxcoup" data-type="Number" type="number" name="maxcoup" value="{{maxCritSuccess}}" />
<label for="minfumble">Fumble from</label> <label for="minfumble">Fumble from</label>
<input id="minfumble" type="number" name="minfumble" value="{{minCritFailure}}" /> <input id="minfumble" data-type="Number" type="number" name="minfumble" value="{{minCritFailure}}" />
<label for="visibility">Visibility</label> <label for="visibility">Visibility</label>
<select id="visibility" data-type="String"> <select id="visibility" data-type="String">
{{#each rollModes as |rollMode|}} {{#each rollModes as |rollMode|}}