workadventure/back/src/Model/Group.ts

140 lines
3.7 KiB
TypeScript
Raw Normal View History

2020-10-06 15:37:00 +02:00
import { ConnectCallback, DisconnectCallback } from "./GameRoom";
2020-09-16 16:06:43 +02:00
import { User } from "./User";
2021-06-24 10:09:10 +02:00
import { PositionInterface } from "_Model/PositionInterface";
import { Movable } from "_Model/Movable";
import { PositionNotifier } from "_Model/PositionNotifier";
import { gaugeManager } from "../Services/GaugeManager";
import { MAX_PER_GROUP } from "../Enum/EnvironmentVariable";
2020-09-16 16:06:43 +02:00
export class Group implements Movable {
private static nextId: number = 1;
private id: number;
2020-09-16 16:06:43 +02:00
private users: Set<User>;
2020-09-21 11:24:03 +02:00
private x!: number;
private y!: number;
private hasEditedGauge: boolean = false;
private wasDestroyed: boolean = false;
private roomId: string;
2021-06-24 10:09:10 +02:00
constructor(
roomId: string,
users: User[],
private connectCallback: ConnectCallback,
private disconnectCallback: DisconnectCallback,
private positionNotifier: PositionNotifier
) {
this.roomId = roomId;
2020-09-16 16:06:43 +02:00
this.users = new Set<User>();
this.id = Group.nextId;
Group.nextId++;
//we only send a event for prometheus metrics if the group lives more than 5 seconds
setTimeout(() => {
if (!this.wasDestroyed) {
this.hasEditedGauge = true;
gaugeManager.incNbGroupsPerRoomGauge(roomId);
}
}, 5000);
2020-09-16 16:06:43 +02:00
users.forEach((user: User) => {
this.join(user);
});
2020-09-21 11:24:03 +02:00
this.updatePosition();
2020-04-07 10:08:04 +02:00
}
2020-09-16 16:06:43 +02:00
getUsers(): User[] {
return Array.from(this.users.values());
2020-04-07 10:08:04 +02:00
}
2021-06-24 10:09:10 +02:00
getId(): number {
return this.id;
}
/**
* Returns the barycenter of all users (i.e. the center of the group)
*/
getPosition(): PositionInterface {
2020-09-21 11:24:03 +02:00
return {
x: this.x,
2021-06-24 10:09:10 +02:00
y: this.y,
2020-09-21 11:24:03 +02:00
};
}
/**
* Computes the barycenter of all users (i.e. the center of the group)
*/
updatePosition(): void {
const oldX = this.x;
const oldY = this.y;
let x = 0;
let y = 0;
// Let's compute the barycenter of all users.
2020-09-16 16:06:43 +02:00
this.users.forEach((user: User) => {
const position = user.getPosition();
x += position.x;
y += position.y;
});
x /= this.users.size;
y /= this.users.size;
2020-09-21 11:24:03 +02:00
if (this.users.size === 0) {
throw new Error("EMPTY GROUP FOUND!!!");
}
this.x = x;
this.y = y;
if (oldX === undefined) {
this.positionNotifier.enter(this);
} else {
2021-06-24 10:09:10 +02:00
this.positionNotifier.updatePosition(this, { x, y }, { x: oldX, y: oldY });
}
}
2020-04-07 10:08:04 +02:00
isFull(): boolean {
return this.users.size >= MAX_PER_GROUP;
2020-04-07 10:08:04 +02:00
}
2020-04-08 20:40:44 +02:00
isEmpty(): boolean {
return this.users.size <= 1;
}
2021-06-24 10:09:10 +02:00
join(user: User): void {
// Broadcast on the right event
2020-09-29 16:01:22 +02:00
this.connectCallback(user, this);
this.users.add(user);
user.group = this;
}
2021-06-24 10:09:10 +02:00
leave(user: User): void {
2020-06-29 22:13:07 +02:00
const success = this.users.delete(user);
if (success === false) {
2021-06-24 10:09:10 +02:00
throw new Error("Could not find user " + user.id + " in the group " + this.id);
}
user.group = undefined;
if (this.users.size !== 0) {
this.updatePosition();
}
// Broadcast on the right event
2020-09-29 16:01:22 +02:00
this.disconnectCallback(user, this);
}
/**
* Let's kick everybody out.
* Usually used when there is only one user left.
*/
2021-06-24 10:09:10 +02:00
destroy(): void {
if (this.hasEditedGauge) gaugeManager.decNbGroupsPerRoomGauge(this.roomId);
2020-06-29 22:13:07 +02:00
for (const user of this.users) {
this.leave(user);
}
this.wasDestroyed = true;
2020-04-08 20:40:44 +02:00
}
2021-06-24 10:09:10 +02:00
get getSize() {
return this.users.size;
}
}