Refactoring World and Group to use Map and Set instead of arrays
This commit is contained in:
parent
981fa84aa7
commit
4fee1ac206
3 changed files with 21 additions and 39 deletions
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue