diff --git a/back/src/Model/GameRoom.ts b/back/src/Model/GameRoom.ts index 9b0fef45..e7f23549 100644 --- a/back/src/Model/GameRoom.ts +++ b/back/src/Model/GameRoom.ts @@ -224,19 +224,18 @@ export class GameRoom { } else { // If the user is part of a group: // should he leave the group? - const leaveIfOutOfRadius = (user: User) => { - if (user.group === undefined) { + const users = user.group.getUsers().filter((u) => !u.hasFollowers() && !u.following); + users.forEach((foreignUser: User) => { + if (foreignUser.group === undefined) { return; } - const usrPos = user.getPosition(); - const grpPos = user.group.getPosition(); + const usrPos = foreignUser.getPosition(); + const grpPos = foreignUser.group.getPosition(); const distance = GameRoom.computeDistanceBetweenPositions(usrPos, grpPos); if (distance > this.groupRadius) { - this.leaveGroup(user); + this.leaveGroup(foreignUser); } - }; - const users = user.group.getUsers().filter((u) => !u.hasFollowers() && !u.following); - users.forEach((foreignUser) => leaveIfOutOfRadius(foreignUser)); + }); } } diff --git a/back/src/Model/Group.ts b/back/src/Model/Group.ts index db9f6305..08d1d21e 100644 --- a/back/src/Model/Group.ts +++ b/back/src/Model/Group.ts @@ -156,7 +156,7 @@ export class Group implements Movable { /** * A group can have at most one person leading the way in it. */ - get leader(): User|undefined { + get leader(): User | undefined { for (const user of this.users) { if (user.hasFollowers()) { return user; diff --git a/back/src/Model/User.ts b/back/src/Model/User.ts index 4619ce2c..50f140fc 100644 --- a/back/src/Model/User.ts +++ b/back/src/Model/User.ts @@ -6,7 +6,9 @@ import { PositionNotifier } from "_Model/PositionNotifier"; import { ServerDuplexStream } from "grpc"; import { BatchMessage, - CompanionMessage, FollowAbortMessage, FollowConfirmationMessage, + CompanionMessage, + FollowAbortMessage, + FollowConfirmationMessage, PusherToBackMessage, ServerToClientMessage, SubMessage, @@ -18,7 +20,7 @@ export type UserSocket = ServerDuplexStream; public group?: Group; - private _following: User|undefined; + private _following: User | undefined; private followedBy: Set = new Set(); public constructor( diff --git a/back/src/Services/SocketManager.ts b/back/src/Services/SocketManager.ts index d7178d3d..bafce4d3 100644 --- a/back/src/Services/SocketManager.ts +++ b/back/src/Services/SocketManager.ts @@ -846,12 +846,13 @@ export class SocketManager { handleFollowConfirmationMessage(room: GameRoom, user: User, message: FollowConfirmationMessage) { const leader = room.getUserById(message.getLeader()); if (!leader) { - console.info('Could not find user "', message.getLeader(), '" while handling a follow confirmation in room "', room.roomUrl,'". Maybe the user just left.'); + const message = `Could not follow user "{message.getLeader()}" in room "{room.roomUrl}".`; + console.info(message, "Maybe the user just left."); return; } - // By security, we look at the group leader. If the group leader is NOT the leader in the message, everybody should - // stop following the group leader (to avoid having 2 group leaders) + // By security, we look at the group leader. If the group leader is NOT the leader in the message, + // everybody should stop following the group leader (to avoid having 2 group leaders) if (user?.group?.leader && user?.group?.leader !== leader) { user?.group?.leader?.stopLeading(); } diff --git a/back/tests/PositionNotifierTest.ts b/back/tests/PositionNotifierTest.ts index 955ed40f..1aaf2e13 100644 --- a/back/tests/PositionNotifierTest.ts +++ b/back/tests/PositionNotifierTest.ts @@ -26,14 +26,14 @@ describe("PositionNotifier", () => { y: 500, moving: false, direction: 'down' - }, false, [], positionNotifier, {} as UserSocket, [], null, 'foo', []); + }, false, positionNotifier, {} as UserSocket, [], null, 'foo', []); const user2 = new User(2, 'test', '10.0.0.2', { x: -9999, y: -9999, moving: false, direction: 'down' - }, false, [], positionNotifier, {} as UserSocket, [], null, 'foo', []); + }, false, positionNotifier, {} as UserSocket, [], null, 'foo', []); positionNotifier.addZoneListener({} as ZoneSocket, 0, 0); positionNotifier.addZoneListener({} as ZoneSocket, 0, 1); @@ -101,14 +101,14 @@ describe("PositionNotifier", () => { y: 500, moving: false, direction: 'down' - }, false, [], positionNotifier, {} as UserSocket, [], null, 'foo', []); + }, false, positionNotifier, {} as UserSocket, [], null, 'foo', []); const user2 = new User(2, 'test', '10.0.0.2', { x: 0, y: 0, moving: false, direction: 'down' - }, false, [], positionNotifier, {} as UserSocket, [], null, 'foo', []); + }, false, positionNotifier, {} as UserSocket, [], null, 'foo', []); const listener = {} as ZoneSocket; positionNotifier.addZoneListener(listener, 0, 0);