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 {PositionInterface} from "_Model/PositionInterface";
|
||||
|
||||
@ -6,11 +6,11 @@ export class Group {
|
||||
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;
|
||||
@ -54,9 +54,7 @@ export class Group {
|
||||
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;
|
||||
}
|
||||
@ -105,9 +103,7 @@ export class Group {
|
||||
user.group = undefined;
|
||||
|
||||
// Broadcast on the right event
|
||||
this.users.forEach((groupUser: UserInterface) => {
|
||||
this.disconnectCallback(user.id, groupUser.id);
|
||||
});
|
||||
this.disconnectCallback(user.id, this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -6,6 +6,9 @@ import {UserInterface} from "./UserInterface";
|
||||
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;
|
||||
|
||||
@ -13,10 +16,10 @@ export class World {
|
||||
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 = [];
|
||||
|
@ -1,6 +1,6 @@
|
||||
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";
|
||||
@ -8,10 +8,10 @@ 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 => {
|
||||
|
||||
}
|
||||
|
||||
@ -56,10 +56,10 @@ describe("World", () => {
|
||||
|
||||
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 => {
|
||||
|
||||
}
|
||||
|
||||
@ -101,10 +101,10 @@ describe("World", () => {
|
||||
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;
|
||||
}
|
||||
|
||||
@ -142,51 +142,4 @@ describe("World", () => {
|
||||
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