2020-04-07 10:08:04 +02:00
|
|
|
import "jasmine";
|
2020-10-09 17:14:03 +02:00
|
|
|
import {GameRoom, ConnectCallback, DisconnectCallback } from "../src/Model/GameRoom";
|
2020-05-16 15:58:20 +02:00
|
|
|
import {Point} from "../src/Model/Websocket/MessageUserPosition";
|
2020-04-08 20:40:44 +02:00
|
|
|
import { Group } from "../src/Model/Group";
|
2020-09-29 16:27:41 +02:00
|
|
|
import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface";
|
|
|
|
import {User} from "_Model/User";
|
|
|
|
|
|
|
|
function createMockUser(userId: number): ExSocketInterface {
|
|
|
|
return {
|
|
|
|
userId
|
|
|
|
} as ExSocketInterface;
|
|
|
|
}
|
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-09-29 16:27:41 +02:00
|
|
|
const connect: ConnectCallback = (user: User, group: Group): void => {
|
2020-05-03 16:22:42 +02:00
|
|
|
connectCalledNumber++;
|
2020-04-07 10:08:04 +02:00
|
|
|
}
|
2020-09-29 16:27:41 +02:00
|
|
|
const disconnect: DisconnectCallback = (user: User, group: Group): void => {
|
2020-04-28 23:01:56 +02:00
|
|
|
|
2020-04-07 10:08:04 +02:00
|
|
|
}
|
|
|
|
|
2020-10-06 15:37:00 +02:00
|
|
|
const world = new GameRoom(connect, disconnect, 160, 160, () => {}, () => {}, () => {});
|
2020-04-07 10:08:04 +02:00
|
|
|
|
2020-09-29 16:27:41 +02:00
|
|
|
world.join(createMockUser(1), new Point(100, 100));
|
2020-05-15 23:24:04 +02:00
|
|
|
|
2020-09-29 16:27:41 +02:00
|
|
|
world.join(createMockUser(2), new Point(500, 100));
|
2020-05-15 23:24:04 +02:00
|
|
|
|
2020-09-18 13:57:38 +02:00
|
|
|
world.updatePosition({ userId: 2 }, new Point(261, 100));
|
2020-04-09 23:26:19 +02:00
|
|
|
|
2020-05-03 16:22:42 +02:00
|
|
|
expect(connectCalledNumber).toBe(0);
|
2020-04-09 23:26:19 +02:00
|
|
|
|
2020-09-18 13:57:38 +02:00
|
|
|
world.updatePosition({ userId: 2 }, new Point(101, 100));
|
2020-04-07 10:08:04 +02:00
|
|
|
|
2020-05-03 16:22:42 +02:00
|
|
|
expect(connectCalledNumber).toBe(2);
|
2020-04-07 10:08:04 +02:00
|
|
|
|
2020-09-18 13:57:38 +02:00
|
|
|
world.updatePosition({ userId: 2 }, 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-09-29 16:27:41 +02:00
|
|
|
const connect: ConnectCallback = (user: User, group: Group): void => {
|
2020-04-29 23:12:55 +02:00
|
|
|
connectCalled = true;
|
|
|
|
}
|
2020-09-29 16:27:41 +02:00
|
|
|
const disconnect: DisconnectCallback = (user: User, group: Group): void => {
|
2020-04-29 23:12:55 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-10-06 15:37:00 +02:00
|
|
|
const world = new GameRoom(connect, disconnect, 160, 160, () => {}, () => {}, () => {});
|
2020-04-29 23:12:55 +02:00
|
|
|
|
2020-09-29 16:27:41 +02:00
|
|
|
world.join(createMockUser(1), new Point(100, 100));
|
2020-04-29 23:12:55 +02:00
|
|
|
|
2020-09-29 16:27:41 +02:00
|
|
|
world.join(createMockUser(2), new Point(200, 100));
|
2020-04-29 23:12:55 +02:00
|
|
|
|
|
|
|
expect(connectCalled).toBe(true);
|
|
|
|
connectCalled = false;
|
|
|
|
|
|
|
|
// baz joins at the outer limit of the group
|
2020-09-29 16:27:41 +02:00
|
|
|
world.join(createMockUser(3), new Point(311, 100));
|
2020-04-29 23:12:55 +02:00
|
|
|
|
|
|
|
expect(connectCalled).toBe(false);
|
|
|
|
|
2020-09-18 13:57:38 +02:00
|
|
|
world.updatePosition({ userId: 3 }, new Point(309, 100));
|
2020-04-29 23:12:55 +02:00
|
|
|
|
|
|
|
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-09-29 16:27:41 +02:00
|
|
|
const connect: ConnectCallback = (user: User, group: Group): void => {
|
2020-04-28 23:01:56 +02:00
|
|
|
connectCalled = true;
|
|
|
|
}
|
2020-09-29 16:27:41 +02:00
|
|
|
const disconnect: DisconnectCallback = (user: User, group: Group): void => {
|
2020-05-03 16:22:42 +02:00
|
|
|
disconnectCallNumber++;
|
2020-04-28 23:01:56 +02:00
|
|
|
}
|
|
|
|
|
2020-10-06 15:37:00 +02:00
|
|
|
const world = new GameRoom(connect, disconnect, 160, 160, () => {}, () => {}, () => {});
|
2020-04-28 23:01:56 +02:00
|
|
|
|
2020-09-29 16:27:41 +02:00
|
|
|
world.join(createMockUser(1), new Point(100, 100));
|
2020-04-28 23:01:56 +02:00
|
|
|
|
2020-09-29 16:27:41 +02:00
|
|
|
world.join(createMockUser(2), new Point(259, 100));
|
2020-04-28 23:01:56 +02:00
|
|
|
|
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-09-18 13:57:38 +02:00
|
|
|
world.updatePosition({ userId: 2 }, 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-09-18 13:57:38 +02:00
|
|
|
world.updatePosition({ userId: 2 }, new Point(262, 100));
|
2020-05-03 16:22:42 +02:00
|
|
|
expect(disconnectCallNumber).toBe(2);
|
2020-04-28 23:01:56 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
})
|