Zod EVERYWHERE (#2027)
* Zod EVERYWHERE * Add no-unused-vars rule to eslint in front * Add no-unused-vars rule to eslint in pusher * Add no-unused-vars rule to eslint in back * Remove unused PlayerTexture guards * Fix data providing on room connection Co-authored-by: Alexis Faizeau <a.faizeau@workadventu.re>
This commit is contained in:
@@ -4,14 +4,13 @@ import type { ITiledMapObject } from "../../Map/ITiledMap";
|
||||
import type { ItemFactoryInterface } from "../ItemFactoryInterface";
|
||||
import type { GameScene } from "../../Game/GameScene";
|
||||
import { ActionableItem } from "../ActionableItem";
|
||||
import * as tg from "generic-type-guard";
|
||||
import { z } from "zod";
|
||||
|
||||
const isComputerState = new tg.IsInterface()
|
||||
.withProperties({
|
||||
status: tg.isString,
|
||||
})
|
||||
.get();
|
||||
type ComputerState = tg.GuardedType<typeof isComputerState>;
|
||||
export const isComputerState = z.object({
|
||||
status: z.string(),
|
||||
});
|
||||
|
||||
export type ComputerState = z.infer<typeof isComputerState>;
|
||||
|
||||
let state: ComputerState = {
|
||||
status: "off",
|
||||
@@ -55,10 +54,14 @@ export default {
|
||||
},
|
||||
factory: (scene: GameScene, object: ITiledMapObject, initState: unknown): ActionableItem => {
|
||||
if (initState !== undefined) {
|
||||
if (!isComputerState(initState)) {
|
||||
throw new Error("Invalid state received for computer object");
|
||||
try {
|
||||
state = isComputerState.parse(initState);
|
||||
} catch (err) {
|
||||
if (err instanceof z.ZodError) {
|
||||
console.error(err.issues);
|
||||
}
|
||||
throw new Error(`Invalid state received for computer object`);
|
||||
}
|
||||
state = initState;
|
||||
}
|
||||
|
||||
const computer = new Sprite(scene, object.x, object.y, "computer");
|
||||
|
||||
Reference in New Issue
Block a user