2020-09-15 10:06:11 +02:00
|
|
|
import "jasmine";
|
2020-10-06 15:37:00 +02:00
|
|
|
import {GameRoom, ConnectCallback, DisconnectCallback } from "_Model/GameRoom";
|
2020-09-15 10:06:11 +02:00
|
|
|
import {Point} from "../src/Model/Websocket/MessageUserPosition";
|
|
|
|
import { Group } from "../src/Model/Group";
|
|
|
|
import {PositionNotifier} from "../src/Model/PositionNotifier";
|
2020-09-16 16:06:43 +02:00
|
|
|
import {User} from "../src/Model/User";
|
2020-09-15 10:06:11 +02:00
|
|
|
import {PointInterface} from "../src/Model/Websocket/PointInterface";
|
|
|
|
import {Zone} from "_Model/Zone";
|
2020-09-16 16:06:43 +02:00
|
|
|
import {Movable} from "_Model/Movable";
|
2020-09-16 16:10:20 +02:00
|
|
|
import {PositionInterface} from "_Model/PositionInterface";
|
2020-09-29 16:27:41 +02:00
|
|
|
import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface";
|
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;
|
|
|
|
|
2020-09-16 16:06:43 +02:00
|
|
|
const positionNotifier = new PositionNotifier(300, 300, (thing: Movable) => {
|
2020-09-15 10:06:11 +02:00
|
|
|
enterTriggered = true;
|
2020-09-16 16:10:20 +02:00
|
|
|
}, (thing: Movable, position: PositionInterface) => {
|
2020-09-15 10:06:11 +02:00
|
|
|
moveTriggered = true;
|
2020-09-16 16:06:43 +02:00
|
|
|
}, (thing: Movable) => {
|
2020-09-15 10:06:11 +02:00
|
|
|
leaveTriggered = true;
|
|
|
|
});
|
|
|
|
|
2020-10-16 14:36:43 +02:00
|
|
|
const user1 = new User(1, 'test', {
|
2020-09-16 16:06:43 +02:00
|
|
|
x: 500,
|
|
|
|
y: 500,
|
|
|
|
moving: false,
|
|
|
|
direction: 'down'
|
2020-09-29 16:27:41 +02:00
|
|
|
}, false, positionNotifier, {} as ExSocketInterface);
|
2020-09-16 16:06:43 +02:00
|
|
|
|
2020-10-16 14:36:43 +02:00
|
|
|
const user2 = new User(2, 'test', {
|
2020-09-16 16:06:43 +02:00
|
|
|
x: -9999,
|
|
|
|
y: -9999,
|
|
|
|
moving: false,
|
|
|
|
direction: 'down'
|
2020-09-29 16:27:41 +02:00
|
|
|
}, false, positionNotifier, {} as ExSocketInterface);
|
2020-09-15 10:06:11 +02:00
|
|
|
|
|
|
|
positionNotifier.setViewport(user1, {
|
|
|
|
left: 200,
|
|
|
|
right: 600,
|
|
|
|
top: 100,
|
|
|
|
bottom: 500
|
|
|
|
});
|
|
|
|
|
2020-09-25 15:25:06 +02: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
|
2020-09-25 15:25:06 +02: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
|
2020-09-25 15:25:06 +02: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
|
2020-09-25 15:25:06 +02: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;
|
|
|
|
|
|
|
|
// Move out of the zone in a zone that we do track
|
2020-09-25 15:25:06 +02:00
|
|
|
user2.setPosition({x: 200, y: 500, direction: 'down', moving: false});
|
2020-09-15 10:06:11 +02:00
|
|
|
expect(enterTriggered).toBe(false);
|
|
|
|
expect(moveTriggered).toBe(true);
|
|
|
|
expect(leaveTriggered).toBe(false);
|
|
|
|
moveTriggered = false;
|
|
|
|
|
|
|
|
// Leave the room
|
|
|
|
positionNotifier.leave(user2);
|
2020-09-16 16:06:43 +02: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;
|
|
|
|
|
2020-09-16 16:06:43 +02:00
|
|
|
const positionNotifier = new PositionNotifier(300, 300, (thing: Movable) => {
|
2020-09-15 10:06:11 +02:00
|
|
|
enterTriggered = true;
|
2020-09-16 16:10:20 +02:00
|
|
|
}, (thing: Movable, position: PositionInterface) => {
|
2020-09-15 10:06:11 +02:00
|
|
|
moveTriggered = true;
|
2020-09-16 16:06:43 +02:00
|
|
|
}, (thing: Movable) => {
|
2020-09-15 10:06:11 +02:00
|
|
|
leaveTriggered = true;
|
|
|
|
});
|
|
|
|
|
2020-10-16 14:36:43 +02:00
|
|
|
const user1 = new User(1, 'test', {
|
2020-09-16 16:06:43 +02:00
|
|
|
x: 500,
|
|
|
|
y: 500,
|
|
|
|
moving: false,
|
|
|
|
direction: 'down'
|
2020-09-29 16:27:41 +02:00
|
|
|
}, false, positionNotifier, {} as ExSocketInterface);
|
2020-09-16 16:06:43 +02:00
|
|
|
|
2020-10-16 14:36:43 +02:00
|
|
|
const user2 = new User(2, 'test', {
|
2020-09-25 15:25:06 +02:00
|
|
|
x: 0,
|
|
|
|
y: 0,
|
2020-09-16 16:06:43 +02:00
|
|
|
moving: false,
|
|
|
|
direction: 'down'
|
2020-09-29 16:27:41 +02:00
|
|
|
}, false, positionNotifier, {} as ExSocketInterface);
|
2020-09-15 10:06:11 +02:00
|
|
|
|
2020-09-15 16:21:41 +02:00
|
|
|
let newUsers = positionNotifier.setViewport(user1, {
|
2020-09-15 10:06:11 +02:00
|
|
|
left: 200,
|
|
|
|
right: 600,
|
|
|
|
top: 100,
|
|
|
|
bottom: 500
|
|
|
|
});
|
|
|
|
|
2020-09-25 15:25:06 +02:00
|
|
|
expect(newUsers.length).toBe(2);
|
2020-09-15 10:06:11 +02:00
|
|
|
expect(enterTriggered).toBe(true);
|
|
|
|
enterTriggered = false;
|
|
|
|
|
2020-09-25 15:25:06 +02:00
|
|
|
user2.setPosition({x: 500, y: 500, direction: 'down', moving: false});
|
|
|
|
|
|
|
|
expect(enterTriggered).toBe(false);
|
|
|
|
expect(moveTriggered).toBe(true);
|
|
|
|
moveTriggered = false;
|
|
|
|
|
2020-09-15 10:06:11 +02:00
|
|
|
// Move the viewport but the user stays inside.
|
|
|
|
positionNotifier.setViewport(user1, {
|
|
|
|
left: 201,
|
|
|
|
right: 601,
|
|
|
|
top: 100,
|
|
|
|
bottom: 500
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(enterTriggered).toBe(false);
|
|
|
|
expect(moveTriggered).toBe(false);
|
|
|
|
expect(leaveTriggered).toBe(false);
|
|
|
|
|
|
|
|
// Move the viewport out of the user.
|
|
|
|
positionNotifier.setViewport(user1, {
|
|
|
|
left: 901,
|
|
|
|
right: 1001,
|
|
|
|
top: 100,
|
|
|
|
bottom: 500
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(enterTriggered).toBe(false);
|
|
|
|
expect(moveTriggered).toBe(false);
|
|
|
|
expect(leaveTriggered).toBe(true);
|
|
|
|
leaveTriggered = false;
|
|
|
|
|
|
|
|
// Move the viewport back on the user.
|
2020-09-15 16:21:41 +02:00
|
|
|
newUsers = positionNotifier.setViewport(user1, {
|
2020-09-15 10:06:11 +02:00
|
|
|
left: 200,
|
|
|
|
right: 600,
|
|
|
|
top: 100,
|
|
|
|
bottom: 500
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(enterTriggered).toBe(true);
|
|
|
|
expect(moveTriggered).toBe(false);
|
|
|
|
expect(leaveTriggered).toBe(false);
|
|
|
|
enterTriggered = false;
|
2020-09-25 15:25:06 +02:00
|
|
|
expect(newUsers.length).toBe(2);
|
2020-09-15 10:06:11 +02:00
|
|
|
});
|
|
|
|
})
|