diff --git a/back/src/Model/GameRoom.ts b/back/src/Model/GameRoom.ts index 1b0db5eb..e8803762 100644 --- a/back/src/Model/GameRoom.ts +++ b/back/src/Model/GameRoom.ts @@ -139,6 +139,7 @@ export class GameRoom { joinRoomMessage.getIpaddress(), position, false, + [], this.positionNotifier, socket, joinRoomMessage.getTagList(), @@ -231,7 +232,7 @@ export class GameRoom { // If the user is part of a group: // should he leave the group? const distance = GameRoom.computeDistanceBetweenPositions(user.getPosition(), user.group.getPosition()); - if (distance > this.groupRadius) { + if (user.following.length === 0 && distance > this.groupRadius) { this.leaveGroup(user); } } diff --git a/back/src/Model/User.ts b/back/src/Model/User.ts index 186fb32a..09c0d3d9 100644 --- a/back/src/Model/User.ts +++ b/back/src/Model/User.ts @@ -25,6 +25,7 @@ export class User implements Movable { public readonly IPAddress: string, private position: PointInterface, public silent: boolean, + public following: string[], private positionNotifier: PositionNotifier, public readonly socket: UserSocket, public readonly tags: string[], @@ -48,6 +49,21 @@ export class User implements Movable { this.positionNotifier.updatePosition(this, position, oldPosition); } + public addFollower(name: string): void { + if (this.following.includes(name)) { + return; + } + this.following.push(name); + } + + public delFollower(name: string): void { + const idx = this.following.indexOf(name); + if (idx === -1) { + return; + } + this.following.splice(idx, 1); + } + private batchedMessages: BatchMessage = new BatchMessage(); private batchTimeout: NodeJS.Timeout | null = null; diff --git a/back/src/Services/SocketManager.ts b/back/src/Services/SocketManager.ts index 3ab53719..94ad2ed3 100644 --- a/back/src/Services/SocketManager.ts +++ b/back/src/Services/SocketManager.ts @@ -847,19 +847,35 @@ export class SocketManager { const clientMessage = new ServerToClientMessage(); clientMessage.setFollowconfirmationmessage(message); room.sendToUserWithName(message.getLeader(), clientMessage); + + room.getUserByName(message.getLeader())?.addFollower(user.name); + user.addFollower(message.getLeader()); } handleFollowAbortMessage(room: GameRoom, user: User, message: FollowAbortMessage) { if (message.getRole() === "leader") { + // Forward message const clientMessage = new ServerToClientMessage(); clientMessage.setFollowabortmessage(message); room.sendToOthersInGroupIncludingUser(user, clientMessage); + + // Update followers + room.getGroupIncludingUser(user) + ?.getUsers() + .forEach((user) => { + user.following = []; + }); } else { + // Forward message const recipient = message.getPlayername(); message.setPlayername(user.name); const clientMessage = new ServerToClientMessage(); clientMessage.setFollowabortmessage(message); room.sendToUserWithName(recipient, clientMessage); + + // Update followers + room.getUserByName(recipient)?.delFollower(user.name); + user.following = []; } } } diff --git a/back/tests/PositionNotifierTest.ts b/back/tests/PositionNotifierTest.ts index 1aaf2e13..955ed40f 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);