Adding a Pusher container as a middleware/dispatcher between front and back
This commit is contained in:
parent
6c6d046891
commit
4c1e566a6c
86 changed files with 12172 additions and 983 deletions
|
@ -2,20 +2,21 @@ import {User} from "./User";
|
|||
import {PositionInterface} from "_Model/PositionInterface";
|
||||
import {Movable} from "./Movable";
|
||||
import {Group} from "./Group";
|
||||
import {ZoneSocket} from "../RoomManager";
|
||||
|
||||
export type EntersCallback = (thing: Movable, listener: User) => void;
|
||||
export type MovesCallback = (thing: Movable, position: PositionInterface, listener: User) => void;
|
||||
export type LeavesCallback = (thing: Movable, listener: User) => void;
|
||||
export type EntersCallback = (thing: Movable, fromZone: Zone|null, listener: ZoneSocket) => void;
|
||||
export type MovesCallback = (thing: Movable, position: PositionInterface, listener: ZoneSocket) => void;
|
||||
export type LeavesCallback = (thing: Movable, newZone: Zone|null, listener: ZoneSocket) => void;
|
||||
|
||||
export class Zone {
|
||||
private things: Set<Movable> = new Set<Movable>();
|
||||
private listeners: Set<User> = new Set<User>();
|
||||
private listeners: Set<ZoneSocket> = new Set<ZoneSocket>();
|
||||
|
||||
/**
|
||||
* @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) {
|
||||
constructor(private onEnters: EntersCallback, private onMoves: MovesCallback, private onLeaves: LeavesCallback, public readonly x: number, public readonly y: number) {
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -40,9 +41,9 @@ export class Zone {
|
|||
*/
|
||||
private notifyLeft(thing: Movable, newZone: Zone|null) {
|
||||
for (const listener of this.listeners) {
|
||||
if (listener !== thing && (newZone === null || !listener.listenedZones.has(newZone))) {
|
||||
this.onLeaves(thing, listener);
|
||||
}
|
||||
//if (listener !== thing && (newZone === null || !listener.listenedZones.has(newZone))) {
|
||||
this.onLeaves(thing, newZone, listener);
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -56,17 +57,20 @@ export class Zone {
|
|||
*/
|
||||
private notifyEnter(thing: Movable, oldZone: Zone|null, position: PositionInterface) {
|
||||
for (const listener of this.listeners) {
|
||||
if (listener === thing) {
|
||||
|
||||
/*if (listener === thing) {
|
||||
continue;
|
||||
}
|
||||
if (oldZone === null || !listener.listenedZones.has(oldZone)) {
|
||||
this.onEnters(thing, listener);
|
||||
} else {
|
||||
this.onMoves(thing, position, listener);
|
||||
}
|
||||
}*/
|
||||
this.onEnters(thing, oldZone, listener);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public move(thing: Movable, position: PositionInterface) {
|
||||
if (!this.things.has(thing)) {
|
||||
this.things.add(thing);
|
||||
|
@ -75,13 +79,13 @@ export class Zone {
|
|||
}
|
||||
|
||||
for (const listener of this.listeners) {
|
||||
if (listener !== thing) {
|
||||
//if (listener !== thing) {
|
||||
this.onMoves(thing,position, listener);
|
||||
}
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
public startListening(listener: User): void {
|
||||
/*public startListening(listener: User): void {
|
||||
for (const thing of this.things) {
|
||||
if (thing !== listener) {
|
||||
this.onEnters(thing, listener);
|
||||
|
@ -101,9 +105,18 @@ export class Zone {
|
|||
|
||||
this.listeners.delete(listener);
|
||||
listener.listenedZones.delete(this);
|
||||
}
|
||||
}*/
|
||||
|
||||
public getThings(): Set<Movable> {
|
||||
return this.things;
|
||||
}
|
||||
|
||||
public addListener(socket: ZoneSocket): void {
|
||||
this.listeners.add(socket);
|
||||
// TODO: here, we should trigger in some way the sending of current items
|
||||
}
|
||||
|
||||
public removeListener(socket: ZoneSocket): void {
|
||||
this.listeners.delete(socket);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue