much cleaner approach to group lock update

This commit is contained in:
Hanusiak Piotr 2022-03-10 12:20:53 +01:00
parent 3a5aa3c3c5
commit 5a8cba24fa
12 changed files with 13 additions and 107 deletions

View File

@ -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) {

View File

@ -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<User> {

View File

@ -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);
}
}

View File

@ -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

View File

@ -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<GameRoom> | undefined
) {
private async onLockGroup(groupId: number, client: ZoneSocket, roomPromise: PromiseLike<GameRoom> | 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());
}
}

View File

@ -48,11 +48,6 @@ export interface GroupCreatedUpdatedMessageInterface {
locked: boolean;
}
export interface LockGroupMessageInterface {
groupId: number;
lock: boolean;
}
export interface GroupUsersUpdateMessageInterface {
groupId: number;
userIds: number[];

View File

@ -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<GroupUsersUpdateMessageInterface>();
public readonly groupUsersUpdateMessageStream = this._groupUsersUpdateMessageStream.asObservable();
private readonly _lockGroupMessageStream = new Subject<LockGroupMessage>();
public readonly lockGroupMessageStream = this._lockGroupMessageStream.asObservable();
private readonly _groupDeleteMessageStream = new Subject<GroupDeleteMessageTsProto>();
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;

View File

@ -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) {

View File

@ -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;
}
}

View File

@ -21,7 +21,6 @@ import {
FollowConfirmationMessage,
FollowAbortMessage,
VariableMessage,
LockGroupMessage,
LockGroupPromptMessage,
} from "../Messages/generated/messages_pb";
import { UserMovesMessage } from "../Messages/generated/messages_pb";

View File

@ -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()) {

View File

@ -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