2020-04-08 20:40:44 +02:00
|
|
|
import { World } from "./World";
|
2020-04-09 23:26:19 +02:00
|
|
|
import { UserInterface } from "./UserInterface";
|
2020-04-29 22:41:48 +02:00
|
|
|
import {PositionInterface} from "_Model/PositionInterface";
|
2020-04-09 23:26:19 +02:00
|
|
|
|
2020-04-07 10:08:04 +02:00
|
|
|
export class Group {
|
|
|
|
static readonly MAX_PER_GROUP = 4;
|
2020-04-28 22:40:54 +02:00
|
|
|
|
2020-04-09 23:26:19 +02:00
|
|
|
private users: UserInterface[];
|
|
|
|
private connectCallback: (user1: string, user2: string) => void;
|
|
|
|
private disconnectCallback: (user1: string, user2: string) => void;
|
|
|
|
|
2020-04-07 10:08:04 +02:00
|
|
|
|
2020-04-09 23:26:19 +02:00
|
|
|
constructor(users: UserInterface[], connectCallback: (user1: string, user2: string) => void, disconnectCallback: (user1: string, user2: string) => void) {
|
|
|
|
this.users = [];
|
|
|
|
this.connectCallback = connectCallback;
|
|
|
|
this.disconnectCallback = disconnectCallback;
|
2020-04-28 22:40:54 +02:00
|
|
|
|
2020-04-09 23:26:19 +02:00
|
|
|
users.forEach((user: UserInterface) => {
|
|
|
|
this.join(user);
|
|
|
|
});
|
2020-04-07 10:08:04 +02:00
|
|
|
}
|
|
|
|
|
2020-04-09 23:26:19 +02:00
|
|
|
getUsers(): UserInterface[] {
|
2020-04-07 10:08:04 +02:00
|
|
|
return this.users;
|
|
|
|
}
|
|
|
|
|
2020-04-29 22:41:48 +02:00
|
|
|
/**
|
|
|
|
* Returns the barycenter of all users (i.e. the center of the group)
|
|
|
|
*/
|
|
|
|
getPosition(): PositionInterface {
|
|
|
|
let x = 0;
|
|
|
|
let y = 0;
|
|
|
|
// Let's compute the barycenter of all users.
|
|
|
|
this.users.forEach((user: UserInterface) => {
|
|
|
|
x += user.position.x;
|
|
|
|
y += user.position.y;
|
|
|
|
});
|
|
|
|
x /= this.users.length;
|
|
|
|
y /= this.users.length;
|
|
|
|
return {
|
|
|
|
x,
|
|
|
|
y
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-07 10:08:04 +02:00
|
|
|
isFull(): boolean {
|
|
|
|
return this.users.length >= Group.MAX_PER_GROUP;
|
|
|
|
}
|
2020-04-08 20:40:44 +02:00
|
|
|
|
2020-04-09 23:26:19 +02:00
|
|
|
join(user: UserInterface): void
|
|
|
|
{
|
|
|
|
// Broadcast on the right event
|
|
|
|
this.users.forEach((groupUser: UserInterface) => {
|
|
|
|
this.connectCallback(user.id, groupUser.id);
|
|
|
|
});
|
|
|
|
this.users.push(user);
|
|
|
|
user.group = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
isPartOfGroup(user: UserInterface): boolean
|
2020-04-08 20:40:44 +02:00
|
|
|
{
|
|
|
|
return this.users.indexOf(user) !== -1;
|
|
|
|
}
|
|
|
|
|
2020-04-09 23:26:19 +02:00
|
|
|
isStillIn(user: UserInterface): boolean
|
2020-04-08 20:40:44 +02:00
|
|
|
{
|
|
|
|
if(!this.isPartOfGroup(user)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
let stillIn = true;
|
|
|
|
for(let i = 0; i <= this.users.length; i++) {
|
|
|
|
let userInGroup = this.users[i];
|
2020-04-09 23:26:19 +02:00
|
|
|
let distance = World.computeDistance(user, userInGroup);
|
2020-04-08 20:40:44 +02:00
|
|
|
if(distance > World.MIN_DISTANCE) {
|
|
|
|
stillIn = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return stillIn;
|
|
|
|
}
|
|
|
|
|
2020-04-09 23:26:19 +02:00
|
|
|
removeFromGroup(users: UserInterface[]): void
|
2020-04-08 20:40:44 +02:00
|
|
|
{
|
2020-04-28 22:40:54 +02:00
|
|
|
for(let i = 0; i < users.length; i++){
|
2020-04-08 20:40:44 +02:00
|
|
|
let user = users[i];
|
|
|
|
const index = this.users.indexOf(user, 0);
|
|
|
|
if (index > -1) {
|
|
|
|
this.users.splice(index, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-28 22:40:54 +02:00
|
|
|
}
|