Cleaning up pusher

This commit is contained in:
David Négrier 2020-12-09 16:07:06 +01:00
parent f40d8024f2
commit ff17cb99d6
6 changed files with 0 additions and 571 deletions

View file

@ -1,138 +0,0 @@
import { ConnectCallback, DisconnectCallback } from "./PusherRoom";
import { User } from "./User";
import {PositionInterface} from "_Model/PositionInterface";
import {Movable} from "_Model/Movable";
import {PositionDispatcher} from "_Model/PositionDispatcher";
import {gaugeManager} from "../Services/GaugeManager";
export class Group implements Movable {
static readonly MAX_PER_GROUP = 4;
private static nextId: number = 1;
private id: number;
private users: Set<User>;
private x!: number;
private y!: number;
private hasEditedGauge: boolean = false;
private wasDestroyed: boolean = false;
private roomId: string;
constructor(roomId: string, users: User[], private connectCallback: ConnectCallback, private disconnectCallback: DisconnectCallback, private positionNotifier: PositionDispatcher) {
this.roomId = roomId;
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);
users.forEach((user: User) => {
this.join(user);
});
this.updatePosition();
}
getUsers(): User[] {
return Array.from(this.users.values());
}
getId() : number {
return this.id;
}
/**
* Returns the barycenter of all users (i.e. the center of the group)
*/
getPosition(): PositionInterface {
return {
x: this.x,
y: this.y
};
}
/**
* 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.
this.users.forEach((user: User) => {
const position = user.getPosition();
x += position.x;
y += position.y;
});
x /= this.users.size;
y /= this.users.size;
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 {
this.positionNotifier.updatePosition(this, {x, y}, {x: oldX, y: oldY});
}
}
isFull(): boolean {
return this.users.size >= Group.MAX_PER_GROUP;
}
isEmpty(): boolean {
return this.users.size <= 1;
}
join(user: User): void
{
// Broadcast on the right event
this.connectCallback(user, this);
this.users.add(user);
user.group = this;
}
leave(user: User): void
{
const success = this.users.delete(user);
if (success === false) {
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
this.disconnectCallback(user, this);
}
/**
* Let's kick everybody out.
* Usually used when there is only one user left.
*/
destroy(): void
{
if (this.hasEditedGauge) gaugeManager.decNbGroupsPerRoomGauge(this.roomId);
for (const user of this.users) {
this.leave(user);
}
this.wasDestroyed = true;
}
get getSize(){
return this.users.size;
}
}

View file

@ -1,20 +1,10 @@
import {PointInterface} from "./Websocket/PointInterface";
import {Group} from "./Group";
import {User} from "./User";
import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface";
import {PositionInterface} from "_Model/PositionInterface";
import {Identificable} from "_Model/Websocket/Identificable";
import {PositionDispatcher} from "./PositionDispatcher";
import {ViewportInterface} from "_Model/Websocket/ViewportMessage";
import {Movable} from "_Model/Movable";
import {extractDataFromPrivateRoomId, extractRoomSlugPublicRoomId, isRoomAnonymous} from "./RoomIdentifier";
import {arrayIntersect} from "../Services/ArrayHelper";
import {MAX_USERS_PER_ROOM} from "../Enum/EnvironmentVariable";
import {ZoneEventListener} from "_Model/Zone";
export type ConnectCallback = (user: User, group: Group) => void;
export type DisconnectCallback = (user: User, group: Group) => void;
export enum GameRoomPolicyTypes {
ANONYMUS_POLICY = 1,
MEMBERS_ONLY_POLICY,

View file

@ -1,35 +0,0 @@
import { Group } from "./Group";
import { PointInterface } from "./Websocket/PointInterface";
import {Zone} from "_Model/Zone";
import {Movable} from "_Model/Movable";
import {PositionInterface} from "_Model/PositionInterface";
import {PositionDispatcher} from "_Model/PositionDispatcher";
import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface";
export class User implements Movable {
public listenedZones: Set<Zone>;
public group?: Group;
public constructor(
public id: number,
public uuid: string,
private position: PointInterface,
public silent: boolean,
private positionNotifier: PositionDispatcher,
public readonly socket: ExSocketInterface
) {
this.listenedZones = new Set<Zone>();
this.positionNotifier.enter(this);
}
public getPosition(): PointInterface {
return this.position;
}
public setPosition(position: PointInterface): void {
const oldPosition = this.position;
this.position = position;
this.positionNotifier.updatePosition(this, position, oldPosition);
}
}