File size: 1,377 Bytes
307d8b2
 
 
9dffdbf
307d8b2
 
 
d32b1fb
 
307d8b2
 
 
 
 
9dffdbf
307d8b2
 
 
 
 
 
 
 
 
 
9dffdbf
 
d32b1fb
 
 
 
 
 
9dffdbf
307d8b2
9dffdbf
307d8b2
d32b1fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
307d8b2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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,
        })
    );
}