refactor: improve structure of src

This commit is contained in:
Johannes Loher 2022-11-04 21:47:18 +01:00
parent b74919b75b
commit c5d4ec1abd
96 changed files with 146 additions and 157 deletions

View file

@ -5,7 +5,7 @@
import { describe, expect, it } from "vitest";
import evaluateCheck from "../../src/rolls/check-evaluation";
import { evaluateCheck } from "../../src/dice/check-evaluation";
describe("evaluateCheck with no dice", () => {
it("should throw an error.", () => {

View file

@ -4,9 +4,9 @@
import { describe, expect, it } from "vitest";
import { calculateSpellPrice } from "../../../src/item/spell/calculate-spell-price";
import { calculateSpellPrice } from "../../../../src/documents/item/spell/calculate-spell-price";
import type { CooldownDuration, DS4SpellDataSourceData } from "../../../src/item/spell/spell-data-source";
import type { CooldownDuration, DS4SpellDataSourceData } from "../../../../src/documents/item/spell/spell-data-source";
const defaultData: DS4SpellDataSourceData = {
description: "",

View file

@ -5,18 +5,17 @@
//
// SPDX-License-Identifier: MIT
import { DS4ActiveEffect } from "../active-effect/active-effect";
import { disableOverriddenFields } from "../apps/sheet-helpers";
import { DS4 } from "../config";
import { getCanvas, getGame } from "../helpers";
import { getDS4Settings } from "../settings";
import notifications from "../ui/notifications";
import { enforce } from "../utils";
import { isCheck } from "./actor-data-properties-base";
import { DS4 } from "../../config";
import { DS4ActiveEffect } from "../../documents/active-effect";
import { isCheck } from "../../documents/actor/actor-data-properties-base";
import { getDS4Settings } from "../../settings";
import { notifications } from "../../ui/notifications";
import { enforce, getCanvas, getGame } from "../../utils/utils";
import { disableOverriddenFields } from "../sheet-helpers";
import type { ModifiableDataBaseTotal } from "../common/common-data";
import type { DS4Settings } from "../settings";
import type { DS4Item } from "../item/item";
import type { ModifiableDataBaseTotal } from "../../documents/common/common-data";
import type { DS4Settings } from "../../settings";
import type { DS4Item } from "../../documents/item/item";
/**
* The base sheet class for all {@link DS4Actor}s.

View file

@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: MIT
import { DS4ActorSheet } from "../actor-sheet";
import { DS4ActorSheet } from "./base-sheet";
/**
* The Sheet class for DS4 Character Actors

View file

@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: MIT
import { DS4ActorSheet } from "../actor-sheet";
import { DS4ActorSheet } from "./base-sheet";
/**
* The Sheet class for DS4 Creature Actors

View file

@ -4,13 +4,12 @@
//
// SPDX-License-Identifier: MIT
import { DS4ActiveEffect } from "../active-effect/active-effect";
import { disableOverriddenFields } from "../apps/sheet-helpers";
import { DS4 } from "../config";
import { getGame } from "../helpers";
import notifications from "../ui/notifications";
import { enforce } from "../utils";
import { isDS4ItemDataTypePhysical } from "./item-data-source-base";
import { DS4ActiveEffect } from "../documents/active-effect";
import { isDS4ItemDataTypePhysical } from "../documents/item/item-data-source-base";
import { notifications } from "../ui/notifications";
import { enforce, getGame } from "../utils/utils";
import { disableOverriddenFields } from "./sheet-helpers";
/**
* The Sheet class for DS4 Items

View file

@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: MIT
import { getGame } from "../helpers";
import { getGame } from "../utils/utils";
export function disableOverriddenFields(
form: HTMLElement | null,

View file

@ -3,9 +3,9 @@
//
// SPDX-License-Identifier: MIT
import { getGame } from "../helpers";
import { getGame } from "../utils/utils";
export default function evaluateCheck(
export function evaluateCheck(
dice: number[],
checkTargetNumber: number,
{

View file

@ -5,7 +5,7 @@
import { DialogWithListeners } from "../apps/dialog-with-listeners";
import { DS4 } from "../config";
import { getGame } from "../helpers";
import { getGame } from "../utils/utils";
/**
* Provides default values for all arguments the `CheckFactory` expects.

View file

@ -3,8 +3,8 @@
//
// SPDX-License-Identifier: MIT
import { getGame } from "../helpers";
import evaluateCheck, { getRequiredNumberOfDice } from "./check-evaluation";
import { getGame } from "../utils/utils";
import { evaluateCheck, getRequiredNumberOfDice } from "./check-evaluation";
/**
* Implements DS4 Checks as an emulated "dice throw".

View file

@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: MIT
import { getGame } from "../helpers";
import { getGame } from "../utils/utils";
import { DS4Check } from "./check";
export class DS4Roll<D extends Record<string, unknown> = Record<string, unknown>> extends Roll<D> {

View file

@ -3,10 +3,10 @@
//
// SPDX-License-Identifier: MIT
import { getGame } from "../helpers";
import { getGame } from "../utils/utils";
import { DS4Check } from "./check";
export default function registerSlayingDiceModifier(): void {
export function registerSlayingDiceModifier(): void {
PoolTerm.MODIFIERS.x = slay;
}

View file

@ -3,10 +3,10 @@
// SPDX-License-Identifier: MIT
import { mathEvaluator } from "../expression-evaluation/evaluator";
import { getGame } from "../helpers";
import { getGame } from "../utils/utils";
import type { DS4Actor } from "../actor/actor";
import type { DS4Item } from "../item/item";
import type { DS4Actor } from "./actor/actor";
import type { DS4Item } from "./item/item";
declare global {
interface DocumentClassConfig {

View file

@ -5,7 +5,7 @@
//
// SPDX-License-Identifier: MIT
import { DS4 } from "../config";
import { DS4 } from "../../config";
import type { ModifiableDataBaseTotal, ResourceDataBaseTotalMax } from "../common/common-data";

View file

@ -5,7 +5,7 @@
//
// SPDX-License-Identifier: MIT
import { DS4 } from "../config";
import { DS4 } from "../../config";
import type { ModifiableData, ModifiableDataBase, ResourceData } from "../common/common-data";

View file

@ -3,12 +3,12 @@
//
// SPDX-License-Identifier: MIT
import { DS4ActiveEffect } from "../active-effect/active-effect";
import { DS4 } from "../config";
import { mathEvaluator } from "../expression-evaluation/evaluator";
import { getGame } from "../helpers";
import logger from "../logger";
import { createCheckRoll } from "../rolls/check-factory";
import { DS4 } from "../../config";
import { createCheckRoll } from "../../dice/check-factory";
import { mathEvaluator } from "../../expression-evaluation/evaluator";
import { logger } from "../../utils/logger";
import { getGame } from "../../utils/utils";
import { DS4ActiveEffect } from "../active-effect";
import { isAttribute, isTrait } from "./actor-data-source-base";
import type { ModifiableDataBaseTotal } from "../common/common-data";

View file

@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: MIT
import type { DS4 } from "../../config";
import type { DS4 } from "../../../config";
import type { DS4ActorDataSourceDataBase } from "../actor-data-source-base";
export interface DS4CreatureDataSource {

View file

@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: MIT
import { getGame } from "../helpers";
import { getGame } from "../../utils/utils";
import { DS4Actor } from "./actor";
import { DS4Character } from "./character/character";
import { DS4Creature } from "./creature/creature";

View file

@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: MIT
import { getGame } from "./helpers";
import { getGame } from "../utils/utils";
declare global {
interface FlagConfig {

View file

@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: MIT
import type { DS4 } from "../../config";
import type { DS4 } from "../../../config";
import type {
DS4ItemDataSourceDataBase,
DS4ItemDataSourceDataEquipable,

View file

@ -4,7 +4,7 @@
//
// SPDX-License-Identifier: MIT
import type { DS4 } from "../config";
import type { DS4 } from "../../config";
export interface DS4ItemDataSourceDataBase {
description: string;

View file

@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: MIT
import type { DS4 } from "../config";
import type { DS4 } from "../../config";
import type { DS4AlphabetDataSource } from "./alphabet/alphabet-data-source";
import type { DS4ArmorDataSource } from "./armor/armor-data-source";
import type { DS4EquipmentDataSource } from "./equipment/equipment-data-source";

View file

@ -3,8 +3,8 @@
//
// SPDX-License-Identifier: MIT
import { DS4ActiveEffect } from "../active-effect/active-effect";
import { getGame } from "../helpers";
import { getGame } from "../../utils/utils";
import { DS4ActiveEffect } from "../active-effect";
import type { ItemType } from "./item-data-source";

View file

@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: MIT
import { getGame } from "../helpers";
import { getGame } from "../../utils/utils";
import { DS4Alphabet } from "./alphabet/alphabet";
import { DS4Armor } from "./armor/armor";
import { DS4Equipment } from "./equipment/equipment";

View file

@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: MIT
import type { DS4 } from "../../config";
import type { DS4 } from "../../../config";
import type { DS4ItemDataSourceDataBase, DS4ItemDataSourceDataEquipable } from "../item-data-source-base";
export interface DS4SpellDataSource {

View file

@ -2,9 +2,9 @@
//
// SPDX-License-Identifier: MIT
import { getGame } from "../../helpers";
import { createCheckRoll } from "../../rolls/check-factory";
import notifications from "../../ui/notifications";
import { createCheckRoll } from "../../../dice/check-factory";
import { notifications } from "../../../ui/notifications";
import { getGame } from "../../../utils/utils";
import { DS4Item } from "../item";
import { calculateSpellPrice } from "./calculate-spell-price";

View file

@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: MIT
import type { DS4 } from "../../config";
import type { DS4 } from "../../../config";
import type {
DS4ItemDataSourceDataBase,
DS4ItemDataSourceDataEquipable,

View file

@ -2,10 +2,10 @@
//
// SPDX-License-Identifier: MIT
import { DS4 } from "../../config";
import { getGame } from "../../helpers";
import { createCheckRoll } from "../../rolls/check-factory";
import notifications from "../../ui/notifications";
import { DS4 } from "../../../config";
import { createCheckRoll } from "../../../dice/check-factory";
import { notifications } from "../../../ui/notifications";
import { getGame } from "../../../utils/utils";
import { DS4Item } from "../item";
import type { AttackType } from "./weapon-data-source";

View file

@ -2,8 +2,8 @@
//
// SPDX-License-Identifier: MIT
import { getGame } from "../utils/utils";
import { DS4ActorProxy } from "./actor/proxy";
import { getGame } from "./helpers";
let fallbackData: foundry.data.ActorData["data"] | undefined = undefined;

View file

@ -4,6 +4,6 @@
import "../scss/ds4.scss";
import registerForHooks from "./hooks/hooks";
import { registerForHooks } from "./hooks/hooks";
registerForHooks();

View file

@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: MIT
export default function registerHandlebarsHelpers(): void {
export function registerHandlebarsHelpers(): void {
Handlebars.registerHelper(helpers);
}

View file

@ -4,7 +4,7 @@
//
// SPDX-License-Identifier: MIT
export default async function registerHandlebarsPartials(): Promise<void> {
export async function registerHandlebarsPartials(): Promise<void> {
const templatePaths = [
"systems/ds4/templates/sheets/actor/components/actor-header.hbs",
"systems/ds4/templates/sheets/actor/components/actor-progression.hbs",

View file

@ -1,21 +0,0 @@
// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT
export function getCanvas(): Canvas {
if (!(canvas instanceof Canvas) || !canvas.ready) {
throw new Error(getGame().i18n.localize("DS4.ErrorCanvasIsNotInitialized"));
}
return canvas;
}
export function getGame(): Game {
if (!(game instanceof Game)) {
throw new Error("Game is not initialized yet."); // Cannot localize this as we would need to access game to do this.
}
return game;
}
export function getGameSafe(): Game | undefined {
return game instanceof Game ? game : undefined;
}

View file

@ -2,13 +2,13 @@
//
// SPDX-License-Identifier: MIT
import registerForHotbarDropHook from "./hotbar-drop";
import registerForInitHook from "./init";
import registerForReadyHook from "./ready";
import registerForRenderHooks from "./render";
import registerForSetupHook from "./setup";
import { registerForHotbarDropHook } from "./hotbar-drop";
import { registerForInitHook } from "./init";
import { registerForReadyHook } from "./ready";
import { registerForRenderHooks } from "./render";
import { registerForSetupHook } from "./setup";
export default function registerForHooks(): void {
export function registerForHooks(): void {
registerForHotbarDropHook();
registerForInitHook();
registerForReadyHook();

View file

@ -2,14 +2,14 @@
//
// SPDX-License-Identifier: MIT
import { isCheck } from "../actor/actor-data-properties-base";
import { getGame } from "../helpers";
import { DS4Item } from "../item/item";
import { isCheck } from "../documents/actor/actor-data-properties-base";
import { DS4Item } from "../documents/item/item";
import { createRollCheckMacro } from "../macros/roll-check";
import { createRollItemMacro } from "../macros/roll-item";
import notifications from "../ui/notifications";
import { notifications } from "../ui/notifications";
import { getGame } from "../utils/utils";
export default function registerForHotbarDropHook(): void {
export function registerForHotbarDropHook(): void {
Hooks.on("hotbarDrop", async (hotbar: Hotbar, data: HotbarDropData, slot: string) => {
switch (data.type) {
case "Item": {

View file

@ -4,33 +4,33 @@
//
// SPDX-License-Identifier: MIT
import { DS4ActiveEffect } from "../active-effect/active-effect";
import { DS4ActiveEffectConfig } from "../active-effect/active-effect-config";
import { DS4CharacterActorSheet } from "../actor/character/character-sheet";
import { DS4CreatureActorSheet } from "../actor/creature/creature-sheet";
import { DS4ActorProxy } from "../actor/proxy";
import { DS4ChatMessage } from "../chat-message";
import { DS4ActiveEffectConfig } from "../apps/active-effect-config";
import { DS4CharacterActorSheet } from "../apps/actor/character-sheet";
import { DS4CreatureActorSheet } from "../apps/actor/creature-sheet";
import { DS4ItemSheet } from "../apps/item-sheet";
import { DS4 } from "../config";
import { preloadFonts } from "../fonts";
import registerHandlebarsHelpers from "../handlebars/handlebars-helpers";
import registerHandlebarsPartials from "../handlebars/handlebars-partials";
import { getGame } from "../helpers";
import { DS4ItemSheet } from "../item/item-sheet";
import { DS4ItemProxy } from "../item/proxy";
import logger from "../logger";
import { DS4Check } from "../dice/check";
import { createCheckRoll } from "../dice/check-factory";
import { DS4Roll } from "../dice/roll";
import { registerSlayingDiceModifier } from "../dice/slaying-dice-modifier";
import { DS4ActiveEffect } from "../documents/active-effect";
import { DS4ActorProxy } from "../documents/actor/proxy";
import { DS4ChatMessage } from "../documents/chat-message";
import { DS4ItemProxy } from "../documents/item/proxy";
import { DS4TokenDocument } from "../documents/token-document";
import { registerHandlebarsHelpers } from "../handlebars/handlebars-helpers";
import { registerHandlebarsPartials } from "../handlebars/handlebars-partials";
import { macros } from "../macros/macros";
import { migration } from "../migrations";
import { DS4Check } from "../rolls/check";
import { createCheckRoll } from "../rolls/check-factory";
import { DS4Roll } from "../rolls/roll";
import registerSlayingDiceModifier from "../rolls/slaying-dice-modifier";
import { migration } from "../migration/migration";
import { registerSystemSettings } from "../settings";
import { DS4TokenDocument } from "../token-document";
import { preloadFonts } from "../ui/fonts";
import { logger } from "../utils/logger";
import { getGame } from "../utils/utils";
import type { DS4Actor } from "../actor/actor";
import type { DS4Item } from "../item/item";
import type { DS4Actor } from "../documents/actor/actor";
import type { DS4Item } from "../documents/item/item";
export default function registerForInitHook(): void {
export function registerForInitHook(): void {
Hooks.once("init", init);
}

View file

@ -2,9 +2,9 @@
//
// SPDX-License-Identifier: MIT
import { migration } from "../migrations";
import { migration } from "../migration/migration";
export default function registerForReadyHook(): void {
export function registerForReadyHook(): void {
Hooks.once("ready", () => {
migration.migrate();
});

View file

@ -7,7 +7,7 @@
* @remarks The render hooks of all classes in the class hierarchy are called, so e.g. for a {@link Dialog}, both the
* "renderDialog" hook and the "renderApplication" hook are called (in this order).
*/
export default function registerForRenderHooks(): void {
export function registerForRenderHooks(): void {
["renderApplication", "renderActorSheet", "renderItemSheet"].forEach((hook) => {
Hooks.on(hook, selectTargetInputOnFocus);
});

View file

@ -6,9 +6,9 @@
// SPDX-License-Identifier: MIT
import { DS4 } from "../config";
import { getGame } from "../helpers";
import { getGame } from "../utils/utils";
export default function registerForSetupHooks(): void {
export function registerForSetupHook(): void {
Hooks.once("setup", () => {
localizeAndSortConfigObjects();
});

View file

@ -2,9 +2,9 @@
//
// SPDX-License-Identifier: MIT
import { getCanvas, getGame } from "../helpers";
import { getCanvas, getGame } from "../utils/utils";
import type { DS4Actor } from "../actor/actor";
import type { DS4Actor } from "../documents/actor/actor";
/**
* Gets the currently active actor and token based on how {@link ChatMessage}

View file

@ -3,11 +3,11 @@
// SPDX-License-Identifier: MIT
import { DS4 } from "../config";
import { getGame } from "../helpers";
import notifications from "../ui/notifications";
import { notifications } from "../ui/notifications";
import { getGame } from "../utils/utils";
import { getActiveActorAndToken } from "./helpers";
import type { Check } from "../actor/actor-data-properties-base";
import type { Check } from "../documents/actor/actor-data-properties-base";
/**
* Creates a macro from a check drop.
* Get an existing roll check macro if one exists, otherwise create a new one.

View file

@ -2,8 +2,8 @@
//
// SPDX-License-Identifier: MIT
import { getGame } from "../helpers";
import notifications from "../ui/notifications";
import { notifications } from "../ui/notifications";
import { getGame } from "../utils/utils";
import { getActiveActorAndToken } from "./helpers";
/**

View file

@ -2,8 +2,8 @@
//
// SPDX-License-Identifier: MIT
import { getGame } from "../helpers";
import notifications from "../ui/notifications";
import { notifications } from "../ui/notifications";
import { getGame } from "../utils/utils";
import { getActiveActorAndToken } from "./helpers";
/**

View file

@ -12,7 +12,7 @@ import {
migrateScenes,
} from "./migrationHelpers";
import type { DS4SpellDataSourceData } from "../item/spell/spell-data-source";
import type { DS4SpellDataSourceData } from "../documents/item/spell/spell-data-source";
async function migrate(): Promise<void> {
await migrateItems(getItemUpdateData);

View file

@ -2,15 +2,15 @@
//
// SPDX-License-Identifier: MIT
import { getGame } from "./helpers";
import logger from "./logger";
import { migration as migration001 } from "./migrations/001";
import { migration as migration002 } from "./migrations/002";
import { migration as migration003 } from "./migrations/003";
import { migration as migration004 } from "./migrations/004";
import { migration as migration005 } from "./migrations/005";
import { migration as migration006 } from "./migrations/006";
import notifications from "./ui/notifications";
import { notifications } from "../ui/notifications";
import { logger } from "../utils/logger";
import { getGame } from "../utils/utils";
import { migration as migration001 } from "./001";
import { migration as migration002 } from "./002";
import { migration as migration003 } from "./003";
import { migration as migration004 } from "./004";
import { migration as migration005 } from "./005";
import { migration as migration006 } from "./006";
async function migrate(): Promise<void> {
if (!getGame().user?.isGM) {

View file

@ -2,10 +2,10 @@
//
// SPDX-License-Identifier: MIT
import { DS4Actor } from "../actor/actor";
import { getGame } from "../helpers";
import { DS4Item } from "../item/item";
import logger from "../logger";
import { DS4Actor } from "../documents/actor/actor";
import { DS4Item } from "../documents/item/item";
import { logger } from "../utils/logger";
import { getGame } from "../utils/utils";
type ItemUpdateDataGetter = (
itemData: Partial<foundry.data.ItemData["_source"]>,

View file

@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: MIT
import { getGame } from "./helpers";
import { getGame } from "./utils/utils";
export function registerSystemSettings(): void {
const game = getGame();

View file

@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: MIT
import logger from "../logger";
import { logger } from "../utils/logger";
function getNotificationFunction(type: "info" | "warn" | "error") {
return (message: string, { permanent = false, log = false }: { permanent?: boolean; log?: boolean } = {}): void => {
@ -17,7 +17,7 @@ function getNotificationFunction(type: "info" | "warn" | "error") {
};
}
const notifications = {
export const notifications = Object.freeze({
info: getNotificationFunction("info"),
warn: getNotificationFunction("warn"),
error: getNotificationFunction("error"),
@ -35,6 +35,4 @@ const notifications = {
logger.getLoggingFunction(type)(message);
}
},
};
export default notifications;
});

View file

@ -13,12 +13,10 @@ const getLoggingFunction = (type: LogLevel = "info"): LoggingFunction => {
return (...data: unknown[]) => log(loggingContext, loggingSeparator, ...data);
};
const logger = Object.freeze({
export const logger = Object.freeze({
debug: getLoggingFunction("debug"),
info: getLoggingFunction("info"),
warn: getLoggingFunction("warning"),
error: getLoggingFunction("error"),
getLoggingFunction,
});
export default logger;

View file

@ -2,8 +2,6 @@
//
// SPDX-License-Identifier: MIT
import { getGameSafe } from "./helpers";
/**
* Tests if the given `value` is truthy.
*
@ -22,3 +20,21 @@ export function enforce(value: unknown, message?: string | Error): asserts value
throw message instanceof Error ? message : new Error(message);
}
}
export function getCanvas(): Canvas {
if (!(canvas instanceof Canvas) || !canvas.ready) {
throw new Error(getGame().i18n.localize("DS4.ErrorCanvasIsNotInitialized"));
}
return canvas;
}
export function getGame(): Game {
if (!(game instanceof Game)) {
throw new Error("Game is not initialized yet.");
}
return game;
}
export function getGameSafe(): Game | undefined {
return game instanceof Game ? game : undefined;
}