Refactoring World and Group to use Map and Set instead of arrays

This commit is contained in:
David Négrier 2020-06-29 22:10:23 +02:00
parent 981fa84aa7
commit 4fee1ac206
3 changed files with 21 additions and 39 deletions

View File

@ -7,13 +7,13 @@ export class Group {
static readonly MAX_PER_GROUP = 4;
private id: string;
private users: UserInterface[];
private users: Set<UserInterface>;
private connectCallback: ConnectCallback;
private disconnectCallback: DisconnectCallback;
constructor(users: UserInterface[], connectCallback: ConnectCallback, disconnectCallback: DisconnectCallback) {
this.users = [];
this.users = new Set<UserInterface>();
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);
})
}
}
}

View File

@ -20,7 +20,7 @@ export class World {
// Users, sorted by ID
private readonly users: Map<string, UserInterface>;
private readonly groups: Group[];
private readonly groups: Map<string, Group>;
private readonly connectCallback: ConnectCallback;
private readonly disconnectCallback: DisconnectCallback;
@ -35,7 +35,7 @@ export class World {
groupDeletedCallback: GroupDeletedCallback)
{
this.users = new Map<string, UserInterface>();
this.groups = [];
this.groups = new Map<string, Group>();
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<string, UserInterface> {
@ -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);
}

View File

@ -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. */