2020-04-07 10:08:04 +02:00
|
|
|
import "jasmine";
|
|
|
|
import {Message} from "../src/Model/Websocket/Message";
|
2020-05-03 16:08:04 +02:00
|
|
|
import {World, ConnectCallback, DisconnectCallback } from "../src/Model/World";
|
2020-04-07 10:08:04 +02:00
|
|
|
import {MessageUserPosition, Point} from "../src/Model/Websocket/MessageUserPosition";
|
2020-04-08 20:40:44 +02:00
|
|
|
import { Group } from "../src/Model/Group";
|
|
|
|
import {Distance} from "../src/Model//Distance";
|
2020-04-07 10:08:04 +02:00
|
|
|
|
|
|
|
describe("World", () => {
|
|
|
|
it("should connect user1 and user2", () => {
|
2020-05-03 16:22:42 +02:00
|
|
|
let connectCalledNumber: number = 0;
|
2020-05-03 16:08:04 +02:00
|
|
|
let connect = (user: string, group: Group): void => {
|
2020-05-03 16:22:42 +02:00
|
|
|
connectCalledNumber++;
|
2020-04-07 10:08:04 +02:00
|
|
|
}
|
2020-05-03 16:08:04 +02:00
|
|
|
let disconnect = (user: string, group: Group): void => {
|
2020-04-28 23:01:56 +02:00
|
|
|
|
2020-04-07 10:08:04 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 00:35:36 +02:00
|
|
|
let world = new World(connect, disconnect, 160, 160, () => {}, () => {});
|
2020-04-07 10:08:04 +02:00
|
|
|
|
2020-05-14 23:19:48 +02:00
|
|
|
world.join({ id: "foo" }, new MessageUserPosition({
|
|
|
|
userId: "foofoo",
|
2020-04-07 10:08:04 +02:00
|
|
|
roomId: 1,
|
|
|
|
position: new Point(100, 100)
|
|
|
|
}));
|
|
|
|
|
2020-05-14 23:19:48 +02:00
|
|
|
world.join({ id: "bar" }, new MessageUserPosition({
|
|
|
|
userId: "barbar",
|
2020-04-07 10:08:04 +02:00
|
|
|
roomId: 1,
|
|
|
|
position: new Point(500, 100)
|
|
|
|
}));
|
|
|
|
|
2020-05-14 23:19:48 +02:00
|
|
|
world.updatePosition({ id: "bar" }, new MessageUserPosition({
|
|
|
|
userId: "barbar",
|
2020-04-09 23:26:19 +02:00
|
|
|
roomId: 1,
|
|
|
|
position: new Point(261, 100)
|
|
|
|
}));
|
|
|
|
|
2020-05-03 16:22:42 +02:00
|
|
|
expect(connectCalledNumber).toBe(0);
|
2020-04-09 23:26:19 +02:00
|
|
|
|
2020-05-14 23:19:48 +02:00
|
|
|
world.updatePosition({ id: "bar" }, new MessageUserPosition({
|
|
|
|
userId: "barbar",
|
2020-04-07 10:08:04 +02:00
|
|
|
roomId: 1,
|
|
|
|
position: new Point(101, 100)
|
|
|
|
}));
|
|
|
|
|
2020-05-03 16:22:42 +02:00
|
|
|
expect(connectCalledNumber).toBe(2);
|
2020-04-07 10:08:04 +02:00
|
|
|
|
2020-05-14 23:19:48 +02:00
|
|
|
world.updatePosition({ id: "bar" }, new MessageUserPosition({
|
|
|
|
userId: "barbar",
|
2020-04-09 23:26:19 +02:00
|
|
|
roomId: 1,
|
|
|
|
position: new Point(102, 100)
|
|
|
|
}));
|
2020-05-03 16:22:42 +02:00
|
|
|
expect(connectCalledNumber).toBe(2);
|
2020-04-09 23:26:19 +02:00
|
|
|
});
|
2020-04-28 23:01:56 +02:00
|
|
|
|
2020-04-29 23:12:55 +02:00
|
|
|
it("should connect 3 users", () => {
|
|
|
|
let connectCalled: boolean = false;
|
2020-05-03 16:08:04 +02:00
|
|
|
let connect = (user: string, group: Group): void => {
|
2020-04-29 23:12:55 +02:00
|
|
|
connectCalled = true;
|
|
|
|
}
|
2020-05-03 16:08:04 +02:00
|
|
|
let disconnect = (user: string, group: Group): void => {
|
2020-04-29 23:12:55 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-08 00:35:36 +02:00
|
|
|
let world = new World(connect, disconnect, 160, 160, () => {}, () => {});
|
2020-04-29 23:12:55 +02:00
|
|
|
|
2020-05-14 23:19:48 +02:00
|
|
|
world.join({ id: "foo" }, new MessageUserPosition({
|
|
|
|
userId: "foofoo",
|
2020-04-29 23:12:55 +02:00
|
|
|
roomId: 1,
|
|
|
|
position: new Point(100, 100)
|
|
|
|
}));
|
|
|
|
|
2020-05-14 23:19:48 +02:00
|
|
|
world.join({ id: "bar" }, new MessageUserPosition({
|
|
|
|
userId: "barbar",
|
2020-04-29 23:12:55 +02:00
|
|
|
roomId: 1,
|
|
|
|
position: new Point(200, 100)
|
|
|
|
}));
|
|
|
|
|
|
|
|
expect(connectCalled).toBe(true);
|
|
|
|
connectCalled = false;
|
|
|
|
|
|
|
|
// baz joins at the outer limit of the group
|
2020-05-14 23:19:48 +02:00
|
|
|
world.join({ id: "baz" }, new MessageUserPosition({
|
|
|
|
userId: "bazbaz",
|
2020-04-29 23:12:55 +02:00
|
|
|
roomId: 1,
|
|
|
|
position: new Point(311, 100)
|
|
|
|
}));
|
|
|
|
|
|
|
|
expect(connectCalled).toBe(false);
|
|
|
|
|
2020-05-14 23:19:48 +02:00
|
|
|
world.updatePosition({ id: "baz" }, new MessageUserPosition({
|
|
|
|
userId: "bazbaz",
|
2020-04-29 23:12:55 +02:00
|
|
|
roomId: 1,
|
|
|
|
position: new Point(309, 100)
|
|
|
|
}));
|
|
|
|
|
|
|
|
expect(connectCalled).toBe(true);
|
|
|
|
});
|
|
|
|
|
2020-04-28 23:01:56 +02:00
|
|
|
it("should disconnect user1 and user2", () => {
|
|
|
|
let connectCalled: boolean = false;
|
2020-05-03 16:22:42 +02:00
|
|
|
let disconnectCallNumber: number = 0;
|
2020-05-03 16:08:04 +02:00
|
|
|
let connect = (user: string, group: Group): void => {
|
2020-04-28 23:01:56 +02:00
|
|
|
connectCalled = true;
|
|
|
|
}
|
2020-05-03 16:08:04 +02:00
|
|
|
let disconnect = (user: string, group: Group): void => {
|
2020-05-03 16:22:42 +02:00
|
|
|
disconnectCallNumber++;
|
2020-04-28 23:01:56 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 00:35:36 +02:00
|
|
|
let world = new World(connect, disconnect, 160, 160, () => {}, () => {});
|
2020-04-28 23:01:56 +02:00
|
|
|
|
2020-05-14 23:19:48 +02:00
|
|
|
world.join({ id: "foo" }, new MessageUserPosition({
|
|
|
|
userId: "foofoo",
|
2020-04-28 23:01:56 +02:00
|
|
|
roomId: 1,
|
|
|
|
position: new Point(100, 100)
|
|
|
|
}));
|
|
|
|
|
2020-05-14 23:19:48 +02:00
|
|
|
world.join({ id: "bar" }, new MessageUserPosition({
|
|
|
|
userId: "barbar",
|
2020-04-28 23:01:56 +02:00
|
|
|
roomId: 1,
|
|
|
|
position: new Point(259, 100)
|
|
|
|
}));
|
|
|
|
|
2020-04-29 23:12:55 +02:00
|
|
|
expect(connectCalled).toBe(true);
|
2020-05-03 16:22:42 +02:00
|
|
|
expect(disconnectCallNumber).toBe(0);
|
2020-04-28 23:01:56 +02:00
|
|
|
|
2020-05-14 23:19:48 +02:00
|
|
|
world.updatePosition({ id: "bar" }, new MessageUserPosition({
|
|
|
|
userId: "barbar",
|
2020-04-28 23:01:56 +02:00
|
|
|
roomId: 1,
|
2020-04-29 23:12:55 +02:00
|
|
|
position: new Point(100+160+160+1, 100)
|
2020-04-28 23:01:56 +02:00
|
|
|
}));
|
|
|
|
|
2020-05-03 16:22:42 +02:00
|
|
|
expect(disconnectCallNumber).toBe(2);
|
2020-04-28 23:01:56 +02:00
|
|
|
|
2020-05-14 23:19:48 +02:00
|
|
|
world.updatePosition({ id: "bar" }, new MessageUserPosition({
|
|
|
|
userId: "barbar",
|
2020-04-28 23:01:56 +02:00
|
|
|
roomId: 1,
|
|
|
|
position: new Point(262, 100)
|
|
|
|
}));
|
2020-05-03 16:22:42 +02:00
|
|
|
expect(disconnectCallNumber).toBe(2);
|
2020-04-28 23:01:56 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
})
|