listening to lockGroupMessage on front

This commit is contained in:
Hanusiak Piotr 2022-03-02 10:44:26 +01:00
parent d43c8d181a
commit ef22d4ba20
12 changed files with 145 additions and 23 deletions

View File

@ -6,6 +6,7 @@ import {
EmoteCallback,
EntersCallback,
LeavesCallback,
LockGroupCallback,
MovesCallback,
PlayerDetailsUpdatedCallback,
} from "_Model/Zone";
@ -22,6 +23,7 @@ import {
VariableMessage,
VariableWithTagMessage,
ServerToClientMessage,
LockGroupMessage,
} from "../Messages/generated/messages_pb";
import { ProtobufUtils } from "../Model/Websocket/ProtobufUtils";
import { RoomSocket, ZoneSocket } from "src/RoomManager";
@ -66,6 +68,7 @@ export class GameRoom {
onMoves: MovesCallback,
onLeaves: LeavesCallback,
onEmote: EmoteCallback,
onLockGroup: LockGroupCallback,
onPlayerDetailsUpdated: PlayerDetailsUpdatedCallback
) {
// A zone is 10 sprites wide.
@ -76,6 +79,7 @@ export class GameRoom {
onMoves,
onLeaves,
onEmote,
onLockGroup,
onPlayerDetailsUpdated
);
}
@ -90,6 +94,7 @@ export class GameRoom {
onMoves: MovesCallback,
onLeaves: LeavesCallback,
onEmote: EmoteCallback,
onLockGroup: LockGroupCallback,
onPlayerDetailsUpdated: PlayerDetailsUpdatedCallback
): Promise<GameRoom> {
const mapDetails = await GameRoom.getMapDetails(roomUrl);
@ -105,6 +110,7 @@ export class GameRoom {
onMoves,
onLeaves,
onEmote,
onLockGroup,
onPlayerDetailsUpdated
);
@ -544,6 +550,12 @@ export class GameRoom {
this.positionNotifier.emitEmoteEvent(user, emoteEventMessage);
}
public emitLockGroupMessage(user: User, lockGroupMessage: LockGroupMessage) {
console.log("D2 GAME ROOM EMIT LOCK GROUP MESSAGE");
console.log(lockGroupMessage.getGroupid());
this.positionNotifier.emitLockGroupEvent(user, lockGroupMessage);
}
public addRoomListener(socket: RoomSocket) {
this.roomListeners.add(socket);
}

View File

@ -12,6 +12,7 @@ import {
EmoteCallback,
EntersCallback,
LeavesCallback,
LockGroupCallback,
MovesCallback,
PlayerDetailsUpdatedCallback,
Zone,
@ -20,7 +21,7 @@ import { Movable } from "_Model/Movable";
import { PositionInterface } from "_Model/PositionInterface";
import { ZoneSocket } from "../RoomManager";
import { User } from "../Model/User";
import { EmoteEventMessage, SetPlayerDetailsMessage } from "../Messages/generated/messages_pb";
import { EmoteEventMessage, LockGroupMessage, SetPlayerDetailsMessage } from "../Messages/generated/messages_pb";
interface ZoneDescriptor {
i: number;
@ -50,6 +51,7 @@ export class PositionNotifier {
private onUserMoves: MovesCallback,
private onUserLeaves: LeavesCallback,
private onEmote: EmoteCallback,
private onLockGroup: LockGroupCallback,
private onPlayerDetailsUpdated: PlayerDetailsUpdatedCallback
) {}
@ -111,6 +113,7 @@ export class PositionNotifier {
this.onUserMoves,
this.onUserLeaves,
this.onEmote,
this.onLockGroup,
this.onPlayerDetailsUpdated,
i,
j
@ -137,6 +140,14 @@ export class PositionNotifier {
zone.emitEmoteEvent(emoteEventMessage);
}
public emitLockGroupEvent(user: User, lockGroupMessage: LockGroupMessage) {
const zoneDesc = this.getZoneDescriptorFromCoordinates(user.getPosition().x, user.getPosition().y);
const zone = this.getZone(zoneDesc.i, zoneDesc.j);
console.log("D3 emit from ZONE");
console.log(lockGroupMessage.getGroupid());
zone.emitLockGroupEvent(lockGroupMessage);
}
public *getAllUsersInSquareAroundZone(zone: Zone): Generator<User> {
const zoneDescriptor = this.getZoneDescriptorFromCoordinates(zone.x, zone.y);
for (const d of getNearbyDescriptorsMatrix(zoneDescriptor)) {

View File

@ -7,12 +7,14 @@ 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 PlayerDetailsUpdatedCallback = (
playerDetailsUpdatedMessage: PlayerDetailsUpdatedMessage,
listener: ZoneSocket
@ -27,6 +29,7 @@ export class Zone {
private onMoves: MovesCallback,
private onLeaves: LeavesCallback,
private onEmote: EmoteCallback,
private onLockGroup: LockGroupCallback,
private onPlayerDetailsUpdated: PlayerDetailsUpdatedCallback,
public readonly x: number,
public readonly y: number
@ -108,6 +111,13 @@ export class Zone {
}
}
public emitLockGroupEvent(lockGroupMessage: LockGroupMessage) {
console.log("D4 ZONE ON LOCK GROUP CALLBACK");
for (const listener of this.listeners) {
this.onLockGroup(lockGroupMessage, listener);
}
}
public updatePlayerDetails(user: User, playerDetails: SetPlayerDetailsMessage) {
const playerDetailsUpdatedMessage = new PlayerDetailsUpdatedMessage();
playerDetailsUpdatedMessage.setUserid(user.id);

View File

@ -138,6 +138,7 @@ const roomManager: IRoomManagerServer = {
);
} else if (message.hasLockgroupmessage()) {
socketManager.handleLockGroupMessage(
room,
user,
message.getLockgroupmessage() as LockGroupMessage
);

View File

@ -285,6 +285,8 @@ export class SocketManager {
this.onClientLeave(thing, newZone, listener),
(emoteEventMessage: EmoteEventMessage, listener: ZoneSocket) =>
this.onEmote(emoteEventMessage, listener),
(lockGroupMessage: LockGroupMessage, listener: ZoneSocket) =>
this.onLockGroup(lockGroupMessage, listener),
(playerDetailsUpdatedMessage: PlayerDetailsUpdatedMessage, listener: ZoneSocket) =>
this.onPlayerDetailsUpdated(playerDetailsUpdatedMessage, listener)
)
@ -388,6 +390,16 @@ export class SocketManager {
emitZoneMessage(subMessage, client);
}
private onLockGroup(lockGroupMessage: LockGroupMessage, client: ZoneSocket) {
const subMessage = new SubToPusherMessage();
subMessage.setLockgroupmessage(lockGroupMessage);
console.log("D5 SOCKET MANAGER ON LOCK GROUP");
console.log(lockGroupMessage.getGroupid());
console.log(lockGroupMessage.getLock());
emitZoneMessage(subMessage, client);
}
private onPlayerDetailsUpdated(playerDetailsUpdatedMessage: PlayerDetailsUpdatedMessage, client: ZoneSocket) {
const subMessage = new SubToPusherMessage();
subMessage.setPlayerdetailsupdatedmessage(playerDetailsUpdatedMessage);
@ -892,9 +904,17 @@ export class SocketManager {
}
}
handleLockGroupMessage(user: User, message: LockGroupMessage) {
console.log(`lock group: ${message.getLock()}`);
user.group?.lock(message.getLock());
handleLockGroupMessage(room: GameRoom, user: User, message: LockGroupMessage) {
console.log("D1 HANDLE LOCK GROUP MESSAGE");
const group = user.group;
if (!group) {
return;
}
group.lock(message.getLock());
const lockGroupMessage = new LockGroupMessage();
lockGroupMessage.setLock(message.getLock());
lockGroupMessage.setGroupid(message.getGroupid());
room.emitLockGroupMessage(user, lockGroupMessage);
}
}

View File

@ -25,6 +25,7 @@ describe("PositionNotifier", () => {
leaveTriggered = true;
},
() => {},
() => {},
() => {}
);
@ -132,6 +133,7 @@ describe("PositionNotifier", () => {
leaveTriggered = true;
},
() => {},
() => {},
() => {}
);

View File

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

View File

@ -40,6 +40,7 @@ import {
PositionMessage_Direction,
SetPlayerDetailsMessage as SetPlayerDetailsMessageTsProto,
PingMessage as PingMessageTsProto,
LockGroupMessage,
} from "../Messages/ts-proto-generated/messages";
import { Subject } from "rxjs";
@ -101,6 +102,9 @@ 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();
@ -264,6 +268,10 @@ 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;
@ -403,8 +411,6 @@ export class RoomConnection implements RoomConnection {
break;
}
case "groupUsersUpdateMessage": {
console.log("GOT GROUP USERS UPDATE MESSAGE");
console.log(message.groupUsersUpdateMessage);
this._groupUsersUpdateMessageStream.next(message.groupUsersUpdateMessage);
break;
}

View File

@ -51,6 +51,7 @@ import { PathfindingManager } from "../../Utils/PathfindingManager";
import { ActivatablesManager } from "./ActivatablesManager";
import type {
GroupCreatedUpdatedMessageInterface,
LockGroupMessageInterface,
MessageUserMovedInterface,
MessageUserPositionInterface,
OnConnectInterface,
@ -132,6 +133,11 @@ interface DeleteGroupEventInterface {
groupId: number;
}
interface LockGroupEventInterface {
type: "LockGroupEvent";
event: LockGroupMessageInterface;
}
interface PlayerDetailsUpdatedInterface {
type: "PlayerDetailsUpdated";
details: PlayerDetailsUpdatedMessageInterface;
@ -147,6 +153,7 @@ export class GameScene extends DirtyScene {
mapFile!: ITiledMap;
animatedTiles!: AnimatedTiles;
groups: Map<number, Sprite>;
currentPlayerGroupId?: number;
circleTexture!: CanvasTexture;
circleRedTexture!: CanvasTexture;
pendingEvents = new Queue<
@ -156,6 +163,7 @@ export class GameScene extends DirtyScene {
| UserMovedEventInterface
| GroupCreatedUpdatedEventInterface
| DeleteGroupEventInterface
| LockGroupEventInterface
| PlayerDetailsUpdatedInterface
>();
private initPosition: PositionInterface | null = null;
@ -225,6 +233,7 @@ export class GameScene extends DirtyScene {
});
this.Terrains = [];
this.groups = new Map<number, Sprite>();
this.currentPlayerGroupId = undefined;
this.instance = room.getInstance();
this.MapUrlFile = MapUrlFile;
@ -720,13 +729,17 @@ export class GameScene extends DirtyScene {
);
this.input.keyboard.on("keydown-L", (event: Event) => {
if (this.currentPlayerGroupId !== undefined) {
console.log("group locked");
this.connection?.emitLockGroup(1, true);
this.connection?.emitLockGroup(this.currentPlayerGroupId, true);
}
});
this.input.keyboard.on("keydown-U", (event: Event) => {
if (this.currentPlayerGroupId !== undefined) {
console.log("group unlocked");
this.connection?.emitLockGroup(1, false);
this.connection?.emitLockGroup(this.currentPlayerGroupId, false);
}
});
}
@ -792,7 +805,6 @@ export class GameScene extends DirtyScene {
this.connection.groupUpdateMessageStream.subscribe(
(groupPositionMessage: GroupCreatedUpdatedMessageInterface) => {
console.log(groupPositionMessage);
this.shareGroupPosition(groupPositionMessage);
}
);
@ -805,6 +817,10 @@ 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();
@ -838,6 +854,12 @@ export class GameScene extends DirtyScene {
});
});
this.connection.groupUsersUpdateMessageStream.subscribe((message) => {
this.currentPlayerGroupId = message.groupId;
console.log("GOT GROUP USERS UPDATE MESSAGE");
console.log(message);
});
/**
* Triggered when we receive the JWT token to connect to Jitsi
*/
@ -1820,19 +1842,22 @@ ${escapedMessage}
break;
}
case "GroupCreatedUpdatedEvent":
console.log("CREATE OR UPDATE GROUP");
this.doShareGroupPosition(event.event);
break;
// TODO: CALL THIS ON GROUP LOCK CHANGE
// case "GroupCreatedUpdatedEvent":
// this.doShareGroupPosition(event.event);
// break;
case "DeleteGroupEvent":
this.doDeleteGroup(event.groupId);
break;
case "PlayerDetailsUpdated":
this.doUpdatePlayerDetails(event.details);
break;
case "DeleteGroupEvent": {
this.doDeleteGroup(event.groupId);
if (this.currentPlayerGroupId === event.groupId) {
this.currentPlayerGroupId = undefined;
}
break;
}
case "LockGroupEvent": {
this.doLockGroup(event.event);
break;
}
default: {
const tmp: never = event;
}
@ -2027,6 +2052,21 @@ ${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;
}
group.setTexture(event.lock ? "circleSprite-red" : "circleSprite-white");
}
doUpdatePlayerDetails(message: PlayerDetailsUpdatedMessageInterface): void {
const character = this.MapPlayersByKey.get(message.userId);
if (character === undefined) {

View File

@ -434,6 +434,7 @@ message SubToPusherMessage {
EmoteEventMessage emoteEventMessage = 9;
ErrorMessage errorMessage = 10;
PlayerDetailsUpdatedMessage playerDetailsUpdatedMessage = 11;
LockGroupMessage lockGroupMessage = 12;
}
}

View File

@ -18,6 +18,7 @@ import {
ErrorMessage,
PlayerDetailsUpdatedMessage,
SetPlayerDetailsMessage,
LockGroupMessage,
} from "../Messages/generated/messages_pb";
import { ClientReadableStream } from "grpc";
import { PositionDispatcher } from "_Model/PositionDispatcher";
@ -32,7 +33,7 @@ export interface ZoneEventListener {
onGroupEnters(group: GroupDescriptor, listener: ExSocketInterface): void;
onGroupMoves(group: GroupDescriptor, listener: ExSocketInterface): void;
onGroupLeaves(groupId: number, listener: ExSocketInterface): void;
// onGroupLock(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;
@ -246,9 +247,9 @@ export class Zone {
} else if (message.hasEmoteeventmessage()) {
const emoteEventMessage = message.getEmoteeventmessage() as EmoteEventMessage;
this.notifyEmote(emoteEventMessage);
} 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;
@ -359,6 +360,12 @@ 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

@ -306,6 +306,13 @@ 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