2022-04-12 14:21:19 +02:00
|
|
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
2020-09-15 10:06:11 +02:00
|
|
|
import "jasmine";
|
2022-01-13 10:44:06 +01:00
|
|
|
import { PositionNotifier } from "../src/Model/PositionNotifier";
|
|
|
|
import { User, UserSocket } from "../src/Model/User";
|
2022-04-12 14:21:19 +02:00
|
|
|
import { Zone } from "../src/Model/Zone";
|
|
|
|
import { Movable } from "../src/Model/Movable";
|
|
|
|
import { PositionInterface } from "../src/Model/PositionInterface";
|
2022-01-13 10:44:06 +01:00
|
|
|
import { ZoneSocket } from "../src/RoomManager";
|
2020-09-15 10:06:11 +02:00
|
|
|
|
|
|
|
describe("PositionNotifier", () => {
|
|
|
|
it("should receive notifications when player moves", () => {
|
|
|
|
let enterTriggered = false;
|
|
|
|
let moveTriggered = false;
|
|
|
|
let leaveTriggered = false;
|
|
|
|
|
2022-01-13 10:44:06 +01:00
|
|
|
const positionNotifier = new PositionNotifier(
|
|
|
|
300,
|
|
|
|
300,
|
|
|
|
(thing: Movable) => {
|
|
|
|
enterTriggered = true;
|
|
|
|
},
|
|
|
|
(thing: Movable, position: PositionInterface) => {
|
|
|
|
moveTriggered = true;
|
|
|
|
},
|
|
|
|
(thing: Movable) => {
|
|
|
|
leaveTriggered = true;
|
|
|
|
},
|
|
|
|
() => {},
|
2022-03-02 10:44:26 +01:00
|
|
|
() => {},
|
2022-01-13 10:44:06 +01:00
|
|
|
() => {}
|
|
|
|
);
|
|
|
|
|
|
|
|
const user1 = new User(
|
|
|
|
1,
|
|
|
|
"test",
|
|
|
|
"10.0.0.2",
|
|
|
|
{
|
|
|
|
x: 500,
|
|
|
|
y: 500,
|
|
|
|
moving: false,
|
|
|
|
direction: "down",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
positionNotifier,
|
2022-04-07 14:23:53 +02:00
|
|
|
false,
|
2022-01-13 10:44:06 +01:00
|
|
|
{} as UserSocket,
|
|
|
|
[],
|
|
|
|
null,
|
|
|
|
"foo",
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
const user2 = new User(
|
|
|
|
2,
|
|
|
|
"test",
|
|
|
|
"10.0.0.2",
|
|
|
|
{
|
|
|
|
x: -9999,
|
|
|
|
y: -9999,
|
|
|
|
moving: false,
|
|
|
|
direction: "down",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
positionNotifier,
|
2022-04-07 14:23:53 +02:00
|
|
|
false,
|
2022-01-13 10:44:06 +01:00
|
|
|
{} as UserSocket,
|
|
|
|
[],
|
|
|
|
null,
|
|
|
|
"foo",
|
|
|
|
[]
|
|
|
|
);
|
2020-09-15 10:06:11 +02:00
|
|
|
|
2020-12-08 18:37:15 +01:00
|
|
|
positionNotifier.addZoneListener({} as ZoneSocket, 0, 0);
|
|
|
|
positionNotifier.addZoneListener({} as ZoneSocket, 0, 1);
|
|
|
|
positionNotifier.addZoneListener({} as ZoneSocket, 1, 1);
|
|
|
|
positionNotifier.addZoneListener({} as ZoneSocket, 1, 0);
|
|
|
|
/*positionNotifier.setViewport(user1, {
|
2020-09-15 10:06:11 +02:00
|
|
|
left: 200,
|
|
|
|
right: 600,
|
|
|
|
top: 100,
|
|
|
|
bottom: 500
|
2020-12-08 18:37:15 +01:00
|
|
|
});*/
|
2020-09-15 10:06:11 +02:00
|
|
|
|
2022-01-13 10:44:06 +01:00
|
|
|
user2.setPosition({ x: 500, y: 500, direction: "down", moving: false });
|
2020-09-15 10:06:11 +02:00
|
|
|
|
|
|
|
expect(enterTriggered).toBe(true);
|
|
|
|
expect(moveTriggered).toBe(false);
|
|
|
|
enterTriggered = false;
|
|
|
|
|
|
|
|
// Move inside the zone
|
2022-01-13 10:44:06 +01:00
|
|
|
user2.setPosition({ x: 501, y: 500, direction: "down", moving: false });
|
2020-09-15 10:06:11 +02:00
|
|
|
|
|
|
|
expect(enterTriggered).toBe(false);
|
|
|
|
expect(moveTriggered).toBe(true);
|
|
|
|
moveTriggered = false;
|
|
|
|
|
|
|
|
// Move out of the zone in a zone that we don't track
|
2022-01-13 10:44:06 +01:00
|
|
|
user2.setPosition({ x: 901, y: 500, direction: "down", moving: false });
|
2020-09-15 10:06:11 +02:00
|
|
|
|
|
|
|
expect(enterTriggered).toBe(false);
|
|
|
|
expect(moveTriggered).toBe(false);
|
|
|
|
expect(leaveTriggered).toBe(true);
|
|
|
|
leaveTriggered = false;
|
|
|
|
|
|
|
|
// Move back in
|
2022-01-13 10:44:06 +01:00
|
|
|
user2.setPosition({ x: 500, y: 500, direction: "down", moving: false });
|
2020-09-15 10:06:11 +02:00
|
|
|
expect(enterTriggered).toBe(true);
|
|
|
|
expect(moveTriggered).toBe(false);
|
|
|
|
expect(leaveTriggered).toBe(false);
|
|
|
|
enterTriggered = false;
|
|
|
|
|
|
|
|
// Leave the room
|
|
|
|
positionNotifier.leave(user2);
|
2020-12-08 18:37:15 +01:00
|
|
|
//positionNotifier.removeViewport(user2);
|
2020-09-15 10:06:11 +02:00
|
|
|
expect(enterTriggered).toBe(false);
|
|
|
|
expect(moveTriggered).toBe(false);
|
|
|
|
expect(leaveTriggered).toBe(true);
|
|
|
|
leaveTriggered = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should receive notifications when camera moves", () => {
|
|
|
|
let enterTriggered = false;
|
|
|
|
let moveTriggered = false;
|
|
|
|
let leaveTriggered = false;
|
|
|
|
|
2022-01-13 10:44:06 +01:00
|
|
|
const positionNotifier = new PositionNotifier(
|
|
|
|
300,
|
|
|
|
300,
|
|
|
|
(thing: Movable, fromZone: Zone | null) => {
|
|
|
|
enterTriggered = true;
|
|
|
|
},
|
|
|
|
(thing: Movable, position: PositionInterface) => {
|
|
|
|
moveTriggered = true;
|
|
|
|
},
|
|
|
|
(thing: Movable) => {
|
|
|
|
leaveTriggered = true;
|
|
|
|
},
|
|
|
|
() => {},
|
2022-03-02 10:44:26 +01:00
|
|
|
() => {},
|
2022-01-13 10:44:06 +01:00
|
|
|
() => {}
|
|
|
|
);
|
|
|
|
|
|
|
|
const user1 = new User(
|
|
|
|
1,
|
|
|
|
"test",
|
|
|
|
"10.0.0.2",
|
|
|
|
{
|
|
|
|
x: 500,
|
|
|
|
y: 500,
|
|
|
|
moving: false,
|
|
|
|
direction: "down",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
positionNotifier,
|
2022-04-07 14:23:53 +02:00
|
|
|
false,
|
2022-01-13 10:44:06 +01:00
|
|
|
{} as UserSocket,
|
|
|
|
[],
|
|
|
|
null,
|
|
|
|
"foo",
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
const user2 = new User(
|
|
|
|
2,
|
|
|
|
"test",
|
|
|
|
"10.0.0.2",
|
|
|
|
{
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
moving: false,
|
|
|
|
direction: "down",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
positionNotifier,
|
2022-04-07 14:23:53 +02:00
|
|
|
false,
|
2022-01-13 10:44:06 +01:00
|
|
|
{} as UserSocket,
|
|
|
|
[],
|
|
|
|
null,
|
|
|
|
"foo",
|
|
|
|
[]
|
|
|
|
);
|
2020-12-08 18:37:15 +01:00
|
|
|
|
|
|
|
const listener = {} as ZoneSocket;
|
|
|
|
positionNotifier.addZoneListener(listener, 0, 0);
|
|
|
|
positionNotifier.addZoneListener(listener, 0, 1);
|
|
|
|
positionNotifier.addZoneListener(listener, 1, 1);
|
|
|
|
positionNotifier.addZoneListener(listener, 1, 0);
|
|
|
|
/*let newUsers = positionNotifier.setViewport(user1, {
|
2020-09-15 10:06:11 +02:00
|
|
|
left: 200,
|
|
|
|
right: 600,
|
|
|
|
top: 100,
|
|
|
|
bottom: 500
|
2020-12-08 18:37:15 +01:00
|
|
|
});*/
|
|
|
|
positionNotifier.enter(user1);
|
|
|
|
positionNotifier.enter(user2);
|
2020-09-15 10:06:11 +02:00
|
|
|
|
2020-12-08 18:37:15 +01:00
|
|
|
//expect(newUsers.length).toBe(2);
|
2020-09-15 10:06:11 +02:00
|
|
|
expect(enterTriggered).toBe(true);
|
|
|
|
enterTriggered = false;
|
|
|
|
|
2020-12-08 18:37:15 +01:00
|
|
|
//positionNotifier.updatePosition(user2, {x:500, y:500}, {x:0, y: 0})
|
2022-01-13 10:44:06 +01:00
|
|
|
user2.setPosition({ x: 500, y: 500, direction: "down", moving: false });
|
2020-09-25 15:25:06 +02:00
|
|
|
|
2020-12-08 18:37:15 +01:00
|
|
|
expect(enterTriggered).toBe(true);
|
|
|
|
expect(moveTriggered).toBe(false);
|
|
|
|
expect(leaveTriggered).toBe(true);
|
|
|
|
enterTriggered = false;
|
|
|
|
leaveTriggered = false;
|
|
|
|
|
|
|
|
// Add a listener, but the user in not in this zone.
|
|
|
|
positionNotifier.addZoneListener(listener, 10, 10);
|
2020-09-25 15:25:06 +02:00
|
|
|
|
2020-12-08 18:37:15 +01:00
|
|
|
/*positionNotifier.setViewport(user1, {
|
2020-09-15 10:06:11 +02:00
|
|
|
left: 201,
|
|
|
|
right: 601,
|
|
|
|
top: 100,
|
|
|
|
bottom: 500
|
2020-12-08 18:37:15 +01:00
|
|
|
});*/
|
2020-09-15 10:06:11 +02:00
|
|
|
|
|
|
|
expect(enterTriggered).toBe(false);
|
|
|
|
expect(moveTriggered).toBe(false);
|
|
|
|
expect(leaveTriggered).toBe(false);
|
|
|
|
|
2020-12-08 18:37:15 +01:00
|
|
|
// Stop listening to zone
|
|
|
|
positionNotifier.removeZoneListener(listener, 1, 1);
|
2020-09-15 10:06:11 +02:00
|
|
|
// Move the viewport out of the user.
|
2020-12-08 18:37:15 +01:00
|
|
|
/*positionNotifier.setViewport(user1, {
|
2020-09-15 10:06:11 +02:00
|
|
|
left: 901,
|
|
|
|
right: 1001,
|
|
|
|
top: 100,
|
|
|
|
bottom: 500
|
2020-12-08 18:37:15 +01:00
|
|
|
});*/
|
2020-09-15 10:06:11 +02:00
|
|
|
|
|
|
|
expect(enterTriggered).toBe(false);
|
|
|
|
expect(moveTriggered).toBe(false);
|
2020-12-08 18:37:15 +01:00
|
|
|
expect(leaveTriggered).toBe(false);
|
2020-09-15 10:06:11 +02:00
|
|
|
|
|
|
|
// Move the viewport back on the user.
|
2020-12-08 18:37:15 +01:00
|
|
|
positionNotifier.addZoneListener(listener, 1, 1);
|
|
|
|
/*newUsers = positionNotifier.setViewport(user1, {
|
2020-09-15 10:06:11 +02:00
|
|
|
left: 200,
|
|
|
|
right: 600,
|
|
|
|
top: 100,
|
|
|
|
bottom: 500
|
2020-12-08 18:37:15 +01:00
|
|
|
});*/
|
2020-09-15 10:06:11 +02:00
|
|
|
|
2020-12-08 18:37:15 +01:00
|
|
|
expect(enterTriggered).toBe(false);
|
2020-09-15 10:06:11 +02:00
|
|
|
expect(moveTriggered).toBe(false);
|
|
|
|
expect(leaveTriggered).toBe(false);
|
|
|
|
enterTriggered = false;
|
2020-12-08 18:37:15 +01:00
|
|
|
//expect(newUsers.length).toBe(2);
|
2020-09-15 10:06:11 +02:00
|
|
|
});
|
2022-01-13 10:44:06 +01:00
|
|
|
});
|