Skip to content

Commit

Permalink
Merge pull request #199 from SuperViz/feat/new-room-oackage-params
Browse files Browse the repository at this point in the history
feat: assiging slots to the participants
  • Loading branch information
carlossantos74 authored Jan 7, 2025
2 parents c08abbd + 7e0a827 commit 35a0554
Show file tree
Hide file tree
Showing 5 changed files with 384 additions and 15 deletions.
88 changes: 88 additions & 0 deletions packages/room/src/common/types/colors.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
export const NAME_IS_WHITE_TEXT = [
'rosybrown',
'red',
'saddlebrown',
'coral',
'orange',
'brown',
'goldenrod',
'olivegreen',
'darkolivegreen',
'seagreen',
'lightsea',
'teal',
'cadetblue',
'pastelblue',
'mediumslateblue',
'bluedark',
'navy',
'rebeccapurple',
'purple',
'vividorchid',
'darkmagenta',
'deepmagenta',
'fuchsia',
'violetred',
'pink',
'vibrantpink',
'paleredviolet',
'carmine',
'wine',
];

export const MEETING_COLORS = {
turquoise: '#31E0B0',
orange: '#FF5E10',
blue: '#00ABF7',
pink: '#FF00BB',
purple: '#9C29FF',
green: '#6FDD00',
red: '#E30000',
bluedark: '#304AFF',
pinklight: '#FF89C4',
purplelight: '#D597FF',
greenlight: '#C6EC5C',
orangelight: '#FFA115',
bluelight: '#75DEFE',
redlight: '#FAA291',
brown: '#BB813F',
yellow: '#FFEF33',
olivegreen: '#93A000',
lightyellow: '#FAE391',
violetred: '#C03FA3',
rosybrown: '#B58787',
cadetblue: '#2095BB',
lightsteelblue: '#ABB5FF',
seagreen: '#04B45F',
palegreen: '#8DE990',
saddlebrown: '#964C42',
pastelblue: '#77A1CC',
palesilver: '#D2BABA',
coral: '#DF6B6B',
bisque: '#FFD9C4',
goldenrod: '#DAA520',
tan: '#D2BD93',
darkolivegreen: '#536C27',
mint: '#ADE6DF',
lightsea: '#45AFAA',
teal: '#036E6E',
wine: '#760040',
cyan: '#00FFFF',
mediumslateblue: '#6674D7',
navy: '#0013BB',
rebeccapurple: '#663399',
vividorchid: '#D429FF',
darkmagenta: '#810E81',
deepmagenta: '#C303C6',
fuchsia: '#FA00FF',
lavendermagenta: '#EE82EE',
thistle: '#EEB4DD',
vibrantpink: '#FF007A',
cottoncandy: '#FFC0DE',
paleredviolet: '#D96598',
carmine: '#B50A52',
gray: '#878291',
};

export const MEETING_COLORS_ARRAY = Object.values(MEETING_COLORS);
export const MEETING_COLORS_KEYS = Object.keys(MEETING_COLORS);
2 changes: 1 addition & 1 deletion packages/room/src/common/types/participant.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type Participant = InitialParticipant & {
}

