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);
}
private checkOptions: DS4CheckFactoryOptions;
async execute(): Promise<void> {
@ -42,13 +41,14 @@ class CheckFactory {
const roll = new rollCls(formula);
const rollModeTemplate = this.checkOptions.rollMode;
console.log(rollModeTemplate);
return roll.toMessage({}, { rollMode: rollModeTemplate, create: true });
}
// Term generators
createTargetValueTerm(): string {
if (this.checkTargetValue != null) {
return "v" + this.checkTargetValue;
return "v" + (this.checkTargetValue + this.gmModifier);
} else {
return null;
}
@ -80,8 +80,15 @@ export async function createCheckRoll(targetValue: number, options: Partial<DS4C
// Ask for additional required data;
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
const cf = new CheckFactory(targetValue, gmModifierData.gmModifier, options);
const cf = new CheckFactory(gmModifierData.checkTargetValue, gmModifierData.gmModifier, newOptions);
// Possibly additional processing
@ -115,30 +122,60 @@ async function askGmModifier(
const renderedHtml = await renderTemplate(usedTemplate, templateData);
// TODO: Localize
const dialogData: DialogData = {
title: title ?? "Roll Options",
close: (html) => null,
content: renderedHtml,
buttons: {
ok: {
label: "OK",
callback: (html) => null,
const dialogPromise = new Promise<HTMLFormElement>((resolve) => {
new Dialog(
{
title: title ?? "Roll Options",
close: (html: JQuery) => resolve(null),
content: renderedHtml,
buttons: {
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",
callback: (html) => null,
},
},
default: "ok",
};
const dialog = new Dialog(dialogData, {}).render(true);
return { gmModifier: 0 };
{},
).render(true);
});
const dialogForm = await dialogPromise;
return parseDialogFormData(dialogForm, targetValue);
}
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 {
checkTargetValue: number;
gmModifier: number;
maxCritSuccess: number;
minCritFailure: number;
// TODO: In final version from system settings
useSlayingDice: boolean;
rollMode: DS4RollMode;
}
export interface DS4CheckFactoryOptions {

View file

@ -1,12 +1,12 @@
<form class="{{cssClass}} grid">
<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>
<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>
<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>
<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>
<select id="visibility" data-type="String">
{{#each rollModes as |rollMode|}}