-import { World } from "./World";
+import { World, ConnectCallback, DisconnectCallback } from "./World";
import { UserInterface } from "./UserInterface";
import {PositionInterface} from "_Model/PositionInterface";
static readonly MAX_PER_GROUP = 4;
private users: UserInterface[];
- private connectCallback: (user1: string, user2: string) => void;
- private disconnectCallback: (user1: string, user2: string) => void;
+ private connectCallback: ConnectCallback;
+ private disconnectCallback: DisconnectCallback;
- constructor(users: UserInterface[], connectCallback: (user1: string, user2: string) => void, disconnectCallback: (user1: string, user2: string) => void) {
+ constructor(users: UserInterface[], connectCallback: ConnectCallback, disconnectCallback: DisconnectCallback) {
this.users = [];
this.connectCallback = connectCallback;
this.disconnectCallback = disconnectCallback;
join(user: UserInterface): void
{
// Broadcast on the right event
- this.users.forEach((groupUser: UserInterface) => {
- this.connectCallback(user.id, groupUser.id);
- });
+ this.connectCallback(user.id, this);
this.users.push(user);
user.group = this;
}
user.group = undefined;
// Broadcast on the right event
- this.users.forEach((groupUser: UserInterface) => {
- this.disconnectCallback(user.id, groupUser.id);
- });
+ this.disconnectCallback(user.id, this);
}
/**
import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface";
import {PositionInterface} from "_Model/PositionInterface";
+export type ConnectCallback = (user: string, group: Group) => void;
+export type DisconnectCallback = (user: string, group: Group) => void;
+
export class World {
static readonly MIN_DISTANCE = 160;
private users: Map<string, UserInterface>;
private groups: Group[];
- private connectCallback: (user1: string, user2: string) => void;
- private disconnectCallback: (user1: string, user2: string) => void;
+ private connectCallback: ConnectCallback;
+ private disconnectCallback: DisconnectCallback;
- constructor(connectCallback: (user1: string, user2: string) => void, disconnectCallback: (user1: string, user2: string) => void)
+ constructor(connectCallback: ConnectCallback, disconnectCallback: DisconnectCallback)
{
this.users = new Map<string, UserInterface>();
this.groups = [];
import "jasmine";
import {Message} from "../src/Model/Websocket/Message";
-import {World} from "../src/Model/World";
+import {World, ConnectCallback, DisconnectCallback } from "../src/Model/World";
import {MessageUserPosition, Point} from "../src/Model/Websocket/MessageUserPosition";
import { Group } from "../src/Model/Group";
import {Distance} from "../src/Model//Distance";
describe("World", () => {
it("should connect user1 and user2", () => {
let connectCalled: boolean = false;
- let connect = (user1: string, user2: string): void => {
+ let connect = (user: string, group: Group): void => {
connectCalled = true;
}
- let disconnect = (user1: string, user2: string): void => {
+ let disconnect = (user: string, group: Group): void => {
}
it("should connect 3 users", () => {
let connectCalled: boolean = false;
- let connect = (user1: string, user2: string): void => {
+ let connect = (user: string, group: Group): void => {
connectCalled = true;
}
- let disconnect = (user1: string, user2: string): void => {
+ let disconnect = (user: string, group: Group): void => {
}
it("should disconnect user1 and user2", () => {
let connectCalled: boolean = false;
let disconnectCalled: boolean = false;
- let connect = (user1: string, user2: string): void => {
+ let connect = (user: string, group: Group): void => {
connectCalled = true;
}
- let disconnect = (user1: string, user2: string): void => {
+ let disconnect = (user: string, group: Group): void => {
disconnectCalled = true;
}
expect(disconnectCalled).toBe(false);
});
- /**
- it('Should return the distances between all users', () => {
- let connectCalled: boolean = false;
- let connect = (user1: string, user2: string): void => {
- connectCalled = true;
- }
- let disconnect = (user1: string, user2: string): void => {
-
- }
-
- let world = new World(connect, disconnect);
- let user1 = new MessageUserPosition({
- userId: "foo",
- roomId: 1,
- position: new Point(100, 100)
- });
-
- world.join(user1);
-
- let user2 = new MessageUserPosition({
- userId: "bar",
- roomId: 1,
- position: new Point(500, 100)
- });
- world.join(user2);
-
- let user3 = new MessageUserPosition({
- userId: "baz",
- roomId: 1,
- position: new Point(101, 100)
- });
-
- let user4 = new MessageUserPosition({
- userId: "buz",
- roomId: 1,
- position: new Point(105, 100)
- })
-
- let group = new Group([user1, user2, user3, user4]);
-
- let distances = world.getDistancesBetweenGroupUsers(group)
-
- console.log(distances);
-
- //expect(distances).toBe([]);
- })
- **/
})