avallef's picture
Formatted with prettier
d32b1fb
import { z } from "zod";
import { Context, Route } from "../hapticLinkServer";
export interface SendVibrationPayload {
id: number;
type: "enabled" | "disabled";
position: {
x: number;
y: number;
};
color?: string;
intensity?: number;
}
export const SendVibrationSchema = z.object({
id: z.number(),
type: z.union([z.literal("enabled"), z.literal("disabled")]),
position: z.object({
x: z.number(),
y: z.number(),
}),
color: z.string().optional(),
intensity: z.number().optional(),
});
export function SendVibrationHandler(ctx: Context<SendVibrationPayload>) {
if (!ctx.user.currentRoom) {
return ctx.ws.send(
JSON.stringify({
message: "send_vibration_response",
status: "not part of a room",
})
);
}
const p = ctx.payload;
ctx.user.currentRoom.broadcast(
JSON.stringify({
message: "receive_touch",
id: ctx.user.id + "_" + p.id,
type: p.type,
user: {
username: ctx.user.username,
id: ctx.user.id,
},
position: {
x: p.position.x,
y: p.position.y,
},
color: p.color || "#FF0000",
intensity: p.intensity ?? 1,
})
);
}