Using a Set for groups

This commit is contained in:
David Négrier 2020-06-29 22:13:07 +02:00
parent 4fee1ac206
commit 31846d1640
2 changed files with 7 additions and 7 deletions

View File

@ -68,7 +68,7 @@ export class Group {
leave(user: UserInterface): void leave(user: UserInterface): void
{ {
let success = this.users.delete(user); const success = this.users.delete(user);
if (success === false) { if (success === false) {
throw new Error("Could not find user "+user.id+" in the group "+this.id); throw new Error("Could not find user "+user.id+" in the group "+this.id);
} }
@ -84,7 +84,7 @@ export class Group {
*/ */
destroy(): void destroy(): void
{ {
for (let user of this.users) { for (const user of this.users) {
this.leave(user); this.leave(user);
} }
} }

View File

@ -20,7 +20,7 @@ export class World {
// Users, sorted by ID // Users, sorted by ID
private readonly users: Map<string, UserInterface>; private readonly users: Map<string, UserInterface>;
private readonly groups: Map<string, Group>; private readonly groups: Set<Group>;
private readonly connectCallback: ConnectCallback; private readonly connectCallback: ConnectCallback;
private readonly disconnectCallback: DisconnectCallback; private readonly disconnectCallback: DisconnectCallback;
@ -35,7 +35,7 @@ export class World {
groupDeletedCallback: GroupDeletedCallback) groupDeletedCallback: GroupDeletedCallback)
{ {
this.users = new Map<string, UserInterface>(); this.users = new Map<string, UserInterface>();
this.groups = new Map<string, Group>(); this.groups = new Set<Group>();
this.connectCallback = connectCallback; this.connectCallback = connectCallback;
this.disconnectCallback = disconnectCallback; this.disconnectCallback = disconnectCallback;
this.minDistance = minDistance; this.minDistance = minDistance;
@ -99,7 +99,7 @@ export class World {
user, user,
closestUser closestUser
], this.connectCallback, this.disconnectCallback); ], this.connectCallback, this.disconnectCallback);
this.groups.set(group.getId(), group); this.groups.add(group);
} }
} }
@ -132,10 +132,10 @@ export class World {
if (group.isEmpty()) { if (group.isEmpty()) {
this.groupDeletedCallback(group.getId(), user); this.groupDeletedCallback(group.getId(), user);
group.destroy(); group.destroy();
if (!this.groups.has(group.getId())) { if (!this.groups.has(group)) {
throw new Error("Could not find group "+group.getId()+" referenced by user "+user.id+" in World."); throw new Error("Could not find group "+group.getId()+" referenced by user "+user.id+" in World.");
} }
this.groups.delete(group.getId()); this.groups.delete(group);
} else { } else {
this.groupUpdatedCallback(group); this.groupUpdatedCallback(group);
} }