Merge remote-tracking branch 'github.com/master' into webrtc

# Conflicts:
#	back/src/Model/Group.ts
#	back/src/Model/World.ts
This commit is contained in:
gparant 2020-05-02 00:36:04 +02:00
commit aff77fe074
9 changed files with 258 additions and 95 deletions

View file

@ -1,6 +1,6 @@
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 {
@ -34,17 +34,39 @@ export class Group {
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;
}
isEmpty(): boolean {
return this.users.length <= 1;
}
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);
}
});
this.users.push(user);
user.group = this;
}
@ -71,7 +93,7 @@ export class Group {
return stillIn;
}
removeFromGroup(users: UserInterface[]): void
/*removeFromGroup(users: UserInterface[]): void
{
for(let i = 0; i < users.length; i++){
let user = users[i];
@ -80,5 +102,32 @@ export class Group {
this.users.splice(index, 1);
}
}
}*/
leave(user: UserInterface): void
{
const index = this.users.indexOf(user, 0);
if (index === -1) {
throw new Error("Could not find user in the group");
}
this.users.splice(index, 1);
user.group = undefined;
// Broadcast on the right event
this.users.forEach((groupUser: UserInterface) => {
this.disconnectCallback(user.id, groupUser.id);
});
}
/**
* Let's kick everybody out.
* Usually used when there is only one user left.
*/
destroy(): void
{
this.users.forEach((user: UserInterface) => {
this.leave(user);
})
}
}