Use user IDs instead of names

This commit is contained in:
PizZaKatZe 2021-12-15 14:48:45 +01:00
parent e528682403
commit 2bd71790b5
9 changed files with 48 additions and 57 deletions

View file

@ -96,11 +96,6 @@ export class GameRoom {
return this.users;
}
public getUserByName(name: string): User | undefined {
let foundUsers = Array.from(this.users.values());
foundUsers = foundUsers.filter((user: User) => user.name === name);
return foundUsers[0];
}
public getUserByUuid(uuid: string): User | undefined {
return this.usersByUuid.get(uuid);
}
@ -237,10 +232,6 @@ export class GameRoom {
});
}
public sendToUserWithName(name: string, message: ServerToClientMessage): void {
this.getUserByName(name)?.socket.write(message);
}
setSilent(user: User, silent: boolean) {
if (user.silent === silent) {
return;

View file

@ -25,7 +25,7 @@ export class User implements Movable {
public readonly IPAddress: string,
private position: PointInterface,
public silent: boolean,
public following: string[],
public following: number[],
private positionNotifier: PositionNotifier,
public readonly socket: UserSocket,
public readonly tags: string[],
@ -49,15 +49,15 @@ export class User implements Movable {
this.positionNotifier.updatePosition(this, position, oldPosition);
}
public addFollower(name: string): void {
if (this.following.includes(name)) {
public addFollower(userId: number): void {
if (this.following.includes(userId)) {
return;
}
this.following.push(name);
this.following.push(userId);
}
public delFollower(name: string): void {
const idx = this.following.indexOf(name);
public delFollower(userId: number): void {
const idx = this.following.indexOf(userId);
if (idx === -1) {
return;
}

View file

@ -846,16 +846,17 @@ export class SocketManager {
handleFollowConfirmationMessage(room: GameRoom, user: User, message: FollowConfirmationMessage) {
const clientMessage = new ServerToClientMessage();
clientMessage.setFollowconfirmationmessage(message);
room.sendToUserWithName(message.getLeader(), clientMessage);
const leader = room.getUserById(message.getLeader());
leader?.socket.write(clientMessage);
room.getUserByName(message.getLeader())?.addFollower(user.name);
leader?.addFollower(user.id);
user.addFollower(message.getLeader());
}
handleFollowAbortMessage(room: GameRoom, user: User, message: FollowAbortMessage) {
const clientMessage = new ServerToClientMessage();
clientMessage.setFollowabortmessage(message);
if (user.name === message.getLeader()) {
if (user.id === message.getLeader()) {
// Forward message
room.sendToOthersInGroupIncludingUser(user, clientMessage);
@ -865,10 +866,11 @@ export class SocketManager {
});
} else {
// Forward message
room.sendToUserWithName(message.getLeader(), clientMessage);
const leader = room.getUserById(message.getLeader());
leader?.socket.write(clientMessage);
// Update followers
room.getUserByName(message.getLeader())?.delFollower(user.name);
leader?.delFollower(user.id);
user.following = [];
}
}