Refactoring World and Group to use Map and Set instead of arrays
This commit is contained in:
parent
981fa84aa7
commit
4fee1ac206
@ -7,13 +7,13 @@ export class Group {
|
|||||||
static readonly MAX_PER_GROUP = 4;
|
static readonly MAX_PER_GROUP = 4;
|
||||||
|
|
||||||
private id: string;
|
private id: string;
|
||||||
private users: UserInterface[];
|
private users: Set<UserInterface>;
|
||||||
private connectCallback: ConnectCallback;
|
private connectCallback: ConnectCallback;
|
||||||
private disconnectCallback: DisconnectCallback;
|
private disconnectCallback: DisconnectCallback;
|
||||||
|
|
||||||
|
|
||||||
constructor(users: UserInterface[], connectCallback: ConnectCallback, disconnectCallback: DisconnectCallback) {
|
constructor(users: UserInterface[], connectCallback: ConnectCallback, disconnectCallback: DisconnectCallback) {
|
||||||
this.users = [];
|
this.users = new Set<UserInterface>();
|
||||||
this.connectCallback = connectCallback;
|
this.connectCallback = connectCallback;
|
||||||
this.disconnectCallback = disconnectCallback;
|
this.disconnectCallback = disconnectCallback;
|
||||||
this.id = uuid();
|
this.id = uuid();
|
||||||
@ -24,7 +24,7 @@ export class Group {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getUsers(): UserInterface[] {
|
getUsers(): UserInterface[] {
|
||||||
return this.users;
|
return Array.from(this.users.values());
|
||||||
}
|
}
|
||||||
|
|
||||||
getId() : string{
|
getId() : string{
|
||||||
@ -42,8 +42,8 @@ export class Group {
|
|||||||
x += user.position.x;
|
x += user.position.x;
|
||||||
y += user.position.y;
|
y += user.position.y;
|
||||||
});
|
});
|
||||||
x /= this.users.length;
|
x /= this.users.size;
|
||||||
y /= this.users.length;
|
y /= this.users.size;
|
||||||
return {
|
return {
|
||||||
x,
|
x,
|
||||||
y
|
y
|
||||||
@ -51,45 +51,27 @@ export class Group {
|
|||||||
}
|
}
|
||||||
|
|
||||||
isFull(): boolean {
|
isFull(): boolean {
|
||||||
return this.users.length >= Group.MAX_PER_GROUP;
|
return this.users.size >= Group.MAX_PER_GROUP;
|
||||||
}
|
}
|
||||||
|
|
||||||
isEmpty(): boolean {
|
isEmpty(): boolean {
|
||||||
return this.users.length <= 1;
|
return this.users.size <= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
join(user: UserInterface): void
|
join(user: UserInterface): void
|
||||||
{
|
{
|
||||||
// Broadcast on the right event
|
// Broadcast on the right event
|
||||||
this.connectCallback(user.id, this);
|
this.connectCallback(user.id, this);
|
||||||
this.users.push(user);
|
this.users.add(user);
|
||||||
user.group = this;
|
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
|
leave(user: UserInterface): void
|
||||||
{
|
{
|
||||||
const index = this.users.indexOf(user, 0);
|
let success = this.users.delete(user);
|
||||||
if (index === -1) {
|
if (success === false) {
|
||||||
throw new Error("Could not find user in the group");
|
throw new Error("Could not find user "+user.id+" in the group "+this.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.users.splice(index, 1);
|
|
||||||
user.group = undefined;
|
user.group = undefined;
|
||||||
|
|
||||||
// Broadcast on the right event
|
// Broadcast on the right event
|
||||||
@ -102,8 +84,8 @@ export class Group {
|
|||||||
*/
|
*/
|
||||||
destroy(): void
|
destroy(): void
|
||||||
{
|
{
|
||||||
this.users.forEach((user: UserInterface) => {
|
for (let user of this.users) {
|
||||||
this.leave(user);
|
this.leave(user);
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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: Group[];
|
private readonly groups: Map<string, 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 = [];
|
this.groups = new Map<string, Group>();
|
||||||
this.connectCallback = connectCallback;
|
this.connectCallback = connectCallback;
|
||||||
this.disconnectCallback = disconnectCallback;
|
this.disconnectCallback = disconnectCallback;
|
||||||
this.minDistance = minDistance;
|
this.minDistance = minDistance;
|
||||||
@ -45,7 +45,7 @@ export class World {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public getGroups(): Group[] {
|
public getGroups(): Group[] {
|
||||||
return this.groups;
|
return Array.from(this.groups.values());
|
||||||
}
|
}
|
||||||
|
|
||||||
public getUsers(): Map<string, UserInterface> {
|
public getUsers(): Map<string, UserInterface> {
|
||||||
@ -99,7 +99,7 @@ export class World {
|
|||||||
user,
|
user,
|
||||||
closestUser
|
closestUser
|
||||||
], this.connectCallback, this.disconnectCallback);
|
], this.connectCallback, this.disconnectCallback);
|
||||||
this.groups.push(group);
|
this.groups.set(group.getId(), group);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,11 +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();
|
||||||
const index = this.groups.indexOf(group, 0);
|
if (!this.groups.has(group.getId())) {
|
||||||
if (index === -1) {
|
throw new Error("Could not find group "+group.getId()+" referenced by user "+user.id+" in World.");
|
||||||
throw new Error("Could not find group");
|
|
||||||
}
|
}
|
||||||
this.groups.splice(index, 1);
|
this.groups.delete(group.getId());
|
||||||
} else {
|
} else {
|
||||||
this.groupUpdatedCallback(group);
|
this.groupUpdatedCallback(group);
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
/* Basic Options */
|
/* Basic Options */
|
||||||
// "incremental": true, /* Enable incremental compilation */
|
// "incremental": true, /* Enable incremental compilation */
|
||||||
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
|
"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'. */
|
"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. */
|
// "lib": [], /* Specify library files to be included in the compilation. */
|
||||||
// "allowJs": true, /* Allow javascript files to be compiled. */
|
// "allowJs": true, /* Allow javascript files to be compiled. */
|
||||||
|
Loading…
Reference in New Issue
Block a user