2020-04-07 10:08:04 +02:00
|
|
|
import "jasmine";
|
2021-07-22 16:26:01 +02:00
|
|
|
import { ConnectCallback, DisconnectCallback, GameRoom } from "../src/Model/GameRoom";
|
|
|
|
import { Point } from "../src/Model/Websocket/MessageUserPosition";
|
|
|
|
import { Group } from "../src/Model/Group";
|
|
|
|
import { User, UserSocket } from "_Model/User";
|
|
|
|
import { JoinRoomMessage, PositionMessage } from "../src/Messages/generated/messages_pb";
|
2020-12-08 18:37:15 +01:00
|
|
|
import Direction = PositionMessage.Direction;
|
2021-07-22 16:26:01 +02:00
|
|
|
import { EmoteCallback } from "_Model/Zone";
|
2020-09-29 16:27:41 +02:00
|
|
|
|
2020-12-08 18:37:15 +01:00
|
|
|
function createMockUser(userId: number): User {
|
2020-09-29 16:27:41 +02:00
|
|
|
return {
|
2021-07-22 16:26:01 +02:00
|
|
|
userId,
|
2020-12-08 18:37:15 +01:00
|
|
|
} as unknown as User;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createMockUserSocket(): UserSocket {
|
2021-07-22 16:26:01 +02:00
|
|
|
return {} as unknown as UserSocket;
|
2020-12-08 18:37:15 +01:00
|
|
|
}
|
|
|
|
|
2021-07-22 16:26:01 +02:00
|
|
|
function createJoinRoomMessage(uuid: string, x: number, y: number): JoinRoomMessage {
|
2020-12-08 18:37:15 +01:00
|
|
|
const positionMessage = new PositionMessage();
|
|
|
|
positionMessage.setX(x);
|
|
|
|
positionMessage.setY(y);
|
|
|
|
positionMessage.setDirection(Direction.DOWN);
|
|
|
|
positionMessage.setMoving(false);
|
|
|
|
const joinRoomMessage = new JoinRoomMessage();
|
2021-07-22 16:26:01 +02:00
|
|
|
joinRoomMessage.setUseruuid("1");
|
|
|
|
joinRoomMessage.setIpaddress("10.0.0.2");
|
|
|
|
joinRoomMessage.setName("foo");
|
|
|
|
joinRoomMessage.setRoomid("_/global/test.json");
|
2020-12-08 18:37:15 +01:00
|
|
|
joinRoomMessage.setPositionmessage(positionMessage);
|
|
|
|
return joinRoomMessage;
|
2020-09-29 16:27:41 +02:00
|
|
|
}
|
2020-04-07 10:08:04 +02:00
|
|
|
|
2021-07-22 16:26:01 +02:00
|
|
|
const emote: EmoteCallback = (emoteEventMessage, listener): void => {};
|
2021-03-31 11:21:06 +02:00
|
|
|
|
2020-10-22 16:14:32 +02:00
|
|
|
describe("GameRoom", () => {
|
2021-07-19 15:57:50 +02:00
|
|
|
it("should connect user1 and user2", async () => {
|
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++;
|
2021-07-22 16:26:01 +02:00
|
|
|
};
|
|
|
|
const disconnect: DisconnectCallback = (user: User, group: Group): void => {};
|
2020-04-07 10:08:04 +02:00
|
|
|
|
2021-07-22 16:26:01 +02:00
|
|
|
const world = await GameRoom.create(
|
|
|
|
"https://play.workadventu.re/_/global/localhost/test.json",
|
|
|
|
connect,
|
|
|
|
disconnect,
|
|
|
|
160,
|
|
|
|
160,
|
|
|
|
() => {},
|
|
|
|
() => {},
|
|
|
|
() => {},
|
|
|
|
emote
|
|
|
|
);
|
2020-05-15 23:24:04 +02:00
|
|
|
|
2021-07-22 16:26:01 +02:00
|
|
|
const user1 = world.join(createMockUserSocket(), createJoinRoomMessage("1", 100, 100));
|
2020-05-15 23:24:04 +02:00
|
|
|
|
2021-07-22 16:26:01 +02:00
|
|
|
const user2 = world.join(createMockUserSocket(), createJoinRoomMessage("2", 500, 100));
|
2020-12-08 18:37:15 +01:00
|
|
|
|
|
|
|
world.updatePosition(user2, 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-12-08 18:37:15 +01:00
|
|
|
world.updatePosition(user2, 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-12-08 18:37:15 +01:00
|
|
|
world.updatePosition(user2, 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
|
|
|
|
2021-07-19 15:57:50 +02:00
|
|
|
it("should connect 3 users", async () => {
|
2020-04-29 23:12:55 +02:00
|
|
|
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;
|
2021-07-22 16:26:01 +02:00
|
|
|
};
|
|
|
|
const disconnect: DisconnectCallback = (user: User, group: Group): void => {};
|
2020-04-29 23:12:55 +02:00
|
|
|
|
2021-07-22 16:26:01 +02:00
|
|
|
const world = await GameRoom.create(
|
|
|
|
"https://play.workadventu.re/_/global/localhost/test.json",
|
|
|
|
connect,
|
|
|
|
disconnect,
|
|
|
|
160,
|
|
|
|
160,
|
|
|
|
() => {},
|
|
|
|
() => {},
|
|
|
|
() => {},
|
|
|
|
emote
|
|
|
|
);
|
2020-04-29 23:12:55 +02:00
|
|
|
|
2021-07-22 16:26:01 +02:00
|
|
|
const user1 = world.join(createMockUserSocket(), createJoinRoomMessage("1", 100, 100));
|
2020-04-29 23:12:55 +02:00
|
|
|
|
2021-07-22 16:26:01 +02:00
|
|
|
const user2 = world.join(createMockUserSocket(), createJoinRoomMessage("2", 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
|
2021-07-22 16:26:01 +02:00
|
|
|
const user3 = world.join(createMockUserSocket(), createJoinRoomMessage("2", 311, 100));
|
2020-04-29 23:12:55 +02:00
|
|
|
|
|
|
|
expect(connectCalled).toBe(false);
|
|
|
|
|
2020-12-08 18:37:15 +01:00
|
|
|
world.updatePosition(user3, new Point(309, 100));
|
2020-04-29 23:12:55 +02:00
|
|
|
|
|
|
|
expect(connectCalled).toBe(true);
|
|
|
|
});
|
|
|
|
|
2021-07-19 15:57:50 +02:00
|
|
|
it("should disconnect user1 and user2", async () => {
|
2020-04-28 23:01:56 +02:00
|
|
|
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;
|
2021-07-22 16:26:01 +02:00
|
|
|
};
|
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++;
|
2021-07-22 16:26:01 +02:00
|
|
|
};
|
2020-04-28 23:01:56 +02:00
|
|
|
|
2021-07-22 16:26:01 +02:00
|
|
|
const world = await GameRoom.create(
|
|
|
|
"https://play.workadventu.re/_/global/localhost/test.json",
|
|
|
|
connect,
|
|
|
|
disconnect,
|
|
|
|
160,
|
|
|
|
160,
|
|
|
|
() => {},
|
|
|
|
() => {},
|
|
|
|
() => {},
|
|
|
|
emote
|
|
|
|
);
|
2020-04-28 23:01:56 +02:00
|
|
|
|
2021-07-22 16:26:01 +02:00
|
|
|
const user1 = world.join(createMockUserSocket(), createJoinRoomMessage("1", 100, 100));
|
2020-04-28 23:01:56 +02:00
|
|
|
|
2021-07-22 16:26:01 +02:00
|
|
|
const user2 = world.join(createMockUserSocket(), createJoinRoomMessage("2", 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
|
|
|
|
2021-07-22 16:26:01 +02:00
|
|
|
world.updatePosition(user2, 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-12-08 18:37:15 +01:00
|
|
|
world.updatePosition(user2, new Point(262, 100));
|
2020-05-03 16:22:42 +02:00
|
|
|
expect(disconnectCallNumber).toBe(2);
|
2020-04-28 23:01:56 +02:00
|
|
|
});
|
2021-07-22 16:26:01 +02:00
|
|
|
});
|