unit test on connect is working, lets start the worst ....

This commit is contained in:
David MAECHLER 2020-04-09 23:26:19 +02:00
parent e3b0f99f04
commit 4bc23ede90
4 changed files with 143 additions and 30 deletions

View file

@ -1,15 +1,26 @@
import {MessageUserPosition} from "./Websocket/MessageUserPosition";
import { World } from "./World";
import { UserInterface } from "./UserInterface";
export class Group {
static readonly MAX_PER_GROUP = 4;
users: MessageUserPosition[];
private users: UserInterface[];
private connectCallback: (user1: string, user2: string) => void;
private disconnectCallback: (user1: string, user2: string) => void;
constructor(users: MessageUserPosition[]) {
this.users = users;
constructor(users: UserInterface[], connectCallback: (user1: string, user2: string) => void, disconnectCallback: (user1: string, user2: string) => void) {
this.users = [];
this.connectCallback = connectCallback;
this.disconnectCallback = disconnectCallback;
users.forEach((user: UserInterface) => {
this.join(user);
});
}
getUsers(): MessageUserPosition[] {
getUsers(): UserInterface[] {
return this.users;
}
@ -17,12 +28,22 @@ export class Group {
return this.users.length >= Group.MAX_PER_GROUP;
}
isPartOfGroup(user: MessageUserPosition): boolean
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
{
return this.users.indexOf(user) !== -1;
}
isStillIn(user: MessageUserPosition): boolean
isStillIn(user: UserInterface): boolean
{
if(!this.isPartOfGroup(user)) {
return false;
@ -30,7 +51,7 @@ export class Group {
let stillIn = true;
for(let i = 0; i <= this.users.length; i++) {
let userInGroup = this.users[i];
let distance = World.computeDistance(user.position, userInGroup.position);
let distance = World.computeDistance(user, userInGroup);
if(distance > World.MIN_DISTANCE) {
stillIn = false;
break;
@ -39,7 +60,7 @@ export class Group {
return stillIn;
}
removeFromGroup(users: MessageUserPosition[]): void
removeFromGroup(users: UserInterface[]): void
{
for(let i = 0; i < users.length; i++) {
let user = users[i];