From 724dbc8efac7a8eea78260e7787c5d8f4be9ab81 Mon Sep 17 00:00:00 2001 From: Hanusiak Piotr Date: Mon, 28 Feb 2022 13:47:08 +0100 Subject: [PATCH] send group userIds to players --- back/src/Model/Group.ts | 2 +- back/src/Services/SocketManager.ts | 24 ++++++++++++++++++++++-- front/src/Connexion/ConnexionModels.ts | 5 +++++ front/src/Connexion/RoomConnection.ts | 10 ++++++++++ front/src/Phaser/Game/GameScene.ts | 1 - messages/protos/messages.proto | 6 ++++++ 6 files changed, 44 insertions(+), 4 deletions(-) diff --git a/back/src/Model/Group.ts b/back/src/Model/Group.ts index c14d509f..65e15c40 100644 --- a/back/src/Model/Group.ts +++ b/back/src/Model/Group.ts @@ -147,9 +147,9 @@ export class Group implements Movable { join(user: User): void { // Broadcast on the right event - this.connectCallback(user, this); this.users.add(user); user.group = this; + this.connectCallback(user, this); } leave(user: User): void { diff --git a/back/src/Services/SocketManager.ts b/back/src/Services/SocketManager.ts index 9233811b..c396893d 100644 --- a/back/src/Services/SocketManager.ts +++ b/back/src/Services/SocketManager.ts @@ -38,6 +38,7 @@ import { SubToPusherRoomMessage, SetPlayerDetailsMessage, PlayerDetailsUpdatedMessage, + GroupUsersUpdateMessage, } from "../Messages/generated/messages_pb"; import { User, UserSocket } from "../Model/User"; import { ProtobufUtils } from "../Model/Websocket/ProtobufUtils"; @@ -265,8 +266,14 @@ export class SocketManager { if (roomPromise === undefined) { roomPromise = GameRoom.create( roomId, - (user: User, group: Group) => this.joinWebRtcRoom(user, group), - (user: User, group: Group) => this.disConnectedUser(user, group), + (user: User, group: Group) => { + this.joinWebRtcRoom(user, group); + this.sendGroupUsersUpdateToGroupMembers(group); + }, + (user: User, group: Group) => { + this.disConnectedUser(user, group); + this.sendGroupUsersUpdateToGroupMembers(group); + }, MINIMUM_DISTANCE, GROUP_RADIUS, (thing: Movable, fromZone: Zone | null, listener: ZoneSocket) => @@ -438,6 +445,19 @@ export class SocketManager { return undefined; } + private sendGroupUsersUpdateToGroupMembers(group: Group) { + const groupUserUpdateMessage = new GroupUsersUpdateMessage(); + groupUserUpdateMessage.setGroupid(group.getId()); + groupUserUpdateMessage.setUseridsList(group.getUsers().map((user) => user.id)); + + const clientMessage = new ServerToClientMessage(); + clientMessage.setGroupusersupdatemessage(groupUserUpdateMessage); + + group.getUsers().forEach((currentUser: User) => { + currentUser.socket.write(clientMessage); + }); + } + private joinWebRtcRoom(user: User, group: Group) { for (const otherUser of group.getUsers()) { if (user === otherUser) { diff --git a/front/src/Connexion/ConnexionModels.ts b/front/src/Connexion/ConnexionModels.ts index bf834a02..3d8893a2 100644 --- a/front/src/Connexion/ConnexionModels.ts +++ b/front/src/Connexion/ConnexionModels.ts @@ -47,6 +47,11 @@ export interface GroupCreatedUpdatedMessageInterface { groupSize: number; } +export interface GroupUsersUpdateMessageInterface { + groupId: number; + userIds: number[]; +} + export interface WebRtcDisconnectMessageInterface { userId: number; } diff --git a/front/src/Connexion/RoomConnection.ts b/front/src/Connexion/RoomConnection.ts index 584a1a51..95c91cd5 100644 --- a/front/src/Connexion/RoomConnection.ts +++ b/front/src/Connexion/RoomConnection.ts @@ -5,6 +5,7 @@ import type { UserSimplePeerInterface } from "../WebRtc/SimplePeer"; import { ProtobufClientUtils } from "../Network/ProtobufClientUtils"; import type { GroupCreatedUpdatedMessageInterface, + GroupUsersUpdateMessageInterface, MessageUserJoined, PlayGlobalMessageInterface, PositionInterface, @@ -97,6 +98,9 @@ export class RoomConnection implements RoomConnection { private readonly _groupUpdateMessageStream = new Subject(); public readonly groupUpdateMessageStream = this._groupUpdateMessageStream.asObservable(); + private readonly _groupUsersUpdateMessageStream = new Subject(); + public readonly groupUsersUpdateMessageStream = this._groupUsersUpdateMessageStream.asObservable(); + private readonly _groupDeleteMessageStream = new Subject(); public readonly groupDeleteMessageStream = this._groupDeleteMessageStream.asObservable(); @@ -398,6 +402,12 @@ export class RoomConnection implements RoomConnection { this._sendJitsiJwtMessageStream.next(message.sendJitsiJwtMessage); break; } + case "groupUsersUpdateMessage": { + console.log("GOT GROUP USERS UPDATE MESSAGE"); + console.log(message.groupUsersUpdateMessage); + this._groupUsersUpdateMessageStream.next(message.groupUsersUpdateMessage); + break; + } case "sendUserMessage": { adminMessagesService.onSendusermessage(message.sendUserMessage); break; diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index 274ae573..0a44cea3 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -1976,7 +1976,6 @@ ${escapedMessage} } private doShareGroupPosition(groupPositionMessage: GroupCreatedUpdatedMessageInterface) { - console.log(groupPositionMessage); //delete previous group this.doDeleteGroup(groupPositionMessage.groupId); diff --git a/messages/protos/messages.proto b/messages/protos/messages.proto index 8ac7bbf0..5c9c95b7 100644 --- a/messages/protos/messages.proto +++ b/messages/protos/messages.proto @@ -215,6 +215,11 @@ message ItemStateMessage { string stateJson = 2; } +message GroupUsersUpdateMessage { + int32 groupId = 1; + repeated int32 userIds = 2; +} + message RoomJoinedMessage { //repeated UserJoinedMessage user = 1; //repeated GroupUpdateMessage group = 2; @@ -310,6 +315,7 @@ message ServerToClientMessage { FollowRequestMessage followRequestMessage = 21; FollowConfirmationMessage followConfirmationMessage = 22; FollowAbortMessage followAbortMessage = 23; + GroupUsersUpdateMessage groupUsersUpdateMessage = 24; } }