- import {MessageUserPosition} from "./Websocket/MessageUserPosition";
import { World } from "./World";
import { UserInterface } from "./UserInterface";
+ import {PositionInterface} from "_Model/PositionInterface";
+import {uuid} from "uuidv4";
export class Group {
static readonly MAX_PER_GROUP = 4;
return this.users;
}
+ getId() : string{
+ return this.id;
+ }
+
+ /**
+ * 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
+ };
+ }
+
isFull(): boolean {
return this.users.length >= Group.MAX_PER_GROUP;
}
join(user: UserInterface): void
{
// Broadcast on the right event
- for(let i = 0; i < this.users.length; i++){
- let groupUser : UserInterface = this.users[i];
+ this.users.forEach((groupUser: UserInterface) => {
- this.connectCallback(user.id, groupUser.id);
+ this.connectCallback(user.id, groupUser.id, this);
- }
+ });
this.users.push(user);
user.group = this;
}