Adding "dump" controller and fixing issue with groups in PositionNotifier by delegating the PositionNotifier.updatePosition call to groups themselves
This commit is contained in:
parent
953912b892
commit
892d1555b8
11 changed files with 127 additions and 18 deletions
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue