Adding "dump" controller and fixing issue with groups in PositionNotifier by delegating the PositionNotifier.updatePosition call to groups themselves

This commit is contained in:
David Négrier 2020-09-25 13:48:02 +02:00
parent 953912b892
commit 892d1555b8
11 changed files with 127 additions and 18 deletions

View File

@ -1 +1,3 @@
DEBUG_MODE=false
DEBUG_MODE=false
JITSI_URL=meet.jit.si
ADMIN_API_TOKEN=123

View File

@ -38,6 +38,7 @@
"dependencies": {
"axios": "^0.20.0",
"body-parser": "^1.19.0",
"circular-json": "^0.5.9",
"express": "^4.17.1",
"generic-type-guard": "^3.2.0",
"google-protobuf": "^3.13.0",
@ -51,6 +52,7 @@
"uuidv4": "^6.0.7"
},
"devDependencies": {
"@types/circular-json": "^0.4.0",
"@types/express": "^4.17.4",
"@types/google-protobuf": "^3.7.3",
"@types/http-status-codes": "^1.2.0",

View File

@ -8,6 +8,7 @@ import * as http from "http";
import {MapController} from "./Controller/MapController";
import {PrometheusController} from "./Controller/PrometheusController";
import {AdminController} from "./Controller/AdminController";
import {DebugController} from "./Controller/DebugController";
class App {
public app: Application;
@ -35,6 +36,7 @@ class App {
this.mapController = new MapController(this.app);
this.prometheusController = new PrometheusController(this.app, this.ioSocketController);
this.adminController = new AdminController(this.app);
this.debugController = new DebugController(this.app, this.ioSocketController);
}
// TODO add session user

View File

@ -0,0 +1,58 @@
import {Application, Request, Response} from "express";
import {OK} from "http-status-codes";
import {ADMIN_API_TOKEN, ADMIN_API_URL} from "../Enum/EnvironmentVariable";
import Axios from "axios";
import {DEBUG_MODE} from "../../../front/src/Enum/EnvironmentVariable";
import {IoSocketController} from "_Controller/IoSocketController";
import Flatted from "flatted";
import {stringify} from "circular-json";
export class DebugController {
constructor(private App : Application, private ioSocketController: IoSocketController) {
this.getDump();
}
getDump(){
this.App.get("/dump", async (req: Request, res: Response) => {
if (req.query.token !== ADMIN_API_TOKEN) {
return res.status(401).send('Invalid token sent!');
}
/* const obj: any = {};
for (const [worldName, world] of this.ioSocketController.getWorlds().entries()) {
let users = new Array();
for (const [worldName, world] of this.ioSocketController.getWorlds().entries()) {
}
obj[worldName] = {
users: world.getUsers()
};
}*/
return res.status(OK).contentType('application/json').send(stringify(
this.ioSocketController.getWorlds(),
(key: any, value: any) => {
if(value instanceof Map) {
const obj: any = {};
for (const [mapKey, mapValue] of value.entries()) {
obj[mapKey] = mapValue;
}
return obj;
} else if(value instanceof Set) {
const obj: Array<any> = [];
for (const [setKey, setValue] of value.entries()) {
obj.push(setValue);
}
return obj;
} else {
return value;
}
}
));
});
}
}

View File

@ -757,4 +757,8 @@ export class IoSocketController {
Client.leave(Client.webRtcRoomId);
delete Client.webRtcRoomId;
}
public getWorlds(): Map<string, World> {
return this.Worlds;
}
}

View File

@ -3,6 +3,7 @@ import { User } from "./User";
import {PositionInterface} from "_Model/PositionInterface";
import {uuid} from "uuidv4";
import {Movable} from "_Model/Movable";
import {PositionNotifier} from "_Model/PositionNotifier";
export class Group implements Movable {
static readonly MAX_PER_GROUP = 4;
@ -11,16 +12,12 @@ export class Group implements Movable {
private id: number;
private users: Set<User>;
private connectCallback: ConnectCallback;
private disconnectCallback: DisconnectCallback;
private x!: number;
private y!: number;
constructor(users: User[], connectCallback: ConnectCallback, disconnectCallback: DisconnectCallback) {
constructor(users: User[], private connectCallback: ConnectCallback, private disconnectCallback: DisconnectCallback, private positionNotifier: PositionNotifier) {
this.users = new Set<User>();
this.connectCallback = connectCallback;
this.disconnectCallback = disconnectCallback;
this.id = Group.nextId;
Group.nextId++;
@ -53,6 +50,9 @@ export class Group implements Movable {
* 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.
@ -67,6 +67,13 @@ export class Group implements Movable {
}
this.x = x;
this.y = y;
if (oldX === undefined) {
// TODO: do we need a "create"
this.positionNotifier.updatePosition(this, {x, y}, {x, y});
} else {
this.positionNotifier.updatePosition(this, {x, y}, {x: oldX, y: oldY});
}
}
isFull(): boolean {
@ -93,6 +100,10 @@ export class Group implements Movable {
}
user.group = undefined;
if (this.users.size !== 0) {
this.updatePosition();
}
// Broadcast on the right event
this.disconnectCallback(user.id, this);
}

View File

@ -117,7 +117,7 @@ export class PositionNotifier {
let zone = this.zones[j][i];
if (zone === undefined) {
zone = new Zone(this.onUserEnters, this.onUserMoves, this.onUserLeaves);
zone = new Zone(this.onUserEnters, this.onUserMoves, this.onUserLeaves, i, j);
this.zones[j][i] = zone;
}
return zone;

View File

@ -93,6 +93,10 @@ export class World {
user.position = userPosition;
user.group?.updatePosition();
/*if (user.group !== undefined) {
// TODO: positionNotifier should be notified by the group itself when it moves!!!
this.positionNotifier.updatePosition(user.group, user.group.getPosition(), oldGroupPosition ? oldGroupPosition : user.group.getPosition());
}*/
if (user.silent) {
return;
@ -112,7 +116,7 @@ export class World {
const group: Group = new Group([
user,
closestUser
], this.connectCallback, this.disconnectCallback);
], this.connectCallback, this.disconnectCallback, this.positionNotifier);
this.groups.add(group);
}
}
@ -127,9 +131,9 @@ export class World {
}
// At the very end, if the user is part of a group, let's call the callback to update group position
if (user.group !== undefined) {
/*if (user.group !== undefined) {
this.positionNotifier.updatePosition(user.group, user.group.getPosition(), oldGroupPosition ? oldGroupPosition : user.group.getPosition());
}
}*/
}
setSilent(socket: Identificable, silent: boolean) {
@ -162,6 +166,7 @@ export class World {
if (group === undefined) {
throw new Error("The user is part of no group");
}
const oldPosition = group.getPosition();
group.leave(user);
if (group.isEmpty()) {
this.positionNotifier.leave(group);
@ -171,7 +176,8 @@ export class World {
}
this.groups.delete(group);
} else {
this.positionNotifier.updatePosition(group, group.getPosition(), group.getPosition());
group.updatePosition();
//this.positionNotifier.updatePosition(group, group.getPosition(), oldPosition);
}
}

View File

@ -1,6 +1,7 @@
import {User} from "./User";
import {PositionInterface} from "_Model/PositionInterface";
import {Movable} from "_Model/Movable";
import {Movable} from "./Movable";
import {Group} from "./Group";
export type EntersCallback = (thing: Movable, listener: User) => void;
export type MovesCallback = (thing: Movable, position: PositionInterface, listener: User) => void;
@ -10,14 +11,27 @@ export class Zone {
private things: Set<Movable> = new Set<Movable>();
private listeners: Set<User> = new Set<User>();
constructor(private onEnters: EntersCallback, private onMoves: MovesCallback, private onLeaves: LeavesCallback) {
/**
* @param x For debugging purpose only
* @param y For debugging purpose only
*/
constructor(private onEnters: EntersCallback, private onMoves: MovesCallback, private onLeaves: LeavesCallback, private x: number, private y: number) {
}
/**
* A user/thing leaves the zone
*/
public leave(thing: Movable, newZone: Zone|null) {
this.things.delete(thing);
const result = this.things.delete(thing);
if (!result) {
if (thing instanceof User) {
console.error('Could not find user in zone '+thing.id);
}
if (thing instanceof Group) {
console.error('Could not find group '+thing.getId()+' in zone ('+this.x+','+this.y+'). Position of group: ('+thing.getPosition().x+','+thing.getPosition().y+')');
}
}
this.notifyLeft(thing, newZone);
}
@ -34,13 +48,13 @@ export class Zone {
public enter(thing: Movable, oldZone: Zone|null, position: PositionInterface) {
this.things.add(thing);
this.notifyUserEnter(thing, oldZone, position);
this.notifyEnter(thing, oldZone, position);
}
/**
* Notify listeners of this zone that this user entered
*/
private notifyUserEnter(thing: Movable, oldZone: Zone|null, position: PositionInterface) {
private notifyEnter(thing: Movable, oldZone: Zone|null, position: PositionInterface) {
for (const listener of this.listeners) {
if (listener === thing) {
continue;
@ -56,8 +70,7 @@ export class Zone {
public move(thing: Movable, position: PositionInterface) {
if (!this.things.has(thing)) {
this.things.add(thing);
const foo = this.things;
this.notifyUserEnter(thing, null, position);
this.notifyEnter(thing, null, position);
return;
}

View File

@ -27,6 +27,11 @@
"@types/connect" "*"
"@types/node" "*"
"@types/circular-json@^0.4.0":
version "0.4.0"
resolved "https://registry.yarnpkg.com/@types/circular-json/-/circular-json-0.4.0.tgz#7401f7e218cfe87ad4c43690da5658b9acaf51be"
integrity sha512-7+kYB7x5a7nFWW1YPBh3KxhwKfiaI4PbZ1RvzBU91LZy7lWJO822CI+pqzSre/DZ7KsCuMKdHnLHHFu8AyXbQg==
"@types/color-name@^1.1.1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0"
@ -359,6 +364,11 @@ chardet@^0.7.0:
version "0.7.0"
resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.7.0.tgz#90094849f0937f2eedc2425d0d28a9e5f0cbad9e"
circular-json@^0.5.9:
version "0.5.9"
resolved "https://registry.yarnpkg.com/circular-json/-/circular-json-0.5.9.tgz#932763ae88f4f7dead7a0d09c8a51a4743a53b1d"
integrity sha512-4ivwqHpIFJZBuhN3g/pEcdbnGUywkBblloGbkglyloVjjR3uT6tieI89MVOfbP2tHX5sgb01FuLgAOzebNlJNQ==
cli-cursor@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/cli-cursor/-/cli-cursor-3.1.0.tgz#264305a7ae490d1d03bf0c9ba7c925d1753af307"

View File

@ -73,6 +73,7 @@ services:
STARTUP_COMMAND_1: yarn install
SECRET_KEY: yourSecretKey
ALLOW_ARTILLERY: "true"
ADMIN_API_TOKEN: "$ADMIN_API_TOKEN"
volumes:
- ./back:/usr/src/app
labels: