Migrating position notification into the User class

This commit is contained in:
David Négrier 2020-09-25 15:25:06 +02:00
parent 892d1555b8
commit 23cea1c835
7 changed files with 59 additions and 52 deletions

View File

@ -18,6 +18,7 @@ class App {
public mapController: MapController; public mapController: MapController;
public prometheusController: PrometheusController; public prometheusController: PrometheusController;
private adminController: AdminController; private adminController: AdminController;
private debugController: DebugController;
constructor() { constructor() {
this.app = express(); this.app = express();

View File

@ -57,8 +57,9 @@ export class Group implements Movable {
let y = 0; let y = 0;
// Let's compute the barycenter of all users. // Let's compute the barycenter of all users.
this.users.forEach((user: User) => { this.users.forEach((user: User) => {
x += user.position.x; const position = user.getPosition();
y += user.position.y; x += position.x;
y += position.y;
}); });
x /= this.users.size; x /= this.users.size;
y /= this.users.size; y /= this.users.size;
@ -69,8 +70,7 @@ export class Group implements Movable {
this.y = y; this.y = y;
if (oldX === undefined) { if (oldX === undefined) {
// TODO: do we need a "create" this.positionNotifier.enter(this);
this.positionNotifier.updatePosition(this, {x, y}, {x, y});
} else { } else {
this.positionNotifier.updatePosition(this, {x, y}, {x: oldX, y: oldY}); this.positionNotifier.updatePosition(this, {x, y}, {x: oldX, y: oldY});
} }

View File

@ -74,6 +74,13 @@ export class PositionNotifier {
return things; return things;
} }
public enter(thing: Movable): void {
const position = thing.getPosition();
const zoneDesc = this.getZoneDescriptorFromCoordinates(position.x, position.y);
const zone = this.getZone(zoneDesc.i, zoneDesc.j);
zone.enter(thing, null, position);
}
public updatePosition(thing: Movable, newPosition: PositionInterface, oldPosition: PositionInterface): void { public updatePosition(thing: Movable, newPosition: PositionInterface, oldPosition: PositionInterface): void {
// Did we change zone? // Did we change zone?
const oldZoneDesc = this.getZoneDescriptorFromCoordinates(oldPosition.x, oldPosition.y); const oldZoneDesc = this.getZoneDescriptorFromCoordinates(oldPosition.x, oldPosition.y);

View File

@ -3,6 +3,7 @@ import { PointInterface } from "./Websocket/PointInterface";
import {Zone} from "_Model/Zone"; import {Zone} from "_Model/Zone";
import {Movable} from "_Model/Movable"; import {Movable} from "_Model/Movable";
import {PositionInterface} from "_Model/PositionInterface"; import {PositionInterface} from "_Model/PositionInterface";
import {PositionNotifier} from "_Model/PositionNotifier";
export class User implements Movable { export class User implements Movable {
public listenedZones: Set<Zone>; public listenedZones: Set<Zone>;
@ -10,14 +11,22 @@ export class User implements Movable {
public constructor( public constructor(
public id: number, public id: number,
public position: PointInterface, private position: PointInterface,
public silent: boolean, public silent: boolean,
private positionNotifier: PositionNotifier
) { ) {
this.listenedZones = new Set<Zone>(); this.listenedZones = new Set<Zone>();
this.positionNotifier.enter(this);
} }
public getPosition(): PositionInterface { public getPosition(): PointInterface {
return this.position; return this.position;
} }
public setPosition(position: PointInterface): void {
const oldPosition = this.position;
this.position = position;
this.positionNotifier.updatePosition(this, position, oldPosition);
}
} }

View File

@ -56,9 +56,11 @@ export class World {
} }
public join(socket : Identificable, userPosition: PointInterface): void { public join(socket : Identificable, userPosition: PointInterface): void {
this.users.set(socket.userId, new User(socket.userId, userPosition, false)); const user = new User(socket.userId, userPosition, false, this.positionNotifier);
this.users.set(socket.userId, user);
// Let's call update position to trigger the join / leave room // Let's call update position to trigger the join / leave room
this.updatePosition(socket, userPosition); //this.updatePosition(socket, userPosition);
this.updateUserGroup(user);
} }
public leave(user : Identificable){ public leave(user : Identificable){
@ -87,16 +89,13 @@ export class World {
return; return;
} }
this.positionNotifier.updatePosition(user, userPosition, user.position); user.setPosition(userPosition);
const oldGroupPosition = user.group?.getPosition(); this.updateUserGroup(user);
}
user.position = userPosition; private updateUserGroup(user: User): void {
user.group?.updatePosition(); 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) { if (user.silent) {
return; return;
@ -124,16 +123,11 @@ export class World {
} else { } else {
// If the user is part of a group: // If the user is part of a group:
// should he leave the group? // should he leave the group?
const distance = World.computeDistanceBetweenPositions(user.position, user.group.getPosition()); const distance = World.computeDistanceBetweenPositions(user.getPosition(), user.group.getPosition());
if (distance > this.groupRadius) { if (distance > this.groupRadius) {
this.leaveGroup(user); this.leaveGroup(user);
} }
} }
// 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) {
this.positionNotifier.updatePosition(user.group, user.group.getPosition(), oldGroupPosition ? oldGroupPosition : user.group.getPosition());
}*/
} }
setSilent(socket: Identificable, silent: boolean) { setSilent(socket: Identificable, silent: boolean) {
@ -152,7 +146,7 @@ export class World {
} }
if (!silent) { if (!silent) {
// If we are back to life, let's trigger a position update to see if we can join some group. // If we are back to life, let's trigger a position update to see if we can join some group.
this.updatePosition(socket, user.position); this.updatePosition(socket, user.getPosition());
} }
} }
@ -251,7 +245,7 @@ export class World {
if (group.isFull()) { if (group.isFull()) {
return; return;
} }
const distance = World.computeDistanceBetweenPositions(user.position, group.getPosition()); const distance = World.computeDistanceBetweenPositions(user.getPosition(), group.getPosition());
if(distance <= minimumDistanceFound && distance <= this.groupRadius) { if(distance <= minimumDistanceFound && distance <= this.groupRadius) {
minimumDistanceFound = distance; minimumDistanceFound = distance;
matchingItem = group; matchingItem = group;
@ -263,7 +257,9 @@ export class World {
public static computeDistance(user1: User, user2: User): number public static computeDistance(user1: User, user2: User): number
{ {
return Math.sqrt(Math.pow(user2.position.x - user1.position.x, 2) + Math.pow(user2.position.y - user1.position.y, 2)); const user1Position = user1.getPosition();
const user2Position = user2.getPosition();
return Math.sqrt(Math.pow(user2Position.x - user1Position.x, 2) + Math.pow(user2Position.y - user1Position.y, 2));
} }
public static computeDistanceBetweenPositions(position1: PositionInterface, position2: PositionInterface): number public static computeDistanceBetweenPositions(position1: PositionInterface, position2: PositionInterface): number

View File

@ -25,10 +25,10 @@ export class Zone {
const result = this.things.delete(thing); const result = this.things.delete(thing);
if (!result) { if (!result) {
if (thing instanceof User) { if (thing instanceof User) {
console.error('Could not find user in zone '+thing.id); throw new Error('Could not find user in zone '+thing.id);
} }
if (thing instanceof Group) { 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+')'); throw new Error('Could not find group '+thing.getId()+' in zone ('+this.x+','+this.y+'). Position of group: ('+thing.getPosition().x+','+thing.getPosition().y+')');
} }
} }

View File

@ -9,14 +9,6 @@ import {Zone} from "_Model/Zone";
import {Movable} from "_Model/Movable"; import {Movable} from "_Model/Movable";
import {PositionInterface} from "_Model/PositionInterface"; import {PositionInterface} from "_Model/PositionInterface";
function move(user: User, x: number, y: number, positionNotifier: PositionNotifier): void {
positionNotifier.updatePosition(user, {
x,
y
}, user.position);
user.position.x = x;
user.position.y = y;
}
describe("PositionNotifier", () => { describe("PositionNotifier", () => {
it("should receive notifications when player moves", () => { it("should receive notifications when player moves", () => {
@ -37,14 +29,14 @@ describe("PositionNotifier", () => {
y: 500, y: 500,
moving: false, moving: false,
direction: 'down' direction: 'down'
}, false); }, false, positionNotifier);
const user2 = new User(2, { const user2 = new User(2, {
x: -9999, x: -9999,
y: -9999, y: -9999,
moving: false, moving: false,
direction: 'down' direction: 'down'
}, false); }, false, positionNotifier);
positionNotifier.setViewport(user1, { positionNotifier.setViewport(user1, {
left: 200, left: 200,
@ -53,21 +45,21 @@ describe("PositionNotifier", () => {
bottom: 500 bottom: 500
}); });
move(user2, 500, 500, positionNotifier); user2.setPosition({x: 500, y: 500, direction: 'down', moving: false});
expect(enterTriggered).toBe(true); expect(enterTriggered).toBe(true);
expect(moveTriggered).toBe(false); expect(moveTriggered).toBe(false);
enterTriggered = false; enterTriggered = false;
// Move inside the zone // Move inside the zone
move(user2, 501, 500, positionNotifier); user2.setPosition({x:501, y:500, direction: 'down', moving: false});
expect(enterTriggered).toBe(false); expect(enterTriggered).toBe(false);
expect(moveTriggered).toBe(true); expect(moveTriggered).toBe(true);
moveTriggered = false; moveTriggered = false;
// Move out of the zone in a zone that we don't track // Move out of the zone in a zone that we don't track
move(user2, 901, 500, positionNotifier); user2.setPosition({x: 901, y: 500, direction: 'down', moving: false});
expect(enterTriggered).toBe(false); expect(enterTriggered).toBe(false);
expect(moveTriggered).toBe(false); expect(moveTriggered).toBe(false);
@ -75,14 +67,14 @@ describe("PositionNotifier", () => {
leaveTriggered = false; leaveTriggered = false;
// Move back in // Move back in
move(user2, 500, 500, positionNotifier); user2.setPosition({x: 500, y: 500, direction: 'down', moving: false});
expect(enterTriggered).toBe(true); expect(enterTriggered).toBe(true);
expect(moveTriggered).toBe(false); expect(moveTriggered).toBe(false);
expect(leaveTriggered).toBe(false); expect(leaveTriggered).toBe(false);
enterTriggered = false; enterTriggered = false;
// Move out of the zone in a zone that we do track // Move out of the zone in a zone that we do track
move(user2, 200, 500, positionNotifier); user2.setPosition({x: 200, y: 500, direction: 'down', moving: false});
expect(enterTriggered).toBe(false); expect(enterTriggered).toBe(false);
expect(moveTriggered).toBe(true); expect(moveTriggered).toBe(true);
expect(leaveTriggered).toBe(false); expect(leaveTriggered).toBe(false);
@ -115,14 +107,14 @@ describe("PositionNotifier", () => {
y: 500, y: 500,
moving: false, moving: false,
direction: 'down' direction: 'down'
}, false); }, false, positionNotifier);
const user2 = new User(2, { const user2 = new User(2, {
x: -9999, x: 0,
y: -9999, y: 0,
moving: false, moving: false,
direction: 'down' direction: 'down'
}, false); }, false, positionNotifier);
let newUsers = positionNotifier.setViewport(user1, { let newUsers = positionNotifier.setViewport(user1, {
left: 200, left: 200,
@ -131,14 +123,16 @@ describe("PositionNotifier", () => {
bottom: 500 bottom: 500
}); });
expect(newUsers.length).toBe(0); expect(newUsers.length).toBe(2);
move(user2, 500, 500, positionNotifier);
expect(enterTriggered).toBe(true); expect(enterTriggered).toBe(true);
expect(moveTriggered).toBe(false);
enterTriggered = false; enterTriggered = false;
user2.setPosition({x: 500, y: 500, direction: 'down', moving: false});
expect(enterTriggered).toBe(false);
expect(moveTriggered).toBe(true);
moveTriggered = false;
// Move the viewport but the user stays inside. // Move the viewport but the user stays inside.
positionNotifier.setViewport(user1, { positionNotifier.setViewport(user1, {
left: 201, left: 201,
@ -176,6 +170,6 @@ describe("PositionNotifier", () => {
expect(moveTriggered).toBe(false); expect(moveTriggered).toBe(false);
expect(leaveTriggered).toBe(false); expect(leaveTriggered).toBe(false);
enterTriggered = false; enterTriggered = false;
expect(newUsers.length).toBe(1); expect(newUsers.length).toBe(2);
}); });
}) })