Kopie van https://gitlab.com/studieverenigingvia/ict/centurion met een paar aanpassingen
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
centurion/backend/src/User.ts

77 lines
1.8 KiB

import {Socket} from "socket.io";
import Room from "./Room";
export default class User {
socket: Socket;
id: string;
room: Room | null = null;
constructor(socket: Socket) {
this.socket = socket;
this.id = socket.id;
}
onDisconnect() {
if (this.room != null) {
}
}
setRoom(room: Room | null) {
if (this.room === room) return;
if (this.room != null) {
this.socket.leave(this.room.id.toString());
}
this.room = room;
if (this.room != null) {
this.socket.join(this.room.id.toString());
}
this.sync();
}
sentRoom: any = null;
sentTimelineName: string | null = null;
sync() {
if (!this.shallowEquals(this.sentRoom, this.room?.serialize(this))) {
this.sentRoom = this.room?.serialize(this);
this.emit('room', {
'room': this.sentRoom
})
}
if (!this.shallowEquals(this.sentTimelineName, this.room?.timelineName)) {
this.sentTimelineName = this.room?.timelineName || null;
this.emit('timeline', {
'timeline': this.sentTimelineName == null ? null : this.room!!.serializeTimeline(this)
})
}
}
emit(eventName: string, obj: any) {
this.socket.emit(eventName, obj);
}
shallowEquals(obj1: any, obj2: any) {
if (obj1 === null && obj2 === null)
return true;
if ((obj1 === null && obj2 !== null) || (obj1 !== null && obj2 === null))
return false;
if (typeof (obj1) != typeof (obj2))
return false;
if (Object.keys(obj1).length !== Object.keys(obj2).length) return false
return Object.keys(obj1).every(key =>
obj2.hasOwnProperty(key) && obj1[key] === obj2[key]
);
}
}