diff --git a/back/src/Model/Group.ts b/back/src/Model/Group.ts index ed09b0cd..16cd5ffe 100644 --- a/back/src/Model/Group.ts +++ b/back/src/Model/Group.ts @@ -7,13 +7,13 @@ export class Group { static readonly MAX_PER_GROUP = 4; private id: string; - private users: UserInterface[]; + private users: Set; private connectCallback: ConnectCallback; private disconnectCallback: DisconnectCallback; constructor(users: UserInterface[], connectCallback: ConnectCallback, disconnectCallback: DisconnectCallback) { - this.users = []; + this.users = new Set(); this.connectCallback = connectCallback; this.disconnectCallback = disconnectCallback; this.id = uuid(); @@ -24,7 +24,7 @@ export class Group { } getUsers(): UserInterface[] { - return this.users; + return Array.from(this.users.values()); } getId() : string{ @@ -42,8 +42,8 @@ export class Group { x += user.position.x; y += user.position.y; }); - x /= this.users.length; - y /= this.users.length; + x /= this.users.size; + y /= this.users.size; return { x, y @@ -51,45 +51,27 @@ export class Group { } isFull(): boolean { - return this.users.length >= Group.MAX_PER_GROUP; + return this.users.size >= Group.MAX_PER_GROUP; } isEmpty(): boolean { - return this.users.length <= 1; + return this.users.size <= 1; } join(user: UserInterface): void { // Broadcast on the right event this.connectCallback(user.id, this); - this.users.push(user); + this.users.add(user); user.group = this; } - isPartOfGroup(user: UserInterface): boolean - { - return this.users.includes(user); - } - - /*removeFromGroup(users: UserInterface[]): void - { - for(let i = 0; i < users.length; i++){ - let user = users[i]; - const index = this.users.indexOf(user, 0); - if (index > -1) { - 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"); + let success = this.users.delete(user); + if (success === false) { + throw new Error("Could not find user "+user.id+" in the group "+this.id); } - - this.users.splice(index, 1); user.group = undefined; // Broadcast on the right event @@ -102,8 +84,8 @@ export class Group { */ destroy(): void { - this.users.forEach((user: UserInterface) => { + for (let user of this.users) { this.leave(user); - }) + } } } diff --git a/back/src/Model/World.ts b/back/src/Model/World.ts index 2cb92dd2..e4ce5c32 100644 --- a/back/src/Model/World.ts +++ b/back/src/Model/World.ts @@ -20,7 +20,7 @@ export class World { // Users, sorted by ID private readonly users: Map; - private readonly groups: Group[]; + private readonly groups: Map; private readonly connectCallback: ConnectCallback; private readonly disconnectCallback: DisconnectCallback; @@ -35,7 +35,7 @@ export class World { groupDeletedCallback: GroupDeletedCallback) { this.users = new Map(); - this.groups = []; + this.groups = new Map(); this.connectCallback = connectCallback; this.disconnectCallback = disconnectCallback; this.minDistance = minDistance; @@ -45,7 +45,7 @@ export class World { } public getGroups(): Group[] { - return this.groups; + return Array.from(this.groups.values()); } public getUsers(): Map { @@ -99,7 +99,7 @@ export class World { user, closestUser ], this.connectCallback, this.disconnectCallback); - this.groups.push(group); + this.groups.set(group.getId(), group); } } @@ -132,11 +132,10 @@ export class World { if (group.isEmpty()) { this.groupDeletedCallback(group.getId(), user); group.destroy(); - const index = this.groups.indexOf(group, 0); - if (index === -1) { - throw new Error("Could not find group"); + if (!this.groups.has(group.getId())) { + throw new Error("Could not find group "+group.getId()+" referenced by user "+user.id+" in World."); } - this.groups.splice(index, 1); + this.groups.delete(group.getId()); } else { this.groupUpdatedCallback(group); } diff --git a/back/tsconfig.json b/back/tsconfig.json index 51858b84..397bb8a2 100644 --- a/back/tsconfig.json +++ b/back/tsconfig.json @@ -4,6 +4,7 @@ /* Basic Options */ // "incremental": true, /* Enable incremental compilation */ "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */ + "downlevelIteration": true, "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ // "lib": [], /* Specify library files to be included in the compilation. */ // "allowJs": true, /* Allow javascript files to be compiled. */