From 9d6608562394e4b614678299323ef902402007b3 Mon Sep 17 00:00:00 2001 From: PizZaKatZe Date: Mon, 22 Mar 2021 23:08:57 +0100 Subject: [PATCH] Front federation (API and Jitsi) --- front/src/Connexion/ConnectionManager.ts | 6 +++--- front/src/Connexion/RoomConnection.ts | 4 ++-- front/src/Phaser/Game/GameScene.ts | 27 +++++++++++++++++++++++- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/front/src/Connexion/ConnectionManager.ts b/front/src/Connexion/ConnectionManager.ts index 932fb1fc..7bac4ba0 100644 --- a/front/src/Connexion/ConnectionManager.ts +++ b/front/src/Connexion/ConnectionManager.ts @@ -88,9 +88,9 @@ class ConnectionManager { this.localUser = new LocalUser('', 'test', []); } - public connectToRoomSocket(roomId: string, name: string, characterLayers: string[], position: PositionInterface, viewport: ViewportInterface, companion: string|null): Promise { + public connectToRoomSocket(apiUrl: string, roomId: string, name: string, characterLayers: string[], position: PositionInterface, viewport: ViewportInterface, companion: string|null): Promise { return new Promise((resolve, reject) => { - const connection = new RoomConnection(this.localUser.jwtToken, roomId, name, characterLayers, position, viewport, companion); + const connection = new RoomConnection(apiUrl, this.localUser.jwtToken, roomId, name, characterLayers, position, viewport, companion); connection.onConnectError((error: object) => { console.log('An error occurred while connecting to socket server. Retrying'); reject(error); @@ -111,7 +111,7 @@ class ConnectionManager { this.reconnectingTimeout = setTimeout(() => { //todo: allow a way to break recursion? //todo: find a way to avoid recursive function. Otherwise, the call stack will grow indefinitely. - this.connectToRoomSocket(roomId, name, characterLayers, position, viewport, companion).then((connection) => resolve(connection)); + this.connectToRoomSocket(apiUrl, roomId, name, characterLayers, position, viewport, companion).then((connection) => resolve(connection)); }, 4000 + Math.floor(Math.random() * 2000) ); }); }); diff --git a/front/src/Connexion/RoomConnection.ts b/front/src/Connexion/RoomConnection.ts index a64e5bfd..abfda56a 100644 --- a/front/src/Connexion/RoomConnection.ts +++ b/front/src/Connexion/RoomConnection.ts @@ -70,8 +70,8 @@ export class RoomConnection implements RoomConnection { * @param token A JWT token containing the UUID of the user * @param roomId The ID of the room in the form "_/[instance]/[map_url]" or "@/[org]/[event]/[map]" */ - public constructor(token: string|null, roomId: string, name: string, characterLayers: string[], position: PositionInterface, viewport: ViewportInterface, companion: string|null) { - let url = new URL(PUSHER_URL, window.location.toString()).toString(); + public constructor(apiUrl: string, token: string|null, roomId: string, name: string, characterLayers: string[], position: PositionInterface, viewport: ViewportInterface, companion: string|null) { + let url = new URL(apiUrl, window.location.toString()).toString(); url = url.replace('http://', 'ws://').replace('https://', 'wss://'); if (!url.endsWith('/')) { url += '/'; diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index 5000e269..05c68842 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -13,8 +13,10 @@ import {hasMovedEventName, Player, requestEmoteEventName} from "../Player/Player import { DEBUG_MODE, JITSI_PRIVATE_MODE, + JITSI_URL, MAX_PER_GROUP, POSITION_DELAY, + PUSHER_URL } from "../../Enum/EnvironmentVariable"; import type { ITiledMap, @@ -134,6 +136,24 @@ interface DeleteGroupEventInterface { groupId: number } +interface MapProperty { + name: string; + type: string; + value: string|number; +} + +// FIXME: See onMapLoad +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function getMapProperty(mapData: any, name: string, type: string, dflt: string|undefined = undefined): any { + if (!Array.isArray(mapData.properties)) { + return dflt; + } + + const properties: MapProperty[] = mapData.properties; + const prop = properties.find((prop: MapProperty) => prop.type === type && prop.name === name) + return prop ? prop.value : dflt; +} + const defaultStartLayerName = 'start'; export class GameScene extends DirtyScene implements CenterListener { @@ -167,6 +187,7 @@ export class GameScene extends DirtyScene implements CenterListener { MapUrlFile: string; RoomId: string; instance: string; + apiUrl: string = PUSHER_URL; currentTick!: number; lastSentTick!: number; // The last tick at which a position was sent. @@ -292,6 +313,9 @@ export class GameScene extends DirtyScene implements CenterListener { // FIXME: we need to put a "unknown" instead of a "any" and validate the structure of the JSON we are receiving. // eslint-disable-next-line @typescript-eslint/no-explicit-any private async onMapLoad(data: any): Promise { + this.apiUrl = getMapProperty(data.data, 'apiUrl', 'string', PUSHER_URL); + console.info('apiUrl:', this.apiUrl); + // Triggered when the map is loaded // Load tiles attached to the map recursively this.mapFile = data.data; @@ -539,6 +563,7 @@ export class GameScene extends DirtyScene implements CenterListener { const camera = this.cameras.main; connectionManager.connectToRoomSocket( + this.apiUrl, this.RoomId, this.playerName, this.characterLayers, @@ -1583,7 +1608,7 @@ ${escapedMessage} const allProps = this.gameMap.getCurrentProperties(); const jitsiConfig = this.safeParseJSONstring(allProps.get("jitsiConfig") as string|undefined, 'jitsiConfig'); const jitsiInterfaceConfig = this.safeParseJSONstring(allProps.get("jitsiInterfaceConfig") as string|undefined, 'jitsiInterfaceConfig'); - const jitsiUrl = allProps.get("jitsiUrl") as string|undefined; + const jitsiUrl = (allProps.get("jitsiUrl") as string|undefined) || getMapProperty(this.mapFile, 'jitsiUrl', 'string', JITSI_URL); jitsiFactory.start(roomName, this.playerName, jwt, jitsiConfig, jitsiInterfaceConfig, jitsiUrl); this.connection?.setSilent(true);