init
This commit is contained in:
commit
28b026a614
17 changed files with 895 additions and 0 deletions
58
common/message/to_server.ts
Normal file
58
common/message/to_server.ts
Normal file
|
@ -0,0 +1,58 @@
|
|||
import { z } from "https://deno.land/x/zod@v3.22.4/mod.ts";
|
||||
import { ParserOutput } from "../utils.ts";
|
||||
|
||||
export type MsgToServer = ParserOutput<typeof message_to_server_parser>;
|
||||
export function message_to_server_parser() {
|
||||
return ping_parser()
|
||||
.or(request_display_parser())
|
||||
.or(input_parser())
|
||||
.or(exit_parser());
|
||||
}
|
||||
|
||||
export type MsgPing = ParserOutput<typeof ping_parser>;
|
||||
export function ping_parser() {
|
||||
return z.object({
|
||||
kind: z.literal("ping"),
|
||||
content: z.object({
|
||||
message: z.string(),
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
export type MsgReqDisplay = ParserOutput<typeof request_display_parser>;
|
||||
export function request_display_parser() {
|
||||
return z.object({
|
||||
kind: z.literal("request_display"),
|
||||
content: z.object({
|
||||
width: z.number(),
|
||||
height: z.number(),
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
export type MsgInput = ParserOutput<typeof input_parser>;
|
||||
export function input_parser() {
|
||||
return z.object({
|
||||
kind: z.literal("input"),
|
||||
content: z.object({
|
||||
control: control_parser(),
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
export type ClientInput = ParserOutput<typeof control_parser>;
|
||||
function control_parser() {
|
||||
return z.literal("up")
|
||||
.or(z.literal("down"))
|
||||
.or(z.literal("left"))
|
||||
.or(z.literal("right"))
|
||||
.or(z.literal("interact"))
|
||||
.or(z.literal("attack"));
|
||||
}
|
||||
|
||||
export type MsgExit = ParserOutput<typeof exit_parser>;
|
||||
export function exit_parser() {
|
||||
return z.object({
|
||||
kind: z.literal("exit"),
|
||||
});
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue