From 5a8cba24fa423727ea3f98466d67007d84fb57b4 Mon Sep 17 00:00:00 2001 From: Hanusiak Piotr Date: Thu, 10 Mar 2022 12:20:53 +0100 Subject: [PATCH] much cleaner approach to group lock update --- back/src/Model/GameRoom.ts | 5 ++-- back/src/Model/PositionNotifier.ts | 6 ++-- back/src/Model/Zone.ts | 7 ++--- back/src/RoomManager.ts | 2 +- back/src/Services/SocketManager.ts | 17 +++-------- front/src/Connexion/ConnexionModels.ts | 5 ---- front/src/Connexion/RoomConnection.ts | 8 ------ front/src/Phaser/Game/GameScene.ts | 31 --------------------- messages/protos/messages.proto | 7 ----- pusher/src/Controller/IoSocketController.ts | 1 - pusher/src/Model/Zone.ts | 23 --------------- pusher/src/Services/SocketManager.ts | 8 ------ 12 files changed, 13 insertions(+), 107 deletions(-) diff --git a/back/src/Model/GameRoom.ts b/back/src/Model/GameRoom.ts index a073f0e5..c1c1c346 100644 --- a/back/src/Model/GameRoom.ts +++ b/back/src/Model/GameRoom.ts @@ -23,7 +23,6 @@ import { VariableMessage, VariableWithTagMessage, ServerToClientMessage, - LockGroupMessage, } from "../Messages/generated/messages_pb"; import { ProtobufUtils } from "../Model/Websocket/ProtobufUtils"; import { RoomSocket, ZoneSocket } from "src/RoomManager"; @@ -550,8 +549,8 @@ export class GameRoom { this.positionNotifier.emitEmoteEvent(user, emoteEventMessage); } - public emitLockGroupMessage(user: User, lockGroupMessage: LockGroupMessage) { - this.positionNotifier.emitLockGroupEvent(user, lockGroupMessage); + public emitLockGroupEvent(user: User, groupId: number) { + this.positionNotifier.emitLockGroupEvent(user, groupId); } public addRoomListener(socket: RoomSocket) { diff --git a/back/src/Model/PositionNotifier.ts b/back/src/Model/PositionNotifier.ts index 6e1fb195..38d538ec 100644 --- a/back/src/Model/PositionNotifier.ts +++ b/back/src/Model/PositionNotifier.ts @@ -21,7 +21,7 @@ import { Movable } from "_Model/Movable"; import { PositionInterface } from "_Model/PositionInterface"; import { ZoneSocket } from "../RoomManager"; import { User } from "../Model/User"; -import { EmoteEventMessage, LockGroupMessage, SetPlayerDetailsMessage } from "../Messages/generated/messages_pb"; +import { EmoteEventMessage, SetPlayerDetailsMessage } from "../Messages/generated/messages_pb"; interface ZoneDescriptor { i: number; @@ -140,10 +140,10 @@ export class PositionNotifier { zone.emitEmoteEvent(emoteEventMessage); } - public emitLockGroupEvent(user: User, lockGroupMessage: LockGroupMessage) { + public emitLockGroupEvent(user: User, groupId: number) { const zoneDesc = this.getZoneDescriptorFromCoordinates(user.getPosition().x, user.getPosition().y); const zone = this.getZone(zoneDesc.i, zoneDesc.j); - zone.emitLockGroupEvent(lockGroupMessage); + zone.emitLockGroupEvent(groupId); } public *getAllUsersInSquareAroundZone(zone: Zone): Generator { diff --git a/back/src/Model/Zone.ts b/back/src/Model/Zone.ts index 4f49ff55..2d0cefd5 100644 --- a/back/src/Model/Zone.ts +++ b/back/src/Model/Zone.ts @@ -7,14 +7,13 @@ import { EmoteEventMessage, SetPlayerDetailsMessage, PlayerDetailsUpdatedMessage, - LockGroupMessage, } from "../Messages/generated/messages_pb"; export type EntersCallback = (thing: Movable, fromZone: Zone | null, listener: ZoneSocket) => void; export type MovesCallback = (thing: Movable, position: PositionInterface, listener: ZoneSocket) => void; export type LeavesCallback = (thing: Movable, newZone: Zone | null, listener: ZoneSocket) => void; export type EmoteCallback = (emoteEventMessage: EmoteEventMessage, listener: ZoneSocket) => void; -export type LockGroupCallback = (lockGroupMessage: LockGroupMessage, listener: ZoneSocket) => void; +export type LockGroupCallback = (groupId: number, listener: ZoneSocket) => void; export type PlayerDetailsUpdatedCallback = ( playerDetailsUpdatedMessage: PlayerDetailsUpdatedMessage, listener: ZoneSocket @@ -111,9 +110,9 @@ export class Zone { } } - public emitLockGroupEvent(lockGroupMessage: LockGroupMessage) { + public emitLockGroupEvent(groupId: number) { for (const listener of this.listeners) { - this.onLockGroup(lockGroupMessage, listener); + this.onLockGroup(groupId, listener); } } diff --git a/back/src/RoomManager.ts b/back/src/RoomManager.ts index 2705787e..ab886f50 100644 --- a/back/src/RoomManager.ts +++ b/back/src/RoomManager.ts @@ -137,7 +137,7 @@ const roomManager: IRoomManagerServer = { message.getFollowabortmessage() as FollowAbortMessage ); } else if (message.hasLockgrouppromptmessage()) { - socketManager.handleLockGroupMessage( + socketManager.handleLockGroupPromptMessage( room, user, message.getLockgrouppromptmessage() as LockGroupPromptMessage diff --git a/back/src/Services/SocketManager.ts b/back/src/Services/SocketManager.ts index f3f34f9b..d0151e30 100644 --- a/back/src/Services/SocketManager.ts +++ b/back/src/Services/SocketManager.ts @@ -39,7 +39,6 @@ import { SetPlayerDetailsMessage, PlayerDetailsUpdatedMessage, GroupUsersUpdateMessage, - LockGroupMessage, LockGroupPromptMessage, RoomMessage, } from "../Messages/generated/messages_pb"; @@ -287,8 +286,7 @@ export class SocketManager { this.onClientLeave(thing, newZone, listener), (emoteEventMessage: EmoteEventMessage, listener: ZoneSocket) => this.onEmote(emoteEventMessage, listener), - (lockGroupMessage: LockGroupMessage, listener: ZoneSocket) => - this.onLockGroup(lockGroupMessage, listener, roomPromise), + (groupId: number, listener: ZoneSocket) => this.onLockGroup(groupId, listener, roomPromise), (playerDetailsUpdatedMessage: PlayerDetailsUpdatedMessage, listener: ZoneSocket) => this.onPlayerDetailsUpdated(playerDetailsUpdatedMessage, listener) ) @@ -392,15 +390,11 @@ export class SocketManager { emitZoneMessage(subMessage, client); } - private async onLockGroup( - lockGroupMessage: LockGroupMessage, - client: ZoneSocket, - roomPromise: PromiseLike | undefined - ) { + private async onLockGroup(groupId: number, client: ZoneSocket, roomPromise: PromiseLike | undefined) { if (!roomPromise) { return; } - const group = (await roomPromise).getGroupById(lockGroupMessage.getGroupid()); + const group = (await roomPromise).getGroupById(groupId); if (!group) { return; } @@ -915,10 +909,7 @@ export class SocketManager { return; } group.lock(message.getLock()); - const lockGroupMessage = new LockGroupMessage(); - lockGroupMessage.setLock(message.getLock()); - lockGroupMessage.setGroupid(group.getId()); - room.emitLockGroupMessage(user, lockGroupMessage); + room.emitLockGroupEvent(user, group.getId()); } } diff --git a/front/src/Connexion/ConnexionModels.ts b/front/src/Connexion/ConnexionModels.ts index 167624e6..04282abc 100644 --- a/front/src/Connexion/ConnexionModels.ts +++ b/front/src/Connexion/ConnexionModels.ts @@ -48,11 +48,6 @@ export interface GroupCreatedUpdatedMessageInterface { locked: boolean; } -export interface LockGroupMessageInterface { - groupId: number; - lock: boolean; -} - export interface GroupUsersUpdateMessageInterface { groupId: number; userIds: number[]; diff --git a/front/src/Connexion/RoomConnection.ts b/front/src/Connexion/RoomConnection.ts index d0cc5c48..48827bc5 100644 --- a/front/src/Connexion/RoomConnection.ts +++ b/front/src/Connexion/RoomConnection.ts @@ -40,7 +40,6 @@ import { PositionMessage_Direction, SetPlayerDetailsMessage as SetPlayerDetailsMessageTsProto, PingMessage as PingMessageTsProto, - LockGroupMessage, } from "../Messages/ts-proto-generated/messages"; import { Subject } from "rxjs"; @@ -102,9 +101,6 @@ export class RoomConnection implements RoomConnection { private readonly _groupUsersUpdateMessageStream = new Subject(); public readonly groupUsersUpdateMessageStream = this._groupUsersUpdateMessageStream.asObservable(); - private readonly _lockGroupMessageStream = new Subject(); - public readonly lockGroupMessageStream = this._lockGroupMessageStream.asObservable(); - private readonly _groupDeleteMessageStream = new Subject(); public readonly groupDeleteMessageStream = this._groupDeleteMessageStream.asObservable(); @@ -268,10 +264,6 @@ export class RoomConnection implements RoomConnection { this._emoteEventMessageStream.next(subMessage.emoteEventMessage); break; } - case "lockGroupMessage": { - this._lockGroupMessageStream.next(subMessage.lockGroupMessage); - break; - } case "playerDetailsUpdatedMessage": { this._playerDetailsUpdatedMessageStream.next(subMessage.playerDetailsUpdatedMessage); break; diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index e795979f..15570bb9 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -51,7 +51,6 @@ import { PathfindingManager } from "../../Utils/PathfindingManager"; import { ActivatablesManager } from "./ActivatablesManager"; import type { GroupCreatedUpdatedMessageInterface, - LockGroupMessageInterface, MessageUserMovedInterface, MessageUserPositionInterface, OnConnectInterface, @@ -134,11 +133,6 @@ interface DeleteGroupEventInterface { groupId: number; } -interface LockGroupEventInterface { - type: "LockGroupEvent"; - event: LockGroupMessageInterface; -} - interface PlayerDetailsUpdatedInterface { type: "PlayerDetailsUpdated"; details: PlayerDetailsUpdatedMessageInterface; @@ -163,7 +157,6 @@ export class GameScene extends DirtyScene { | UserMovedEventInterface | GroupCreatedUpdatedEventInterface | DeleteGroupEventInterface - | LockGroupEventInterface | PlayerDetailsUpdatedInterface >(); private initPosition: PositionInterface | null = null; @@ -808,10 +801,6 @@ export class GameScene extends DirtyScene { } }); - this.connection.lockGroupMessageStream.subscribe((message) => { - this.lockGroup(message); - }); - this.connection.onServerDisconnected(() => { console.log("Player disconnected from server. Reloading scene."); this.cleanupClosingScene(); @@ -1847,10 +1836,6 @@ ${escapedMessage} currentPlayerGroupLockStateStore.set(undefined); break; } - case "LockGroupEvent": { - this.doLockGroup(event.event); - break; - } default: { const tmp: never = event; } @@ -2049,22 +2034,6 @@ ${escapedMessage} this.groups.delete(groupId); } - lockGroup(event: LockGroupMessageInterface): void { - this.pendingEvents.enqueue({ - type: "LockGroupEvent", - event, - }); - } - - doLockGroup(event: LockGroupMessageInterface): void { - const group = this.groups.get(event.groupId); - if (!group) { - return; - } - currentPlayerGroupLockStateStore.set(event.lock); - group.setTexture(event.lock ? "circleSprite-red" : "circleSprite-white"); - } - doUpdatePlayerDetails(message: PlayerDetailsUpdatedMessageInterface): void { const character = this.MapPlayersByKey.get(message.userId); if (character === undefined) { diff --git a/messages/protos/messages.proto b/messages/protos/messages.proto index b6e8d98f..78858499 100644 --- a/messages/protos/messages.proto +++ b/messages/protos/messages.proto @@ -102,11 +102,6 @@ message LockGroupPromptMessage { bool lock = 1; } -message LockGroupMessage { - int32 groupId = 1; - bool lock = 2; -} - message ClientToServerMessage { oneof message { UserMovesMessage userMovesMessage = 2; @@ -182,7 +177,6 @@ message SubMessage { VariableMessage variableMessage = 8; ErrorMessage errorMessage = 9; PlayerDetailsUpdatedMessage playerDetailsUpdatedMessage = 10; - LockGroupMessage lockGroupMessage = 11; } } @@ -437,7 +431,6 @@ message SubToPusherMessage { EmoteEventMessage emoteEventMessage = 9; ErrorMessage errorMessage = 10; PlayerDetailsUpdatedMessage playerDetailsUpdatedMessage = 11; - LockGroupMessage lockGroupMessage = 12; } } diff --git a/pusher/src/Controller/IoSocketController.ts b/pusher/src/Controller/IoSocketController.ts index 7c7928d6..781b4fc0 100644 --- a/pusher/src/Controller/IoSocketController.ts +++ b/pusher/src/Controller/IoSocketController.ts @@ -21,7 +21,6 @@ import { FollowConfirmationMessage, FollowAbortMessage, VariableMessage, - LockGroupMessage, LockGroupPromptMessage, } from "../Messages/generated/messages_pb"; import { UserMovesMessage } from "../Messages/generated/messages_pb"; diff --git a/pusher/src/Model/Zone.ts b/pusher/src/Model/Zone.ts index 92f82222..02a01d8d 100644 --- a/pusher/src/Model/Zone.ts +++ b/pusher/src/Model/Zone.ts @@ -18,7 +18,6 @@ import { ErrorMessage, PlayerDetailsUpdatedMessage, SetPlayerDetailsMessage, - LockGroupMessage, } from "../Messages/generated/messages_pb"; import { ClientReadableStream } from "grpc"; import { PositionDispatcher } from "_Model/PositionDispatcher"; @@ -33,7 +32,6 @@ export interface ZoneEventListener { onGroupEnters(group: GroupDescriptor, listener: ExSocketInterface): void; onGroupMoves(group: GroupDescriptor, listener: ExSocketInterface): void; onGroupLeaves(groupId: number, listener: ExSocketInterface): void; - onLockGroup(lockGroupMessage: LockGroupMessage, listener: ExSocketInterface): void; onEmote(emoteMessage: EmoteEventMessage, listener: ExSocketInterface): void; onError(errorMessage: ErrorMessage, listener: ExSocketInterface): void; onPlayerDetailsUpdated(playerDetailsUpdatedMessage: PlayerDetailsUpdatedMessage, listener: ExSocketInterface): void; @@ -140,10 +138,6 @@ export class GroupDescriptor { return new GroupDescriptor(message.getGroupid(), message.getGroupsize(), position, message.getLocked()); } - public updateFromLockGroupMessage(message: LockGroupMessage): void { - this.locked = message.getLock(); - } - public update(groupDescriptor: GroupDescriptor) { this.groupSize = groupDescriptor.groupSize; this.position = groupDescriptor.position; @@ -221,14 +215,6 @@ export class Zone { const fromZone = groupUpdateZoneMessage.getFromzone(); this.notifyGroupEnter(groupDescriptor, fromZone?.toObject()); } - } else if (message.hasLockgroupmessage()) { - const lockGroupMessage = message.getLockgroupmessage() as LockGroupMessage; - const groupId = lockGroupMessage.getGroupid(); - const oldGroupDescriptor = this.groups.get(groupId); - if (oldGroupDescriptor !== undefined) { - oldGroupDescriptor.updateFromLockGroupMessage(lockGroupMessage); - this.notifyLockGroup(lockGroupMessage); - } } else if (message.hasUserleftzonemessage()) { const userLeftMessage = message.getUserleftzonemessage() as UserLeftZoneMessage; this.users.delete(userLeftMessage.getUserid()); @@ -256,9 +242,6 @@ export class Zone { } else if (message.hasEmoteeventmessage()) { const emoteEventMessage = message.getEmoteeventmessage() as EmoteEventMessage; this.notifyEmote(emoteEventMessage); - } else if (message.hasLockgroupmessage()) { - const lockGroupMessage = message.getLockgroupmessage() as LockGroupMessage; - this.notifyLockGroup(lockGroupMessage); } else if (message.hasPlayerdetailsupdatedmessage()) { const playerDetailsUpdatedMessage = message.getPlayerdetailsupdatedmessage() as PlayerDetailsUpdatedMessage; @@ -369,12 +352,6 @@ export class Zone { } } - private notifyLockGroup(lockGroupMessage: LockGroupMessage) { - for (const listener of this.listeners) { - this.socketListener.onLockGroup(lockGroupMessage, listener); - } - } - private notifyPlayerDetailsUpdated(playerDetailsUpdatedMessage: PlayerDetailsUpdatedMessage) { for (const listener of this.listeners) { if (listener.userId === playerDetailsUpdatedMessage.getUserid()) { diff --git a/pusher/src/Services/SocketManager.ts b/pusher/src/Services/SocketManager.ts index 436ecd39..f6e2feb0 100644 --- a/pusher/src/Services/SocketManager.ts +++ b/pusher/src/Services/SocketManager.ts @@ -38,7 +38,6 @@ import { ErrorMessage, WorldFullMessage, PlayerDetailsUpdatedMessage, - LockGroupMessage, LockGroupPromptMessage, } from "../Messages/generated/messages_pb"; import { ProtobufUtils } from "../Model/Websocket/ProtobufUtils"; @@ -307,13 +306,6 @@ export class SocketManager implements ZoneEventListener { emitInBatch(listener, subMessage); } - onLockGroup(lockGroupMessage: LockGroupMessage, listener: ExSocketInterface): void { - const subMessage = new SubMessage(); - subMessage.setLockgroupmessage(lockGroupMessage); - - emitInBatch(listener, subMessage); - } - onPlayerDetailsUpdated( playerDetailsUpdatedMessage: PlayerDetailsUpdatedMessage, listener: ExSocketInterface