added watchparties
This commit is contained in:
130
docker/src/api/rooms/rooms.service.ts
Normal file
130
docker/src/api/rooms/rooms.service.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
import crypto from 'crypto';
|
||||
|
||||
interface RoomUser {
|
||||
id: string;
|
||||
username: string;
|
||||
avatar?: string;
|
||||
isHost: boolean;
|
||||
isGuest: boolean;
|
||||
userId?: number; // ID real del usuario si está logueado
|
||||
}
|
||||
|
||||
interface RoomMetadata {
|
||||
id: string;
|
||||
title: string;
|
||||
episode: number;
|
||||
image?: string;
|
||||
source?: string;
|
||||
}
|
||||
|
||||
interface RoomData {
|
||||
id: string;
|
||||
name: string;
|
||||
host: RoomUser;
|
||||
users: Map<string, RoomUser>;
|
||||
createdAt: number;
|
||||
currentVideo: {
|
||||
animeId?: number;
|
||||
episode?: number;
|
||||
source?: string;
|
||||
videoData?: any;
|
||||
currentTime: number;
|
||||
isPlaying: boolean;
|
||||
} | null;
|
||||
password?: string;
|
||||
metadata?: RoomMetadata | null;
|
||||
}
|
||||
|
||||
const rooms = new Map<string, RoomData>();
|
||||
|
||||
export function generateRoomId(): string {
|
||||
return crypto.randomBytes(8).toString('hex');
|
||||
}
|
||||
|
||||
export function createRoom(name: string, host: RoomUser, password?: string): RoomData {
|
||||
const roomId = generateRoomId();
|
||||
|
||||
const room: RoomData = {
|
||||
id: roomId,
|
||||
name,
|
||||
host,
|
||||
users: new Map([[host.id, host]]),
|
||||
createdAt: Date.now(),
|
||||
currentVideo: null,
|
||||
password: password || undefined,
|
||||
metadata: null,
|
||||
};
|
||||
|
||||
rooms.set(roomId, room);
|
||||
return room;
|
||||
}
|
||||
|
||||
export function getRoom(roomId: string): RoomData | null {
|
||||
return rooms.get(roomId) || null;
|
||||
}
|
||||
|
||||
export function getAllRooms(): RoomData[] {
|
||||
return Array.from(rooms.values()).map(room => ({
|
||||
...room,
|
||||
users: room.users
|
||||
}));
|
||||
}
|
||||
|
||||
export function addUserToRoom(roomId: string, user: RoomUser): boolean {
|
||||
const room = rooms.get(roomId);
|
||||
if (!room) return false;
|
||||
|
||||
room.users.set(user.id, user);
|
||||
return true;
|
||||
}
|
||||
|
||||
export function removeUserFromRoom(roomId: string, userId: string): boolean {
|
||||
const room = rooms.get(roomId);
|
||||
if (!room) return false;
|
||||
|
||||
room.users.delete(userId);
|
||||
|
||||
// Si no quedan usuarios, eliminar la sala
|
||||
if (room.users.size === 0) {
|
||||
rooms.delete(roomId);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Si era el host, asignar nuevo host
|
||||
if (room.host.id === userId && room.users.size > 0) {
|
||||
const newHost = Array.from(room.users.values())[0];
|
||||
newHost.isHost = true;
|
||||
room.host = newHost;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
export function updateRoomVideo(roomId: string, videoData: any): boolean {
|
||||
const room = rooms.get(roomId);
|
||||
if (!room) return false;
|
||||
|
||||
room.currentVideo = videoData;
|
||||
return true;
|
||||
}
|
||||
|
||||
export function deleteRoom(roomId: string): boolean {
|
||||
return rooms.delete(roomId);
|
||||
}
|
||||
|
||||
export function verifyRoomPassword(roomId: string, password?: string): boolean {
|
||||
const room = rooms.get(roomId);
|
||||
if (!room) return false;
|
||||
if (!room.password) return true;
|
||||
if (!password) return false;
|
||||
|
||||
return room.password === password;
|
||||
}
|
||||
|
||||
export function updateRoomMetadata(roomId: string, metadata: any): boolean {
|
||||
const room = rooms.get(roomId);
|
||||
if (!room) return false;
|
||||
|
||||
room.metadata = metadata;
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user