Spaces:
Runtime error
Runtime error
File size: 1,819 Bytes
2b43822 34a23a6 2b43822 9dffdbf 2b43822 9dffdbf 2b43822 d32b1fb 2b43822 9dffdbf 2b43822 34a23a6 9dffdbf 2b43822 9dffdbf 0beae8c d32b1fb d4fcf4a 9dffdbf 0beae8c 9dffdbf 34a23a6 9dffdbf 0beae8c d32b1fb 0beae8c d32b1fb 0beae8c 9dffdbf 2b43822 9dffdbf 307d8b2 9dffdbf 2b43822 | 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 57 58 59 60 | import { z } from "zod";
import { Context } from "../hapticLinkServer";
import { Room } from "../room";
export interface JoinRoomPayload {
roomId?: string;
username?: string;
}
export const JoinRoomSchema = z.object({
roomId: z.string().optional(),
username: z.string().optional(),
});
export function JoinRoomHandler(ctx: Context<JoinRoomPayload>) {
// Set username if payload has it
if (ctx.payload.username) {
ctx.user.username = ctx.payload.username;
}
if (ctx.payload.roomId) ctx.payload.roomId = ctx.payload.roomId.toUpperCase();
let room: Room;
if (ctx.payload.roomId && !ctx.server.rooms[ctx.payload.roomId]) {
// User sent roomId, but room doesn't exist
return ctx.ws.send(
JSON.stringify({
message: "join_room_response",
status: "room doesn't exist",
})
);
// Custom ID Code:
// room = new Room(ctx.payload.roomId);
// room.id = room.id.toUpperCase();
// ctx.server.rooms[room.id] = room;
} else if (!ctx.payload.roomId) {
// Didn't include RoomID, creating new one
room = new Room(ctx.payload.roomId);
room.id = room.id.toUpperCase();
ctx.payload.roomId = room.id;
ctx.server.rooms[room.id] = room;
} else {
// RoomId included and room exists, joining...
room = ctx.server.rooms[ctx.payload.roomId];
if (room.hasUser(ctx.user.id)) {
return ctx.ws.send(
JSON.stringify({
message: "join_room_response",
status: "failed, you are already part of this room",
})
);
}
}
ctx.user.currentRoom = room;
// Broadcasts message to room
room.addUser(ctx.user);
}
|