diff --git a/back/src/Model/GameRoom.ts b/back/src/Model/GameRoom.ts index 79ed9d3c..9afeae55 100644 --- a/back/src/Model/GameRoom.ts +++ b/back/src/Model/GameRoom.ts @@ -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; diff --git a/back/src/Model/User.ts b/back/src/Model/User.ts index 09c0d3d9..13a61c3f 100644 --- a/back/src/Model/User.ts +++ b/back/src/Model/User.ts @@ -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; } diff --git a/back/src/Services/SocketManager.ts b/back/src/Services/SocketManager.ts index 5818afa9..8c7eecac 100644 --- a/back/src/Services/SocketManager.ts +++ b/back/src/Services/SocketManager.ts @@ -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 = []; } } diff --git a/front/src/Components/InteractMenu/InteractMenu.svelte b/front/src/Components/InteractMenu/InteractMenu.svelte index 6f5e62b2..dae7f099 100644 --- a/front/src/Components/InteractMenu/InteractMenu.svelte +++ b/front/src/Components/InteractMenu/InteractMenu.svelte @@ -19,7 +19,7 @@ vim: ft=typescript let followState: string; let followRole: string; - let followUsers: string[]; + let followUsers: number[]; let stateUnsubscriber: Unsubscriber; let roleUnsubscriber: Unsubscriber; let nameUnsubscriber: Unsubscriber; @@ -51,14 +51,18 @@ vim: ft=typescript } }); + function name(userId: number): string | undefined { + return gameScene.MapPlayersByKey.get(userId)?.PlayerValue; + } + function sendFollowRequest() { - gameScene.connection?.emitFollowRequest(gameManager.getPlayerName()); + gameScene.connection?.emitFollowRequest(); followStateStore.set(followStates.active); } function acceptFollowRequest() { gameScene.CurrentPlayer.enableFollowing(); - gameScene.connection?.emitFollowConfirmation(followUsers[0], gameManager.getPlayerName()); + gameScene.connection?.emitFollowConfirmation(); } function abortEnding() { @@ -66,11 +70,7 @@ vim: ft=typescript } function reset() { - if (followRole === followRoles.leader && followUsers.length > 0) { - gameScene.connection?.emitFollowAbort(gameManager.getPlayerName(), "*"); - } else { - gameScene.connection?.emitFollowAbort(followUsers[0], gameManager.getPlayerName()); - } + gameScene.connection?.emitFollowAbort(); followStateStore.set(followStates.off); followRoleStore.set(followRoles.leader); followUsersStore.set([]); @@ -92,7 +92,7 @@ vim: ft=typescript {#if followRole === followRoles.follower}
-

Do you want to follow {followUsers[0]}?

+

Do you want to follow {name(followUsers[0])}?

@@ -117,7 +117,7 @@ vim: ft=typescript
{#if followRole === followRoles.follower}
-

Do you want to stop following {followUsers[0]}?

+

Do you want to stop following {name(followUsers[0])}?

{:else if followRole === followRoles.leader}
@@ -135,15 +135,15 @@ vim: ft=typescript
{#if followRole === followRoles.follower} -

Following {followUsers[0]}

+

Following {name(followUsers[0])}

{:else if followUsers.length === 0}

Waiting for followers' confirmation

{:else if followUsers.length === 1} -

{followUsers[0]} is following you

+

{name(followUsers[0])} is following you

{:else if followUsers.length === 2} -

{followUsers[0]} and {followUsers[1]} are following you

+

{name(followUsers[0])} and {name(followUsers[1])} are following you

{:else} -

{followUsers[0]}, {followUsers[1]} and {followUsers[2]} are following you

+

{name(followUsers[0])}, {name(followUsers[1])} and {name(followUsers[2])} are following you

{/if}
diff --git a/front/src/Connexion/RoomConnection.ts b/front/src/Connexion/RoomConnection.ts index 6d0abaef..49b2dc4c 100644 --- a/front/src/Connexion/RoomConnection.ts +++ b/front/src/Connexion/RoomConnection.ts @@ -750,39 +750,41 @@ export class RoomConnection implements RoomConnection { this.socket.send(clientToServerMessage.serializeBinary().buffer); } - public emitFollowRequest(user: string | null): void { - if (!user) { + public emitFollowRequest(): void { + if (!this.userId) { return; } console.log("Emitting follow request"); const message = new FollowRequestMessage(); - message.setLeader(user); + message.setLeader(this.userId); const clientToServerMessage = new ClientToServerMessage(); clientToServerMessage.setFollowrequestmessage(message); this.socket.send(clientToServerMessage.serializeBinary().buffer); } - public emitFollowConfirmation(leader: string | null, follower: string | null): void { - if (!leader || !follower) { + public emitFollowConfirmation(): void { + if (!this.userId) { return; } console.log("Emitting follow confirmation"); const message = new FollowConfirmationMessage(); - message.setLeader(leader); - message.setFollower(follower); + message.setLeader(get(followUsersStore)[0]); + message.setFollower(this.userId); const clientToServerMessage = new ClientToServerMessage(); clientToServerMessage.setFollowconfirmationmessage(message); this.socket.send(clientToServerMessage.serializeBinary().buffer); } - public emitFollowAbort(leader: string | null, follower: string | null): void { - if (!leader || !follower) { + public emitFollowAbort(): void { + const isLeader = get(followRoleStore) === followRoles.leader; + const hasFollowers = get(followUsersStore).length > 0; + if (!this.userId || (isLeader && !hasFollowers)) { return; } console.log("Emitting follow abort"); const message = new FollowAbortMessage(); - message.setLeader(leader); - message.setFollower(follower); + message.setLeader(isLeader ? this.userId : get(followUsersStore)[0]); + message.setFollower(isLeader ? 0 : this.userId); const clientToServerMessage = new ClientToServerMessage(); clientToServerMessage.setFollowabortmessage(message); this.socket.send(clientToServerMessage.serializeBinary().buffer); diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index 6d735182..ae89e2c3 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -1715,10 +1715,6 @@ ${escapedMessage} }); } - public findPlayer(testFunction: (player: RemotePlayer) => boolean): RemotePlayer | undefined { - return Array.from(this.MapPlayersByKey.values()).find(testFunction); - } - /** * Called by the connexion when a new player arrives on a map */ diff --git a/front/src/Phaser/Player/Player.ts b/front/src/Phaser/Player/Player.ts index 285163e8..159816e3 100644 --- a/front/src/Phaser/Player/Player.ts +++ b/front/src/Phaser/Player/Player.ts @@ -86,9 +86,9 @@ export class Player extends Character { private computeFollowMovement(): number[] { // Find followed WOKA and abort following if we lost it - const player = this.scene.findPlayer((p) => p.PlayerValue === get(followUsersStore)[0]); + const player = this.scene.MapPlayersByKey.get(get(followUsersStore)[0]); if (!player) { - this.scene.connection?.emitFollowAbort(get(followUsersStore)[0], this.PlayerValue); + this.scene.connection?.emitFollowAbort(); followStateStore.set(followStates.off); return [0, 0]; } diff --git a/front/src/Stores/InteractStore.ts b/front/src/Stores/InteractStore.ts index 960a6954..6c85ab17 100644 --- a/front/src/Stores/InteractStore.ts +++ b/front/src/Stores/InteractStore.ts @@ -14,4 +14,4 @@ export const followRoles = { export const followStateStore = writable(followStates.off); export const followRoleStore = writable(followRoles.leader); -export const followUsersStore = writable([]); +export const followUsersStore = writable([]); diff --git a/messages/protos/messages.proto b/messages/protos/messages.proto index 7152f43f..96fecb69 100644 --- a/messages/protos/messages.proto +++ b/messages/protos/messages.proto @@ -81,17 +81,17 @@ message QueryJitsiJwtMessage { } message FollowRequestMessage { - string leader = 1; + int32 leader = 1; } message FollowConfirmationMessage { - string leader = 1; - string follower = 2; + int32 leader = 1; + int32 follower = 2; } message FollowAbortMessage { - string leader = 1; - string follower = 2; + int32 leader = 1; + int32 follower = 2; } message ClientToServerMessage {