Merge branch '085-fix-problem-with-negative-ctn' into 'master'

Fix problem with check target numbers <= 0

Closes #85

See merge request dungeonslayers/ds4!114
This commit is contained in:
Johannes Loher 2021-06-26 14:37:05 +00:00
commit 3f6682c9b2
15 changed files with 225 additions and 77 deletions

View file

@ -48,30 +48,134 @@ describe("evaluateCheck with a single die", () => {
{ result: 20, checkTargetNumber: 4, active: false, discarded: true, failure: true },
]);
});
it("should roll a die even when the checkTargetNumber is 0 and coup on 1", () => {
expect(evaluateCheck([1], 0)).toEqual([
{ result: 1, checkTargetNumber: 0, active: true, discarded: false, success: true, count: 0 },
]);
});
it("should roll a die even when the checkTargetNumber is 0 and fail on values > 1 and < 20", () => {
for (let i = 2; i < 20; i++) {
expect(evaluateCheck([i], 0)).toEqual([
{ result: i, checkTargetNumber: 0, active: false, discarded: true },
]);
}
});
it("should roll a die even when the checkTargetNumber is 0 and fumble on 20", () => {
expect(evaluateCheck([20], 0)).toEqual([
{ result: 20, checkTargetNumber: 0, active: false, discarded: true, failure: true },
]);
});
it("should roll a die even when the checkTargetNumber is < 0 and coup on 1", () => {
expect(evaluateCheck([1], -1)).toEqual([
{ result: 1, checkTargetNumber: -1, active: true, discarded: false, success: true, count: -1 },
]);
});
it("should roll a die even when the checkTargetNumber is < 0 and fail on values > 1 and < 20", () => {
for (let i = 2; i < 20; i++) {
expect(evaluateCheck([i], -1)).toEqual([
{ result: i, checkTargetNumber: -1, active: false, discarded: true },
]);
}
});
it("should roll a die even when the checkTargetNumber is < 0 and fumble on 20", () => {
expect(evaluateCheck([20], -1)).toEqual([
{ result: 20, checkTargetNumber: -1, active: false, discarded: true, failure: true },
]);
});
});
describe("evaluateCheck with a single die and coup / fumble modification", () => {
const maximumCoupResult = 2;
const minimumFumbleResult = 19;
it("should assign the checkTargetNumber to the single die and coup on 'maximumCoupResult'", () => {
expect(evaluateCheck([2], 4, { maximumCoupResult: 2 })).toEqual([
{ result: 2, checkTargetNumber: 4, active: true, discarded: false, success: true, count: 4 },
expect(evaluateCheck([maximumCoupResult], 4, { maximumCoupResult })).toEqual([
{
result: maximumCoupResult,
checkTargetNumber: 4,
active: true,
discarded: false,
success: true,
count: 4,
},
]);
});
it("should assign the checkTargetNumber to the single die and not coup on lower edge case '3'", () => {
expect(evaluateCheck([3], 4, { maximumCoupResult: 2 })).toEqual([
{ result: 3, checkTargetNumber: 4, active: true, discarded: false },
it("should assign the checkTargetNumber to the single die and not coup on lower edge case 'maximumCoupResult + 1'", () => {
expect(evaluateCheck([maximumCoupResult + 1], 4, { maximumCoupResult })).toEqual([
{ result: maximumCoupResult + 1, checkTargetNumber: 4, active: true, discarded: false },
]);
});
it("should assign the checkTargetNumber to the single die and fumble on 'minimumFUmbleResultResult'", () => {
expect(evaluateCheck([19], 20, { minimumFumbleResult: 19 })).toEqual([
{ result: 19, checkTargetNumber: 20, active: true, discarded: false, failure: true },
it("should assign the checkTargetNumber to the single die and fumble on 'minimumFumbleResultResult'", () => {
expect(evaluateCheck([minimumFumbleResult], 20, { minimumFumbleResult })).toEqual([
{ result: minimumFumbleResult, checkTargetNumber: 20, active: true, discarded: false, failure: true },
]);
});
it("should assign the checkTargetNumber to the single die and not fumble on upper edge case '18'", () => {
expect(evaluateCheck([18], 20, { minimumFumbleResult: 19 })).toEqual([
{ result: 18, checkTargetNumber: 20, active: true, discarded: false },
it("should assign the checkTargetNumber to the single die and not fumble on upper edge case 'minimumFumbleResult - 1'", () => {
expect(evaluateCheck([minimumFumbleResult - 1], 20, { minimumFumbleResult })).toEqual([
{ result: minimumFumbleResult - 1, checkTargetNumber: 20, active: true, discarded: false },
]);
});
it("should roll a die even when the checkTargetNumber is 0 and coup on 'maximumCoupResult'", () => {
expect(evaluateCheck([maximumCoupResult], 0, { maximumCoupResult })).toEqual([
{
result: maximumCoupResult,
checkTargetNumber: 0,
active: true,
discarded: false,
success: true,
count: 0,
},
]);
});
it("should roll a die even when the checkTargetNumber is 0 and fail on '> maximumCoupResult and < minimumFumbleResult", () => {
for (let i = maximumCoupResult + 1; i < minimumFumbleResult; i++) {
expect(evaluateCheck([i], 0, { maximumCoupResult, minimumFumbleResult })).toEqual([
{ result: i, checkTargetNumber: 0, active: false, discarded: true },
]);
}
});
it("should roll a die even when the checkTargetNumber is 0 and fumble on 'minimumFumbleResult'", () => {
expect(evaluateCheck([minimumFumbleResult], 0, { minimumFumbleResult })).toEqual([
{ result: minimumFumbleResult, checkTargetNumber: 0, active: false, discarded: true, failure: true },
]);
});
it("should roll a die even when the checkTargetNumber is < 0 and coup on 'maximumCoupResult'", () => {
expect(evaluateCheck([maximumCoupResult], -1, { maximumCoupResult })).toEqual([
{
result: maximumCoupResult,
checkTargetNumber: -1,
active: true,
discarded: false,
success: true,
count: -1,
},
]);
});
it("should roll a die even when the checkTargetNumber is < 0 and fail on '> maximumCoupResult and < minimumFumbleResult", () => {
for (let i = maximumCoupResult + 1; i < minimumFumbleResult; i++) {
expect(evaluateCheck([i], -1, { maximumCoupResult, minimumFumbleResult })).toEqual([
{ result: i, checkTargetNumber: -1, active: false, discarded: true },
]);
}
});
it("should roll a die even when the checkTargetNumber is < 0 and fumble on 'minimumFumbleResult'", () => {
expect(evaluateCheck([minimumFumbleResult], -1, { minimumFumbleResult })).toEqual([
{ result: minimumFumbleResult, checkTargetNumber: -1, active: false, discarded: true, failure: true },
]);
});
});

View file

@ -235,7 +235,7 @@ export class DS4ActorSheet extends ActorSheet<ActorSheet.Data<DS4Actor>> {
event.preventDefault();
const id = $(event.currentTarget).parents(".item").data("itemId");
const item = this.actor.getOwnedItem(id);
item.roll();
item.roll().catch((e) => notifications.error(e, { log: true }));
}
/**
@ -245,7 +245,7 @@ export class DS4ActorSheet extends ActorSheet<ActorSheet.Data<DS4Actor>> {
protected _onRollCheck(event: JQuery.ClickEvent): void {
event.preventDefault();
const check = event.currentTarget.dataset["check"];
this.actor.rollCheck(check);
this.actor.rollCheck(check).catch((e) => notifications.error(e, { log: true }));
}
/** @override */

View file

@ -6,6 +6,7 @@ import registerHandlebarsHelpers from "../handlebars/handlebars-helpers";
import registerHandlebarsPartials from "../handlebars/handlebars-partials";
import { DS4Item } from "../item/item";
import { DS4ItemSheet } from "../item/item-sheet";
import logger from "../logger";
import { macros } from "../macros/macros";
import { migration } from "../migrations";
import { DS4Check } from "../rolls/check";
@ -19,7 +20,7 @@ export default function registerForInitHook(): void {
}
async function init() {
console.log(`DS4 | Initializing the DS4 Game System\n${DS4.ASCII}`);
logger.info(`Initializing the DS4 Game System\n${DS4.ASCII}`);
game.ds4 = {
DS4Actor,

27
src/module/logger.ts Normal file
View file

@ -0,0 +1,27 @@
const loggingContext = "DS4";
const loggingSeparator = "|";
type LogLevel = "debug" | "info" | "warning" | "error";
type LoggingFunction = (...data: unknown[]) => void;
class Logger {
readonly debug: LoggingFunction;
readonly info: LoggingFunction;
readonly warn: LoggingFunction;
readonly error: LoggingFunction;
constructor() {
this.debug = this.getLoggingFunction("debug");
this.info = this.getLoggingFunction("info");
this.warn = this.getLoggingFunction("warning");
this.error = this.getLoggingFunction("error");
}
getLoggingFunction(type: LogLevel = "info") {
const log = { debug: console.debug, info: console.info, warning: console.warn, error: console.error }[type];
return (...data: unknown[]) => log(loggingContext, loggingSeparator, ...data);
}
}
const logger = new Logger();
export default logger;

View file

@ -45,5 +45,5 @@ export async function rollCheck(check: Check): Promise<void> {
return notifications.warn(game.i18n.localize("DS4.WarningMustControlActorToUseRollCheckMacro"));
}
return actor.rollCheck(check);
return actor.rollCheck(check).catch((e) => notifications.error(e, { log: true }));
}

View file

@ -9,5 +9,5 @@ export async function rollGenericCheck(): Promise<void> {
return notifications.warn(game.i18n.localize("DS4.WarningMustControlActorToUseRollCheckMacro"));
}
return actor.rollGenericCheck();
return actor.rollGenericCheck().catch((e) => notifications.error(e, { log: true }));
}

View file

@ -53,5 +53,5 @@ export async function rollItem(itemId: string): Promise<void> {
);
}
return item.roll();
return item.roll().catch((e) => notifications.error(e, { log: true }));
}

View file

@ -1,3 +1,4 @@
import logger from "./logger";
import { migrate as migrate001 } from "./migrations/001";
import { migrate as migrate002 } from "./migrations/002";
import { migrate as migrate003 } from "./migrations/003";
@ -40,7 +41,7 @@ async function migrateFromTo(oldMigrationVersion: number, targetMigrationVersion
for (const [i, migration] of migrationsToExecute.entries()) {
const currentMigrationVersion = oldMigrationVersion + i + 1;
console.log("executing migration script ", currentMigrationVersion);
logger.info("executing migration script ", currentMigrationVersion);
try {
await migration();
game.settings.set("ds4", "systemMigrationVersion", currentMigrationVersion);
@ -54,7 +55,7 @@ async function migrateFromTo(oldMigrationVersion: number, targetMigrationVersion
{ permanent: true },
);
err.message = `Failed ds4 system migration: ${err.message}`;
console.error(err);
logger.error(err);
return;
}
}

View file

@ -1,7 +1,9 @@
import logger from "../logger";
export async function migrate(): Promise<void> {
for (const a of game.actors?.entities ?? []) {
const updateData = getActorUpdateData();
console.log(`Migrating actor ${a.name}`);
logger.info(`Migrating actor ${a.name}`);
await a.update(updateData, { enforceTypes: false });
}
}

View file

@ -1,3 +1,5 @@
import logger from "../logger";
export async function migrate(): Promise<void> {
await migrateItems();
await migrateActors();
@ -10,12 +12,12 @@ async function migrateItems() {
try {
const updateData = getItemUpdateData(item._data);
if (updateData) {
console.log(`Migrating Item entity ${item.name} (${item.id})`);
logger.info(`Migrating Item entity ${item.name} (${item.id})`);
await item.update(updateData), { enforceTypes: false };
}
} catch (err) {
err.message = `Error during migration of Item entity ${item.name} (${item.id}), continuing anyways.`;
console.error(err);
logger.error(err);
}
}
}
@ -30,12 +32,12 @@ async function migrateActors() {
try {
const updateData = getActorUpdateData(actor._data);
if (updateData) {
console.log(`Migrating Actor entity ${actor.name} (${actor.id})`);
logger.info(`Migrating Actor entity ${actor.name} (${actor.id})`);
await actor.update(updateData, { enforceTypes: false });
}
} catch (err) {
err.message = `Error during migration of Actor entity ${actor.name} (${actor.id}), continuing anyways.`;
console.error(err);
logger.error(err);
}
}
}
@ -59,12 +61,12 @@ async function migrateScenes() {
try {
const updateData = getSceneUpdateData(scene._data);
if (updateData) {
console.log(`Migrating Scene entity ${scene.name} (${scene.id})`);
logger.info(`Migrating Scene entity ${scene.name} (${scene.id})`);
await scene.update(updateData, { enforceTypes: false });
}
} catch (err) {
err.message = `Error during migration of Scene entity ${scene.name} (${scene.id}), continuing anyways.`;
console.error(err);
logger.error(err);
}
}
}
@ -123,12 +125,12 @@ async function migrateCompendium(compendium: Compendium) {
};
const updateData = getUpdateData(entity);
if (updateData) {
console.log(`Migrating entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}`);
logger.info(`Migrating entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}`);
await compendium.updateEntity({ ...updateData, _id: entity._id });
}
} catch (err) {
err.message = `Error during migration of entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}, continuing anyways.`;
console.error(err);
logger.error(err);
}
}

View file

@ -1,3 +1,5 @@
import logger from "../logger";
export async function migrate(): Promise<void> {
await migrateItems();
await migrateActors();
@ -10,12 +12,12 @@ async function migrateItems() {
try {
const updateData = getItemUpdateData(item._data);
if (updateData) {
console.log(`Migrating Item entity ${item.name} (${item.id})`);
logger.info(`Migrating Item entity ${item.name} (${item.id})`);
await item.update(updateData), { enforceTypes: false };
}
} catch (err) {
err.message = `Error during migration of Item entity ${item.name} (${item.id}), continuing anyways.`;
console.error(err);
logger.error(err);
}
}
}
@ -34,12 +36,12 @@ async function migrateActors() {
try {
const updateData = getActorUpdateData(actor._data);
if (updateData) {
console.log(`Migrating Actor entity ${actor.name} (${actor.id})`);
logger.info(`Migrating Actor entity ${actor.name} (${actor.id})`);
await actor.update(updateData, { enforceTypes: false });
}
} catch (err) {
err.message = `Error during migration of Actor entity ${actor.name} (${actor.id}), continuing anyways.`;
console.error(err);
logger.error(err);
}
}
}
@ -63,12 +65,12 @@ async function migrateScenes() {
try {
const updateData = getSceneUpdateData(scene._data);
if (updateData) {
console.log(`Migrating Scene entity ${scene.name} (${scene.id})`);
logger.info(`Migrating Scene entity ${scene.name} (${scene.id})`);
await scene.update(updateData, { enforceTypes: false });
}
} catch (err) {
err.message = `Error during migration of Scene entity ${scene.name} (${scene.id}), continuing anyways.`;
console.error(err);
logger.error(err);
}
}
}
@ -127,12 +129,12 @@ async function migrateCompendium(compendium: Compendium) {
};
const updateData = getUpdateData(entity);
if (updateData) {
console.log(`Migrating entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}`);
logger.info(`Migrating entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}`);
await compendium.updateEntity({ ...updateData, _id: entity._id });
}
} catch (err) {
err.message = `Error during migration of entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}, continuing anyways.`;
console.error(err);
logger.error(err);
}
}

View file

@ -1,4 +1,5 @@
import { DS4SpellDataData } from "../item/item-data";
import logger from "../logger";
export async function migrate(): Promise<void> {
await migrateItems();
@ -12,12 +13,12 @@ async function migrateItems() {
try {
const updateData = getItemUpdateData(item._data);
if (updateData) {
console.log(`Migrating Item entity ${item.name} (${item.id})`);
logger.info(`Migrating Item entity ${item.name} (${item.id})`);
await item.update(updateData), { enforceTypes: false };
}
} catch (err) {
err.message = `Error during migration of Item entity ${item.name} (${item.id}), continuing anyways.`;
console.error(err);
logger.error(err);
}
}
}
@ -39,12 +40,12 @@ async function migrateActors() {
try {
const updateData = getActorUpdateData(actor._data);
if (updateData) {
console.log(`Migrating Actor entity ${actor.name} (${actor.id})`);
logger.info(`Migrating Actor entity ${actor.name} (${actor.id})`);
await actor.update(updateData, { enforceTypes: false });
}
} catch (err) {
err.message = `Error during migration of Actor entity ${actor.name} (${actor.id}), continuing anyways.`;
console.error(err);
logger.error(err);
}
}
}
@ -75,12 +76,12 @@ async function migrateScenes() {
try {
const updateData = getSceneUpdateData(scene._data);
if (updateData) {
console.log(`Migrating Scene entity ${scene.name} (${scene.id})`);
logger.info(`Migrating Scene entity ${scene.name} (${scene.id})`);
await scene.update(updateData, { enforceTypes: false });
}
} catch (err) {
err.message = `Error during migration of Scene entity ${scene.name} (${scene.id}), continuing anyways.`;
console.error(err);
logger.error(err);
}
}
}
@ -140,12 +141,12 @@ async function migrateCompendium(compendium: Compendium) {
};
const updateData = getUpdateData(entity);
if (updateData) {
console.log(`Migrating entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}`);
logger.info(`Migrating entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}`);
await compendium.updateEntity({ ...updateData, _id: entity._id });
}
} catch (err) {
err.message = `Error during migration of entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}, continuing anyways.`;
console.error(err);
logger.error(err);
}
}

View file

@ -113,5 +113,5 @@ function evaluateDiceWithSubChecks(
}
export function getRequiredNumberOfDice(checkTargetNumber: number): number {
return Math.ceil(checkTargetNumber / 20);
return Math.max(Math.ceil(checkTargetNumber / 20), 1);
}

View file

@ -45,8 +45,9 @@ class CheckFactory {
);
}
createCheckTargetNumberModifier(): string | null {
return "v" + (this.checkTargetNumber + this.gmModifier);
createCheckTargetNumberModifier(): string {
const totalCheckTargetNumber = Math.max(this.checkTargetNumber + this.gmModifier, 0);
return `v${totalCheckTargetNumber}`;
}
createCoupFumbleModifier(): string | null {
@ -55,7 +56,7 @@ class CheckFactory {
const isMaximumCoupResultRequired = this.options.maximumCoupResult !== defaultCheckOptions.maximumCoupResult;
if (isMinimumFumbleResultRequired || isMaximumCoupResultRequired) {
return "c" + (this.options.maximumCoupResult ?? "") + ":" + (this.options.minimumFumbleResult ?? "");
return `c${this.options.maximumCoupResult ?? ""}:${this.options.minimumFumbleResult ?? ""}`;
} else {
return null;
}
@ -162,12 +163,20 @@ async function askGmModifier(
* @param formData - The filed dialog
*/
function parseDialogFormData(formData: HTMLFormElement): Partial<IntermediateGmModifierData> {
const chosenCheckTargetNumber = parseInt(formData["check-target-number"]?.value);
const chosenGMModifier = parseInt(formData["gm-modifier"]?.value);
const chosenMaximumCoupResult = parseInt(formData["maximum-coup-result"]?.value);
const chosenMinimumFumbleResult = parseInt(formData["minimum-fumble-result"]?.value);
const chosenRollMode = formData["roll-mode"]?.value;
const invalidNumbers = [NaN, Infinity, -Infinity];
return {
checkTargetNumber: parseInt(formData["check-target-number"]?.value),
gmModifier: parseInt(formData["gm-modifier"]?.value),
maximumCoupResult: parseInt(formData["maximum-coup-result"]?.value),
minimumFumbleResult: parseInt(formData["minimum-fumble-result"]?.value),
rollMode: formData["roll-mode"]?.value,
checkTargetNumber: invalidNumbers.includes(chosenCheckTargetNumber) ? undefined : chosenCheckTargetNumber,
gmModifier: invalidNumbers.includes(chosenGMModifier) ? undefined : chosenGMModifier,
maximumCoupResult: invalidNumbers.includes(chosenMaximumCoupResult) ? undefined : chosenMaximumCoupResult,
minimumFumbleResult: invalidNumbers.includes(chosenMinimumFumbleResult) ? undefined : chosenMinimumFumbleResult,
rollMode: Object.values(CONST.DICE_ROLL_MODES).includes(chosenRollMode) ? chosenRollMode : undefined,
};
}

View file

@ -1,35 +1,34 @@
import logger from "../logger";
function getNotificationFunction(type: "info" | "warn" | "error") {
return (message: string, { permanent = false, log = false }: { permanent?: boolean; log?: boolean } = {}): void => {
if (ui.notifications) {
ui.notifications[type](message, { permanent });
if (log) {
logger[type](message);
}
} else {
logger[type](message);
}
};
}
const notifications = {
info: (message: string, { permanent = false }: { permanent?: boolean } = {}): void => {
if (ui.notifications) {
ui.notifications.info(message, { permanent });
} else {
console.info(message);
}
},
warn: (message: string, { permanent = false }: { permanent?: boolean } = {}): void => {
if (ui.notifications) {
ui.notifications.warn(message, { permanent });
} else {
console.log(message);
}
},
error: (message: string, { permanent = false }: { permanent?: boolean } = {}): void => {
if (ui.notifications) {
ui.notifications.error(message, { permanent });
} else {
console.warn(message);
}
},
info: getNotificationFunction("info"),
warn: getNotificationFunction("warn"),
error: getNotificationFunction("error"),
notify: (
message: string,
type: "info" | "warning" | "error" = "info",
{ permanent = false }: { permanent?: boolean } = {},
{ permanent = false, log = false }: { permanent?: boolean; log?: boolean } = {},
): void => {
if (ui.notifications) {
ui.notifications.notify(message, type, { permanent });
if (log) {
logger.getLoggingFunction(type)(message);
}
} else {
const log = { info: console.info, warning: console.warn, error: console.error }[type];
log(message);
logger.getLoggingFunction(type)(message);
}
},
};