This commit is contained in:
David Négrier 2020-05-03 17:47:54 +02:00
commit 9c32c930a0
12 changed files with 374 additions and 343 deletions

View file

@ -1,19 +1,22 @@
import { World } from "./World";
import { World, ConnectCallback, DisconnectCallback } from "./World";
import { UserInterface } from "./UserInterface";
import {PositionInterface} from "_Model/PositionInterface";
import {uuid} from "uuidv4";
export class Group {
static readonly MAX_PER_GROUP = 4;
private id: string;
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;
this.id = uuid();
users.forEach((user: UserInterface) => {
this.join(user);
@ -24,6 +27,10 @@ export class Group {
return this.users;
}
getId() : string{
return this.id;
}
/**
* Returns the barycenter of all users (i.e. the center of the group)
*/
@ -54,9 +61,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;
}
@ -88,9 +93,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);
}
/**