Refactoring events to connect/disconnect a user to a group
This commit is contained in:
parent
dcaf9a8e46
commit
58565a7f45
@ -1,4 +1,4 @@
|
|||||||
import { World } from "./World";
|
import { World, ConnectCallback, DisconnectCallback } from "./World";
|
||||||
import { UserInterface } from "./UserInterface";
|
import { UserInterface } from "./UserInterface";
|
||||||
import {PositionInterface} from "_Model/PositionInterface";
|
import {PositionInterface} from "_Model/PositionInterface";
|
||||||
|
|
||||||
@ -6,11 +6,11 @@ export class Group {
|
|||||||
static readonly MAX_PER_GROUP = 4;
|
static readonly MAX_PER_GROUP = 4;
|
||||||
|
|
||||||
private users: UserInterface[];
|
private users: UserInterface[];
|
||||||
private connectCallback: (user1: string, user2: string) => void;
|
private connectCallback: ConnectCallback;
|
||||||
private disconnectCallback: (user1: string, user2: string) => void;
|
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.users = [];
|
||||||
this.connectCallback = connectCallback;
|
this.connectCallback = connectCallback;
|
||||||
this.disconnectCallback = disconnectCallback;
|
this.disconnectCallback = disconnectCallback;
|
||||||
@ -54,9 +54,7 @@ export class Group {
|
|||||||
join(user: UserInterface): void
|
join(user: UserInterface): void
|
||||||
{
|
{
|
||||||
// Broadcast on the right event
|
// Broadcast on the right event
|
||||||
this.users.forEach((groupUser: UserInterface) => {
|
this.connectCallback(user.id, this);
|
||||||
this.connectCallback(user.id, groupUser.id);
|
|
||||||
});
|
|
||||||
this.users.push(user);
|
this.users.push(user);
|
||||||
user.group = this;
|
user.group = this;
|
||||||
}
|
}
|
||||||
@ -105,9 +103,7 @@ export class Group {
|
|||||||
user.group = undefined;
|
user.group = undefined;
|
||||||
|
|
||||||
// Broadcast on the right event
|
// Broadcast on the right event
|
||||||
this.users.forEach((groupUser: UserInterface) => {
|
this.disconnectCallback(user.id, this);
|
||||||
this.disconnectCallback(user.id, groupUser.id);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -6,6 +6,9 @@ import {UserInterface} from "./UserInterface";
|
|||||||
import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface";
|
import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface";
|
||||||
import {PositionInterface} from "_Model/PositionInterface";
|
import {PositionInterface} from "_Model/PositionInterface";
|
||||||
|
|
||||||
|
export type ConnectCallback = (user: string, group: Group) => void;
|
||||||
|
export type DisconnectCallback = (user: string, group: Group) => void;
|
||||||
|
|
||||||
export class World {
|
export class World {
|
||||||
static readonly MIN_DISTANCE = 160;
|
static readonly MIN_DISTANCE = 160;
|
||||||
|
|
||||||
@ -13,10 +16,10 @@ export class World {
|
|||||||
private users: Map<string, UserInterface>;
|
private users: Map<string, UserInterface>;
|
||||||
private groups: Group[];
|
private groups: Group[];
|
||||||
|
|
||||||
private connectCallback: (user1: string, user2: string) => void;
|
private connectCallback: ConnectCallback;
|
||||||
private disconnectCallback: (user1: string, user2: string) => void;
|
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.users = new Map<string, UserInterface>();
|
||||||
this.groups = [];
|
this.groups = [];
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import "jasmine";
|
import "jasmine";
|
||||||
import {Message} from "../src/Model/Websocket/Message";
|
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 {MessageUserPosition, Point} from "../src/Model/Websocket/MessageUserPosition";
|
||||||
import { Group } from "../src/Model/Group";
|
import { Group } from "../src/Model/Group";
|
||||||
import {Distance} from "../src/Model//Distance";
|
import {Distance} from "../src/Model//Distance";
|
||||||
@ -8,10 +8,10 @@ import {Distance} from "../src/Model//Distance";
|
|||||||
describe("World", () => {
|
describe("World", () => {
|
||||||
it("should connect user1 and user2", () => {
|
it("should connect user1 and user2", () => {
|
||||||
let connectCalled: boolean = false;
|
let connectCalled: boolean = false;
|
||||||
let connect = (user1: string, user2: string): void => {
|
let connect = (user: string, group: Group): void => {
|
||||||
connectCalled = true;
|
connectCalled = true;
|
||||||
}
|
}
|
||||||
let disconnect = (user1: string, user2: string): void => {
|
let disconnect = (user: string, group: Group): void => {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,10 +56,10 @@ describe("World", () => {
|
|||||||
|
|
||||||
it("should connect 3 users", () => {
|
it("should connect 3 users", () => {
|
||||||
let connectCalled: boolean = false;
|
let connectCalled: boolean = false;
|
||||||
let connect = (user1: string, user2: string): void => {
|
let connect = (user: string, group: Group): void => {
|
||||||
connectCalled = true;
|
connectCalled = true;
|
||||||
}
|
}
|
||||||
let disconnect = (user1: string, user2: string): void => {
|
let disconnect = (user: string, group: Group): void => {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,10 +101,10 @@ describe("World", () => {
|
|||||||
it("should disconnect user1 and user2", () => {
|
it("should disconnect user1 and user2", () => {
|
||||||
let connectCalled: boolean = false;
|
let connectCalled: boolean = false;
|
||||||
let disconnectCalled: boolean = false;
|
let disconnectCalled: boolean = false;
|
||||||
let connect = (user1: string, user2: string): void => {
|
let connect = (user: string, group: Group): void => {
|
||||||
connectCalled = true;
|
connectCalled = true;
|
||||||
}
|
}
|
||||||
let disconnect = (user1: string, user2: string): void => {
|
let disconnect = (user: string, group: Group): void => {
|
||||||
disconnectCalled = true;
|
disconnectCalled = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -142,51 +142,4 @@ describe("World", () => {
|
|||||||
expect(disconnectCalled).toBe(false);
|
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([]);
|
|
||||||
})
|
|
||||||
**/
|
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user