From 8cc1584c2ed773ddf0b58b7dc65b597bf0c0b6fb Mon Sep 17 00:00:00 2001 From: _Bastler <_Bastler@bstly.de> Date: Mon, 27 Sep 2021 20:00:45 +0200 Subject: [PATCH] worlds + fix map update --- pusher/src/Controller/MapController.ts | 62 +++++++++++++++++++++++++- pusher/src/Model/PusherRoom.ts | 4 ++ pusher/src/Services/SocketManager.ts | 18 ++++++-- 3 files changed, 78 insertions(+), 6 deletions(-) diff --git a/pusher/src/Controller/MapController.ts b/pusher/src/Controller/MapController.ts index ae8135a3..862baacd 100644 --- a/pusher/src/Controller/MapController.ts +++ b/pusher/src/Controller/MapController.ts @@ -3,7 +3,7 @@ import { BaseController } from "./BaseController"; import { parse } from "query-string"; import { adminApi } from "../Services/AdminApi"; import { ADMIN_API_URL } from "../Enum/EnvironmentVariable"; -import { GameRoomPolicyTypes } from "../Model/PusherRoom"; +import { PusherRoom, GameRoomPolicyTypes } from "../Model/PusherRoom"; import { MapDetailsData } from "../Services/AdminApi/MapDetailsData"; import { socketManager } from "../Services/SocketManager"; import { AuthTokenData, jwtTokenManager } from "../Services/JWTTokenManager"; @@ -14,6 +14,7 @@ export class MapController extends BaseController { super(); this.App = App; this.getMapUrl(); + this.getWorlds(); } // Returns a map mapping map name to file name of the map @@ -51,7 +52,7 @@ export class MapController extends BaseController { return; } - const mapUrl = roomUrl.protocol + "//" + match[1]; + const mapUrl = roomUrl.protocol + "//" + match[ 1 ]; res.writeStatus("200 OK"); this.addCorsHeaders(res); @@ -95,4 +96,61 @@ export class MapController extends BaseController { })(); }); } + + + getWorlds() { + this.App.options("/worlds", (res: HttpResponse, req: HttpRequest) => { + this.addCorsHeaders(res); + + res.end(); + }); + + this.App.get("/worlds", async (res: HttpResponse, req: HttpRequest) => { + res.onAborted(() => { + console.warn("/message request was aborted"); + }); + const { userIdentify, token } = parse(req.getQuery()); + try { + //verify connected by token + if (token != undefined) { + try { + const authTokenData: AuthTokenData = jwtTokenManager.verifyJWTToken(token as string, false); + if (authTokenData.hydraAccessToken == undefined) { + throw Error("Token cannot to be check on Hydra"); + } + + const worlds: Map = socketManager.getWorlds(); + + const result: any = {}; + + for (const room of worlds.values()) { + result[room.roomUrl] = room.getListeners().size; + /* + for (const listener of room.getListeners()) { + const position: any = {}; + position.name = listener.name; + position.roomId = listener.roomId; + position.position = listener.position; + position.viewport = listener.viewport; + result.push(position); + } + */ + } + + return res + .writeStatus("200 OK") + .writeHeader("Content-Type", "application/json") + .end( + JSON.stringify(result) + ); + } catch (error) { + return this.errorToResponse(error, res); + } + } + } catch (error) { + console.error("getWorlds => ERROR", error); + this.errorToResponse(error, res); + } + }); + } } diff --git a/pusher/src/Model/PusherRoom.ts b/pusher/src/Model/PusherRoom.ts index 89ed772a..27e33451 100644 --- a/pusher/src/Model/PusherRoom.ts +++ b/pusher/src/Model/PusherRoom.ts @@ -80,6 +80,10 @@ export class PusherRoom { } } + public getListeners(): Set { + return this.listeners; + } + /** * Creates a connection to the back server to track global messages relative to this room (like variable changes). */ diff --git a/pusher/src/Services/SocketManager.ts b/pusher/src/Services/SocketManager.ts index 31e84de7..c48fdaa3 100644 --- a/pusher/src/Services/SocketManager.ts +++ b/pusher/src/Services/SocketManager.ts @@ -34,6 +34,7 @@ import { VariableMessage, ErrorMessage, WorldFullMessage, + PositionMessage, } from "../Messages/generated/messages_pb"; import { ProtobufUtils } from "../Model/Websocket/ProtobufUtils"; import { ADMIN_API_URL, JITSI_ISS, JITSI_URL, SECRET_JITSI_KEY, PUSHER_FORCE_ROOM_UPDATE } from "../Enum/EnvironmentVariable"; @@ -53,10 +54,10 @@ import { CharacterTexture } from "./AdminApi/CharacterTexture"; const debug = Debug("socket"); interface AdminSocketRoomsList { - [index: string]: number; + [ index: string ]: number; } interface AdminSocketUsersList { - [index: string]: boolean; + [ index: string ]: boolean; } export interface AdminSocketData { @@ -249,6 +250,13 @@ export class SocketManager implements ZoneEventListener { client.backConnection.write(pusherToBackMessage); + const position = userMovesMessage.getPosition(); + if (position === undefined) { + throw new Error("Missing position in UserMovesMessage"); + } + // update position + client.position = ProtobufUtils.toPointInterface(position); + const viewport = userMovesMessage.getViewport(); if (viewport === undefined) { throw new Error("Missing viewport in UserMovesMessage"); @@ -366,13 +374,15 @@ export class SocketManager implements ZoneEventListener { async getOrCreateRoom(roomUrl: string): Promise { //check and create new world for a room let room = this.rooms.get(roomUrl); - if (PUSHER_FORCE_ROOM_UPDATE || room === undefined) { + if (room === undefined) { room = new PusherRoom(roomUrl, this); if (ADMIN_API_URL) { await this.updateRoomWithAdminData(room); } await room.init(); this.rooms.set(roomUrl, room); + } else if (PUSHER_FORCE_ROOM_UPDATE && ADMIN_API_URL) { + await this.updateRoomWithAdminData(room); } return room; } @@ -625,7 +635,7 @@ export class SocketManager implements ZoneEventListener { if (playGlobalMessageEvent.getBroadcasttoworld()) { tabUrlRooms = await adminApi.getUrlRoomsFromSameWorld(clientRoomUrl); } else { - tabUrlRooms = [clientRoomUrl]; + tabUrlRooms = [ clientRoomUrl ]; } const roomMessage = new AdminRoomMessage();