Skip to content

Commit

Permalink
Move system code around to avoid top level await
Browse files Browse the repository at this point in the history
  • Loading branch information
Kruptein committed Aug 11, 2024
1 parent 083c98a commit c99f42d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
10 changes: 9 additions & 1 deletion client/src/game/systems/dice/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Part, RollResult } from "@planarally/dice/core";
import { SYSTEMS, type Part, type RollResult } from "@planarally/dice/core";
import type { DeepReadonly } from "vue";

import { registerSystem } from "..";
Expand All @@ -19,6 +19,14 @@ class DiceSystem implements System {
return rollString;
}

async loadSystems(): Promise<void> {
if ($.systems) return;

const { DX } = await SYSTEMS.DX();
const { DX3 } = await SYSTEMS.DX3();
$.systems = { "2d": DX, "3d": DX3 };
}

async load3d(): Promise<void> {
if (!diceState.raw.loaded3d) {
await loadDiceEnv();
Expand Down
6 changes: 4 additions & 2 deletions client/src/game/systems/dice/state.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import type { Part, RollResult } from "@planarally/dice/core";
import { type Part, type RollResult, type SYSTEMS } from "@planarally/dice/core";
import type { DeepReadonly } from "vue";

import { buildState } from "../state";

type AsyncReturnType<T extends (..._args: any) => Promise<any>> = Awaited<ReturnType<T>>;

interface DiceState {
dimensions3d: { width: number; height: number };
history: { roll: RollResult<Part>; name: string; player: string }[];
loaded3d: boolean;
systems?: { "2d": AsyncReturnType<typeof SYSTEMS.DX>["DX"]; "3d": AsyncReturnType<typeof SYSTEMS.DX3>["DX3"] };
result?: DeepReadonly<RollResult<Part>>;
}

Expand All @@ -15,7 +18,6 @@ const state = buildState<DiceState>({
history: [],
loaded3d: false,
});

export const diceState = {
...state,
};
13 changes: 7 additions & 6 deletions client/src/game/tools/variants/dice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Vector3 } from "@babylonjs/core/Maths/math";
import { type Part, type RollResult, rollString, SYSTEMS } from "@planarally/dice/core";
import { type Part, type RollResult, rollString } from "@planarally/dice/core";
import tinycolor from "tinycolor2";
import { reactive } from "vue";

Expand All @@ -17,9 +17,6 @@ import { playerSettingsState } from "../../systems/settings/players/state";
import { SelectFeatures } from "../models/select";
import { Tool } from "../tool";

export const { DX } = await SYSTEMS.DX();
const { DX3 } = await SYSTEMS.DX3();

function generate3dOptions(): {
color: string;
physics: () => {
Expand Down Expand Up @@ -85,6 +82,10 @@ class DiceTool extends Tool implements ITool {
// );
// }

async onSelect(): Promise<void> {
await diceSystem.loadSystems();
}

get permittedTools(): ToolPermission[] {
return [{ name: ToolName.Select, features: { disabled: [SelectFeatures.Resize, SelectFeatures.Rotate] } }];
}
Expand All @@ -93,9 +94,9 @@ class DiceTool extends Tool implements ITool {
let roll: RollResult<Part>;
if (use3d) {
const dieDefaults = generate3dOptions();
roll = await rollString(input, DX3, { thrower: diceThrower!, dieDefaults });
roll = await rollString(input, diceState.raw.systems!["3d"], { thrower: diceThrower!, dieDefaults });
} else {
roll = await rollString(input, DX);
roll = await rollString(input, diceState.raw.systems!["2d"]);
}

if (shareWith !== "none") {
Expand Down
4 changes: 2 additions & 2 deletions client/src/game/ui/tools/DiceTool.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import ToggleGroup from "../../../core/components/ToggleGroup.vue";
import { diceSystem } from "../../systems/dice";
import { DxHelper } from "../../systems/dice/dx";
import { diceState } from "../../systems/dice/state";
import { diceTool, DX } from "../../tools/variants/dice";
import { diceTool } from "../../tools/variants/dice";
const showHistory = ref(false);
Expand Down Expand Up @@ -79,7 +79,7 @@ function addSymbol(symbol: (typeof DxConfig.symbolOptions)[number]): void {
}
function updateFromString(event: Event): void {
input.value = DX.parse((event.target as HTMLInputElement).value);
input.value = diceState.raw.systems!["2d"].parse((event.target as HTMLInputElement).value);
}
async function roll(): Promise<void> {
Expand Down

0 comments on commit c99f42d

Please sign in to comment.