export type Slot = {
index: number;
index: number | null;
color: string;
textColor: string;
colorName: string;
Expand Down
21 changes: 7 additions & 14 deletions packages/room/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { InitialParticipant, Participant } from '../common/types/participant.typ
import { Logger } from '../common/utils/logger';
import { IOC } from '../services/io';
import { IOCState } from '../services/io/types';
import { SlotService } from '../services/slot';

import { GeneralEvent, ParticipantEvent, RoomEventPayload, RoomParams, Callback, EventOptions, RoomEvent } from './types';

Expand All @@ -14,6 +15,8 @@ export class Room {
private io: IOC;
private room: SocketRoomType;

private slotService: SlotService;

private participants: Map<string, Participant> = new Map();
private state: IOCState = IOCState.DISCONNECTED;
private logger: Logger;
Expand Down Expand Up @@ -54,6 +57,7 @@ export class Room {
this.subscriptions.clear();
this.observers.clear();
this.participants.clear();
this.slotService;

if (typeof window !== 'undefined') {
delete window.SUPERVIZ_ROOM;
Expand Down Expand Up @@ -138,6 +142,7 @@ export class Room {
private init() {
this.io.stateSubject.subscribe(this.onConnectionStateChange);
this.room = this.io.createRoom('room', 'unlimited');
this.slotService = new SlotService(this.room, this.participant);

this.subscribeToRoomEvents();
}
Expand All @@ -158,13 +163,7 @@ export class Room {
name: participant?.name ? participant.name : message.name,
activeComponents: participant?.activeComponents ?? [],
email: participant?.email ?? null,
slot: participant?.slot ?? {
index: null,
color: '#878291',
textColor: '#fff',
colorName: 'gray',
timestamp: Date.now(),
},
slot: participant?.slot ?? SlotService.getDefaultSlot(),
};
}

Expand All @@ -180,13 +179,7 @@ export class Room {
name: initialData.name,
email: initialData.email ?? null,
activeComponents: [],
slot: {
index: null,
color: '#878291',
textColor: '#fff',
colorName: 'gray',
timestamp: Date.now(),
},
slot: SlotService.getDefaultSlot(),
};
}

Expand Down
92 changes: 92 additions & 0 deletions packages/room/src/services/slot/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { PresenceEvent, PresenceEvents, Room } from '@superviz/socket-client';

import { Participant, Slot } from '../../common/types/participant.types';

import { SlotService } from './index';

describe('SlotService', () => {
let room: Room;
let participant: Participant;
let slotService: SlotService;

beforeEach(() => {
room = {
presence: {
on: jest.fn(),
get: jest.fn(),
update: jest.fn(),
},
} as unknown as Room;

participant = {
id: 'participant1',
slot: SlotService.getDefaultSlot(),
} as Participant;

slotService = new SlotService(room, participant);
});

it('should initialize with default slot', () => {
expect(slotService.slot).toEqual({
...SlotService.getDefaultSlot(),
timestamp: expect.any(Number),
});
});

it('should assign a slot to the participant', async () => {
room.presence.get = jest.fn((callback) => callback([]));
room.presence.update = jest.fn();

const slot = await slotService['assignSlot']();

expect(slot).toBeDefined();
expect(slot.index).toBeGreaterThanOrEqual(0);
expect(slot.index).toBeLessThan(50);
expect(room.presence.update).toHaveBeenCalledWith({ slot });
});

it('should handle presence update for the same participant', async () => {
const event: PresenceEvent<Participant> = {
id: participant.id,
data: { slot: { index: 1 } },
} as PresenceEvent<Participant>;

slotService['validateSlotType'] = jest.fn().mockResolvedValue({ index: 1 });

await slotService['onPresenceUpdate'](event);

expect(slotService['validateSlotType']).toHaveBeenCalledWith(event.data);
expect(participant.slot.index).toBe(1);
});

it('should set default slot', () => {
slotService['setDefaultSlot']();

expect(slotService.slot).toEqual(SlotService.getDefaultSlot());
expect(room.presence.update).toHaveBeenCalledWith({ slot: SlotService.getDefaultSlot() });
});

it('should validate and assign slot type', async () => {
slotService['assignSlot'] = jest.fn().mockResolvedValue({ index: 1 });
slotService['setDefaultSlot'] = jest.fn();

const slot = await slotService['validateSlotType'](participant);

expect(slotService['assignSlot']).toHaveBeenCalled();
expect(slot.index).toBe(1);
});

it('should not assign slot if already assigning', async () => {
slotService['isAssigningSlot'] = true;

const slot = await slotService['assignSlot']();

expect(slot).toEqual(slotService.slot);
});

it('should throw error if no more slots are available', async () => {
room.presence.get = jest.fn((callback) => callback(Array(50).fill({})));

await expect(slotService['assignSlot']()).resolves.toEqual(null);
});
});
Loading

0 comments on commit 35a0554

Please sign in to comment.