diff --git a/.gitignore b/.gitignore index 2cbf66b1..8fa69985 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ docker-compose.override.yaml maps/yarn.lock maps/dist/computer.js maps/dist/computer.js.map -/node_modules/ +node_modules +_ \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b85d0a98..8bbbc93e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -42,7 +42,7 @@ Before committing, be sure to install the "Prettier" precommit hook that will re In order to enable the "Prettier" precommit hook, at the root of the project, run: ```console -$ yarn run install +$ yarn install $ yarn run prepare ``` diff --git a/front/package.json b/front/package.json index 0e0439ed..9c592578 100644 --- a/front/package.json +++ b/front/package.json @@ -61,7 +61,7 @@ "serve": "cross-env TS_NODE_PROJECT=\"tsconfig-for-webpack.json\" webpack serve --open", "build": "cross-env TS_NODE_PROJECT=\"tsconfig-for-webpack.json\" NODE_ENV=production webpack", "build-typings": "cross-env TS_NODE_PROJECT=\"tsconfig-for-webpack.json\" NODE_ENV=production BUILD_TYPINGS=1 webpack", - "test": "TS_NODE_PROJECT=\"tsconfig-for-jasmine.json\" ts-node node_modules/jasmine/bin/jasmine --config=jasmine.json", + "test": "cross-env TS_NODE_PROJECT=\"tsconfig-for-jasmine.json\" ts-node node_modules/jasmine/bin/jasmine --config=jasmine.json", "lint": "node_modules/.bin/eslint src/ . --ext .ts", "fix": "node_modules/.bin/eslint --fix src/ . --ext .ts", "precommit": "lint-staged", diff --git a/front/src/Api/Events/DataLayerEvent.ts b/front/src/Api/Events/DataLayerEvent.ts index 096d6ef5..3062c1bc 100644 --- a/front/src/Api/Events/DataLayerEvent.ts +++ b/front/src/Api/Events/DataLayerEvent.ts @@ -1,13 +1,12 @@ import * as tg from "generic-type-guard"; - - -export const isDataLayerEvent = - new tg.IsInterface().withProperties({ - data: tg.isObject - }).get(); +export const isDataLayerEvent = new tg.IsInterface() + .withProperties({ + data: tg.isObject, + }) + .get(); /** * A message sent from the game to the iFrame when the data of the layers change after the iFrame send a message to the game that it want to listen to the data of the layers */ -export type DataLayerEvent = tg.GuardedType; \ No newline at end of file +export type DataLayerEvent = tg.GuardedType; diff --git a/front/src/Api/Events/GameStateEvent.ts b/front/src/Api/Events/GameStateEvent.ts index 85fb37e9..edeeef80 100644 --- a/front/src/Api/Events/GameStateEvent.ts +++ b/front/src/Api/Events/GameStateEvent.ts @@ -1,14 +1,15 @@ import * as tg from "generic-type-guard"; -export const isGameStateEvent = - new tg.IsInterface().withProperties({ - roomId: tg.isString, - mapUrl: tg.isString, - nickname: tg.isUnion(tg.isString, tg.isNull), - uuid: tg.isUnion(tg.isString, tg.isUndefined), - startLayerName: tg.isUnion(tg.isString, tg.isNull), - tags : tg.isArray(tg.isString), - }).get(); +export const isGameStateEvent = new tg.IsInterface() + .withProperties({ + roomId: tg.isString, + mapUrl: tg.isString, + nickname: tg.isUnion(tg.isString, tg.isNull), + uuid: tg.isUnion(tg.isString, tg.isUndefined), + startLayerName: tg.isUnion(tg.isString, tg.isNull), + tags: tg.isArray(tg.isString), + }) + .get(); /** * A message sent from the game to the iFrame when the gameState is received by the script */ diff --git a/front/src/Api/Events/HasPlayerMovedEvent.ts b/front/src/Api/Events/HasPlayerMovedEvent.ts index 50f017df..87b45482 100644 --- a/front/src/Api/Events/HasPlayerMovedEvent.ts +++ b/front/src/Api/Events/HasPlayerMovedEvent.ts @@ -1,19 +1,17 @@ import * as tg from "generic-type-guard"; - - -export const isHasPlayerMovedEvent = - new tg.IsInterface().withProperties({ - direction: tg.isElementOf('right', 'left', 'up', 'down'), +export const isHasPlayerMovedEvent = new tg.IsInterface() + .withProperties({ + direction: tg.isElementOf("right", "left", "up", "down"), moving: tg.isBoolean, x: tg.isNumber, - y: tg.isNumber - }).get(); + y: tg.isNumber, + }) + .get(); /** * A message sent from the game to the iFrame to notify a movement from the current player. */ export type HasPlayerMovedEvent = tg.GuardedType; - -export type HasPlayerMovedEventCallback = (event: HasPlayerMovedEvent) => void +export type HasPlayerMovedEventCallback = (event: HasPlayerMovedEvent) => void; diff --git a/front/src/Api/Events/IframeEvent.ts b/front/src/Api/Events/IframeEvent.ts index 137eccad..67775c03 100644 --- a/front/src/Api/Events/IframeEvent.ts +++ b/front/src/Api/Events/IframeEvent.ts @@ -1,73 +1,71 @@ - -import type { GameStateEvent } from './GameStateEvent'; -import type { ButtonClickedEvent } from './ButtonClickedEvent'; -import type { ChatEvent } from './ChatEvent'; -import type { ClosePopupEvent } from './ClosePopupEvent'; -import type { EnterLeaveEvent } from './EnterLeaveEvent'; -import type { GoToPageEvent } from './GoToPageEvent'; -import type { LoadPageEvent } from './LoadPageEvent'; -import type { OpenCoWebSiteEvent } from './OpenCoWebSiteEvent'; -import type { OpenPopupEvent } from './OpenPopupEvent'; -import type { OpenTabEvent } from './OpenTabEvent'; -import type { UserInputChatEvent } from './UserInputChatEvent'; +import type { GameStateEvent } from "./GameStateEvent"; +import type { ButtonClickedEvent } from "./ButtonClickedEvent"; +import type { ChatEvent } from "./ChatEvent"; +import type { ClosePopupEvent } from "./ClosePopupEvent"; +import type { EnterLeaveEvent } from "./EnterLeaveEvent"; +import type { GoToPageEvent } from "./GoToPageEvent"; +import type { LoadPageEvent } from "./LoadPageEvent"; +import type { OpenCoWebSiteEvent } from "./OpenCoWebSiteEvent"; +import type { OpenPopupEvent } from "./OpenPopupEvent"; +import type { OpenTabEvent } from "./OpenTabEvent"; +import type { UserInputChatEvent } from "./UserInputChatEvent"; import type { DataLayerEvent } from "./DataLayerEvent"; -import type { LayerEvent } from './LayerEvent'; +import type { LayerEvent } from "./LayerEvent"; import type { SetPropertyEvent } from "./setPropertyEvent"; import type { LoadSoundEvent } from "./LoadSoundEvent"; import type { PlaySoundEvent } from "./PlaySoundEvent"; import type { MenuItemClickedEvent } from "./ui/MenuItemClickedEvent"; -import type { MenuItemRegisterEvent } from './ui/MenuItemRegisterEvent'; +import type { MenuItemRegisterEvent } from "./ui/MenuItemRegisterEvent"; import type { HasPlayerMovedEvent } from "./HasPlayerMovedEvent"; import type { SetTilesEvent } from "./SetTilesEvent"; export interface TypedMessageEvent extends MessageEvent { - data: T + data: T; } export type IframeEventMap = { - //getState: GameStateEvent, - loadPage: LoadPageEvent - chat: ChatEvent, - openPopup: OpenPopupEvent - closePopup: ClosePopupEvent - openTab: OpenTabEvent - goToPage: GoToPageEvent - openCoWebSite: OpenCoWebSiteEvent - closeCoWebSite: null - disablePlayerControls: null - restorePlayerControls: null - displayBubble: null - removeBubble: null - onPlayerMove: undefined - showLayer: LayerEvent - hideLayer: LayerEvent - setProperty: SetPropertyEvent - getDataLayer: undefined - loadSound: LoadSoundEvent - playSound: PlaySoundEvent - stopSound: null - setTiles: SetTilesEvent - getState: undefined, - registerMenuCommand: MenuItemRegisterEvent -} + loadPage: LoadPageEvent; + chat: ChatEvent; + openPopup: OpenPopupEvent; + closePopup: ClosePopupEvent; + openTab: OpenTabEvent; + goToPage: GoToPageEvent; + openCoWebSite: OpenCoWebSiteEvent; + closeCoWebSite: null; + disablePlayerControls: null; + restorePlayerControls: null; + displayBubble: null; + removeBubble: null; + onPlayerMove: undefined; + showLayer: LayerEvent; + hideLayer: LayerEvent; + setProperty: SetPropertyEvent; + getDataLayer: undefined; + loadSound: LoadSoundEvent; + playSound: PlaySoundEvent; + stopSound: null; + getState: undefined; + registerMenuCommand: MenuItemRegisterEvent; + setTiles: SetTilesEvent; +}; export interface IframeEvent { type: T; data: IframeEventMap[T]; } - // eslint-disable-next-line @typescript-eslint/no-explicit-any -export const isIframeEventWrapper = (event: any): event is IframeEvent => typeof event.type === 'string'; +export const isIframeEventWrapper = (event: any): event is IframeEvent => + typeof event.type === "string"; export interface IframeResponseEventMap { - userInputChat: UserInputChatEvent - enterEvent: EnterLeaveEvent - leaveEvent: EnterLeaveEvent - buttonClickedEvent: ButtonClickedEvent - gameState: GameStateEvent - hasPlayerMoved: HasPlayerMovedEvent - dataLayer: DataLayerEvent - menuItemClicked: MenuItemClickedEvent + userInputChat: UserInputChatEvent; + enterEvent: EnterLeaveEvent; + leaveEvent: EnterLeaveEvent; + buttonClickedEvent: ButtonClickedEvent; + gameState: GameStateEvent; + hasPlayerMoved: HasPlayerMovedEvent; + dataLayer: DataLayerEvent; + menuItemClicked: MenuItemClickedEvent; } export interface IframeResponseEvent { type: T; @@ -75,4 +73,6 @@ export interface IframeResponseEvent { } // eslint-disable-next-line @typescript-eslint/no-explicit-any -export const isIframeResponseEventWrapper = (event: { type?: string }): event is IframeResponseEvent => typeof event.type === 'string'; +export const isIframeResponseEventWrapper = (event: { + type?: string; +}): event is IframeResponseEvent => typeof event.type === "string"; diff --git a/front/src/Api/Events/LayerEvent.ts b/front/src/Api/Events/LayerEvent.ts index f854248b..b56c3163 100644 --- a/front/src/Api/Events/LayerEvent.ts +++ b/front/src/Api/Events/LayerEvent.ts @@ -1,9 +1,10 @@ import * as tg from "generic-type-guard"; -export const isLayerEvent = - new tg.IsInterface().withProperties({ +export const isLayerEvent = new tg.IsInterface() + .withProperties({ name: tg.isString, - }).get(); + }) + .get(); /** * A message sent from the iFrame to the game to show/hide a layer. */ diff --git a/front/src/Api/Events/LoadPageEvent.ts b/front/src/Api/Events/LoadPageEvent.ts index 9bc7f32a..63600a28 100644 --- a/front/src/Api/Events/LoadPageEvent.ts +++ b/front/src/Api/Events/LoadPageEvent.ts @@ -1,13 +1,12 @@ import * as tg from "generic-type-guard"; - - -export const isLoadPageEvent = - new tg.IsInterface().withProperties({ +export const isLoadPageEvent = new tg.IsInterface() + .withProperties({ url: tg.isString, - }).get(); + }) + .get(); /** * A message sent from the iFrame to the game to add a message in the chat. */ -export type LoadPageEvent = tg.GuardedType; \ No newline at end of file +export type LoadPageEvent = tg.GuardedType; diff --git a/front/src/Api/Events/OpenCoWebSiteEvent.ts b/front/src/Api/Events/OpenCoWebSiteEvent.ts index d2937405..7b5e6070 100644 --- a/front/src/Api/Events/OpenCoWebSiteEvent.ts +++ b/front/src/Api/Events/OpenCoWebSiteEvent.ts @@ -1,13 +1,12 @@ import * as tg from "generic-type-guard"; - - -export const isOpenCoWebsite = - new tg.IsInterface().withProperties({ +export const isOpenCoWebsite = new tg.IsInterface() + .withProperties({ url: tg.isString, allowApi: tg.isBoolean, allowPolicy: tg.isString, - }).get(); + }) + .get(); /** * A message sent from the iFrame to the game to add a message in the chat. diff --git a/front/src/Api/Events/SetTilesEvent.ts b/front/src/Api/Events/SetTilesEvent.ts index 24dd2e35..c7f8f16d 100644 --- a/front/src/Api/Events/SetTilesEvent.ts +++ b/front/src/Api/Events/SetTilesEvent.ts @@ -1,14 +1,15 @@ import * as tg from "generic-type-guard"; -export const isSetTilesEvent = - tg.isArray( - new tg.IsInterface().withProperties({ +export const isSetTilesEvent = tg.isArray( + new tg.IsInterface() + .withProperties({ x: tg.isNumber, y: tg.isNumber, tile: tg.isUnion(tg.isNumber, tg.isString), - layer: tg.isString - }).get() - ); + layer: tg.isString, + }) + .get() +); /** * A message sent from the iFrame to the game to set one or many tiles. */ diff --git a/front/src/Api/Events/setPropertyEvent.ts b/front/src/Api/Events/setPropertyEvent.ts index 39785bc6..7335f781 100644 --- a/front/src/Api/Events/setPropertyEvent.ts +++ b/front/src/Api/Events/setPropertyEvent.ts @@ -1,12 +1,13 @@ import * as tg from "generic-type-guard"; -export const isSetPropertyEvent = - new tg.IsInterface().withProperties({ +export const isSetPropertyEvent = new tg.IsInterface() + .withProperties({ layerName: tg.isString, propertyName: tg.isString, - propertyValue: tg.isUnion(tg.isString, tg.isUnion(tg.isNumber, tg.isUnion(tg.isBoolean, tg.isUndefined))) - }).get(); + propertyValue: tg.isUnion(tg.isString, tg.isUnion(tg.isNumber, tg.isUnion(tg.isBoolean, tg.isUndefined))), + }) + .get(); /** * A message sent from the iFrame to the game to change the value of the property of the layer */ -export type SetPropertyEvent = tg.GuardedType; \ No newline at end of file +export type SetPropertyEvent = tg.GuardedType; diff --git a/front/src/Api/Events/ui/MenuItemClickedEvent.ts b/front/src/Api/Events/ui/MenuItemClickedEvent.ts index fad2944f..a8c8d0ed 100644 --- a/front/src/Api/Events/ui/MenuItemClickedEvent.ts +++ b/front/src/Api/Events/ui/MenuItemClickedEvent.ts @@ -1,12 +1,11 @@ import * as tg from "generic-type-guard"; -export const isMenuItemClickedEvent = - new tg.IsInterface().withProperties({ - menuItem: tg.isString - }).get(); +export const isMenuItemClickedEvent = new tg.IsInterface() + .withProperties({ + menuItem: tg.isString, + }) + .get(); /** * A message sent from the game to the iFrame when a menu item is clicked. */ export type MenuItemClickedEvent = tg.GuardedType; - - diff --git a/front/src/Api/Events/ui/MenuItemRegisterEvent.ts b/front/src/Api/Events/ui/MenuItemRegisterEvent.ts index 4a56d8a0..404bdb13 100644 --- a/front/src/Api/Events/ui/MenuItemRegisterEvent.ts +++ b/front/src/Api/Events/ui/MenuItemRegisterEvent.ts @@ -1,25 +1,26 @@ import * as tg from "generic-type-guard"; -import { Subject } from 'rxjs'; +import { Subject } from "rxjs"; -export const isMenuItemRegisterEvent = - new tg.IsInterface().withProperties({ - menutItem: tg.isString - }).get(); +export const isMenuItemRegisterEvent = new tg.IsInterface() + .withProperties({ + menutItem: tg.isString, + }) + .get(); /** * A message sent from the iFrame to the game to add a new menu item. */ export type MenuItemRegisterEvent = tg.GuardedType; -export const isMenuItemRegisterIframeEvent = - new tg.IsInterface().withProperties({ +export const isMenuItemRegisterIframeEvent = new tg.IsInterface() + .withProperties({ type: tg.isSingletonString("registerMenuCommand"), - data: isMenuItemRegisterEvent - }).get(); - + data: isMenuItemRegisterEvent, + }) + .get(); const _registerMenuCommandStream: Subject = new Subject(); export const registerMenuCommandStream = _registerMenuCommandStream.asObservable(); export function handleMenuItemRegistrationEvent(event: MenuItemRegisterEvent) { - _registerMenuCommandStream.next(event.menutItem) -} \ No newline at end of file + _registerMenuCommandStream.next(event.menutItem); +} diff --git a/front/src/Api/IframeListener.ts b/front/src/Api/IframeListener.ts index 3143238d..b97fc567 100644 --- a/front/src/Api/IframeListener.ts +++ b/front/src/Api/IframeListener.ts @@ -1,42 +1,41 @@ -import {Subject} from "rxjs"; -import {ChatEvent, isChatEvent} from "./Events/ChatEvent"; -import {HtmlUtils} from "../WebRtc/HtmlUtils"; -import type {EnterLeaveEvent} from "./Events/EnterLeaveEvent"; -import {isOpenPopupEvent, OpenPopupEvent} from "./Events/OpenPopupEvent"; -import {isOpenTabEvent, OpenTabEvent} from "./Events/OpenTabEvent"; -import type {ButtonClickedEvent} from "./Events/ButtonClickedEvent"; -import {ClosePopupEvent, isClosePopupEvent} from "./Events/ClosePopupEvent"; -import {scriptUtils} from "./ScriptUtils"; -import {GoToPageEvent, isGoToPageEvent} from "./Events/GoToPageEvent"; -import {isOpenCoWebsite, OpenCoWebSiteEvent} from "./Events/OpenCoWebSiteEvent"; +import { Subject } from "rxjs"; +import { ChatEvent, isChatEvent } from "./Events/ChatEvent"; +import { HtmlUtils } from "../WebRtc/HtmlUtils"; +import type { EnterLeaveEvent } from "./Events/EnterLeaveEvent"; +import { isOpenPopupEvent, OpenPopupEvent } from "./Events/OpenPopupEvent"; +import { isOpenTabEvent, OpenTabEvent } from "./Events/OpenTabEvent"; +import type { ButtonClickedEvent } from "./Events/ButtonClickedEvent"; +import { ClosePopupEvent, isClosePopupEvent } from "./Events/ClosePopupEvent"; +import { scriptUtils } from "./ScriptUtils"; +import { GoToPageEvent, isGoToPageEvent } from "./Events/GoToPageEvent"; +import { isOpenCoWebsite, OpenCoWebSiteEvent } from "./Events/OpenCoWebSiteEvent"; import { IframeEvent, IframeEventMap, IframeResponseEvent, IframeResponseEventMap, isIframeEventWrapper, - TypedMessageEvent + TypedMessageEvent, } from "./Events/IframeEvent"; -import type {UserInputChatEvent} from "./Events/UserInputChatEvent"; -import {isPlaySoundEvent, PlaySoundEvent} from "./Events/PlaySoundEvent"; -import {isStopSoundEvent, StopSoundEvent} from "./Events/StopSoundEvent"; -import {isLoadSoundEvent, LoadSoundEvent} from "./Events/LoadSoundEvent"; -import {isSetPropertyEvent, SetPropertyEvent} from "./Events/setPropertyEvent"; -import {isLayerEvent, LayerEvent} from "./Events/LayerEvent"; -import {isMenuItemRegisterEvent,} from "./Events/ui/MenuItemRegisterEvent"; -import type {DataLayerEvent} from "./Events/DataLayerEvent"; -import type {GameStateEvent} from "./Events/GameStateEvent"; -import type {HasPlayerMovedEvent} from "./Events/HasPlayerMovedEvent"; -import {isLoadPageEvent} from "./Events/LoadPageEvent"; -import {handleMenuItemRegistrationEvent, isMenuItemRegisterIframeEvent} from "./Events/ui/MenuItemRegisterEvent"; -import {SetTilesEvent, isSetTilesEvent} from "./Events/SetTilesEvent"; +import type { UserInputChatEvent } from "./Events/UserInputChatEvent"; +import { isPlaySoundEvent, PlaySoundEvent } from "./Events/PlaySoundEvent"; +import { isStopSoundEvent, StopSoundEvent } from "./Events/StopSoundEvent"; +import { isLoadSoundEvent, LoadSoundEvent } from "./Events/LoadSoundEvent"; +import { isSetPropertyEvent, SetPropertyEvent } from "./Events/setPropertyEvent"; +import { isLayerEvent, LayerEvent } from "./Events/LayerEvent"; +import { isMenuItemRegisterEvent } from "./Events/ui/MenuItemRegisterEvent"; +import type { DataLayerEvent } from "./Events/DataLayerEvent"; +import type { GameStateEvent } from "./Events/GameStateEvent"; +import type { HasPlayerMovedEvent } from "./Events/HasPlayerMovedEvent"; +import { isLoadPageEvent } from "./Events/LoadPageEvent"; +import { handleMenuItemRegistrationEvent, isMenuItemRegisterIframeEvent } from "./Events/ui/MenuItemRegisterEvent"; +import { SetTilesEvent, isSetTilesEvent } from "./Events/SetTilesEvent"; /** * Listens to messages from iframes and turn those messages into easy to use observables. * Also allows to send messages to those iframes. */ class IframeListener { - private readonly _chatStream: Subject = new Subject(); public readonly chatStream = this._chatStream.asObservable(); @@ -112,116 +111,113 @@ class IframeListener { private sendPlayerMove: boolean = false; init() { - window.addEventListener("message", (message: TypedMessageEvent>) => { - // Do we trust the sender of this message? - // Let's only accept messages from the iframe that are allowed. - // Note: maybe we could restrict on the domain too for additional security (in case the iframe goes to another domain). - let foundSrc: string | undefined; + window.addEventListener( + "message", + (message: TypedMessageEvent>) => { + // Do we trust the sender of this message? + // Let's only accept messages from the iframe that are allowed. + // Note: maybe we could restrict on the domain too for additional security (in case the iframe goes to another domain). + let foundSrc: string | undefined; - let iframe: HTMLIFrameElement; - for (iframe of this.iframes) { - if (iframe.contentWindow === message.source) { - foundSrc = iframe.src; - break; + let iframe: HTMLIFrameElement; + for (iframe of this.iframes) { + if (iframe.contentWindow === message.source) { + foundSrc = iframe.src; + break; + } } - } - const payload = message.data; + const payload = message.data; + + if (foundSrc === undefined) { + if (isIframeEventWrapper(payload)) { + console.warn( + "It seems an iFrame is trying to communicate with WorkAdventure but was not explicitly granted the permission to do so. " + + "If you are looking to use the WorkAdventure Scripting API inside an iFrame, you should allow the " + + 'iFrame to communicate with WorkAdventure by using the "openWebsiteAllowApi" property in your map (or passing "true" as a second' + + "parameter to WA.nav.openCoWebSite())" + ); + } + return; + } + + foundSrc = this.getBaseUrl(foundSrc, message.source); - if (foundSrc === undefined) { if (isIframeEventWrapper(payload)) { - console.warn('It seems an iFrame is trying to communicate with WorkAdventure but was not explicitly granted the permission to do so. ' + - 'If you are looking to use the WorkAdventure Scripting API inside an iFrame, you should allow the ' + - 'iFrame to communicate with WorkAdventure by using the "openWebsiteAllowApi" property in your map (or passing "true" as a second' + - 'parameter to WA.nav.openCoWebSite())'); + if (payload.type === "showLayer" && isLayerEvent(payload.data)) { + this._showLayerStream.next(payload.data); + } else if (payload.type === "hideLayer" && isLayerEvent(payload.data)) { + this._hideLayerStream.next(payload.data); + } else if (payload.type === "setProperty" && isSetPropertyEvent(payload.data)) { + this._setPropertyStream.next(payload.data); + } else if (payload.type === "chat" && isChatEvent(payload.data)) { + this._chatStream.next(payload.data); + } else if (payload.type === "openPopup" && isOpenPopupEvent(payload.data)) { + this._openPopupStream.next(payload.data); + } else if (payload.type === "closePopup" && isClosePopupEvent(payload.data)) { + this._closePopupStream.next(payload.data); + } else if (payload.type === "openTab" && isOpenTabEvent(payload.data)) { + scriptUtils.openTab(payload.data.url); + } else if (payload.type === "goToPage" && isGoToPageEvent(payload.data)) { + scriptUtils.goToPage(payload.data.url); + } else if (payload.type === "loadPage" && isLoadPageEvent(payload.data)) { + this._loadPageStream.next(payload.data.url); + } else if (payload.type === "playSound" && isPlaySoundEvent(payload.data)) { + this._playSoundStream.next(payload.data); + } else if (payload.type === "stopSound" && isStopSoundEvent(payload.data)) { + this._stopSoundStream.next(payload.data); + } else if (payload.type === "loadSound" && isLoadSoundEvent(payload.data)) { + this._loadSoundStream.next(payload.data); + } else if (payload.type === "openCoWebSite" && isOpenCoWebsite(payload.data)) { + scriptUtils.openCoWebsite( + payload.data.url, + foundSrc, + payload.data.allowApi, + payload.data.allowPolicy + ); + } else if (payload.type === "closeCoWebSite") { + scriptUtils.closeCoWebSite(); + } else if (payload.type === "disablePlayerControls") { + this._disablePlayerControlStream.next(); + } else if (payload.type === "restorePlayerControls") { + this._enablePlayerControlStream.next(); + } else if (payload.type === "displayBubble") { + this._displayBubbleStream.next(); + } else if (payload.type === "removeBubble") { + this._removeBubbleStream.next(); + } else if (payload.type == "getState") { + this._gameStateStream.next(); + } else if (payload.type == "onPlayerMove") { + this.sendPlayerMove = true; + } else if (payload.type == "getDataLayer") { + this._dataLayerChangeStream.next(); + } else if (isMenuItemRegisterIframeEvent(payload)) { + const data = payload.data.menutItem; + // @ts-ignore + this.iframeCloseCallbacks.get(iframe).push(() => { + this._unregisterMenuCommandStream.next(data); + }); + handleMenuItemRegistrationEvent(payload.data); + } else if (payload.type == "setTiles" && isSetTilesEvent(payload.data)) { + this._setTilesStream.next(payload.data); + } } - return; - } - - foundSrc = this.getBaseUrl(foundSrc, message.source); - - if (isIframeEventWrapper(payload)) { - if (payload.type === 'showLayer' && isLayerEvent(payload.data)) { - this._showLayerStream.next(payload.data); - } else if (payload.type === 'hideLayer' && isLayerEvent(payload.data)) { - this._hideLayerStream.next(payload.data); - } else if (payload.type === 'setProperty' && isSetPropertyEvent(payload.data)) { - this._setPropertyStream.next(payload.data); - } else if (payload.type === 'chat' && isChatEvent(payload.data)) { - this._chatStream.next(payload.data); - } else if (payload.type === 'openPopup' && isOpenPopupEvent(payload.data)) { - this._openPopupStream.next(payload.data); - } else if (payload.type === 'closePopup' && isClosePopupEvent(payload.data)) { - this._closePopupStream.next(payload.data); - } - else if (payload.type === 'openTab' && isOpenTabEvent(payload.data)) { - scriptUtils.openTab(payload.data.url); - } - else if (payload.type === 'goToPage' && isGoToPageEvent(payload.data)) { - scriptUtils.goToPage(payload.data.url); - } - else if (payload.type === 'loadPage' && isLoadPageEvent(payload.data)) { - this._loadPageStream.next(payload.data.url); - } - else if (payload.type === 'playSound' && isPlaySoundEvent(payload.data)) { - this._playSoundStream.next(payload.data); - } - else if (payload.type === 'stopSound' && isStopSoundEvent(payload.data)) { - this._stopSoundStream.next(payload.data); - } - else if (payload.type === 'loadSound' && isLoadSoundEvent(payload.data)) { - this._loadSoundStream.next(payload.data); - } - else if (payload.type === 'openCoWebSite' && isOpenCoWebsite(payload.data)) { - scriptUtils.openCoWebsite(payload.data.url, foundSrc, payload.data.allowApi, payload.data.allowPolicy); - } - - else if (payload.type === 'closeCoWebSite') { - scriptUtils.closeCoWebSite(); - } - - else if (payload.type === 'disablePlayerControls') { - this._disablePlayerControlStream.next(); - } - else if (payload.type === 'restorePlayerControls') { - this._enablePlayerControlStream.next(); - } else if (payload.type === 'displayBubble') { - this._displayBubbleStream.next(); - } else if (payload.type === 'removeBubble') { - this._removeBubbleStream.next(); - } else if (payload.type == "getState") { - this._gameStateStream.next(); - } else if (payload.type == "onPlayerMove") { - this.sendPlayerMove = true - } else if (payload.type == "getDataLayer") { - this._dataLayerChangeStream.next(); - } else if (isMenuItemRegisterIframeEvent(payload)) { - const data = payload.data.menutItem; - // @ts-ignore - this.iframeCloseCallbacks.get(iframe).push(() => { - this._unregisterMenuCommandStream.next(data); - }) - handleMenuItemRegistrationEvent(payload.data) - } else if (payload.type == "setTiles" && isSetTilesEvent(payload.data)) { - this._setTilesStream.next(payload.data); - } - } - }, false); - + }, + false + ); } sendDataLayerEvent(dataLayerEvent: DataLayerEvent) { this.postMessage({ - 'type' : 'dataLayer', - 'data' : dataLayerEvent - }) + type: "dataLayer", + data: dataLayerEvent, + }); } - sendGameStateEvent(gameStateEvent: GameStateEvent) { this.postMessage({ - 'type': 'gameState', - 'data': gameStateEvent + type: "gameState", + data: gameStateEvent, }); } @@ -234,25 +230,25 @@ class IframeListener { } unregisterIframe(iframe: HTMLIFrameElement): void { - this.iframeCloseCallbacks.get(iframe)?.forEach(callback => { + this.iframeCloseCallbacks.get(iframe)?.forEach((callback) => { callback(); }); this.iframes.delete(iframe); } registerScript(scriptUrl: string): void { - console.log('Loading map related script at ', scriptUrl) + console.log("Loading map related script at ", scriptUrl); - if (!process.env.NODE_ENV || process.env.NODE_ENV === 'development') { + if (!process.env.NODE_ENV || process.env.NODE_ENV === "development") { // Using external iframe mode ( - const iframe = document.createElement('iframe'); + const iframe = document.createElement("iframe"); iframe.id = IframeListener.getIFrameId(scriptUrl); - iframe.style.display = 'none'; - iframe.src = '/iframe.html?script=' + encodeURIComponent(scriptUrl); + iframe.style.display = "none"; + iframe.src = "/iframe.html?script=" + encodeURIComponent(scriptUrl); // We are putting a sandbox on this script because it will run in the same domain as the main website. - iframe.sandbox.add('allow-scripts'); - iframe.sandbox.add('allow-top-navigation-by-user-activation'); + iframe.sandbox.add("allow-scripts"); + iframe.sandbox.add("allow-top-navigation-by-user-activation"); document.body.prepend(iframe); @@ -260,45 +256,50 @@ class IframeListener { this.registerIframe(iframe); } else { // production code - const iframe = document.createElement('iframe'); + const iframe = document.createElement("iframe"); iframe.id = IframeListener.getIFrameId(scriptUrl); - iframe.style.display = 'none'; + iframe.style.display = "none"; // We are putting a sandbox on this script because it will run in the same domain as the main website. - iframe.sandbox.add('allow-scripts'); - iframe.sandbox.add('allow-top-navigation-by-user-activation'); + iframe.sandbox.add("allow-scripts"); + iframe.sandbox.add("allow-top-navigation-by-user-activation"); //iframe.src = "data:text/html;charset=utf-8," + escape(html); - iframe.srcdoc = '\n' + - '\n' + + iframe.srcdoc = + "\n" + + "\n" + '\n' + - '\n' + - '\n' + - '\n' + - '\n' + - '\n' + - '\n'; + "\n" + + '\n' + + '\n' + + "\n" + + "\n" + + "\n"; document.body.prepend(iframe); this.scripts.set(scriptUrl, iframe); this.registerIframe(iframe); } - - } - private getBaseUrl(src: string, source: MessageEventSource | null): string{ - for (const script of this.scripts) { - if (script[1].contentWindow === source) { - return script[0]; - } - } - return src; + private getBaseUrl(src: string, source: MessageEventSource | null): string { + for (const script of this.scripts) { + if (script[1].contentWindow === source) { + return script[0]; + } + } + return src; } private static getIFrameId(scriptUrl: string): string { - return 'script' + btoa(scriptUrl); + return "script" + btoa(scriptUrl); } unregisterScript(scriptUrl: string): void { @@ -315,47 +316,47 @@ class IframeListener { sendUserInputChat(message: string) { this.postMessage({ - 'type': 'userInputChat', - 'data': { - 'message': message, - } as UserInputChatEvent + type: "userInputChat", + data: { + message: message, + } as UserInputChatEvent, }); } sendEnterEvent(name: string) { this.postMessage({ - 'type': 'enterEvent', - 'data': { - "name": name - } as EnterLeaveEvent + type: "enterEvent", + data: { + name: name, + } as EnterLeaveEvent, }); } sendLeaveEvent(name: string) { this.postMessage({ - 'type': 'leaveEvent', - 'data': { - "name": name - } as EnterLeaveEvent + type: "leaveEvent", + data: { + name: name, + } as EnterLeaveEvent, }); } hasPlayerMoved(event: HasPlayerMovedEvent) { if (this.sendPlayerMove) { this.postMessage({ - 'type': 'hasPlayerMoved', - 'data': event + type: "hasPlayerMoved", + data: event, }); } } sendButtonClickedEvent(popupId: number, buttonId: number): void { this.postMessage({ - 'type': 'buttonClickedEvent', - 'data': { + type: "buttonClickedEvent", + data: { popupId, - buttonId - } as ButtonClickedEvent + buttonId, + } as ButtonClickedEvent, }); } @@ -364,10 +365,9 @@ class IframeListener { */ public postMessage(message: IframeResponseEvent) { for (const iframe of this.iframes) { - iframe.contentWindow?.postMessage(message, '*'); + iframe.contentWindow?.postMessage(message, "*"); } } - } export const iframeListener = new IframeListener(); diff --git a/front/src/Api/ScriptUtils.ts b/front/src/Api/ScriptUtils.ts index 75a18dc0..0dbe40fe 100644 --- a/front/src/Api/ScriptUtils.ts +++ b/front/src/Api/ScriptUtils.ts @@ -1,21 +1,19 @@ -import {coWebsiteManager} from "../WebRtc/CoWebsiteManager"; +import { coWebsiteManager } from "../WebRtc/CoWebsiteManager"; class ScriptUtils { - - public openTab(url : string){ + public openTab(url: string) { window.open(url); } - public goToPage(url : string){ - window.location.href = url; - + public goToPage(url: string) { + window.location.href = url; } public openCoWebsite(url: string, base: string, api: boolean, policy: string) { - coWebsiteManager.loadCoWebsite(url, base, api, policy); + coWebsiteManager.loadCoWebsite(url, base, api, policy); } - public closeCoWebSite(){ + public closeCoWebSite() { coWebsiteManager.closeCoWebsite(); } } diff --git a/front/src/Api/iframe/Ui/MenuItem.ts b/front/src/Api/iframe/Ui/MenuItem.ts index 9782ea7a..aa61d749 100644 --- a/front/src/Api/iframe/Ui/MenuItem.ts +++ b/front/src/Api/iframe/Ui/MenuItem.ts @@ -1,11 +1,11 @@ -import type { MenuItemClickedEvent } from '../../Events/ui/MenuItemClickedEvent'; -import { iframeListener } from '../../IframeListener'; +import type { MenuItemClickedEvent } from "../../Events/ui/MenuItemClickedEvent"; +import { iframeListener } from "../../IframeListener"; export function sendMenuClickedEvent(menuItem: string) { iframeListener.postMessage({ - 'type': 'menuItemClicked', - 'data': { + type: "menuItemClicked", + data: { menuItem: menuItem, - } as MenuItemClickedEvent + } as MenuItemClickedEvent, }); -} \ No newline at end of file +} diff --git a/front/src/Api/iframe/room.ts b/front/src/Api/iframe/room.ts index 84e9d6ca..78fad58b 100644 --- a/front/src/Api/iframe/room.ts +++ b/front/src/Api/iframe/room.ts @@ -16,7 +16,7 @@ const leaveStreams: Map> = new Map(); const stateResolvers = new Subject(); -let immutableDataPromise: Promise | undefined = undefined; +let immutableData: GameStateEvent; interface Room { id: string; @@ -32,21 +32,21 @@ interface User { } interface TileDescriptor { - x: number - y: number - tile: number | string - layer: string + x: number; + y: number; + tile: number | string; + layer: string; } - function getGameState(): Promise { - if (immutableDataPromise === undefined) { - immutableDataPromise = new Promise((resolver, thrower) => { + if (immutableData) { + return Promise.resolve(immutableData); + } else { + return new Promise((resolver, thrower) => { stateResolvers.subscribe(resolver); sendToWorkadventure({ type: "getState", data: null }); }); } - return immutableDataPromise; } function getDataLayer(): Promise { @@ -139,11 +139,10 @@ export class WorkadventureRoomCommands extends IframeApiContribution> (i * 8)) & 255; - color += ('00' + value.toString(16)).substr(-2); + color += ("00" + value.toString(16)).substr(-2); } return color; } @@ -20,8 +20,8 @@ export function srcObject(node: HTMLVideoElement, stream: MediaStream) { return { update(newStream: MediaStream) { if (node.srcObject != newStream) { - node.srcObject = newStream + node.srcObject = newStream; } - } - } + }, + }; } diff --git a/front/src/Connexion/Room.ts b/front/src/Connexion/Room.ts index 3ae8d2ed..434f9060 100644 --- a/front/src/Connexion/Room.ts +++ b/front/src/Connexion/Room.ts @@ -1,18 +1,18 @@ import Axios from "axios"; -import {PUSHER_URL} from "../Enum/EnvironmentVariable"; -import type {CharacterTexture} from "./LocalUser"; +import { PUSHER_URL } from "../Enum/EnvironmentVariable"; +import type { CharacterTexture } from "./LocalUser"; -export class MapDetail{ - constructor(public readonly mapUrl: string, public readonly textures : CharacterTexture[]|undefined) { +export class MapDetail { + constructor(public readonly mapUrl: string, public readonly textures: CharacterTexture[] | undefined) { } } export class Room { public readonly id: string; public readonly isPublic: boolean; - private mapUrl: string|undefined; - private textures: CharacterTexture[]|undefined; - private instance: string|undefined; + private mapUrl: string | undefined; + private textures: CharacterTexture[] | undefined; + private instance: string | undefined; private _search: URLSearchParams; constructor(id: string) { @@ -34,18 +34,21 @@ export class Room { this._search = new URLSearchParams(url.search); } - public static getIdFromIdentifier(identifier: string, baseUrl: string, currentInstance: string): {roomId: string, hash: string} { + public static getIdFromIdentifier(identifier: string, baseUrl: string, currentInstance: string): { roomId: string, hash: string | null } { let roomId = ''; - let hash = ''; + let hash = null; if (!identifier.startsWith('/_/') && !identifier.startsWith('/@/')) { //relative file link //Relative identifier can be deep enough to rewrite the base domain, so we cannot use the variable 'baseUrl' as the actual base url for the URL objects. //We instead use 'workadventure' as a dummy base value. const baseUrlObject = new URL(baseUrl); - const absoluteExitSceneUrl = new URL(identifier, 'http://workadventure/_/'+currentInstance+'/'+baseUrlObject.hostname+baseUrlObject.pathname); + const absoluteExitSceneUrl = new URL(identifier, 'http://workadventure/_/' + currentInstance + '/' + baseUrlObject.hostname + baseUrlObject.pathname); roomId = absoluteExitSceneUrl.pathname; //in case of a relative url, we need to create a public roomId roomId = roomId.substring(1); //remove the leading slash hash = absoluteExitSceneUrl.hash; hash = hash.substring(1); //remove the leading diese + if (!hash.length) { + hash = null + } } else { //absolute room Id const parts = identifier.split('#'); roomId = parts[0]; @@ -54,7 +57,7 @@ export class Room { hash = parts[1] } } - return {roomId, hash} + return { roomId, hash } } public async getMapDetail(): Promise { @@ -66,8 +69,8 @@ export class Room { if (this.isPublic) { const match = /_\/[^/]+\/(.+)/.exec(this.id); - if (!match) throw new Error('Could not extract url from "'+this.id+'"'); - this.mapUrl = window.location.protocol+'//'+match[1]; + if (!match) throw new Error('Could not extract url from "' + this.id + '"'); + this.mapUrl = window.location.protocol + '//' + match[1]; resolve(new MapDetail(this.mapUrl, this.textures)); return; } else { @@ -76,7 +79,7 @@ export class Room { Axios.get(`${PUSHER_URL}/map`, { params: urlParts - }).then(({data}) => { + }).then(({ data }) => { console.log('Map ', this.id, ' resolves to URL ', data.mapUrl); resolve(data); return; @@ -99,13 +102,13 @@ export class Room { if (this.isPublic) { const match = /_\/([^/]+)\/.+/.exec(this.id); - if (!match) throw new Error('Could not extract instance from "'+this.id+'"'); + if (!match) throw new Error('Could not extract instance from "' + this.id + '"'); this.instance = match[1]; return this.instance; } else { const match = /@\/([^/]+)\/([^/]+)\/.+/.exec(this.id); - if (!match) throw new Error('Could not extract instance from "'+this.id+'"'); - this.instance = match[1]+'/'+match[2]; + if (!match) throw new Error('Could not extract instance from "' + this.id + '"'); + this.instance = match[1] + '/' + match[2]; return this.instance; } } @@ -114,7 +117,7 @@ export class Room { const regex = /@\/([^/]+)\/([^/]+)(?:\/([^/]*))?/gm; const match = regex.exec(url); if (!match) { - throw new Error('Invalid URL '+url); + throw new Error('Invalid URL ' + url); } const results: { organizationSlug: string, worldSlug: string, roomSlug?: string } = { organizationSlug: match[1], @@ -126,8 +129,7 @@ export class Room { return results; } - public isDisconnected(): boolean - { + public isDisconnected(): boolean { const alone = this._search.get('alone'); if (alone && alone !== '0' && alone.toLowerCase() !== 'false') { return true; diff --git a/front/src/Connexion/RoomConnection.ts b/front/src/Connexion/RoomConnection.ts index dc9830c3..1b080a55 100644 --- a/front/src/Connexion/RoomConnection.ts +++ b/front/src/Connexion/RoomConnection.ts @@ -11,7 +11,8 @@ import { RoomJoinedMessage, ServerToClientMessage, SetPlayerDetailsMessage, - SilentMessage, StopGlobalMessage, + SilentMessage, + StopGlobalMessage, UserJoinedMessage, UserLeftMessage, UserMovedMessage, @@ -31,17 +32,22 @@ import { EmotePromptMessage, SendUserMessage, BanUserMessage, -} from "../Messages/generated/messages_pb" +} from "../Messages/generated/messages_pb"; import type { UserSimplePeerInterface } from "../WebRtc/SimplePeer"; import Direction = PositionMessage.Direction; import { ProtobufClientUtils } from "../Network/ProtobufClientUtils"; import { EventMessage, - GroupCreatedUpdatedMessageInterface, ItemEventMessageInterface, - MessageUserJoined, OnConnectInterface, PlayGlobalMessageInterface, PositionInterface, + GroupCreatedUpdatedMessageInterface, + ItemEventMessageInterface, + MessageUserJoined, + OnConnectInterface, + PlayGlobalMessageInterface, + PositionInterface, RoomJoinedMessageInterface, - ViewportInterface, WebRtcDisconnectMessageInterface, + ViewportInterface, + WebRtcDisconnectMessageInterface, WebRtcSignalReceivedMessageInterface, } from "./ConnexionModels"; import type { BodyResourceDescriptionInterface } from "../Phaser/Entity/PlayerTextures"; @@ -61,7 +67,8 @@ export class RoomConnection implements RoomConnection { private closed: boolean = false; private tags: string[] = []; - public static setWebsocketFactory(websocketFactory: (url: string) => any): void { // eslint-disable-line @typescript-eslint/no-explicit-any + // eslint-disable-next-line @typescript-eslint/no-explicit-any + public static setWebsocketFactory(websocketFactory: (url: string) => any): void { RoomConnection.websocketFactory = websocketFactory; } @@ -70,27 +77,35 @@ 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) { + 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(); - url = url.replace('http://', 'ws://').replace('https://', 'wss://'); - if (!url.endsWith('/')) { - url += '/'; + url = url.replace("http://", "ws://").replace("https://", "wss://"); + if (!url.endsWith("/")) { + url += "/"; } - url += 'room'; - url += '?roomId=' + (roomId ? encodeURIComponent(roomId) : ''); - url += '&token=' + (token ? encodeURIComponent(token) : ''); - url += '&name=' + encodeURIComponent(name); + url += "room"; + url += "?roomId=" + (roomId ? encodeURIComponent(roomId) : ""); + url += "&token=" + (token ? encodeURIComponent(token) : ""); + url += "&name=" + encodeURIComponent(name); for (const layer of characterLayers) { - url += '&characterLayers=' + encodeURIComponent(layer); + url += "&characterLayers=" + encodeURIComponent(layer); } - url += '&x=' + Math.floor(position.x); - url += '&y=' + Math.floor(position.y); - url += '&top=' + Math.floor(viewport.top); - url += '&bottom=' + Math.floor(viewport.bottom); - url += '&left=' + Math.floor(viewport.left); - url += '&right=' + Math.floor(viewport.right); - if (typeof companion === 'string') { - url += '&companion=' + encodeURIComponent(companion); + url += "&x=" + Math.floor(position.x); + url += "&y=" + Math.floor(position.y); + url += "&top=" + Math.floor(viewport.top); + url += "&bottom=" + Math.floor(viewport.bottom); + url += "&left=" + Math.floor(viewport.left); + url += "&right=" + Math.floor(viewport.right); + if (typeof companion === "string") { + url += "&companion=" + encodeURIComponent(companion); } if (RoomConnection.websocketFactory) { @@ -99,7 +114,7 @@ export class RoomConnection implements RoomConnection { this.socket = new WebSocket(url); } - this.socket.binaryType = 'arraybuffer'; + this.socket.binaryType = "arraybuffer"; let interval: ReturnType | undefined = undefined; @@ -109,7 +124,7 @@ export class RoomConnection implements RoomConnection { interval = setInterval(() => this.socket.send(pingMessage.serializeBinary().buffer), manualPingDelay); }; - this.socket.addEventListener('close', (event) => { + this.socket.addEventListener("close", (event) => { if (interval) { clearInterval(interval); } @@ -126,7 +141,7 @@ export class RoomConnection implements RoomConnection { if (message.hasBatchmessage()) { for (const subMessage of (message.getBatchmessage() as BatchMessage).getPayloadList()) { - let event: string|null = null; + let event: string | null = null; let payload; if (subMessage.hasUsermovedmessage()) { event = EventMessage.USER_MOVED; @@ -150,7 +165,7 @@ export class RoomConnection implements RoomConnection { const emoteMessage = subMessage.getEmoteeventmessage() as EmoteEventMessage; emoteEventStream.fire(emoteMessage.getActoruserid(), emoteMessage.getEmote()); } else { - throw new Error('Unexpected batch message type'); + throw new Error("Unexpected batch message type"); } if (event) { @@ -171,8 +186,8 @@ export class RoomConnection implements RoomConnection { this.dispatch(EventMessage.CONNECT, { connection: this, room: { - items - } as RoomJoinedMessageInterface + items, + } as RoomJoinedMessageInterface, }); } else if (message.hasWorldfullmessage()) { worldFullMessageStream.onMessage(); @@ -183,7 +198,10 @@ export class RoomConnection implements RoomConnection { } else if (message.hasWebrtcsignaltoclientmessage()) { this.dispatch(EventMessage.WEBRTC_SIGNAL, message.getWebrtcsignaltoclientmessage()); } else if (message.hasWebrtcscreensharingsignaltoclientmessage()) { - this.dispatch(EventMessage.WEBRTC_SCREEN_SHARING_SIGNAL, message.getWebrtcscreensharingsignaltoclientmessage()); + this.dispatch( + EventMessage.WEBRTC_SCREEN_SHARING_SIGNAL, + message.getWebrtcscreensharingsignaltoclientmessage() + ); } else if (message.hasWebrtcstartmessage()) { this.dispatch(EventMessage.WEBRTC_START, message.getWebrtcstartmessage()); } else if (message.hasWebrtcdisconnectmessage()) { @@ -205,10 +223,9 @@ export class RoomConnection implements RoomConnection { } else if (message.hasRefreshroommessage()) { //todo: implement a way to notify the user the room was refreshed. } else { - throw new Error('Unknown message received'); + throw new Error("Unknown message received"); } - - } + }; } private dispatch(event: string, payload: unknown): void { @@ -243,16 +260,16 @@ export class RoomConnection implements RoomConnection { positionMessage.setY(Math.floor(y)); let directionEnum: Direction; switch (direction) { - case 'up': + case "up": directionEnum = Direction.UP; break; - case 'down': + case "down": directionEnum = Direction.DOWN; break; - case 'left': + case "left": directionEnum = Direction.LEFT; break; - case 'right': + case "right": directionEnum = Direction.RIGHT; break; default: @@ -327,15 +344,17 @@ export class RoomConnection implements RoomConnection { private toMessageUserJoined(message: UserJoinedMessage): MessageUserJoined { const position = message.getPosition(); if (position === undefined) { - throw new Error('Invalid JOIN_ROOM message'); + throw new Error("Invalid JOIN_ROOM message"); } - const characterLayers = message.getCharacterlayersList().map((characterLayer: CharacterLayerMessage): BodyResourceDescriptionInterface => { - return { - name: characterLayer.getName(), - img: characterLayer.getUrl() - } - }) + const characterLayers = message + .getCharacterlayersList() + .map((characterLayer: CharacterLayerMessage): BodyResourceDescriptionInterface => { + return { + name: characterLayer.getName(), + img: characterLayer.getUrl(), + }; + }); const companion = message.getCompanion(); @@ -345,8 +364,8 @@ export class RoomConnection implements RoomConnection { characterLayers, visitCardUrl: message.getVisitcardurl(), position: ProtobufClientUtils.toPointInterface(position), - companion: companion ? companion.getName() : null - } + companion: companion ? companion.getName() : null, + }; } public onUserMoved(callback: (message: UserMovedMessage) => void): void { @@ -372,7 +391,9 @@ export class RoomConnection implements RoomConnection { }); } - public onGroupUpdatedOrCreated(callback: (groupCreateUpdateMessage: GroupCreatedUpdatedMessageInterface) => void): void { + public onGroupUpdatedOrCreated( + callback: (groupCreateUpdateMessage: GroupCreatedUpdatedMessageInterface) => void + ): void { this.onMessage(EventMessage.GROUP_CREATE_UPDATE, (message: GroupUpdateMessage) => { callback(this.toGroupCreatedUpdatedMessage(message)); }); @@ -381,14 +402,14 @@ export class RoomConnection implements RoomConnection { private toGroupCreatedUpdatedMessage(message: GroupUpdateMessage): GroupCreatedUpdatedMessageInterface { const position = message.getPosition(); if (position === undefined) { - throw new Error('Missing position in GROUP_CREATE_UPDATE'); + throw new Error("Missing position in GROUP_CREATE_UPDATE"); } return { groupId: message.getGroupid(), position: position.toObject(), - groupSize: message.getGroupsize() - } + groupSize: message.getGroupsize(), + }; } public onGroupDeleted(callback: (groupId: number) => void): void { @@ -404,7 +425,7 @@ export class RoomConnection implements RoomConnection { } public onConnectError(callback: (error: Event) => void): void { - this.socket.addEventListener('error', callback) + this.socket.addEventListener("error", callback); } public onConnect(callback: (roomConnection: OnConnectInterface) => void): void { @@ -476,11 +497,11 @@ export class RoomConnection implements RoomConnection { } public onServerDisconnected(callback: () => void): void { - this.socket.addEventListener('close', (event) => { + this.socket.addEventListener("close", (event) => { if (this.closed === true || connectionManager.unloading) { return; } - console.log('Socket closed with code ' + event.code + ". Reason: " + event.reason); + console.log("Socket closed with code " + event.code + ". Reason: " + event.reason); if (event.code === 1000) { // Normal closure case return; @@ -490,14 +511,14 @@ export class RoomConnection implements RoomConnection { } public getUserId(): number { - if (this.userId === null) throw 'UserId cannot be null!' + if (this.userId === null) throw "UserId cannot be null!"; return this.userId; } disconnectMessage(callback: (message: WebRtcDisconnectMessageInterface) => void): void { this.onMessage(EventMessage.WEBRTC_DISCONNECT, (message: WebRtcDisconnectMessage) => { callback({ - userId: message.getUserid() + userId: message.getUserid(), }); }); } @@ -521,21 +542,22 @@ export class RoomConnection implements RoomConnection { itemId: message.getItemid(), event: message.getEvent(), parameters: JSON.parse(message.getParametersjson()), - state: JSON.parse(message.getStatejson()) + state: JSON.parse(message.getStatejson()), }); }); } public uploadAudio(file: FormData) { - return Axios.post(`${UPLOADER_URL}/upload-audio-message`, file).then((res: { data: {} }) => { - return res.data; - }).catch((err) => { - console.error(err); - throw err; - }); + return Axios.post(`${UPLOADER_URL}/upload-audio-message`, file) + .then((res: { data: {} }) => { + return res.data; + }) + .catch((err) => { + console.error(err); + throw err; + }); } - public receivePlayGlobalMessage(callback: (message: PlayGlobalMessageInterface) => void) { return this.onMessage(EventMessage.PLAY_GLOBAL_MESSAGE, (message: PlayGlobalMessage) => { callback({ @@ -605,12 +627,12 @@ export class RoomConnection implements RoomConnection { } public isAdmin(): boolean { - return this.hasTag('admin'); + return this.hasTag("admin"); } public emitEmoteEvent(emoteName: string): void { const emoteMessage = new EmotePromptMessage(); - emoteMessage.setEmote(emoteName) + emoteMessage.setEmote(emoteName); const clientToServerMessage = new ClientToServerMessage(); clientToServerMessage.setEmotepromptmessage(emoteMessage); @@ -618,7 +640,7 @@ export class RoomConnection implements RoomConnection { this.socket.send(clientToServerMessage.serializeBinary().buffer); } - public getAllTags() : string[] { + public getAllTags(): string[] { return this.tags; } } diff --git a/front/src/Phaser/Game/DirtyScene.ts b/front/src/Phaser/Game/DirtyScene.ts index 20602cca..13b0fa14 100644 --- a/front/src/Phaser/Game/DirtyScene.ts +++ b/front/src/Phaser/Game/DirtyScene.ts @@ -1,17 +1,17 @@ -import {ResizableScene} from "../Login/ResizableScene"; +import { ResizableScene } from "../Login/ResizableScene"; import GameObject = Phaser.GameObjects.GameObject; import Events = Phaser.Scenes.Events; import AnimationEvents = Phaser.Animations.Events; import StructEvents = Phaser.Structs.Events; -import {SKIP_RENDER_OPTIMIZATIONS} from "../../Enum/EnvironmentVariable"; +import { SKIP_RENDER_OPTIMIZATIONS } from "../../Enum/EnvironmentVariable"; /** * A scene that can track its dirty/pristine state. */ export abstract class DirtyScene extends ResizableScene { private isAlreadyTracking: boolean = false; - protected dirty:boolean = true; - private objectListChanged:boolean = true; + protected dirty: boolean = true; + private objectListChanged: boolean = true; private physicsEnabled: boolean = false; /** @@ -59,7 +59,6 @@ export abstract class DirtyScene extends ResizableScene { this.physicsEnabled = false; } }); - } private trackAnimation(): void { @@ -71,7 +70,7 @@ export abstract class DirtyScene extends ResizableScene { } public markDirty(): void { - this.events.once(Phaser.Scenes.Events.POST_UPDATE, () => this.dirty = true); + this.events.once(Phaser.Scenes.Events.POST_UPDATE, () => (this.dirty = true)); } public onResize(): void { diff --git a/front/src/Phaser/Game/GameManager.ts b/front/src/Phaser/Game/GameManager.ts index a694b32e..3e39de9a 100644 --- a/front/src/Phaser/Game/GameManager.ts +++ b/front/src/Phaser/Game/GameManager.ts @@ -1,26 +1,24 @@ -import {GameScene} from "./GameScene"; -import {connectionManager} from "../../Connexion/ConnectionManager"; -import type {Room} from "../../Connexion/Room"; -import {MenuScene, MenuSceneName} from "../Menu/MenuScene"; -import {LoginSceneName} from "../Login/LoginScene"; -import {SelectCharacterSceneName} from "../Login/SelectCharacterScene"; -import {EnableCameraSceneName} from "../Login/EnableCameraScene"; -import {localUserStore} from "../../Connexion/LocalUserStore"; -import {get} from "svelte/store"; -import {requestedCameraState, requestedMicrophoneState} from "../../Stores/MediaStore"; -import {helpCameraSettingsVisibleStore} from "../../Stores/HelpCameraSettingsStore"; - - +import { GameScene } from "./GameScene"; +import { connectionManager } from "../../Connexion/ConnectionManager"; +import type { Room } from "../../Connexion/Room"; +import { MenuScene, MenuSceneName } from "../Menu/MenuScene"; +import { LoginSceneName } from "../Login/LoginScene"; +import { SelectCharacterSceneName } from "../Login/SelectCharacterScene"; +import { EnableCameraSceneName } from "../Login/EnableCameraScene"; +import { localUserStore } from "../../Connexion/LocalUserStore"; +import { get } from "svelte/store"; +import { requestedCameraState, requestedMicrophoneState } from "../../Stores/MediaStore"; +import { helpCameraSettingsVisibleStore } from "../../Stores/HelpCameraSettingsStore"; /** * This class should be responsible for any scene starting/stopping */ export class GameManager { - private playerName: string|null; - private characterLayers: string[]|null; - private companion: string|null; - private startRoom!:Room; - currentGameSceneName: string|null = null; + private playerName: string | null; + private characterLayers: string[] | null; + private companion: string | null; + private startRoom!: Room; + currentGameSceneName: string | null = null; constructor() { this.playerName = localUserStore.getName(); @@ -51,23 +49,22 @@ export class GameManager { localUserStore.setCharacterLayers(layers); } - getPlayerName(): string|null { + getPlayerName(): string | null { return this.playerName; } getCharacterLayers(): string[] { if (!this.characterLayers) { - throw 'characterLayers are not set'; + throw "characterLayers are not set"; } return this.characterLayers; } - - setCompanion(companion: string|null): void { + setCompanion(companion: string | null): void { this.companion = companion; } - getCompanion(): string|null { + getCompanion(): string | null { return this.companion; } @@ -76,18 +73,21 @@ export class GameManager { const mapDetail = await room.getMapDetail(); const gameIndex = scenePlugin.getIndex(roomID); - if(gameIndex === -1){ - const game : Phaser.Scene = new GameScene(room, mapDetail.mapUrl); + if (gameIndex === -1) { + const game: Phaser.Scene = new GameScene(room, mapDetail.mapUrl); scenePlugin.add(roomID, game, false); } } public goToStartingMap(scenePlugin: Phaser.Scenes.ScenePlugin): void { - console.log('starting '+ (this.currentGameSceneName || this.startRoom.id)) + console.log("starting " + (this.currentGameSceneName || this.startRoom.id)); scenePlugin.start(this.currentGameSceneName || this.startRoom.id); scenePlugin.launch(MenuSceneName); - if(!localUserStore.getHelpCameraSettingsShown() && (!get(requestedMicrophoneState) || !get(requestedCameraState))){ + if ( + !localUserStore.getHelpCameraSettingsShown() && + (!get(requestedMicrophoneState) || !get(requestedCameraState)) + ) { helpCameraSettingsVisibleStore.set(true); localUserStore.setHelpCameraSettingsShown(); } @@ -104,7 +104,7 @@ export class GameManager { * This will close the socket connections and stop the gameScene, but won't remove it. */ leaveGame(scene: Phaser.Scene, targetSceneName: string, sceneClass: Phaser.Scene): void { - if (this.currentGameSceneName === null) throw 'No current scene id set!'; + if (this.currentGameSceneName === null) throw "No current scene id set!"; const gameScene: GameScene = scene.scene.get(this.currentGameSceneName) as GameScene; gameScene.cleanupClosingScene(); scene.scene.stop(this.currentGameSceneName); @@ -123,13 +123,13 @@ export class GameManager { scene.scene.start(this.currentGameSceneName); scene.scene.wake(MenuSceneName); } else { - scene.scene.run(fallbackSceneName) + scene.scene.run(fallbackSceneName); } } public getCurrentGameScene(scene: Phaser.Scene): GameScene { - if (this.currentGameSceneName === null) throw 'No current scene id set!'; - return scene.scene.get(this.currentGameSceneName) as GameScene + if (this.currentGameSceneName === null) throw "No current scene id set!"; + return scene.scene.get(this.currentGameSceneName) as GameScene; } } diff --git a/front/src/Phaser/Game/GameMap.ts b/front/src/Phaser/Game/GameMap.ts index f68c414a..a616cf4a 100644 --- a/front/src/Phaser/Game/GameMap.ts +++ b/front/src/Phaser/Game/GameMap.ts @@ -1,9 +1,13 @@ -import type {ITiledMap, ITiledMapLayer, ITiledMapLayerProperty} from "../Map/ITiledMap"; +import type { ITiledMap, ITiledMapLayer, ITiledMapLayerProperty } from "../Map/ITiledMap"; import { flattenGroupLayersMap } from "../Map/LayersFlattener"; import TilemapLayer = Phaser.Tilemaps.TilemapLayer; import { DEPTH_OVERLAY_INDEX } from "./DepthIndexes"; -export type PropertyChangeCallback = (newValue: string | number | boolean | undefined, oldValue: string | number | boolean | undefined, allProps: Map) => void; +export type PropertyChangeCallback = ( + newValue: string | number | boolean | undefined, + oldValue: string | number | boolean | undefined, + allProps: Map +) => void; /** * A wrapper around a ITiledMap interface to provide additional capabilities. @@ -19,37 +23,50 @@ export class GameMap { public readonly flatLayers: ITiledMapLayer[]; public readonly phaserLayers: TilemapLayer[] = []; - public exitUrls: Array = [] + public exitUrls: Array = []; - public constructor(private map: ITiledMap, phaserMap: Phaser.Tilemaps.Tilemap, terrains: Array) { + public hasStartTile = false; + + public constructor( + private map: ITiledMap, + phaserMap: Phaser.Tilemaps.Tilemap, + terrains: Array + ) { this.flatLayers = flattenGroupLayersMap(map); let depth = -2; for (const layer of this.flatLayers) { - if(layer.type === 'tilelayer'){ + if (layer.type === "tilelayer") { this.phaserLayers.push(phaserMap.createLayer(layer.name, terrains, 0, 0).setDepth(depth)); } - if (layer.type === 'objectgroup' && layer.name === 'floorLayer') { + if (layer.type === "objectgroup" && layer.name === "floorLayer") { depth = DEPTH_OVERLAY_INDEX; } } for (const tileset of map.tilesets) { - tileset?.tiles?.forEach(tile => { + tileset?.tiles?.forEach((tile) => { if (tile.properties) { - this.tileSetPropertyMap[tileset.firstgid + tile.id] = tile.properties - tile.properties.forEach(prop => { - if (prop.name == 'name' && typeof prop.value == "string") { + this.tileSetPropertyMap[tileset.firstgid + tile.id] = tile.properties; + tile.properties.forEach((prop) => { + if (prop.name == "name" && typeof prop.value == "string") { this.tileNameMap.set(prop.value, tileset.firstgid + tile.id); } if (prop.name == "exitUrl" && typeof prop.value == "string") { this.exitUrls.push(prop.value); + } else if (prop.name == "start") { + this.hasStartTile = true; } - }) + }); } - }) + }); } } - + public getPropertiesForIndex(index: number): Array { + if (this.tileSetPropertyMap[index]) { + return this.tileSetPropertyMap[index]; + } + return []; + } /** * Sets the position of the current player (in pixels) @@ -93,7 +110,7 @@ export class GameMap { const properties = new Map(); for (const layer of this.flatLayers) { - if (layer.type !== 'tilelayer') { + if (layer.type !== "tilelayer") { continue; } @@ -103,7 +120,7 @@ export class GameMap { if (tiles[key] == 0) { continue; } - tileIndex = tiles[key] + tileIndex = tiles[key]; } // There is a tile in this layer, let's embed the properties @@ -117,20 +134,20 @@ export class GameMap { } if (tileIndex) { - this.tileSetPropertyMap[tileIndex]?.forEach(property => { + this.tileSetPropertyMap[tileIndex]?.forEach((property) => { if (property.value) { - properties.set(property.name, property.value) + properties.set(property.name, property.value); } else if (properties.has(property.name)) { - properties.delete(property.name) + properties.delete(property.name); } - }) + }); } } return properties; } - public getMap(): ITiledMap{ + public getMap(): ITiledMap { return this.map; } @@ -138,7 +155,12 @@ export class GameMap { return this.tileSetPropertyMap[index]; } - private trigger(propName: string, oldValue: string | number | boolean | undefined, newValue: string | number | boolean | undefined, allProps: Map) { + private trigger( + propName: string, + oldValue: string | number | boolean | undefined, + newValue: string | number | boolean | undefined, + allProps: Map + ) { const callbacksArray = this.callbacks.get(propName); if (callbacksArray !== undefined) { for (const callback of callbacksArray) { @@ -167,7 +189,7 @@ export class GameMap { return this.phaserLayers.find((layer) => layer.layer.name === layerName); } - public addTerrain(terrain : Phaser.Tilemaps.Tileset): void { + public addTerrain(terrain: Phaser.Tilemaps.Tileset): void { for (const phaserLayer of this.phaserLayers) { phaserLayer.tileset.push(terrain); } @@ -175,11 +197,11 @@ export class GameMap { private putTileInFlatLayer(index: number, x: number, y: number, layer: string): void { const fLayer = this.findLayer(layer); - if ( fLayer == undefined ) { + if (fLayer == undefined) { console.error("The layer that you want to change doesn't exist."); return; } - if (fLayer.type !== 'tilelayer') { + if (fLayer.type !== "tilelayer") { console.error("The layer that you want to change is not a tilelayer. Tile can only be put in tilelayer."); return; } @@ -187,27 +209,25 @@ export class GameMap { console.error("Data of the layer that you want to change is only readable."); return; } - fLayer.data[x+y*fLayer.height] = index; + fLayer.data[x + y * fLayer.height] = index; } public putTile(tile: string | number, x: number, y: number, layer: string): void { const phaserLayer = this.findPhaserLayer(layer); - if ( phaserLayer ) { + if (phaserLayer) { const tileIndex = this.getIndexForTileType(tile); - if ( tileIndex !== undefined ) { + if (tileIndex !== undefined) { this.putTileInFlatLayer(tileIndex, x, y, layer); const phaserTile = phaserLayer.putTileAt(tileIndex, x, y); for (const property of this.getTileProperty(tileIndex)) { - if ( property.name === "collides" && property.value === "true") { + if (property.name === "collides" && property.value === "true") { phaserTile.setCollision(true); } } - } - else { + } else { console.error("The tile that you want to place doesn't exist."); } - } - else { + } else { console.error("The layer that you want to change is not a tilelayer. Tile can only be put in tilelayer."); } } @@ -218,5 +238,4 @@ export class GameMap { } return this.tileNameMap.get(tile); } - } diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index fa95399e..d137c9e0 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -1,4 +1,3 @@ -import { Queue } from "queue-typescript"; import type { Subscription } from "rxjs"; import { GlobalMessageManager } from "../../Administration/GlobalMessageManager"; import { userMessageManager } from "../../Administration/UserMessageManager"; @@ -14,20 +13,9 @@ import type { PositionInterface, RoomJoinedMessageInterface, } from "../../Connexion/ConnexionModels"; -import { localUserStore } from "../../Connexion/LocalUserStore"; -import { Room } from "../../Connexion/Room"; -import type { RoomConnection } from "../../Connexion/RoomConnection"; -import { worldFullMessageStream } from "../../Connexion/WorldFullMessageStream"; import { DEBUG_MODE, JITSI_PRIVATE_MODE, MAX_PER_GROUP, POSITION_DELAY } from "../../Enum/EnvironmentVariable"; -import { TextureError } from "../../Exception/TextureError"; -import type { UserMovedMessage } from "../../Messages/generated/messages_pb"; -import { ProtobufClientUtils } from "../../Network/ProtobufClientUtils"; -import { touchScreenManager } from "../../Touch/TouchScreenManager"; -import { urlManager } from "../../Url/UrlManager"; -import { audioManager } from "../../WebRtc/AudioManager"; -import { coWebsiteManager } from "../../WebRtc/CoWebsiteManager"; -import { HtmlUtils } from "../../WebRtc/HtmlUtils"; -import { jitsiFactory } from "../../WebRtc/JitsiFactory"; + +import { Queue } from "queue-typescript"; import { AUDIO_LOOP_PROPERTY, AUDIO_VOLUME_PROPERTY, @@ -39,15 +27,21 @@ import { TRIGGER_WEBSITE_PROPERTIES, WEBSITE_MESSAGE_PROPERTIES, } from "../../WebRtc/LayoutManager"; +import { coWebsiteManager } from "../../WebRtc/CoWebsiteManager"; +import type { UserMovedMessage } from "../../Messages/generated/messages_pb"; +import { ProtobufClientUtils } from "../../Network/ProtobufClientUtils"; +import type { RoomConnection } from "../../Connexion/RoomConnection"; +import { Room } from "../../Connexion/Room"; +import { jitsiFactory } from "../../WebRtc/JitsiFactory"; +import { urlManager } from "../../Url/UrlManager"; +import { audioManager } from "../../WebRtc/AudioManager"; +import { TextureError } from "../../Exception/TextureError"; +import { localUserStore } from "../../Connexion/LocalUserStore"; +import { HtmlUtils } from "../../WebRtc/HtmlUtils"; import { mediaManager } from "../../WebRtc/MediaManager"; -import { SimplePeer, UserSimplePeerInterface } from "../../WebRtc/SimplePeer"; -import { lazyLoadCompanionResource } from "../Companion/CompanionTexturesLoadingManager"; -import { ChatModeIcon } from "../Components/ChatModeIcon"; +import { SimplePeer } from "../../WebRtc/SimplePeer"; import { addLoader } from "../Components/Loader"; -import { joystickBaseImg, joystickBaseKey, joystickThumbImg, joystickThumbKey } from "../Components/MobileJoystick"; import { OpenChatIcon, openChatIconName } from "../Components/OpenChatIcon"; -import { PresentationModeIcon } from "../Components/PresentationModeIcon"; -import { TextUtils } from "../Components/TextUtils"; import { lazyLoadPlayerCharacterTextures, loadCustomTexture } from "../Entity/PlayerTexturesLoadingManager"; import { RemotePlayer } from "../Entity/RemotePlayer"; import type { ActionableItem } from "../Items/ActionableItem"; @@ -58,7 +52,6 @@ import type { ITiledMapLayer, ITiledMapLayerProperty, ITiledMapObject, - ITiledMapTileLayer, ITiledTileSet, } from "../Map/ITiledMap"; import { MenuScene, MenuSceneName } from "../Menu/MenuScene"; @@ -66,13 +59,8 @@ import { PlayerAnimationDirections } from "../Player/Animation"; import { hasMovedEventName, Player, requestEmoteEventName } from "../Player/Player"; import { ErrorSceneName } from "../Reconnecting/ErrorScene"; import { ReconnectingSceneName } from "../Reconnecting/ReconnectingScene"; -import { waScaleManager } from "../Services/WaScaleManager"; -import { PinchManager } from "../UserInput/PinchManager"; import { UserInputManager } from "../UserInput/UserInputManager"; import type { AddPlayerInterface } from "./AddPlayerInterface"; -import { DEPTH_OVERLAY_INDEX } from "./DepthIndexes"; -import { DirtyScene } from "./DirtyScene"; -import { EmoteManager } from "./EmoteManager"; import { gameManager } from "./GameManager"; import { GameMap } from "./GameMap"; import { PlayerMovement } from "./PlayerMovement"; @@ -83,12 +71,22 @@ import CanvasTexture = Phaser.Textures.CanvasTexture; import GameObject = Phaser.GameObjects.GameObject; import FILE_LOAD_ERROR = Phaser.Loader.Events.FILE_LOAD_ERROR; import DOMElement = Phaser.GameObjects.DOMElement; +import { worldFullMessageStream } from "../../Connexion/WorldFullMessageStream"; +import { lazyLoadCompanionResource } from "../Companion/CompanionTexturesLoadingManager"; +import { DirtyScene } from "./DirtyScene"; +import { TextUtils } from "../Components/TextUtils"; +import { touchScreenManager } from "../../Touch/TouchScreenManager"; +import { PinchManager } from "../UserInput/PinchManager"; +import { joystickBaseImg, joystickBaseKey, joystickThumbImg, joystickThumbKey } from "../Components/MobileJoystick"; +import { waScaleManager } from "../Services/WaScaleManager"; +import { EmoteManager } from "./EmoteManager"; import EVENT_TYPE = Phaser.Scenes.Events; import RenderTexture = Phaser.GameObjects.RenderTexture; import Tilemap = Phaser.Tilemaps.Tilemap; import type { HasPlayerMovedEvent } from "../../Api/Events/HasPlayerMovedEvent"; import AnimatedTiles from "phaser-animated-tiles"; +import { StartPositionCalculator } from "./StartPositionCalculator"; import { soundManager } from "./SoundManager"; import { peerStore, screenSharingPeerStore } from "../../Stores/PeerStore"; import { videoFocusStore } from "../../Stores/VideoFocusStore"; @@ -129,8 +127,6 @@ interface DeleteGroupEventInterface { groupId: number; } -const defaultStartLayerName = "start"; - export class GameScene extends DirtyScene { Terrains: Array; CurrentPlayer!: Player; @@ -141,8 +137,6 @@ export class GameScene extends DirtyScene { mapFile!: ITiledMap; animatedTiles!: AnimatedTiles; groups: Map; - startX!: number; - startY!: number; circleTexture!: CanvasTexture; circleRedTexture!: CanvasTexture; pendingEvents: Queue< @@ -194,7 +188,6 @@ export class GameScene extends DirtyScene { private outlinedItem: ActionableItem | null = null; public userInputManager!: UserInputManager; private isReconnecting: boolean | undefined = undefined; - private startLayerName!: string | null; private openChatIcon!: OpenChatIcon; private playerName!: string; private characterLayers!: string[]; @@ -206,6 +199,7 @@ export class GameScene extends DirtyScene { private mapTransitioning: boolean = false; //used to prevent transitions happenning at the same time. private emoteManager!: EmoteManager; private preloading: boolean = true; + startPositionCalculator!: StartPositionCalculator; constructor(private room: Room, MapUrlFile: string, customKey?: string | undefined) { super({ @@ -426,7 +420,6 @@ export class GameScene extends DirtyScene { gameManager.gameSceneIsCreated(this); urlManager.pushRoomIdToUrl(this.room); - this.startLayerName = urlManager.getStartLayerNameFromUrl(); if (touchScreenManager.supportTouchScreen) { this.pinchManager = new PinchManager(this); @@ -489,7 +482,12 @@ export class GameScene extends DirtyScene { this.loadNextGame(exitUrl); }); - this.initStartXAndStartY(); + this.startPositionCalculator = new StartPositionCalculator( + this.gameMap, + this.mapFile, + this.initPosition, + urlManager.getStartLayerNameFromUrl() + ); //add entities this.Objects = new Array(); @@ -586,8 +584,7 @@ export class GameScene extends DirtyScene { this.playerName, this.characterLayers, { - x: this.startX, - y: this.startY, + ...this.startPositionCalculator.startPosition, }, { left: camera.scrollX, @@ -989,8 +986,8 @@ ${escapedMessage} }) ); - this.iframeSubscriptionList.push( - iframeListener.enablePlayerControlStream.subscribe(()=>{ + this.iframeSubscriptionList.push( + iframeListener.enablePlayerControlStream.subscribe(() => { this.userInputManager.restoreControls(); }) ); @@ -1051,20 +1048,21 @@ ${escapedMessage} iframeListener.gameStateStream.subscribe(() => { iframeListener.sendGameStateEvent({ mapUrl: this.MapUrlFile, - startLayerName: this.startLayerName, + startLayerName: this.startPositionCalculator.startLayerName, uuid: localUserStore.getLocalUser()?.uuid, nickname: localUserStore.getName(), roomId: this.RoomId, tags: this.connection ? this.connection.getAllTags() : [], }); }) - ) - this.iframeSubscriptionList.push(iframeListener.setTilesStream.subscribe((eventTiles) => { - for (const eventTile of eventTiles) { - this.gameMap.putTile(eventTile.tile, eventTile.x, eventTile.y, eventTile.layer); - } - })) - + ); + this.iframeSubscriptionList.push( + iframeListener.setTilesStream.subscribe((eventTiles) => { + for (const eventTile of eventTiles) { + this.gameMap.putTile(eventTile.tile, eventTile.x, eventTile.y, eventTile.layer); + } + }) + ); } private setPropertyLayer( @@ -1077,11 +1075,11 @@ ${escapedMessage} console.warn('Could not find layer "' + layerName + '" when calling setProperty'); return; } - const property = (layer.properties as ITiledMapLayerProperty[])?.find( - (property) => property.name === propertyName - ); - if (property === undefined) { + if (layer.properties === undefined) { layer.properties = []; + } + const property = layer.properties.find((property) => property.name === propertyName); + if (property === undefined) { layer.properties.push({ name: propertyName, type: typeof propertyValue, value: propertyValue }); return; } @@ -1107,7 +1105,9 @@ ${escapedMessage} this.mapTransitioning = true; const { roomId, hash } = Room.getIdFromIdentifier(exitKey, this.MapUrlFile, this.instance); if (!roomId) throw new Error("Could not find the room from its exit key: " + exitKey); - urlManager.pushStartLayerNameToUrl(hash); + if (hash) { + urlManager.pushStartLayerNameToUrl(hash); + } const menuScene: MenuScene = this.scene.get(MenuSceneName) as MenuScene; menuScene.reset(); if (roomId !== this.scene.key) { @@ -1121,9 +1121,9 @@ ${escapedMessage} this.scene.start(roomId); } else { //if the exit points to the current map, we simply teleport the user back to the startLayer - this.initPositionFromLayerName(hash || defaultStartLayerName); - this.CurrentPlayer.x = this.startX; - this.CurrentPlayer.y = this.startY; + this.startPositionCalculator.initPositionFromLayerName(hash, hash); + this.CurrentPlayer.x = this.startPositionCalculator.startPosition.x; + this.CurrentPlayer.y = this.startPositionCalculator.startPosition.y; setTimeout(() => (this.mapTransitioning = false), 500); } } @@ -1170,46 +1170,6 @@ ${escapedMessage} this.MapPlayersByKey = new Map(); } - private initStartXAndStartY() { - // If there is an init position passed - if (this.initPosition !== null) { - this.startX = this.initPosition.x; - this.startY = this.initPosition.y; - } else { - // Now, let's find the start layer - if (this.startLayerName) { - this.initPositionFromLayerName(this.startLayerName); - } - if (this.startX === undefined) { - // If we have no start layer specified or if the hash passed does not exist, let's go with the default start position. - this.initPositionFromLayerName(defaultStartLayerName); - } - } - // Still no start position? Something is wrong with the map, we need a "start" layer. - if (this.startX === undefined) { - console.warn( - 'This map is missing a layer named "start" that contains the available default start positions.' - ); - // Let's start in the middle of the map - this.startX = this.mapFile.width * 16; - this.startY = this.mapFile.height * 16; - } - } - - private initPositionFromLayerName(layerName: string) { - for (const layer of this.gameMap.flatLayers) { - if ( - (layerName === layer.name || layer.name.endsWith("/" + layerName)) && - layer.type === "tilelayer" && - (layerName === defaultStartLayerName || this.isStartLayer(layer)) - ) { - const startPosition = this.startUser(layer); - this.startX = startPosition.x + this.mapFile.tilewidth / 2; - this.startY = startPosition.y + this.mapFile.tileheight / 2; - } - } - } - private getExitUrl(layer: ITiledMapLayer): string | undefined { return this.getProperty(layer, "exitUrl") as string | undefined; } @@ -1221,10 +1181,6 @@ ${escapedMessage} return this.getProperty(layer, "exitSceneUrl") as string | undefined; } - private isStartLayer(layer: ITiledMapLayer): boolean { - return this.getProperty(layer, "startLayer") == true; - } - private getScriptUrls(map: ITiledMap): string[] { return (this.getProperties(map, "script") as string[]).map((script) => new URL(script, this.MapUrlFile).toString() @@ -1262,33 +1218,6 @@ ${escapedMessage} return gameManager.loadMap(room, this.scene).catch(() => {}); } - private startUser(layer: ITiledMapTileLayer): PositionInterface { - const tiles = layer.data; - if (typeof tiles === "string") { - throw new Error("The content of a JSON map must be filled as a JSON array, not as a string"); - } - const possibleStartPositions: PositionInterface[] = []; - tiles.forEach((objectKey: number, key: number) => { - if (objectKey === 0) { - return; - } - const y = Math.floor(key / layer.width); - const x = key % layer.width; - - possibleStartPositions.push({ x: x * this.mapFile.tilewidth, y: y * this.mapFile.tilewidth }); - }); - // Get a value at random amongst allowed values - if (possibleStartPositions.length === 0) { - console.warn('The start layer "' + layer.name + '" for this map is empty.'); - return { - x: 0, - y: 0, - }; - } - // Choose one of the available start positions at random amongst the list of available start positions. - return possibleStartPositions[Math.floor(Math.random() * possibleStartPositions.length)]; - } - //todo: in a dedicated class/function? initCamera() { this.cameras.main.setBounds(0, 0, this.Map.widthInPixels, this.Map.heightInPixels); @@ -1321,8 +1250,8 @@ ${escapedMessage} try { this.CurrentPlayer = new Player( this, - this.startX, - this.startY, + this.startPositionCalculator.startPosition.x, + this.startPositionCalculator.startPosition.y, this.playerName, texturesPromise, PlayerAnimationDirections.Down, diff --git a/front/src/Phaser/Game/PlayerMovement.ts b/front/src/Phaser/Game/PlayerMovement.ts index 2369b86b..c3daedad 100644 --- a/front/src/Phaser/Game/PlayerMovement.ts +++ b/front/src/Phaser/Game/PlayerMovement.ts @@ -1,10 +1,14 @@ import { MAX_EXTRAPOLATION_TIME } from "../../Enum/EnvironmentVariable"; import type { PositionInterface } from "../../Connexion/ConnexionModels"; -import type { HasPlayerMovedEvent } from '../../Api/Events/HasPlayerMovedEvent'; +import type { HasPlayerMovedEvent } from "../../Api/Events/HasPlayerMovedEvent"; export class PlayerMovement { - public constructor(private startPosition: PositionInterface, private startTick: number, private endPosition: HasPlayerMovedEvent, private endTick: number) { - } + public constructor( + private startPosition: PositionInterface, + private startTick: number, + private endPosition: HasPlayerMovedEvent, + private endTick: number + ) {} public isOutdated(tick: number): boolean { //console.log(tick, this.endTick, MAX_EXTRAPOLATION_TIME) @@ -24,14 +28,18 @@ export class PlayerMovement { return this.endPosition; } - const x = (this.endPosition.x - this.startPosition.x) * ((tick - this.startTick) / (this.endTick - this.startTick)) + this.startPosition.x; - const y = (this.endPosition.y - this.startPosition.y) * ((tick - this.startTick) / (this.endTick - this.startTick)) + this.startPosition.y; + const x = + (this.endPosition.x - this.startPosition.x) * ((tick - this.startTick) / (this.endTick - this.startTick)) + + this.startPosition.x; + const y = + (this.endPosition.y - this.startPosition.y) * ((tick - this.startTick) / (this.endTick - this.startTick)) + + this.startPosition.y; //console.log('Computed position ', x, y) return { x, y, direction: this.endPosition.direction, - moving: true - } + moving: true, + }; } } diff --git a/front/src/Phaser/Game/PlayersPositionInterpolator.ts b/front/src/Phaser/Game/PlayersPositionInterpolator.ts index 86b6c761..b9a7c8ec 100644 --- a/front/src/Phaser/Game/PlayersPositionInterpolator.ts +++ b/front/src/Phaser/Game/PlayersPositionInterpolator.ts @@ -2,7 +2,7 @@ * This class is in charge of computing the position of all players. * Player movement is delayed by 200ms so position depends on ticks. */ -import type { HasPlayerMovedEvent } from '../../Api/Events/HasPlayerMovedEvent'; +import type { HasPlayerMovedEvent } from "../../Api/Events/HasPlayerMovedEvent"; import type { PlayerMovement } from "./PlayerMovement"; export class PlayersPositionInterpolator { @@ -24,7 +24,7 @@ export class PlayersPositionInterpolator { this.playerMovements.delete(userId); } //console.log("moving") - positions.set(userId, playerMovement.getPosition(tick)) + positions.set(userId, playerMovement.getPosition(tick)); }); return positions; } diff --git a/front/src/Phaser/Game/StartPositionCalculator.ts b/front/src/Phaser/Game/StartPositionCalculator.ts new file mode 100644 index 00000000..7460c81c --- /dev/null +++ b/front/src/Phaser/Game/StartPositionCalculator.ts @@ -0,0 +1,127 @@ +import type { PositionInterface } from "../../Connexion/ConnexionModels"; +import type { ITiledMap, ITiledMapLayer, ITiledMapLayerProperty, ITiledMapTileLayer } from "../Map/ITiledMap"; +import type { GameMap } from "./GameMap"; + +const defaultStartLayerName = "start"; + +export class StartPositionCalculator { + public startPosition!: PositionInterface; + + constructor( + private readonly gameMap: GameMap, + private readonly mapFile: ITiledMap, + private readonly initPosition: PositionInterface | null, + public readonly startLayerName: string | null + ) { + this.initStartXAndStartY(); + } + private initStartXAndStartY() { + // If there is an init position passed + if (this.initPosition !== null) { + this.startPosition = this.initPosition; + } else { + // Now, let's find the start layer + if (this.startLayerName) { + this.initPositionFromLayerName(this.startLayerName, this.startLayerName); + } + if (this.startPosition === undefined) { + // If we have no start layer specified or if the hash passed does not exist, let's go with the default start position. + this.initPositionFromLayerName(defaultStartLayerName, this.startLayerName); + } + } + // Still no start position? Something is wrong with the map, we need a "start" layer. + if (this.startPosition === undefined) { + console.warn( + 'This map is missing a layer named "start" that contains the available default start positions.' + ); + // Let's start in the middle of the map + this.startPosition = { + x: this.mapFile.width * 16, + y: this.mapFile.height * 16, + }; + } + } + + /** + * + * @param selectedLayer this is always the layer that is selected with the hash in the url + * @param selectedOrDefaultLayer this can also be the {defaultStartLayerName} if the {selectedLayer} didnt yield any start points + */ + public initPositionFromLayerName(selectedOrDefaultLayer: string | null, selectedLayer: string | null) { + if (!selectedOrDefaultLayer) { + selectedOrDefaultLayer = defaultStartLayerName; + } + for (const layer of this.gameMap.flatLayers) { + if ( + (selectedOrDefaultLayer === layer.name || layer.name.endsWith("/" + selectedOrDefaultLayer)) && + layer.type === "tilelayer" && + (selectedOrDefaultLayer === defaultStartLayerName || this.isStartLayer(layer)) + ) { + const startPosition = this.startUser(layer, selectedLayer); + this.startPosition = { + x: startPosition.x + this.mapFile.tilewidth / 2, + y: startPosition.y + this.mapFile.tileheight / 2, + }; + } + } + } + + private isStartLayer(layer: ITiledMapLayer): boolean { + return this.getProperty(layer, "startLayer") == true; + } + + /** + * + * @param selectedLayer this is always the layer that is selected with the hash in the url + * @param selectedOrDefaultLayer this can also be the default layer if the {selectedLayer} didnt yield any start points + */ + private startUser(selectedOrDefaultLayer: ITiledMapTileLayer, selectedLayer: string | null): PositionInterface { + const tiles = selectedOrDefaultLayer.data; + if (typeof tiles === "string") { + throw new Error("The content of a JSON map must be filled as a JSON array, not as a string"); + } + const possibleStartPositions: PositionInterface[] = []; + tiles.forEach((objectKey: number, key: number) => { + if (objectKey === 0) { + return; + } + const y = Math.floor(key / selectedOrDefaultLayer.width); + const x = key % selectedOrDefaultLayer.width; + + if (selectedLayer && this.gameMap.hasStartTile) { + const properties = this.gameMap.getPropertiesForIndex(objectKey); + if ( + !properties.length || + !properties.some((property) => property.name == "start" && property.value == selectedLayer) + ) { + return; + } + } + possibleStartPositions.push({ x: x * this.mapFile.tilewidth, y: y * this.mapFile.tilewidth }); + }); + // Get a value at random amongst allowed values + if (possibleStartPositions.length === 0) { + console.warn('The start layer "' + selectedOrDefaultLayer.name + '" for this map is empty.'); + return { + x: 0, + y: 0, + }; + } + // Choose one of the available start positions at random amongst the list of available start positions. + return possibleStartPositions[Math.floor(Math.random() * possibleStartPositions.length)]; + } + + private getProperty(layer: ITiledMapLayer | ITiledMap, name: string): string | boolean | number | undefined { + const properties: ITiledMapLayerProperty[] | undefined = layer.properties; + if (!properties) { + return undefined; + } + const obj = properties.find( + (property: ITiledMapLayerProperty) => property.name.toLowerCase() === name.toLowerCase() + ); + if (obj === undefined) { + return undefined; + } + return obj.value; + } +} diff --git a/front/src/Phaser/Map/ITiledMap.ts b/front/src/Phaser/Map/ITiledMap.ts index 8ea9baf1..0653e83a 100644 --- a/front/src/Phaser/Map/ITiledMap.ts +++ b/front/src/Phaser/Map/ITiledMap.ts @@ -36,7 +36,7 @@ export interface ITiledMap { export interface ITiledMapLayerProperty { name: string; type: string; - value: string|boolean|number|undefined; + value: string | boolean | number | undefined; } /*export interface ITiledMapLayerBooleanProperty { @@ -48,7 +48,7 @@ export interface ITiledMapLayerProperty { export type ITiledMapLayer = ITiledMapGroupLayer | ITiledMapObjectLayer | ITiledMapTileLayer; export interface ITiledMapGroupLayer { - id?: number, + id?: number; name: string; opacity: number; properties?: ITiledMapLayerProperty[]; @@ -64,8 +64,8 @@ export interface ITiledMapGroupLayer { } export interface ITiledMapTileLayer { - id?: number, - data: number[]|string; + id?: number; + data: number[] | string; height: number; name: string; opacity: number; @@ -87,7 +87,7 @@ export interface ITiledMapTileLayer { } export interface ITiledMapObjectLayer { - id?: number, + id?: number; height: number; name: string; opacity: number; @@ -117,7 +117,7 @@ export interface ITiledMapObject { gid: number; height: number; name: string; - properties: {[key: string]: string}; + properties: { [key: string]: string }; rotation: number; type: string; visible: boolean; @@ -133,26 +133,26 @@ export interface ITiledMapObject { /** * Polygon points */ - polygon: {x: number, y: number}[]; + polygon: { x: number; y: number }[]; /** * Polyline points */ - polyline: {x: number, y: number}[]; + polyline: { x: number; y: number }[]; - text?: ITiledText + text?: ITiledText; } export interface ITiledText { - text: string, - wrap?: boolean, - fontfamily?: string, - pixelsize?: number, - color?: string, - underline?: boolean, - italic?: boolean, - strikeout?: boolean, - halign?: "center"|"right"|"justify"|"left" + text: string; + wrap?: boolean; + fontfamily?: string; + pixelsize?: number; + color?: string; + underline?: boolean; + italic?: boolean; + strikeout?: boolean; + halign?: "center" | "right" | "justify" | "left"; } export interface ITiledTileSet { @@ -163,7 +163,7 @@ export interface ITiledTileSet { imagewidth: number; margin: number; name: string; - properties: {[key: string]: string}; + properties: { [key: string]: string }; spacing: number; tilecount: number; tileheight: number; @@ -179,10 +179,10 @@ export interface ITiledTileSet { } export interface ITile { - id: number, - type?: string + id: number; + type?: string; - properties?: Array + properties?: Array; } export interface ITiledMapTerrain { diff --git a/front/src/Phaser/Map/LayersFlattener.ts b/front/src/Phaser/Map/LayersFlattener.ts index c5092779..d28402b5 100644 --- a/front/src/Phaser/Map/LayersFlattener.ts +++ b/front/src/Phaser/Map/LayersFlattener.ts @@ -1,21 +1,21 @@ -import type {ITiledMap, ITiledMapLayer} from "./ITiledMap"; +import type { ITiledMap, ITiledMapLayer } from "./ITiledMap"; /** * Flatten the grouped layers */ export function flattenGroupLayersMap(map: ITiledMap) { const flatLayers: ITiledMapLayer[] = []; - flattenGroupLayers(map.layers, '', flatLayers); + flattenGroupLayers(map.layers, "", flatLayers); return flatLayers; } -function flattenGroupLayers(layers : ITiledMapLayer[], prefix : string, flatLayers: ITiledMapLayer[]) { +function flattenGroupLayers(layers: ITiledMapLayer[], prefix: string, flatLayers: ITiledMapLayer[]) { for (const layer of layers) { - if (layer.type === 'group') { - flattenGroupLayers(layer.layers, prefix + layer.name + '/', flatLayers); + if (layer.type === "group") { + flattenGroupLayers(layer.layers, prefix + layer.name + "/", flatLayers); } else { - layer.name = prefix+layer.name + layer.name = prefix + layer.name; flatLayers.push(layer); } } -} \ No newline at end of file +} diff --git a/front/src/Phaser/Menu/MenuScene.ts b/front/src/Phaser/Menu/MenuScene.ts index e5ca5023..d0d6f982 100644 --- a/front/src/Phaser/Menu/MenuScene.ts +++ b/front/src/Phaser/Menu/MenuScene.ts @@ -1,29 +1,29 @@ -import {LoginScene, LoginSceneName} from "../Login/LoginScene"; -import {SelectCharacterScene, SelectCharacterSceneName} from "../Login/SelectCharacterScene"; -import {SelectCompanionScene, SelectCompanionSceneName} from "../Login/SelectCompanionScene"; -import {gameManager} from "../Game/GameManager"; -import {localUserStore} from "../../Connexion/LocalUserStore"; -import {gameReportKey, gameReportRessource, ReportMenu} from "./ReportMenu"; -import {connectionManager} from "../../Connexion/ConnectionManager"; -import {GameConnexionTypes} from "../../Url/UrlManager"; -import {WarningContainer, warningContainerHtml, warningContainerKey} from "../Components/WarningContainer"; -import {worldFullWarningStream} from "../../Connexion/WorldFullWarningStream"; -import {menuIconVisible} from "../../Stores/MenuStore"; -import {videoConstraintStore} from "../../Stores/MediaStore"; -import {showReportScreenStore} from "../../Stores/ShowReportScreenStore"; -import { HtmlUtils } from '../../WebRtc/HtmlUtils'; -import { iframeListener } from '../../Api/IframeListener'; -import { Subscription } from 'rxjs'; -import {registerMenuCommandStream} from "../../Api/Events/ui/MenuItemRegisterEvent"; -import {sendMenuClickedEvent} from "../../Api/iframe/Ui/MenuItem"; -import {consoleGlobalMessageManagerVisibleStore} from "../../Stores/ConsoleGlobalMessageManagerStore"; -import {get} from "svelte/store"; +import { LoginScene, LoginSceneName } from "../Login/LoginScene"; +import { SelectCharacterScene, SelectCharacterSceneName } from "../Login/SelectCharacterScene"; +import { SelectCompanionScene, SelectCompanionSceneName } from "../Login/SelectCompanionScene"; +import { gameManager } from "../Game/GameManager"; +import { localUserStore } from "../../Connexion/LocalUserStore"; +import { gameReportKey, gameReportRessource, ReportMenu } from "./ReportMenu"; +import { connectionManager } from "../../Connexion/ConnectionManager"; +import { GameConnexionTypes } from "../../Url/UrlManager"; +import { WarningContainer, warningContainerHtml, warningContainerKey } from "../Components/WarningContainer"; +import { worldFullWarningStream } from "../../Connexion/WorldFullWarningStream"; +import { menuIconVisible } from "../../Stores/MenuStore"; +import { videoConstraintStore } from "../../Stores/MediaStore"; +import { showReportScreenStore } from "../../Stores/ShowReportScreenStore"; +import { HtmlUtils } from "../../WebRtc/HtmlUtils"; +import { iframeListener } from "../../Api/IframeListener"; +import { Subscription } from "rxjs"; +import { registerMenuCommandStream } from "../../Api/Events/ui/MenuItemRegisterEvent"; +import { sendMenuClickedEvent } from "../../Api/iframe/Ui/MenuItem"; +import { consoleGlobalMessageManagerVisibleStore } from "../../Stores/ConsoleGlobalMessageManagerStore"; +import { get } from "svelte/store"; -export const MenuSceneName = 'MenuScene'; -const gameMenuKey = 'gameMenu'; -const gameMenuIconKey = 'gameMenuIcon'; -const gameSettingsMenuKey = 'gameSettingsMenu'; -const gameShare = 'gameShare'; +export const MenuSceneName = "MenuScene"; +const gameMenuKey = "gameMenu"; +const gameMenuIconKey = "gameMenuIcon"; +const gameSettingsMenuKey = "gameSettingsMenu"; +const gameShare = "gameShare"; const closedSideMenuX = -1000; const openedSideMenuX = 0; @@ -44,45 +44,49 @@ export class MenuScene extends Phaser.Scene { private menuButton!: Phaser.GameObjects.DOMElement; private warningContainer: WarningContainer | null = null; private warningContainerTimeout: NodeJS.Timeout | null = null; - private subscriptions = new Subscription() + private subscriptions = new Subscription(); constructor() { super({ key: MenuSceneName }); this.gameQualityValue = localUserStore.getGameQualityValue(); this.videoQualityValue = localUserStore.getVideoQualityValue(); - this.subscriptions.add(registerMenuCommandStream.subscribe(menuCommand => { - this.addMenuOption(menuCommand); - })) + this.subscriptions.add( + registerMenuCommandStream.subscribe((menuCommand) => { + this.addMenuOption(menuCommand); + }) + ); - this.subscriptions.add(iframeListener.unregisterMenuCommandStream.subscribe(menuCommand => { - this.destroyMenu(menuCommand); - })) + this.subscriptions.add( + iframeListener.unregisterMenuCommandStream.subscribe((menuCommand) => { + this.destroyMenu(menuCommand); + }) + ); } reset() { const addedMenuItems = [...this.menuElement.node.querySelectorAll(".fromApi")]; for (let index = addedMenuItems.length - 1; index >= 0; index--) { - addedMenuItems[index].remove() + addedMenuItems[index].remove(); } } public addMenuOption(menuText: string) { - const wrappingSection = document.createElement("section") + const wrappingSection = document.createElement("section"); const escapedHtml = HtmlUtils.escapeHtml(menuText); - wrappingSection.innerHTML = `` + wrappingSection.innerHTML = ``; const menuItemContainer = this.menuElement.node.querySelector("#gameMenu main"); if (menuItemContainer) { - menuItemContainer.querySelector(`#${escapedHtml}.fromApi`)?.remove() - menuItemContainer.insertBefore(wrappingSection, menuItemContainer.querySelector("#socialLinks")) + menuItemContainer.querySelector(`#${escapedHtml}.fromApi`)?.remove(); + menuItemContainer.insertBefore(wrappingSection, menuItemContainer.querySelector("#socialLinks")); } } preload() { - this.load.html(gameMenuKey, 'resources/html/gameMenu.html'); - this.load.html(gameMenuIconKey, 'resources/html/gameMenuIcon.html'); - this.load.html(gameSettingsMenuKey, 'resources/html/gameQualityMenu.html'); - this.load.html(gameShare, 'resources/html/gameShare.html'); + this.load.html(gameMenuKey, "resources/html/gameMenu.html"); + this.load.html(gameMenuIconKey, "resources/html/gameMenuIcon.html"); + this.load.html(gameSettingsMenuKey, "resources/html/gameQualityMenu.html"); + this.load.html(gameShare, "resources/html/gameShare.html"); this.load.html(gameReportKey, gameReportRessource); this.load.html(warningContainerKey, warningContainerHtml); } @@ -91,26 +95,28 @@ export class MenuScene extends Phaser.Scene { menuIconVisible.set(true); this.menuElement = this.add.dom(closedSideMenuX, 30).createFromCache(gameMenuKey); this.menuElement.setOrigin(0); - MenuScene.revealMenusAfterInit(this.menuElement, 'gameMenu'); + MenuScene.revealMenusAfterInit(this.menuElement, "gameMenu"); - const middleX = (window.innerWidth / 3) - 298; + const middleX = window.innerWidth / 3 - 298; this.gameQualityMenuElement = this.add.dom(middleX, -400).createFromCache(gameSettingsMenuKey); - MenuScene.revealMenusAfterInit(this.gameQualityMenuElement, 'gameQuality'); - + MenuScene.revealMenusAfterInit(this.gameQualityMenuElement, "gameQuality"); this.gameShareElement = this.add.dom(middleX, -400).createFromCache(gameShare); MenuScene.revealMenusAfterInit(this.gameShareElement, gameShare); - this.gameShareElement.addListener('click'); - this.gameShareElement.on('click', (event: MouseEvent) => { + this.gameShareElement.addListener("click"); + this.gameShareElement.on("click", (event: MouseEvent) => { event.preventDefault(); - if ((event?.target as HTMLInputElement).id === 'gameShareFormSubmit') { + if ((event?.target as HTMLInputElement).id === "gameShareFormSubmit") { this.copyLink(); - } else if ((event?.target as HTMLInputElement).id === 'gameShareFormCancel') { + } else if ((event?.target as HTMLInputElement).id === "gameShareFormCancel") { this.closeGameShare(); } }); - this.gameReportElement = new ReportMenu(this, connectionManager.getConnexionType === GameConnexionTypes.anonymous); + this.gameReportElement = new ReportMenu( + this, + connectionManager.getConnexionType === GameConnexionTypes.anonymous + ); showReportScreenStore.subscribe((user) => { if (user !== null) { this.closeAll(); @@ -118,17 +124,17 @@ export class MenuScene extends Phaser.Scene { } }); - this.input.keyboard.on('keyup-TAB', () => { + this.input.keyboard.on("keyup-TAB", () => { this.sideMenuOpened ? this.closeSideMenu() : this.openSideMenu(); }); this.menuButton = this.add.dom(0, 0).createFromCache(gameMenuIconKey); - this.menuButton.addListener('click'); - this.menuButton.on('click', () => { + this.menuButton.addListener("click"); + this.menuButton.on("click", () => { this.sideMenuOpened ? this.closeSideMenu() : this.openSideMenu(); }); - this.menuElement.addListener('click'); - this.menuElement.on('click', this.onMenuClick.bind(this)); + this.menuElement.addListener("click"); + this.menuElement.on("click", this.onMenuClick.bind(this)); worldFullWarningStream.stream.subscribe(() => this.showWorldCapacityWarning()); } @@ -145,7 +151,7 @@ export class MenuScene extends Phaser.Scene { public revealMenuIcon(): void { //TODO fix me: add try catch because at the same time, 'this.menuButton' variable doesn't exist and there is error on 'getChildByID' function try { - (this.menuButton.getChildByID('menuIcon') as HTMLElement).hidden = false; + (this.menuButton.getChildByID("menuIcon") as HTMLElement).hidden = false; } catch (err) { console.error(err); } @@ -155,22 +161,22 @@ export class MenuScene extends Phaser.Scene { if (this.sideMenuOpened) return; this.closeAll(); this.sideMenuOpened = true; - this.menuButton.getChildByID('openMenuButton').innerHTML = 'X'; + this.menuButton.getChildByID("openMenuButton").innerHTML = "X"; const connection = gameManager.getCurrentGameScene(this).connection; if (connection && connection.isAdmin()) { - const adminSection = this.menuElement.getChildByID('adminConsoleSection') as HTMLElement; + const adminSection = this.menuElement.getChildByID("adminConsoleSection") as HTMLElement; adminSection.hidden = false; } //TODO bind with future metadata of card //if (connectionManager.getConnexionType === GameConnexionTypes.anonymous){ - const adminSection = this.menuElement.getChildByID('socialLinks') as HTMLElement; + const adminSection = this.menuElement.getChildByID("socialLinks") as HTMLElement; adminSection.hidden = false; //} this.tweens.add({ targets: this.menuElement, x: openedSideMenuX, duration: 500, - ease: 'Power3' + ease: "Power3", }); } @@ -183,23 +189,22 @@ export class MenuScene extends Phaser.Scene { } this.warningContainerTimeout = setTimeout(() => { this.warningContainer?.destroy(); - this.warningContainer = null - this.warningContainerTimeout = null + this.warningContainer = null; + this.warningContainerTimeout = null; }, 120000); - } private closeSideMenu(): void { if (!this.sideMenuOpened) return; this.sideMenuOpened = false; this.closeAll(); - this.menuButton.getChildByID('openMenuButton').innerHTML = ``; + this.menuButton.getChildByID("openMenuButton").innerHTML = ``; consoleGlobalMessageManagerVisibleStore.set(false); this.tweens.add({ targets: this.menuElement, x: closedSideMenuX, duration: 500, - ease: 'Power3' + ease: "Power3", }); } @@ -213,19 +218,23 @@ export class MenuScene extends Phaser.Scene { this.settingsMenuOpened = true; - const gameQualitySelect = this.gameQualityMenuElement.getChildByID('select-game-quality') as HTMLInputElement; - gameQualitySelect.value = '' + this.gameQualityValue; - const videoQualitySelect = this.gameQualityMenuElement.getChildByID('select-video-quality') as HTMLInputElement; - videoQualitySelect.value = '' + this.videoQualityValue; + const gameQualitySelect = this.gameQualityMenuElement.getChildByID("select-game-quality") as HTMLInputElement; + gameQualitySelect.value = "" + this.gameQualityValue; + const videoQualitySelect = this.gameQualityMenuElement.getChildByID("select-video-quality") as HTMLInputElement; + videoQualitySelect.value = "" + this.videoQualityValue; - this.gameQualityMenuElement.addListener('click'); - this.gameQualityMenuElement.on('click', (event: MouseEvent) => { + this.gameQualityMenuElement.addListener("click"); + this.gameQualityMenuElement.on("click", (event: MouseEvent) => { event.preventDefault(); - if ((event?.target as HTMLInputElement).id === 'gameQualityFormSubmit') { - const gameQualitySelect = this.gameQualityMenuElement.getChildByID('select-game-quality') as HTMLInputElement; - const videoQualitySelect = this.gameQualityMenuElement.getChildByID('select-video-quality') as HTMLInputElement; + if ((event?.target as HTMLInputElement).id === "gameQualityFormSubmit") { + const gameQualitySelect = this.gameQualityMenuElement.getChildByID( + "select-game-quality" + ) as HTMLInputElement; + const videoQualitySelect = this.gameQualityMenuElement.getChildByID( + "select-video-quality" + ) as HTMLInputElement; this.saveSetting(parseInt(gameQualitySelect.value), parseInt(videoQualitySelect.value)); - } else if ((event?.target as HTMLInputElement).id === 'gameQualityFormCancel') { + } else if ((event?.target as HTMLInputElement).id === "gameQualityFormCancel") { this.closeGameQualityMenu(); } }); @@ -243,7 +252,7 @@ export class MenuScene extends Phaser.Scene { y: middleY, x: middleX, duration: 1000, - ease: 'Power3' + ease: "Power3", }); } @@ -251,16 +260,15 @@ export class MenuScene extends Phaser.Scene { if (!this.settingsMenuOpened) return; this.settingsMenuOpened = false; - this.gameQualityMenuElement.removeListener('click'); + this.gameQualityMenuElement.removeListener("click"); this.tweens.add({ targets: this.gameQualityMenuElement, y: -400, duration: 1000, - ease: 'Power3' + ease: "Power3", }); } - private openGameShare(): void { if (this.gameShareOpened) { this.closeGameShare(); @@ -269,7 +277,7 @@ export class MenuScene extends Phaser.Scene { //close all this.closeAll(); - const gameShareLink = this.gameShareElement.getChildByID('gameShareLink') as HTMLInputElement; + const gameShareLink = this.gameShareElement.getChildByID("gameShareLink") as HTMLInputElement; gameShareLink.value = location.toString(); this.gameShareOpened = true; @@ -287,64 +295,64 @@ export class MenuScene extends Phaser.Scene { y: middleY, x: middleX, duration: 1000, - ease: 'Power3' + ease: "Power3", }); } private closeGameShare(): void { - const gameShareInfo = this.gameShareElement.getChildByID('gameShareInfo') as HTMLParagraphElement; - gameShareInfo.innerText = ''; - gameShareInfo.style.display = 'none'; + const gameShareInfo = this.gameShareElement.getChildByID("gameShareInfo") as HTMLParagraphElement; + gameShareInfo.innerText = ""; + gameShareInfo.style.display = "none"; this.gameShareOpened = false; this.tweens.add({ targets: this.gameShareElement, y: -400, duration: 1000, - ease: 'Power3' + ease: "Power3", }); } private onMenuClick(event: MouseEvent) { - const htmlMenuItem = (event?.target as HTMLInputElement); - if (htmlMenuItem.classList.contains('not-button')) { + const htmlMenuItem = event?.target as HTMLInputElement; + if (htmlMenuItem.classList.contains("not-button")) { return; } event.preventDefault(); if (htmlMenuItem.classList.contains("fromApi")) { - sendMenuClickedEvent(htmlMenuItem.id) - return + sendMenuClickedEvent(htmlMenuItem.id); + return; } switch ((event?.target as HTMLInputElement).id) { - case 'changeNameButton': + case "changeNameButton": this.closeSideMenu(); gameManager.leaveGame(this, LoginSceneName, new LoginScene()); break; - case 'sparkButton': + case "sparkButton": this.gotToCreateMapPage(); break; - case 'changeSkinButton': + case "changeSkinButton": this.closeSideMenu(); gameManager.leaveGame(this, SelectCharacterSceneName, new SelectCharacterScene()); break; - case 'changeCompanionButton': + case "changeCompanionButton": this.closeSideMenu(); gameManager.leaveGame(this, SelectCompanionSceneName, new SelectCompanionScene()); break; - case 'closeButton': + case "closeButton": this.closeSideMenu(); break; - case 'shareButton': + case "shareButton": this.openGameShare(); break; - case 'editGameSettingsButton': + case "editGameSettingsButton": this.openGameSettingsMenu(); break; - case 'toggleFullscreen': + case "toggleFullscreen": this.toggleFullscreen(); break; - case 'adminConsoleButton': + case "adminConsoleButton": if (get(consoleGlobalMessageManagerVisibleStore)) { consoleGlobalMessageManagerVisibleStore.set(false); } else { @@ -356,9 +364,9 @@ export class MenuScene extends Phaser.Scene { private async copyLink() { await navigator.clipboard.writeText(location.toString()); - const gameShareInfo = this.gameShareElement.getChildByID('gameShareInfo') as HTMLParagraphElement; - gameShareInfo.innerText = 'Link copied, you can share it now!'; - gameShareInfo.style.display = 'block'; + const gameShareInfo = this.gameShareElement.getChildByID("gameShareInfo") as HTMLParagraphElement; + gameShareInfo.innerText = "Link copied, you can share it now!"; + gameShareInfo.style.display = "block"; } private saveSetting(valueGame: number, valueVideo: number) { @@ -378,8 +386,8 @@ export class MenuScene extends Phaser.Scene { private gotToCreateMapPage() { //const sparkHost = 'https://'+window.location.host.replace('play.', '')+'/choose-map.html'; //TODO fix me: this button can to send us on WorkAdventure BO. - const sparkHost = 'https://workadventu.re/getting-started'; - window.open(sparkHost, '_blank'); + const sparkHost = "https://workadventu.re/getting-started"; + window.open(sparkHost, "_blank"); } private closeAll() { @@ -389,10 +397,10 @@ export class MenuScene extends Phaser.Scene { } private toggleFullscreen() { - const body = document.querySelector('body') + const body = document.querySelector("body"); if (body) { if (document.fullscreenElement ?? document.fullscreen) { - document.exitFullscreen() + document.exitFullscreen(); } else { body.requestFullscreen(); } diff --git a/front/src/Stores/BiggestAvailableAreaStore.ts b/front/src/Stores/BiggestAvailableAreaStore.ts index 716f37fc..e0a21cc6 100644 --- a/front/src/Stores/BiggestAvailableAreaStore.ts +++ b/front/src/Stores/BiggestAvailableAreaStore.ts @@ -1,16 +1,16 @@ -import {get, writable} from "svelte/store"; -import type {Box} from "../WebRtc/LayoutManager"; -import {HtmlUtils} from "../WebRtc/HtmlUtils"; -import {LayoutMode} from "../WebRtc/LayoutManager"; -import {layoutModeStore} from "./StreamableCollectionStore"; +import { get, writable } from "svelte/store"; +import type { Box } from "../WebRtc/LayoutManager"; +import { HtmlUtils } from "../WebRtc/HtmlUtils"; +import { LayoutMode } from "../WebRtc/LayoutManager"; +import { layoutModeStore } from "./StreamableCollectionStore"; /** * Tries to find the biggest available box of remaining space (this is a space where we can center the character) */ function findBiggestAvailableArea(): Box { - const game = HtmlUtils.querySelectorOrFail('#game canvas'); + const game = HtmlUtils.querySelectorOrFail("#game canvas"); if (get(layoutModeStore) === LayoutMode.VideoChat) { - const children = document.querySelectorAll('div.chat-mode > div'); + const children = document.querySelectorAll("div.chat-mode > div"); const htmlChildren = Array.from(children.values()); // No chat? Let's go full center @@ -19,18 +19,17 @@ function findBiggestAvailableArea(): Box { xStart: 0, yStart: 0, xEnd: game.offsetWidth, - yEnd: game.offsetHeight - } + yEnd: game.offsetHeight, + }; } const lastDiv = htmlChildren[htmlChildren.length - 1]; // Compute area between top right of the last div and bottom right of window - const area1 = (game.offsetWidth - (lastDiv.offsetLeft + lastDiv.offsetWidth)) - * (game.offsetHeight - lastDiv.offsetTop); + const area1 = + (game.offsetWidth - (lastDiv.offsetLeft + lastDiv.offsetWidth)) * (game.offsetHeight - lastDiv.offsetTop); // Compute area between bottom of last div and bottom of the screen on whole width - const area2 = game.offsetWidth - * (game.offsetHeight - (lastDiv.offsetTop + lastDiv.offsetHeight)); + const area2 = game.offsetWidth * (game.offsetHeight - (lastDiv.offsetTop + lastDiv.offsetHeight)); if (area1 < 0 && area2 < 0) { // If screen is full, let's not attempt something foolish and simply center character in the middle. @@ -38,28 +37,30 @@ function findBiggestAvailableArea(): Box { xStart: 0, yStart: 0, xEnd: game.offsetWidth, - yEnd: game.offsetHeight - } + yEnd: game.offsetHeight, + }; } if (area1 <= area2) { return { xStart: 0, yStart: lastDiv.offsetTop + lastDiv.offsetHeight, xEnd: game.offsetWidth, - yEnd: game.offsetHeight - } + yEnd: game.offsetHeight, + }; } else { return { xStart: lastDiv.offsetLeft + lastDiv.offsetWidth, yStart: lastDiv.offsetTop, xEnd: game.offsetWidth, - yEnd: game.offsetHeight - } + yEnd: game.offsetHeight, + }; } } else { // Possible destinations: at the center bottom or at the right bottom. - const mainSectionChildren = Array.from(document.querySelectorAll('div.main-section > div').values()); - const sidebarChildren = Array.from(document.querySelectorAll('aside.sidebar > div').values()); + const mainSectionChildren = Array.from( + document.querySelectorAll("div.main-section > div").values() + ); + const sidebarChildren = Array.from(document.querySelectorAll("aside.sidebar > div").values()); // No presentation? Let's center on the screen if (mainSectionChildren.length === 0) { @@ -67,60 +68,58 @@ function findBiggestAvailableArea(): Box { xStart: 0, yStart: 0, xEnd: game.offsetWidth, - yEnd: game.offsetHeight - } + yEnd: game.offsetHeight, + }; } // At this point, we know we have at least one element in the main section. - const lastPresentationDiv = mainSectionChildren[mainSectionChildren.length-1]; + const lastPresentationDiv = mainSectionChildren[mainSectionChildren.length - 1]; - const presentationArea = (game.offsetHeight - (lastPresentationDiv.offsetTop + lastPresentationDiv.offsetHeight)) - * (lastPresentationDiv.offsetLeft + lastPresentationDiv.offsetWidth); + const presentationArea = + (game.offsetHeight - (lastPresentationDiv.offsetTop + lastPresentationDiv.offsetHeight)) * + (lastPresentationDiv.offsetLeft + lastPresentationDiv.offsetWidth); let leftSideBar: number; let bottomSideBar: number; if (sidebarChildren.length === 0) { - leftSideBar = HtmlUtils.getElementByIdOrFail('sidebar').offsetLeft; + leftSideBar = HtmlUtils.getElementByIdOrFail("sidebar").offsetLeft; bottomSideBar = 0; } else { const lastSideBarChildren = sidebarChildren[sidebarChildren.length - 1]; leftSideBar = lastSideBarChildren.offsetLeft; bottomSideBar = lastSideBarChildren.offsetTop + lastSideBarChildren.offsetHeight; } - const sideBarArea = (game.offsetWidth - leftSideBar) - * (game.offsetHeight - bottomSideBar); + const sideBarArea = (game.offsetWidth - leftSideBar) * (game.offsetHeight - bottomSideBar); if (presentationArea <= sideBarArea) { return { xStart: leftSideBar, yStart: bottomSideBar, xEnd: game.offsetWidth, - yEnd: game.offsetHeight - } + yEnd: game.offsetHeight, + }; } else { return { xStart: 0, yStart: lastPresentationDiv.offsetTop + lastPresentationDiv.offsetHeight, - xEnd: /*lastPresentationDiv.offsetLeft + lastPresentationDiv.offsetWidth*/ game.offsetWidth , // To avoid flickering when a chat start, we center on the center of the screen, not the center of the main content area - yEnd: game.offsetHeight - } + xEnd: /*lastPresentationDiv.offsetLeft + lastPresentationDiv.offsetWidth*/ game.offsetWidth, // To avoid flickering when a chat start, we center on the center of the screen, not the center of the main content area + yEnd: game.offsetHeight, + }; } } } - /** * A store that contains the list of (video) peers we are connected to. */ function createBiggestAvailableAreaStore() { - - const { subscribe, set } = writable({xStart:0, yStart: 0, xEnd: 1, yEnd: 1}); + const { subscribe, set } = writable({ xStart: 0, yStart: 0, xEnd: 1, yEnd: 1 }); return { subscribe, recompute: () => { set(findBiggestAvailableArea()); - } + }, }; } diff --git a/front/src/Stores/GameOverlayStoreVisibility.ts b/front/src/Stores/GameOverlayStoreVisibility.ts index c58c929d..b1ca358b 100644 --- a/front/src/Stores/GameOverlayStoreVisibility.ts +++ b/front/src/Stores/GameOverlayStoreVisibility.ts @@ -1,4 +1,4 @@ -import {writable} from "svelte/store"; +import { writable } from "svelte/store"; /** * A store that contains whether the game overlay is shown or not. diff --git a/front/src/Stores/MediaStore.ts b/front/src/Stores/MediaStore.ts index 6cb9f75c..9144a6ee 100644 --- a/front/src/Stores/MediaStore.ts +++ b/front/src/Stores/MediaStore.ts @@ -1,14 +1,14 @@ -import {derived, get, Readable, readable, writable, Writable} from "svelte/store"; -import {localUserStore} from "../Connexion/LocalUserStore"; -import {userMovingStore} from "./GameStore"; -import {HtmlUtils} from "../WebRtc/HtmlUtils"; -import {BrowserTooOldError} from "./Errors/BrowserTooOldError"; -import {errorStore} from "./ErrorStore"; -import {isIOS} from "../WebRtc/DeviceUtils"; -import {WebviewOnOldIOS} from "./Errors/WebviewOnOldIOS"; -import {gameOverlayVisibilityStore} from "./GameOverlayStoreVisibility"; -import {peerStore} from "./PeerStore"; -import {privacyShutdownStore} from "./PrivacyShutdownStore"; +import { derived, get, Readable, readable, writable, Writable } from "svelte/store"; +import { localUserStore } from "../Connexion/LocalUserStore"; +import { userMovingStore } from "./GameStore"; +import { HtmlUtils } from "../WebRtc/HtmlUtils"; +import { BrowserTooOldError } from "./Errors/BrowserTooOldError"; +import { errorStore } from "./ErrorStore"; +import { isIOS } from "../WebRtc/DeviceUtils"; +import { WebviewOnOldIOS } from "./Errors/WebviewOnOldIOS"; +import { gameOverlayVisibilityStore } from "./GameOverlayStoreVisibility"; +import { peerStore } from "./PeerStore"; +import { privacyShutdownStore } from "./PrivacyShutdownStore"; /** * A store that contains the camera state requested by the user (on or off). @@ -57,7 +57,7 @@ export const enableCameraSceneVisibilityStore = createEnableCameraSceneVisibilit * A store containing whether the webcam was enabled in the last 10 seconds */ const enabledWebCam10secondsAgoStore = readable(false, function start(set) { - let timeout: NodeJS.Timeout|null = null; + let timeout: NodeJS.Timeout | null = null; const unsubscribe = requestedCameraState.subscribe((enabled) => { if (enabled === true) { @@ -71,7 +71,7 @@ const enabledWebCam10secondsAgoStore = readable(false, function start(set) { } else { set(false); } - }) + }); return function stop() { unsubscribe(); @@ -82,7 +82,7 @@ const enabledWebCam10secondsAgoStore = readable(false, function start(set) { * A store containing whether the webcam was enabled in the last 5 seconds */ const userMoved5SecondsAgoStore = readable(false, function start(set) { - let timeout: NodeJS.Timeout|null = null; + let timeout: NodeJS.Timeout | null = null; const unsubscribe = userMovingStore.subscribe((moving) => { if (moving === true) { @@ -94,45 +94,51 @@ const userMoved5SecondsAgoStore = readable(false, function start(set) { timeout = setTimeout(() => { set(false); }, 5000); - } - }) + }); return function stop() { unsubscribe(); }; }); - /** * A store containing whether the mouse is getting close the bottom right corner. */ const mouseInBottomRight = readable(false, function start(set) { let lastInBottomRight = false; - const gameDiv = HtmlUtils.getElementByIdOrFail('game'); + const gameDiv = HtmlUtils.getElementByIdOrFail("game"); const detectInBottomRight = (event: MouseEvent) => { const rect = gameDiv.getBoundingClientRect(); - const inBottomRight = event.x - rect.left > rect.width * 3 / 4 && event.y - rect.top > rect.height * 3 / 4; + const inBottomRight = event.x - rect.left > (rect.width * 3) / 4 && event.y - rect.top > (rect.height * 3) / 4; if (inBottomRight !== lastInBottomRight) { lastInBottomRight = inBottomRight; set(inBottomRight); } }; - document.addEventListener('mousemove', detectInBottomRight); + document.addEventListener("mousemove", detectInBottomRight); return function stop() { - document.removeEventListener('mousemove', detectInBottomRight); - } + document.removeEventListener("mousemove", detectInBottomRight); + }; }); /** * A store that contains "true" if the webcam should be stopped for energy efficiency reason - i.e. we are not moving and not in a conversation. */ -export const cameraEnergySavingStore = derived([userMoved5SecondsAgoStore, peerStore, enabledWebCam10secondsAgoStore, mouseInBottomRight], ([$userMoved5SecondsAgoStore,$peerStore, $enabledWebCam10secondsAgoStore, $mouseInBottomRight]) => { - return !$mouseInBottomRight && !$userMoved5SecondsAgoStore && $peerStore.size === 0 && !$enabledWebCam10secondsAgoStore; -}); +export const cameraEnergySavingStore = derived( + [userMoved5SecondsAgoStore, peerStore, enabledWebCam10secondsAgoStore, mouseInBottomRight], + ([$userMoved5SecondsAgoStore, $peerStore, $enabledWebCam10secondsAgoStore, $mouseInBottomRight]) => { + return ( + !$mouseInBottomRight && + !$userMoved5SecondsAgoStore && + $peerStore.size === 0 && + !$enabledWebCam10secondsAgoStore + ); + } +); /** * A store that contains video constraints. @@ -143,28 +149,30 @@ function createVideoConstraintStore() { height: { min: 400, ideal: 720 }, frameRate: { ideal: localUserStore.getVideoQualityValue() }, facingMode: "user", - resizeMode: 'crop-and-scale', - aspectRatio: 1.777777778 + resizeMode: "crop-and-scale", + aspectRatio: 1.777777778, } as MediaTrackConstraints); return { subscribe, - setDeviceId: (deviceId: string|undefined) => update((constraints) => { - if (deviceId !== undefined) { - constraints.deviceId = { - exact: deviceId - }; - } else { - delete constraints.deviceId; - } + setDeviceId: (deviceId: string | undefined) => + update((constraints) => { + if (deviceId !== undefined) { + constraints.deviceId = { + exact: deviceId, + }; + } else { + delete constraints.deviceId; + } - return constraints; - }), - setFrameRate: (frameRate: number) => update((constraints) => { - constraints.frameRate = { ideal: frameRate }; + return constraints; + }), + setFrameRate: (frameRate: number) => + update((constraints) => { + constraints.frameRate = { ideal: frameRate }; - return constraints; - }) + return constraints; + }), }; } @@ -178,39 +186,39 @@ function createAudioConstraintStore() { //TODO: make these values configurable in the game settings menu and store them in localstorage autoGainControl: false, echoCancellation: true, - noiseSuppression: true - } as boolean|MediaTrackConstraints); + noiseSuppression: true, + } as boolean | MediaTrackConstraints); let selectedDeviceId = null; return { subscribe, - setDeviceId: (deviceId: string|undefined) => update((constraints) => { - selectedDeviceId = deviceId; + setDeviceId: (deviceId: string | undefined) => + update((constraints) => { + selectedDeviceId = deviceId; - if (typeof(constraints) === 'boolean') { - constraints = {} - } - if (deviceId !== undefined) { - constraints.deviceId = { - exact: selectedDeviceId - }; - } else { - delete constraints.deviceId; - } + if (typeof constraints === "boolean") { + constraints = {}; + } + if (deviceId !== undefined) { + constraints.deviceId = { + exact: selectedDeviceId, + }; + } else { + delete constraints.deviceId; + } - return constraints; - }) + return constraints; + }), }; } export const audioConstraintStore = createAudioConstraintStore(); - let timeout: NodeJS.Timeout; -let previousComputedVideoConstraint: boolean|MediaTrackConstraints = false; -let previousComputedAudioConstraint: boolean|MediaTrackConstraints = false; +let previousComputedVideoConstraint: boolean | MediaTrackConstraints = false; +let previousComputedAudioConstraint: boolean | MediaTrackConstraints = false; /** * A store containing the media constraints we want to apply. @@ -225,7 +233,8 @@ export const mediaStreamConstraintsStore = derived( audioConstraintStore, privacyShutdownStore, cameraEnergySavingStore, - ], ( + ], + ( [ $requestedCameraState, $requestedMicrophoneState, @@ -235,92 +244,97 @@ export const mediaStreamConstraintsStore = derived( $audioConstraintStore, $privacyShutdownStore, $cameraEnergySavingStore, - ], set + ], + set ) => { + let currentVideoConstraint: boolean | MediaTrackConstraints = $videoConstraintStore; + let currentAudioConstraint: boolean | MediaTrackConstraints = $audioConstraintStore; - let currentVideoConstraint: boolean|MediaTrackConstraints = $videoConstraintStore; - let currentAudioConstraint: boolean|MediaTrackConstraints = $audioConstraintStore; - - if ($enableCameraSceneVisibilityStore) { - set({ - video: currentVideoConstraint, - audio: currentAudioConstraint, - }); - return; - } - - // Disable webcam if the user requested so - if ($requestedCameraState === false) { - currentVideoConstraint = false; - } - - // Disable microphone if the user requested so - if ($requestedMicrophoneState === false) { - currentAudioConstraint = false; - } - - // Disable webcam and microphone when in a Jitsi - if ($gameOverlayVisibilityStore === false) { - currentVideoConstraint = false; - currentAudioConstraint = false; - } - - // Disable webcam for privacy reasons (the game is not visible and we were talking to noone) - if ($privacyShutdownStore === true) { - currentVideoConstraint = false; - } - - // Disable webcam for energy reasons (the user is not moving and we are talking to noone) - if ($cameraEnergySavingStore === true) { - currentVideoConstraint = false; - currentAudioConstraint = false; - } - - // Let's make the changes only if the new value is different from the old one. - if (previousComputedVideoConstraint != currentVideoConstraint || previousComputedAudioConstraint != currentAudioConstraint) { - previousComputedVideoConstraint = currentVideoConstraint; - previousComputedAudioConstraint = currentAudioConstraint; - // Let's copy the objects. - if (typeof previousComputedVideoConstraint !== 'boolean') { - previousComputedVideoConstraint = {...previousComputedVideoConstraint}; - } - if (typeof previousComputedAudioConstraint !== 'boolean') { - previousComputedAudioConstraint = {...previousComputedAudioConstraint}; - } - - if (timeout) { - clearTimeout(timeout); - } - - // Let's wait a little bit to avoid sending too many constraint changes. - timeout = setTimeout(() => { + if ($enableCameraSceneVisibilityStore) { set({ video: currentVideoConstraint, audio: currentAudioConstraint, }); - }, 100); - } -}, { - video: false, - audio: false -} as MediaStreamConstraints); + return; + } + + // Disable webcam if the user requested so + if ($requestedCameraState === false) { + currentVideoConstraint = false; + } + + // Disable microphone if the user requested so + if ($requestedMicrophoneState === false) { + currentAudioConstraint = false; + } + + // Disable webcam and microphone when in a Jitsi + if ($gameOverlayVisibilityStore === false) { + currentVideoConstraint = false; + currentAudioConstraint = false; + } + + // Disable webcam for privacy reasons (the game is not visible and we were talking to noone) + if ($privacyShutdownStore === true) { + currentVideoConstraint = false; + } + + // Disable webcam for energy reasons (the user is not moving and we are talking to noone) + if ($cameraEnergySavingStore === true) { + currentVideoConstraint = false; + currentAudioConstraint = false; + } + + // Let's make the changes only if the new value is different from the old one. + if ( + previousComputedVideoConstraint != currentVideoConstraint || + previousComputedAudioConstraint != currentAudioConstraint + ) { + previousComputedVideoConstraint = currentVideoConstraint; + previousComputedAudioConstraint = currentAudioConstraint; + // Let's copy the objects. + if (typeof previousComputedVideoConstraint !== "boolean") { + previousComputedVideoConstraint = { ...previousComputedVideoConstraint }; + } + if (typeof previousComputedAudioConstraint !== "boolean") { + previousComputedAudioConstraint = { ...previousComputedAudioConstraint }; + } + + if (timeout) { + clearTimeout(timeout); + } + + // Let's wait a little bit to avoid sending too many constraint changes. + timeout = setTimeout(() => { + set({ + video: currentVideoConstraint, + audio: currentAudioConstraint, + }); + }, 100); + } + }, + { + video: false, + audio: false, + } as MediaStreamConstraints +); export type LocalStreamStoreValue = StreamSuccessValue | StreamErrorValue; interface StreamSuccessValue { - type: "success", - stream: MediaStream|null, + type: "success"; + stream: MediaStream | null; // The constraints that we got (and not the one that have been requested) - constraints: MediaStreamConstraints + constraints: MediaStreamConstraints; } interface StreamErrorValue { - type: "error", - error: Error, - constraints: MediaStreamConstraints + type: "error"; + error: Error; + constraints: MediaStreamConstraints; } -let currentStream : MediaStream|null = null; +let currentStream: MediaStream | null = null; /** * Stops the camera from filming @@ -347,84 +361,94 @@ function stopMicrophone(): void { /** * A store containing the MediaStream object (or null if nothing requested, or Error if an error occurred) */ -export const localStreamStore = derived, LocalStreamStoreValue>(mediaStreamConstraintsStore, ($mediaStreamConstraintsStore, set) => { - const constraints = { ...$mediaStreamConstraintsStore }; +export const localStreamStore = derived, LocalStreamStoreValue>( + mediaStreamConstraintsStore, + ($mediaStreamConstraintsStore, set) => { + const constraints = { ...$mediaStreamConstraintsStore }; - if (navigator.mediaDevices === undefined) { - if (window.location.protocol === 'http:') { - //throw new Error('Unable to access your camera or microphone. You need to use a HTTPS connection.'); + if (navigator.mediaDevices === undefined) { + if (window.location.protocol === "http:") { + //throw new Error('Unable to access your camera or microphone. You need to use a HTTPS connection.'); + set({ + type: "error", + error: new Error("Unable to access your camera or microphone. You need to use a HTTPS connection."), + constraints, + }); + return; + } else if (isIOS()) { + set({ + type: "error", + error: new WebviewOnOldIOS(), + constraints, + }); + return; + } else { + set({ + type: "error", + error: new BrowserTooOldError(), + constraints, + }); + return; + } + } + + if (constraints.audio === false) { + stopMicrophone(); + } + if (constraints.video === false) { + stopCamera(); + } + + if (constraints.audio === false && constraints.video === false) { + currentStream = null; set({ - type: 'error', - error: new Error('Unable to access your camera or microphone. You need to use a HTTPS connection.'), - constraints - }); - return; - } else if (isIOS()) { - set({ - type: 'error', - error: new WebviewOnOldIOS(), - constraints - }); - return; - } else { - set({ - type: 'error', - error: new BrowserTooOldError(), - constraints + type: "success", + stream: null, + constraints, }); return; } - } - if (constraints.audio === false) { - stopMicrophone(); - } - if (constraints.video === false) { - stopCamera(); - } - - if (constraints.audio === false && constraints.video === false) { - currentStream = null; - set({ - type: 'success', - stream: null, - constraints - }); - return; - } - - (async () => { - try { - stopMicrophone(); - stopCamera(); - currentStream = await navigator.mediaDevices.getUserMedia(constraints); - set({ - type: 'success', - stream: currentStream, - constraints - }); - return; - } catch (e) { - if (constraints.video !== false) { - console.info("Error. Unable to get microphone and/or camera access. Trying audio only.", $mediaStreamConstraintsStore, e); - // TODO: does it make sense to pop this error when retrying? + (async () => { + try { + stopMicrophone(); + stopCamera(); + currentStream = await navigator.mediaDevices.getUserMedia(constraints); set({ - type: 'error', - error: e, - constraints + type: "success", + stream: currentStream, + constraints, }); - // Let's try without video constraints - requestedCameraState.disableWebcam(); - } else { - console.info("Error. Unable to get microphone and/or camera access.", $mediaStreamConstraintsStore, e); - set({ - type: 'error', - error: e, - constraints - }); - } + return; + } catch (e) { + if (constraints.video !== false) { + console.info( + "Error. Unable to get microphone and/or camera access. Trying audio only.", + $mediaStreamConstraintsStore, + e + ); + // TODO: does it make sense to pop this error when retrying? + set({ + type: "error", + error: e, + constraints, + }); + // Let's try without video constraints + requestedCameraState.disableWebcam(); + } else { + console.info( + "Error. Unable to get microphone and/or camera access.", + $mediaStreamConstraintsStore, + e + ); + set({ + type: "error", + error: e, + constraints, + }); + } - /*constraints.video = false; + /*constraints.video = false; if (constraints.audio === false) { console.info("Error. Unable to get microphone and/or camera access.", $mediaStreamConstraintsStore, e); set({ @@ -453,9 +477,10 @@ export const localStreamStore = derived, LocalS }); } }*/ - } - })(); -}); + } + })(); + } +); /** * A store containing the real active media constrained (not the one requested by the user, but the one we got from the system) @@ -472,12 +497,15 @@ export const deviceListStore = readable([], function start(se const queryDeviceList = () => { // Note: so far, we are ignoring any failures. - navigator.mediaDevices.enumerateDevices().then((mediaDeviceInfos) => { - set(mediaDeviceInfos); - }).catch((e) => { - console.error(e); - throw e; - }); + navigator.mediaDevices + .enumerateDevices() + .then((mediaDeviceInfos) => { + set(mediaDeviceInfos); + }) + .catch((e) => { + console.error(e); + throw e; + }); }; const unsubscribe = localStreamStore.subscribe((streamResult) => { @@ -490,23 +518,23 @@ export const deviceListStore = readable([], function start(se }); if (navigator.mediaDevices) { - navigator.mediaDevices.addEventListener('devicechange', queryDeviceList); + navigator.mediaDevices.addEventListener("devicechange", queryDeviceList); } return function stop() { unsubscribe(); if (navigator.mediaDevices) { - navigator.mediaDevices.removeEventListener('devicechange', queryDeviceList); + navigator.mediaDevices.removeEventListener("devicechange", queryDeviceList); } }; }); export const cameraListStore = derived(deviceListStore, ($deviceListStore) => { - return $deviceListStore.filter(device => device.kind === 'videoinput'); + return $deviceListStore.filter((device) => device.kind === "videoinput"); }); export const microphoneListStore = derived(deviceListStore, ($deviceListStore) => { - return $deviceListStore.filter(device => device.kind === 'audioinput'); + return $deviceListStore.filter((device) => device.kind === "audioinput"); }); // TODO: detect the new webcam and automatically switch on it. @@ -519,7 +547,7 @@ cameraListStore.subscribe((devices) => { // If we cannot find the device ID, let's remove it. // @ts-ignore - if (!devices.find(device => device.deviceId === constraints.deviceId.exact)) { + if (!devices.find((device) => device.deviceId === constraints.deviceId.exact)) { videoConstraintStore.setDeviceId(undefined); } }); @@ -527,7 +555,7 @@ cameraListStore.subscribe((devices) => { microphoneListStore.subscribe((devices) => { // If the selected camera is unplugged, let's remove the constraint on deviceId const constraints = get(audioConstraintStore); - if (typeof constraints === 'boolean') { + if (typeof constraints === "boolean") { return; } if (!constraints.deviceId) { @@ -536,13 +564,13 @@ microphoneListStore.subscribe((devices) => { // If we cannot find the device ID, let's remove it. // @ts-ignore - if (!devices.find(device => device.deviceId === constraints.deviceId.exact)) { + if (!devices.find((device) => device.deviceId === constraints.deviceId.exact)) { audioConstraintStore.setDeviceId(undefined); } }); -localStreamStore.subscribe(streamResult => { - if (streamResult.type === 'error') { +localStreamStore.subscribe((streamResult) => { + if (streamResult.type === "error") { if (streamResult.error.name === BrowserTooOldError.NAME || streamResult.error.name === WebviewOnOldIOS.NAME) { errorStore.addErrorMessage(streamResult.error); } diff --git a/front/src/Stores/PeerStore.ts b/front/src/Stores/PeerStore.ts index 725c8940..11217b08 100644 --- a/front/src/Stores/PeerStore.ts +++ b/front/src/Stores/PeerStore.ts @@ -1,7 +1,7 @@ -import {readable, writable} from "svelte/store"; -import type {RemotePeer, SimplePeer} from "../WebRtc/SimplePeer"; -import {VideoPeer} from "../WebRtc/VideoPeer"; -import {ScreenSharingPeer} from "../WebRtc/ScreenSharingPeer"; +import { readable, writable } from "svelte/store"; +import type { RemotePeer, SimplePeer } from "../WebRtc/SimplePeer"; +import { VideoPeer } from "../WebRtc/VideoPeer"; +import { ScreenSharingPeer } from "../WebRtc/ScreenSharingPeer"; /** * A store that contains the list of (video) peers we are connected to. @@ -19,20 +19,20 @@ function createPeerStore() { simplePeer.registerPeerConnectionListener({ onConnect(peer: RemotePeer) { if (peer instanceof VideoPeer) { - update(users => { + update((users) => { users.set(peer.userId, peer); return users; }); } }, onDisconnect(userId: number) { - update(users => { + update((users) => { users.delete(userId); return users; }); - } - }) - } + }, + }); + }, }; } @@ -52,20 +52,20 @@ function createScreenSharingPeerStore() { simplePeer.registerPeerConnectionListener({ onConnect(peer: RemotePeer) { if (peer instanceof ScreenSharingPeer) { - update(users => { + update((users) => { users.set(peer.userId, peer); return users; }); } }, onDisconnect(userId: number) { - update(users => { + update((users) => { users.delete(userId); return users; }); - } - }) - } + }, + }); + }, }; } @@ -79,8 +79,7 @@ function createScreenSharingStreamStore() { let peers = new Map(); return readable>(peers, function start(set) { - - let unsubscribes: (()=>void)[] = []; + let unsubscribes: (() => void)[] = []; const unsubscribe = screenSharingPeerStore.subscribe((screenSharingPeers) => { for (const unsubscribe of unsubscribes) { @@ -91,24 +90,23 @@ function createScreenSharingStreamStore() { peers = new Map(); screenSharingPeers.forEach((screenSharingPeer: ScreenSharingPeer, key: number) => { - if (screenSharingPeer.isReceivingScreenSharingStream()) { peers.set(key, screenSharingPeer); } - unsubscribes.push(screenSharingPeer.streamStore.subscribe((stream) => { - if (stream) { - peers.set(key, screenSharingPeer); - } else { - peers.delete(key); - } - set(peers); - })); - + unsubscribes.push( + screenSharingPeer.streamStore.subscribe((stream) => { + if (stream) { + peers.set(key, screenSharingPeer); + } else { + peers.delete(key); + } + set(peers); + }) + ); }); set(peers); - }); return function stop() { @@ -117,9 +115,7 @@ function createScreenSharingStreamStore() { unsubscribe(); } }; - }) + }); } export const screenSharingStreamStore = createScreenSharingStreamStore(); - - diff --git a/front/src/Stores/PrivacyShutdownStore.ts b/front/src/Stores/PrivacyShutdownStore.ts index cd9cb1b9..6ef31fe7 100644 --- a/front/src/Stores/PrivacyShutdownStore.ts +++ b/front/src/Stores/PrivacyShutdownStore.ts @@ -1,6 +1,6 @@ -import {get, writable} from "svelte/store"; -import {peerStore} from "./PeerStore"; -import {visibilityStore} from "./VisibilityStore"; +import { get, writable } from "svelte/store"; +import { peerStore } from "./PeerStore"; +import { visibilityStore } from "./VisibilityStore"; /** * A store that contains "true" if the webcam should be stopped for privacy reasons - i.e. if the the user left the the page while not in a discussion. @@ -28,7 +28,6 @@ function createPrivacyShutdownStore() { } }); - return { subscribe, }; diff --git a/front/src/Stores/ScreenSharingStore.ts b/front/src/Stores/ScreenSharingStore.ts index ccb8c02c..e69307c8 100644 --- a/front/src/Stores/ScreenSharingStore.ts +++ b/front/src/Stores/ScreenSharingStore.ts @@ -1,12 +1,10 @@ -import {derived, get, Readable, readable, writable, Writable} from "svelte/store"; -import {peerStore} from "./PeerStore"; -import type { - LocalStreamStoreValue, -} from "./MediaStore"; -import {DivImportance} from "../WebRtc/LayoutManager"; -import {gameOverlayVisibilityStore} from "./GameOverlayStoreVisibility"; +import { derived, get, Readable, readable, writable, Writable } from "svelte/store"; +import { peerStore } from "./PeerStore"; +import type { LocalStreamStoreValue } from "./MediaStore"; +import { DivImportance } from "../WebRtc/LayoutManager"; +import { gameOverlayVisibilityStore } from "./GameOverlayStoreVisibility"; -declare const navigator:any; // eslint-disable-line @typescript-eslint/no-explicit-any +declare const navigator: any; // eslint-disable-line @typescript-eslint/no-explicit-any /** * A store that contains the camera state requested by the user (on or off). @@ -23,7 +21,7 @@ function createRequestedScreenSharingState() { export const requestedScreenSharingState = createRequestedScreenSharingState(); -let currentStream : MediaStream|null = null; +let currentStream: MediaStream | null = null; /** * Stops the camera from filming @@ -37,27 +35,17 @@ function stopScreenSharing(): void { currentStream = null; } -let previousComputedVideoConstraint: boolean|MediaTrackConstraints = false; -let previousComputedAudioConstraint: boolean|MediaTrackConstraints = false; +let previousComputedVideoConstraint: boolean | MediaTrackConstraints = false; +let previousComputedAudioConstraint: boolean | MediaTrackConstraints = false; /** * A store containing the media constraints we want to apply. */ export const screenSharingConstraintsStore = derived( - [ - requestedScreenSharingState, - gameOverlayVisibilityStore, - peerStore, - ], ( - [ - $requestedScreenSharingState, - $gameOverlayVisibilityStore, - $peerStore, - ], set - ) => { - - let currentVideoConstraint: boolean|MediaTrackConstraints = true; - let currentAudioConstraint: boolean|MediaTrackConstraints = false; + [requestedScreenSharingState, gameOverlayVisibilityStore, peerStore], + ([$requestedScreenSharingState, $gameOverlayVisibilityStore, $peerStore], set) => { + let currentVideoConstraint: boolean | MediaTrackConstraints = true; + let currentAudioConstraint: boolean | MediaTrackConstraints = false; // Disable screen sharing if the user requested so if (!$requestedScreenSharingState) { @@ -78,7 +66,10 @@ export const screenSharingConstraintsStore = derived( } // Let's make the changes only if the new value is different from the old one. - if (previousComputedVideoConstraint != currentVideoConstraint || previousComputedAudioConstraint != currentAudioConstraint) { + if ( + previousComputedVideoConstraint != currentVideoConstraint || + previousComputedAudioConstraint != currentAudioConstraint + ) { previousComputedVideoConstraint = currentVideoConstraint; previousComputedAudioConstraint = currentAudioConstraint; // Let's copy the objects. @@ -94,85 +85,89 @@ export const screenSharingConstraintsStore = derived( audio: currentAudioConstraint, }); } - }, { + }, + { video: false, - audio: false - } as MediaStreamConstraints); - + audio: false, + } as MediaStreamConstraints +); /** * A store containing the MediaStream object for ScreenSharing (or null if nothing requested, or Error if an error occurred) */ -export const screenSharingLocalStreamStore = derived, LocalStreamStoreValue>(screenSharingConstraintsStore, ($screenSharingConstraintsStore, set) => { - const constraints = $screenSharingConstraintsStore; +export const screenSharingLocalStreamStore = derived, LocalStreamStoreValue>( + screenSharingConstraintsStore, + ($screenSharingConstraintsStore, set) => { + const constraints = $screenSharingConstraintsStore; - if ($screenSharingConstraintsStore.video === false && $screenSharingConstraintsStore.audio === false) { - stopScreenSharing(); - requestedScreenSharingState.disableScreenSharing(); - set({ - type: 'success', - stream: null, - constraints - }); - return; - } - - let currentStreamPromise: Promise; - if (navigator.getDisplayMedia) { - currentStreamPromise = navigator.getDisplayMedia({constraints}); - } else if (navigator.mediaDevices && navigator.mediaDevices.getDisplayMedia) { - currentStreamPromise = navigator.mediaDevices.getDisplayMedia({constraints}); - } else { - stopScreenSharing(); - set({ - type: 'error', - error: new Error('Your browser does not support sharing screen'), - constraints - }); - return; - } - - (async () => { - try { + if ($screenSharingConstraintsStore.video === false && $screenSharingConstraintsStore.audio === false) { stopScreenSharing(); - currentStream = await currentStreamPromise; - - // If stream ends (for instance if user clicks the stop screen sharing button in the browser), let's close the view - for (const track of currentStream.getTracks()) { - track.onended = () => { - stopScreenSharing(); - requestedScreenSharingState.disableScreenSharing(); - previousComputedVideoConstraint = false; - previousComputedAudioConstraint = false; - set({ - type: 'success', - stream: null, - constraints: { - video: false, - audio: false - } - }); - }; - } - + requestedScreenSharingState.disableScreenSharing(); set({ - type: 'success', - stream: currentStream, - constraints + type: "success", + stream: null, + constraints, }); return; - } catch (e) { - currentStream = null; - requestedScreenSharingState.disableScreenSharing(); - console.info("Error. Unable to share screen.", e); - set({ - type: 'error', - error: e, - constraints - }); } - })(); -}); + + let currentStreamPromise: Promise; + if (navigator.getDisplayMedia) { + currentStreamPromise = navigator.getDisplayMedia({ constraints }); + } else if (navigator.mediaDevices && navigator.mediaDevices.getDisplayMedia) { + currentStreamPromise = navigator.mediaDevices.getDisplayMedia({ constraints }); + } else { + stopScreenSharing(); + set({ + type: "error", + error: new Error("Your browser does not support sharing screen"), + constraints, + }); + return; + } + + (async () => { + try { + stopScreenSharing(); + currentStream = await currentStreamPromise; + + // If stream ends (for instance if user clicks the stop screen sharing button in the browser), let's close the view + for (const track of currentStream.getTracks()) { + track.onended = () => { + stopScreenSharing(); + requestedScreenSharingState.disableScreenSharing(); + previousComputedVideoConstraint = false; + previousComputedAudioConstraint = false; + set({ + type: "success", + stream: null, + constraints: { + video: false, + audio: false, + }, + }); + }; + } + + set({ + type: "success", + stream: currentStream, + constraints, + }); + return; + } catch (e) { + currentStream = null; + requestedScreenSharingState.disableScreenSharing(); + console.info("Error. Unable to share screen.", e); + set({ + type: "error", + error: e, + constraints, + }); + } + })(); + } +); /** * A store containing whether the screen sharing button should be displayed or hidden. @@ -188,19 +183,18 @@ export const screenSharingAvailableStore = derived(peerStore, ($peerStore, set) export interface ScreenSharingLocalMedia { uniqueId: string; - stream: MediaStream|null; + stream: MediaStream | null; //subscribe(this: void, run: Subscriber, invalidate?: (value?: ScreenSharingLocalMedia) => void): Unsubscriber; } /** * The representation of the screen sharing stream. */ -export const screenSharingLocalMedia = readable(null, function start(set) { - +export const screenSharingLocalMedia = readable(null, function start(set) { const localMedia: ScreenSharingLocalMedia = { uniqueId: "localScreenSharingStream", - stream: null - } + stream: null, + }; const unsubscribe = screenSharingLocalStreamStore.subscribe((screenSharingLocalStream) => { if (screenSharingLocalStream.type === "success") { @@ -214,4 +208,4 @@ export const screenSharingLocalMedia = readable(nu return function stop() { unsubscribe(); }; -}) +}); diff --git a/front/src/Stores/ShowReportScreenStore.ts b/front/src/Stores/ShowReportScreenStore.ts index f05e545a..665048da 100644 --- a/front/src/Stores/ShowReportScreenStore.ts +++ b/front/src/Stores/ShowReportScreenStore.ts @@ -1,3 +1,3 @@ -import {writable} from "svelte/store"; +import { writable } from "svelte/store"; -export const showReportScreenStore = writable<{userId: number, userName: string}|null>(null); +export const showReportScreenStore = writable<{ userId: number; userName: string } | null>(null); diff --git a/front/src/Stores/StreamableCollectionStore.ts b/front/src/Stores/StreamableCollectionStore.ts index bbcd354f..687afc75 100644 --- a/front/src/Stores/StreamableCollectionStore.ts +++ b/front/src/Stores/StreamableCollectionStore.ts @@ -1,8 +1,8 @@ -import {derived, get, Readable, writable} from "svelte/store"; -import {ScreenSharingLocalMedia, screenSharingLocalMedia} from "./ScreenSharingStore"; -import { peerStore, screenSharingStreamStore} from "./PeerStore"; -import type {RemotePeer} from "../WebRtc/SimplePeer"; -import {LayoutMode} from "../WebRtc/LayoutManager"; +import { derived, get, Readable, writable } from "svelte/store"; +import { ScreenSharingLocalMedia, screenSharingLocalMedia } from "./ScreenSharingStore"; +import { peerStore, screenSharingStreamStore } from "./PeerStore"; +import type { RemotePeer } from "../WebRtc/SimplePeer"; +import { LayoutMode } from "../WebRtc/LayoutManager"; export type Streamable = RemotePeer | ScreenSharingLocalMedia; @@ -12,32 +12,25 @@ export const layoutModeStore = writable(LayoutMode.Presentation); * A store that contains everything that can produce a stream (so the peers + the local screen sharing stream) */ function createStreamableCollectionStore(): Readable> { + return derived( + [screenSharingStreamStore, peerStore, screenSharingLocalMedia], + ([$screenSharingStreamStore, $peerStore, $screenSharingLocalMedia], set) => { + const peers = new Map(); - return derived([ - screenSharingStreamStore, - peerStore, - screenSharingLocalMedia, - ], ([ - $screenSharingStreamStore, - $peerStore, - $screenSharingLocalMedia, - ], set) => { + const addPeer = (peer: Streamable) => { + peers.set(peer.uniqueId, peer); + }; - const peers = new Map(); + $screenSharingStreamStore.forEach(addPeer); + $peerStore.forEach(addPeer); - const addPeer = (peer: Streamable) => { - peers.set(peer.uniqueId, peer); - }; + if ($screenSharingLocalMedia?.stream) { + addPeer($screenSharingLocalMedia); + } - $screenSharingStreamStore.forEach(addPeer); - $peerStore.forEach(addPeer); - - if ($screenSharingLocalMedia?.stream) { - addPeer($screenSharingLocalMedia); + set(peers); } - - set(peers); - }); + ); } export const streamableCollectionStore = createStreamableCollectionStore(); diff --git a/front/src/Stores/VideoFocusStore.ts b/front/src/Stores/VideoFocusStore.ts index 28e948ce..e183a5f3 100644 --- a/front/src/Stores/VideoFocusStore.ts +++ b/front/src/Stores/VideoFocusStore.ts @@ -1,8 +1,8 @@ -import {writable} from "svelte/store"; -import type {RemotePeer, SimplePeer} from "../WebRtc/SimplePeer"; -import {VideoPeer} from "../WebRtc/VideoPeer"; -import {ScreenSharingPeer} from "../WebRtc/ScreenSharingPeer"; -import type {Streamable} from "./StreamableCollectionStore"; +import { writable } from "svelte/store"; +import type { RemotePeer, SimplePeer } from "../WebRtc/SimplePeer"; +import { VideoPeer } from "../WebRtc/VideoPeer"; +import { ScreenSharingPeer } from "../WebRtc/ScreenSharingPeer"; +import type { Streamable } from "./StreamableCollectionStore"; /** * A store that contains the peer / media that has currently the "importance" focus. @@ -32,15 +32,17 @@ function createVideoFocusStore() { }, connectToSimplePeer: (simplePeer: SimplePeer) => { simplePeer.registerPeerConnectionListener({ - onConnect(peer: RemotePeer) { - }, + onConnect(peer: RemotePeer) {}, onDisconnect(userId: number) { - if ((focusedMedia instanceof VideoPeer || focusedMedia instanceof ScreenSharingPeer) && focusedMedia.userId === userId) { + if ( + (focusedMedia instanceof VideoPeer || focusedMedia instanceof ScreenSharingPeer) && + focusedMedia.userId === userId + ) { set(null); } - } - }) - } + }, + }); + }, }; } diff --git a/front/src/Stores/VisibilityStore.ts b/front/src/Stores/VisibilityStore.ts index 4a13ffae..f2ce596c 100644 --- a/front/src/Stores/VisibilityStore.ts +++ b/front/src/Stores/VisibilityStore.ts @@ -1,16 +1,16 @@ -import {readable} from "svelte/store"; +import { readable } from "svelte/store"; /** * A store containing whether the current page is visible or not. */ -export const visibilityStore = readable(document.visibilityState === 'visible', function start(set) { +export const visibilityStore = readable(document.visibilityState === "visible", function start(set) { const onVisibilityChange = () => { - set(document.visibilityState === 'visible'); + set(document.visibilityState === "visible"); }; - document.addEventListener('visibilitychange', onVisibilityChange); + document.addEventListener("visibilitychange", onVisibilityChange); return function stop() { - document.removeEventListener('visibilitychange', onVisibilityChange); + document.removeEventListener("visibilitychange", onVisibilityChange); }; }); diff --git a/front/src/WebRtc/DiscussionManager.ts b/front/src/WebRtc/DiscussionManager.ts index b728eca0..ae351f76 100644 --- a/front/src/WebRtc/DiscussionManager.ts +++ b/front/src/WebRtc/DiscussionManager.ts @@ -1,11 +1,11 @@ -import {HtmlUtils} from "./HtmlUtils"; -import type {UserInputManager} from "../Phaser/UserInput/UserInputManager"; -import {connectionManager} from "../Connexion/ConnectionManager"; -import {GameConnexionTypes} from "../Url/UrlManager"; -import {iframeListener} from "../Api/IframeListener"; -import {showReportScreenStore} from "../Stores/ShowReportScreenStore"; +import { HtmlUtils } from "./HtmlUtils"; +import type { UserInputManager } from "../Phaser/UserInput/UserInputManager"; +import { connectionManager } from "../Connexion/ConnectionManager"; +import { GameConnexionTypes } from "../Url/UrlManager"; +import { iframeListener } from "../Api/IframeListener"; +import { showReportScreenStore } from "../Stores/ShowReportScreenStore"; -export type SendMessageCallback = (message:string) => void; +export type SendMessageCallback = (message: string) => void; export class DiscussionManager { private mainContainer: HTMLDivElement; @@ -15,80 +15,81 @@ export class DiscussionManager { private nbpParticipants?: HTMLParagraphElement; private divMessages?: HTMLParagraphElement; - private participants: Map = new Map(); + private participants: Map = new Map(); private activeDiscussion: boolean = false; - private sendMessageCallBack : Map = new Map(); + private sendMessageCallBack: Map = new Map< + number | string, + SendMessageCallback + >(); private userInputManager?: UserInputManager; constructor() { - this.mainContainer = HtmlUtils.getElementByIdOrFail('main-container'); - this.createDiscussPart(''); //todo: why do we always use empty string? + this.mainContainer = HtmlUtils.getElementByIdOrFail("main-container"); + this.createDiscussPart(""); //todo: why do we always use empty string? iframeListener.chatStream.subscribe((chatEvent) => { this.addMessage(chatEvent.author, chatEvent.message, false); this.showDiscussion(); }); - this.onSendMessageCallback('iframe_listener', (message) => { + this.onSendMessageCallback("iframe_listener", (message) => { iframeListener.sendUserInputChat(message); - }) + }); } private createDiscussPart(name: string) { - this.divDiscuss = document.createElement('div'); - this.divDiscuss.classList.add('discussion'); + this.divDiscuss = document.createElement("div"); + this.divDiscuss.classList.add("discussion"); - const buttonCloseDiscussion: HTMLButtonElement = document.createElement('button'); - buttonCloseDiscussion.classList.add('close-btn'); + const buttonCloseDiscussion: HTMLButtonElement = document.createElement("button"); + buttonCloseDiscussion.classList.add("close-btn"); buttonCloseDiscussion.innerHTML = ``; - buttonCloseDiscussion.addEventListener('click', () => { + buttonCloseDiscussion.addEventListener("click", () => { this.hideDiscussion(); }); this.divDiscuss.appendChild(buttonCloseDiscussion); - const myName: HTMLParagraphElement = document.createElement('p'); + const myName: HTMLParagraphElement = document.createElement("p"); myName.innerText = name.toUpperCase(); - this.nbpParticipants = document.createElement('p'); - this.nbpParticipants.innerText = 'PARTICIPANTS (1)'; + this.nbpParticipants = document.createElement("p"); + this.nbpParticipants.innerText = "PARTICIPANTS (1)"; - this.divParticipants = document.createElement('div'); - this.divParticipants.classList.add('participants'); + this.divParticipants = document.createElement("div"); + this.divParticipants.classList.add("participants"); - this.divMessages = document.createElement('div'); - this.divMessages.classList.add('messages'); - this.divMessages.innerHTML = "

Local messages

" + this.divMessages = document.createElement("div"); + this.divMessages.classList.add("messages"); + this.divMessages.innerHTML = "

Local messages

"; this.divDiscuss.appendChild(myName); this.divDiscuss.appendChild(this.nbpParticipants); this.divDiscuss.appendChild(this.divParticipants); this.divDiscuss.appendChild(this.divMessages); - const sendDivMessage: HTMLDivElement = document.createElement('div'); - sendDivMessage.classList.add('send-message'); - const inputMessage: HTMLInputElement = document.createElement('input'); + const sendDivMessage: HTMLDivElement = document.createElement("div"); + sendDivMessage.classList.add("send-message"); + const inputMessage: HTMLInputElement = document.createElement("input"); inputMessage.onfocus = () => { - if(this.userInputManager) { + if (this.userInputManager) { this.userInputManager.disableControls(); } - } + }; inputMessage.onblur = () => { - if(this.userInputManager) { + if (this.userInputManager) { this.userInputManager.restoreControls(); } - } + }; inputMessage.type = "text"; - inputMessage.addEventListener('keyup', (event: KeyboardEvent) => { - if (event.key === 'Enter') { + inputMessage.addEventListener("keyup", (event: KeyboardEvent) => { + if (event.key === "Enter") { event.preventDefault(); - if(inputMessage.value === null - || inputMessage.value === '' - || inputMessage.value === undefined) { + if (inputMessage.value === null || inputMessage.value === "" || inputMessage.value === undefined) { return; } this.addMessage(name, inputMessage.value, true); - for(const callback of this.sendMessageCallBack.values()) { + for (const callback of this.sendMessageCallBack.values()) { callback(inputMessage.value); } inputMessage.value = ""; @@ -100,44 +101,44 @@ export class DiscussionManager { //append in main container this.mainContainer.appendChild(this.divDiscuss); - this.addParticipant('me', 'Moi', undefined, true); + this.addParticipant("me", "Moi", undefined, true); } public addParticipant( - userId: number|'me', - name: string|undefined, - img?: string|undefined, - isMe: boolean = false, + userId: number | "me", + name: string | undefined, + img?: string | undefined, + isMe: boolean = false ) { - const divParticipant: HTMLDivElement = document.createElement('div'); - divParticipant.classList.add('participant'); + const divParticipant: HTMLDivElement = document.createElement("div"); + divParticipant.classList.add("participant"); divParticipant.id = `participant-${userId}`; - const divImgParticipant: HTMLImageElement = document.createElement('img'); - divImgParticipant.src = 'resources/logos/boy.svg'; + const divImgParticipant: HTMLImageElement = document.createElement("img"); + divImgParticipant.src = "resources/logos/boy.svg"; if (img !== undefined) { divImgParticipant.src = img; } - const divPParticipant: HTMLParagraphElement = document.createElement('p'); - if(!name){ - name = 'Anonymous'; + const divPParticipant: HTMLParagraphElement = document.createElement("p"); + if (!name) { + name = "Anonymous"; } divPParticipant.innerText = name; divParticipant.appendChild(divImgParticipant); divParticipant.appendChild(divPParticipant); - if( - !isMe - && connectionManager.getConnexionType - && connectionManager.getConnexionType !== GameConnexionTypes.anonymous - && userId !== 'me' + if ( + !isMe && + connectionManager.getConnexionType && + connectionManager.getConnexionType !== GameConnexionTypes.anonymous && + userId !== "me" ) { - const reportBanUserAction: HTMLButtonElement = document.createElement('button'); - reportBanUserAction.classList.add('report-btn') - reportBanUserAction.innerText = 'Report'; - reportBanUserAction.addEventListener('click', () => { - showReportScreenStore.set({ userId: userId, userName: name ? name : ''}); + const reportBanUserAction: HTMLButtonElement = document.createElement("button"); + reportBanUserAction.classList.add("report-btn"); + reportBanUserAction.innerText = "Report"; + reportBanUserAction.addEventListener("click", () => { + showReportScreenStore.set({ userId: userId, userName: name ? name : "" }); }); divParticipant.appendChild(reportBanUserAction); } @@ -157,16 +158,16 @@ export class DiscussionManager { } public addMessage(name: string, message: string, isMe: boolean = false) { - const divMessage: HTMLDivElement = document.createElement('div'); - divMessage.classList.add('message'); - if(isMe){ - divMessage.classList.add('me'); + const divMessage: HTMLDivElement = document.createElement("div"); + divMessage.classList.add("message"); + if (isMe) { + divMessage.classList.add("me"); } - const pMessage: HTMLParagraphElement = document.createElement('p'); + const pMessage: HTMLParagraphElement = document.createElement("p"); const date = new Date(); - if(isMe){ - name = 'Me'; + if (isMe) { + name = "Me"; } else { name = HtmlUtils.escapeHtml(name); } @@ -176,9 +177,9 @@ export class DiscussionManager { `; divMessage.appendChild(pMessage); - const userMessage: HTMLParagraphElement = document.createElement('p'); + const userMessage: HTMLParagraphElement = document.createElement("p"); userMessage.innerHTML = HtmlUtils.urlify(message); - userMessage.classList.add('body'); + userMessage.classList.add("body"); divMessage.appendChild(userMessage); this.divMessages?.appendChild(divMessage); @@ -186,14 +187,14 @@ export class DiscussionManager { setTimeout(() => { this.divMessages?.scroll({ top: this.divMessages?.scrollTop + divMessage.getBoundingClientRect().y, - behavior: 'smooth' + behavior: "smooth", }); }, 200); } - public removeParticipant(userId: number|string){ + public removeParticipant(userId: number | string) { const element = this.participants.get(userId); - if(element){ + if (element) { element.remove(); this.participants.delete(userId); } @@ -202,29 +203,29 @@ export class DiscussionManager { this.sendMessageCallBack.delete(userId); } - public onSendMessageCallback(userId: string|number, callback: SendMessageCallback): void { + public onSendMessageCallback(userId: string | number, callback: SendMessageCallback): void { this.sendMessageCallBack.set(userId, callback); } - get activatedDiscussion(){ + get activatedDiscussion() { return this.activeDiscussion; } - private showDiscussion(){ + private showDiscussion() { this.activeDiscussion = true; - this.divDiscuss?.classList.add('active'); + this.divDiscuss?.classList.add("active"); } - private hideDiscussion(){ + private hideDiscussion() { this.activeDiscussion = false; - this.divDiscuss?.classList.remove('active'); + this.divDiscuss?.classList.remove("active"); } - public setUserInputManager(userInputManager : UserInputManager){ + public setUserInputManager(userInputManager: UserInputManager) { this.userInputManager = userInputManager; } - public showDiscussionPart(){ + public showDiscussionPart() { this.showDiscussion(); } } diff --git a/front/src/WebRtc/LayoutManager.ts b/front/src/WebRtc/LayoutManager.ts index da295a98..47864139 100644 --- a/front/src/WebRtc/LayoutManager.ts +++ b/front/src/WebRtc/LayoutManager.ts @@ -1,5 +1,5 @@ import type { UserInputManager } from "../Phaser/UserInput/UserInputManager"; -import {HtmlUtils} from "./HtmlUtils"; +import { HtmlUtils } from "./HtmlUtils"; export enum LayoutMode { // All videos are displayed on the right side of the screen. If there is a screen sharing, it is displayed in the middle. @@ -15,40 +15,40 @@ export enum DivImportance { Normal = "Normal", } -export const ON_ACTION_TRIGGER_BUTTON = 'onaction'; +export const ON_ACTION_TRIGGER_BUTTON = "onaction"; -export const TRIGGER_WEBSITE_PROPERTIES = 'openWebsiteTrigger'; -export const TRIGGER_JITSI_PROPERTIES = 'jitsiTrigger'; +export const TRIGGER_WEBSITE_PROPERTIES = "openWebsiteTrigger"; +export const TRIGGER_JITSI_PROPERTIES = "jitsiTrigger"; -export const WEBSITE_MESSAGE_PROPERTIES = 'openWebsiteTriggerMessage'; -export const JITSI_MESSAGE_PROPERTIES = 'jitsiTriggerMessage'; +export const WEBSITE_MESSAGE_PROPERTIES = "openWebsiteTriggerMessage"; +export const JITSI_MESSAGE_PROPERTIES = "jitsiTriggerMessage"; -export const AUDIO_VOLUME_PROPERTY = 'audioVolume'; -export const AUDIO_LOOP_PROPERTY = 'audioLoop'; +export const AUDIO_VOLUME_PROPERTY = "audioVolume"; +export const AUDIO_LOOP_PROPERTY = "audioLoop"; -export type Box = {xStart: number, yStart: number, xEnd: number, yEnd: number}; +export type Box = { xStart: number; yStart: number; xEnd: number; yEnd: number }; class LayoutManager { private actionButtonTrigger: Map = new Map(); private actionButtonInformation: Map = new Map(); - public addActionButton(id: string, text: string, callBack: Function, userInputManager: UserInputManager){ + public addActionButton(id: string, text: string, callBack: Function, userInputManager: UserInputManager) { //delete previous element this.removeActionButton(id, userInputManager); //create div and text html component - const p = document.createElement('p'); - p.classList.add('action-body'); + const p = document.createElement("p"); + p.classList.add("action-body"); p.innerText = text; - const div = document.createElement('div'); - div.classList.add('action'); + const div = document.createElement("div"); + div.classList.add("action"); div.id = id; div.appendChild(p); this.actionButtonInformation.set(id, div); - const mainContainer = HtmlUtils.getElementByIdOrFail('main-container'); + const mainContainer = HtmlUtils.getElementByIdOrFail("main-container"); mainContainer.appendChild(div); //add trigger action @@ -57,42 +57,42 @@ class LayoutManager { userInputManager.addSpaceEventListner(callBack); } - public removeActionButton(id: string, userInputManager?: UserInputManager){ + public removeActionButton(id: string, userInputManager?: UserInputManager) { //delete previous element const previousDiv = this.actionButtonInformation.get(id); - if(previousDiv){ + if (previousDiv) { previousDiv.remove(); this.actionButtonInformation.delete(id); } const previousEventCallback = this.actionButtonTrigger.get(id); - if(previousEventCallback && userInputManager){ + if (previousEventCallback && userInputManager) { userInputManager.removeSpaceEventListner(previousEventCallback); } } - public addInformation(id: string, text: string, callBack?: Function, userInputManager?: UserInputManager){ + public addInformation(id: string, text: string, callBack?: Function, userInputManager?: UserInputManager) { //delete previous element - for ( const [key, value] of this.actionButtonInformation ) { + for (const [key, value] of this.actionButtonInformation) { this.removeActionButton(key, userInputManager); } //create div and text html component - const p = document.createElement('p'); - p.classList.add('action-body'); + const p = document.createElement("p"); + p.classList.add("action-body"); p.innerText = text; - const div = document.createElement('div'); - div.classList.add('action'); + const div = document.createElement("div"); + div.classList.add("action"); div.classList.add(id); div.id = id; div.appendChild(p); this.actionButtonInformation.set(id, div); - const mainContainer = HtmlUtils.getElementByIdOrFail('main-container'); + const mainContainer = HtmlUtils.getElementByIdOrFail("main-container"); mainContainer.appendChild(div); //add trigger action - if(callBack){ + if (callBack) { div.onpointerdown = () => { callBack(); this.removeActionButton(id, userInputManager); @@ -102,7 +102,7 @@ class LayoutManager { //remove it after 10 sec setTimeout(() => { this.removeActionButton(id, userInputManager); - }, 10000) + }, 10000); } } diff --git a/front/src/WebRtc/MediaManager.ts b/front/src/WebRtc/MediaManager.ts index faa5edf7..d9847f44 100644 --- a/front/src/WebRtc/MediaManager.ts +++ b/front/src/WebRtc/MediaManager.ts @@ -6,26 +6,20 @@ import { localUserStore } from "../Connexion/LocalUserStore"; import type { UserSimplePeerInterface } from "./SimplePeer"; import { SoundMeter } from "../Phaser/Components/SoundMeter"; import { DISABLE_NOTIFICATIONS } from "../Enum/EnvironmentVariable"; -import { - localStreamStore, -} from "../Stores/MediaStore"; -import { - screenSharingLocalStreamStore -} from "../Stores/ScreenSharingStore"; -import {helpCameraSettingsVisibleStore} from "../Stores/HelpCameraSettingsStore"; +import { localStreamStore } from "../Stores/MediaStore"; +import { screenSharingLocalStreamStore } from "../Stores/ScreenSharingStore"; +import { helpCameraSettingsVisibleStore } from "../Stores/HelpCameraSettingsStore"; export type UpdatedLocalStreamCallback = (media: MediaStream | null) => void; export type StartScreenSharingCallback = (media: MediaStream) => void; export type StopScreenSharingCallback = (media: MediaStream) => void; -import {cowebsiteCloseButtonId} from "./CoWebsiteManager"; -import {gameOverlayVisibilityStore} from "../Stores/GameOverlayStoreVisibility"; +import { cowebsiteCloseButtonId } from "./CoWebsiteManager"; +import { gameOverlayVisibilityStore } from "../Stores/GameOverlayStoreVisibility"; export class MediaManager { - startScreenSharingCallBacks : Set = new Set(); - stopScreenSharingCallBacks : Set = new Set(); - - + startScreenSharingCallBacks: Set = new Set(); + stopScreenSharingCallBacks: Set = new Set(); private focused: boolean = true; @@ -34,40 +28,49 @@ export class MediaManager { private userInputManager?: UserInputManager; constructor() { - //Check of ask notification navigator permission this.getNotification(); localStreamStore.subscribe((result) => { - if (result.type === 'error') { + if (result.type === "error") { console.error(result.error); - layoutManager.addInformation('warning', 'Camera access denied. Click here and check your browser permissions.', () => { - helpCameraSettingsVisibleStore.set(true); - }, this.userInputManager); + layoutManager.addInformation( + "warning", + "Camera access denied. Click here and check your browser permissions.", + () => { + helpCameraSettingsVisibleStore.set(true); + }, + this.userInputManager + ); return; } }); screenSharingLocalStreamStore.subscribe((result) => { - if (result.type === 'error') { + if (result.type === "error") { console.error(result.error); - layoutManager.addInformation('warning', 'Screen sharing denied. Click here and check your browser permissions.', () => { - helpCameraSettingsVisibleStore.set(true); - }, this.userInputManager); + layoutManager.addInformation( + "warning", + "Screen sharing denied. Click here and check your browser permissions.", + () => { + helpCameraSettingsVisibleStore.set(true); + }, + this.userInputManager + ); return; } }); } public showGameOverlay(): void { - const gameOverlay = HtmlUtils.getElementByIdOrFail('game-overlay'); - gameOverlay.classList.add('active'); + const gameOverlay = HtmlUtils.getElementByIdOrFail("game-overlay"); + gameOverlay.classList.add("active"); const buttonCloseFrame = HtmlUtils.getElementByIdOrFail(cowebsiteCloseButtonId); const functionTrigger = () => { this.triggerCloseJitsiFrameButton(); - } - buttonCloseFrame.removeEventListener('click', () => { + }; + buttonCloseFrame.removeEventListener("click", () => { buttonCloseFrame.blur(); functionTrigger(); }); @@ -76,14 +79,14 @@ export class MediaManager { } public hideGameOverlay(): void { - const gameOverlay = HtmlUtils.getElementByIdOrFail('game-overlay'); - gameOverlay.classList.remove('active'); + const gameOverlay = HtmlUtils.getElementByIdOrFail("game-overlay"); + gameOverlay.classList.remove("active"); const buttonCloseFrame = HtmlUtils.getElementByIdOrFail(cowebsiteCloseButtonId); const functionTrigger = () => { this.triggerCloseJitsiFrameButton(); - } - buttonCloseFrame.addEventListener('click', () => { + }; + buttonCloseFrame.addEventListener("click", () => { buttonCloseFrame.blur(); functionTrigger(); }); @@ -100,7 +103,7 @@ export class MediaManager { if (!element) { return; } - element.classList.add('active') //todo: why does a method 'disable' add a class 'active'? + element.classList.add("active"); //todo: why does a method 'disable' add a class 'active'? } enabledMicrophoneByUserId(userId: number) { @@ -108,7 +111,7 @@ export class MediaManager { if (!element) { return; } - element.classList.remove('active') //todo: why does a method 'enable' remove a class 'active'? + element.classList.remove("active"); //todo: why does a method 'enable' remove a class 'active'? } disabledVideoByUserId(userId: number) { @@ -134,8 +137,8 @@ export class MediaManager { } toggleBlockLogo(userId: number, show: boolean): void { - const blockLogoElement = HtmlUtils.getElementByIdOrFail('blocking-' + userId); - show ? blockLogoElement.classList.add('active') : blockLogoElement.classList.remove('active'); + const blockLogoElement = HtmlUtils.getElementByIdOrFail("blocking-" + userId); + show ? blockLogoElement.classList.add("active") : blockLogoElement.classList.remove("active"); } isError(userId: string): void { @@ -144,27 +147,28 @@ export class MediaManager { if (!element) { return; } - const errorDiv = element.getElementsByClassName('rtc-error').item(0) as HTMLDivElement | null; + const errorDiv = element.getElementsByClassName("rtc-error").item(0) as HTMLDivElement | null; if (errorDiv === null) { return; } - errorDiv.style.display = 'block'; + errorDiv.style.display = "block"; } isErrorScreenSharing(userId: string): void { this.isError(this.getScreenSharingId(userId)); } - private getSpinner(userId: string): HTMLDivElement | null { const element = document.getElementById(`div-${userId}`); if (!element) { return null; } - const connectingSpinnerDiv = element.getElementsByClassName('connecting-spinner').item(0) as HTMLDivElement|null; + const connectingSpinnerDiv = element + .getElementsByClassName("connecting-spinner") + .item(0) as HTMLDivElement | null; return connectingSpinnerDiv; } - public addTriggerCloseJitsiFrameButton(id: String, Function: Function){ + public addTriggerCloseJitsiFrameButton(id: String, Function: Function) { this.triggerCloseJistiFrame.set(id, Function); } @@ -191,12 +195,12 @@ export class MediaManager { discussionManager.onSendMessageCallback(userId, callback); } - public setUserInputManager(userInputManager : UserInputManager){ + public setUserInputManager(userInputManager: UserInputManager) { this.userInputManager = userInputManager; discussionManager.setUserInputManager(userInputManager); } - public getNotification(){ + public getNotification() { //Get notification if (!DISABLE_NOTIFICATIONS && window.Notification && Notification.permission !== "granted") { if (this.checkNotificationPromise()) { @@ -218,24 +222,24 @@ export class MediaManager { private checkNotificationPromise(): boolean { try { Notification.requestPermission().then(); - } catch(e) { + } catch (e) { return false; } return true; } - public createNotification(userName: string){ - if(this.focused){ + public createNotification(userName: string) { + if (this.focused) { return; } if (window.Notification && Notification.permission === "granted") { - const title = 'WorkAdventure'; + const title = "WorkAdventure"; const options = { body: `Hi! ${userName} wants to discuss with you, don't be afraid!`, - icon: '/resources/logos/logo-WA-min.png', - image: '/resources/logos/logo-WA-min.png', - badge: '/resources/logos/logo-WA-min.png', + icon: "/resources/logos/logo-WA-min.png", + image: "/resources/logos/logo-WA-min.png", + badge: "/resources/logos/logo-WA-min.png", }; new Notification(title, options); //new Notification(`Hi! ${userName} wants to discuss with you, don't be afraid!`); diff --git a/front/src/WebRtc/ScreenSharingPeer.ts b/front/src/WebRtc/ScreenSharingPeer.ts index be534276..9beab732 100644 --- a/front/src/WebRtc/ScreenSharingPeer.ts +++ b/front/src/WebRtc/ScreenSharingPeer.ts @@ -1,13 +1,13 @@ import type * as SimplePeerNamespace from "simple-peer"; -import {mediaManager} from "./MediaManager"; -import {STUN_SERVER, TURN_PASSWORD, TURN_SERVER, TURN_USER} from "../Enum/EnvironmentVariable"; -import type {RoomConnection} from "../Connexion/RoomConnection"; -import {MESSAGE_TYPE_CONSTRAINT, PeerStatus} from "./VideoPeer"; -import type {UserSimplePeerInterface} from "./SimplePeer"; -import {Readable, readable, writable, Writable} from "svelte/store"; -import {videoFocusStore} from "../Stores/VideoFocusStore"; +import { mediaManager } from "./MediaManager"; +import { STUN_SERVER, TURN_PASSWORD, TURN_SERVER, TURN_USER } from "../Enum/EnvironmentVariable"; +import type { RoomConnection } from "../Connexion/RoomConnection"; +import { MESSAGE_TYPE_CONSTRAINT, PeerStatus } from "./VideoPeer"; +import type { UserSimplePeerInterface } from "./SimplePeer"; +import { Readable, readable, writable, Writable } from "svelte/store"; +import { videoFocusStore } from "../Stores/VideoFocusStore"; -const Peer: SimplePeerNamespace.SimplePeer = require('simple-peer'); +const Peer: SimplePeerNamespace.SimplePeer = require("simple-peer"); /** * A peer connection used to transmit video / audio signals between 2 peers. @@ -16,7 +16,7 @@ export class ScreenSharingPeer extends Peer { /** * Whether this connection is currently receiving a video stream from a remote user. */ - private isReceivingStream:boolean = false; + private isReceivingStream: boolean = false; public toClose: boolean = false; public _connected: boolean = false; public readonly userId: number; @@ -24,29 +24,37 @@ export class ScreenSharingPeer extends Peer { public readonly streamStore: Readable; public readonly statusStore: Readable; - constructor(user: UserSimplePeerInterface, initiator: boolean, public readonly userName: string, private connection: RoomConnection, stream: MediaStream | null) { + constructor( + user: UserSimplePeerInterface, + initiator: boolean, + public readonly userName: string, + private connection: RoomConnection, + stream: MediaStream | null + ) { super({ initiator: initiator ? initiator : false, //reconnectTimer: 10000, config: { iceServers: [ { - urls: STUN_SERVER.split(',') + urls: STUN_SERVER.split(","), }, - TURN_SERVER !== '' ? { - urls: TURN_SERVER.split(','), - username: user.webRtcUser || TURN_USER, - credential: user.webRtcPassword || TURN_PASSWORD - } : undefined, - ].filter((value) => value !== undefined) - } + TURN_SERVER !== "" + ? { + urls: TURN_SERVER.split(","), + username: user.webRtcUser || TURN_USER, + credential: user.webRtcPassword || TURN_PASSWORD, + } + : undefined, + ].filter((value) => value !== undefined), + }, }); this.userId = user.userId; - this.uniqueId = 'screensharing_'+this.userId; + this.uniqueId = "screensharing_" + this.userId; - this.streamStore = readable(null, (set) => { - const onStream = (stream: MediaStream|null) => { + this.streamStore = readable(null, (set) => { + const onStream = (stream: MediaStream | null) => { videoFocusStore.focus(this); set(stream); }; @@ -54,71 +62,71 @@ export class ScreenSharingPeer extends Peer { // We unfortunately need to rely on an event to let the other party know a stream has stopped. // It seems there is no native way to detect that. // TODO: we might rely on the "ended" event: https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/ended_event - const message = JSON.parse(chunk.toString('utf8')); + const message = JSON.parse(chunk.toString("utf8")); if (message.streamEnded !== true) { - console.error('Unexpected message on screen sharing peer connection'); + console.error("Unexpected message on screen sharing peer connection"); return; } set(null); - } + }; - this.on('stream', onStream); - this.on('data', onData); + this.on("stream", onStream); + this.on("data", onData); return () => { - this.off('stream', onStream); - this.off('data', onData); + this.off("stream", onStream); + this.off("data", onData); }; }); this.statusStore = readable("connecting", (set) => { const onConnect = () => { - set('connected'); + set("connected"); }; const onError = () => { - set('error'); + set("error"); }; const onClose = () => { - set('closed'); + set("closed"); }; - this.on('connect', onConnect); - this.on('error', onError); - this.on('close', onClose); + this.on("connect", onConnect); + this.on("error", onError); + this.on("close", onClose); return () => { - this.off('connect', onConnect); - this.off('error', onError); - this.off('close', onClose); + this.off("connect", onConnect); + this.off("error", onError); + this.off("close", onClose); }; }); //start listen signal for the peer connection - this.on('signal', (data: unknown) => { + this.on("signal", (data: unknown) => { this.sendWebrtcScreenSharingSignal(data); }); - this.on('stream', (stream: MediaStream) => { + this.on("stream", (stream: MediaStream) => { this.stream(stream); }); - this.on('close', () => { + this.on("close", () => { this._connected = false; this.toClose = true; this.destroy(); }); // eslint-disable-next-line @typescript-eslint/no-explicit-any - this.on('error', (err: any) => { + this.on("error", (err: any) => { console.error(`screen sharing error => ${this.userId} => ${err.code}`, err); }); - this.on('connect', () => { + this.on("connect", () => { this._connected = true; console.info(`connect => ${this.userId}`); }); - this.once('finish', () => { + this.once("finish", () => { this._onFinish(); }); @@ -130,7 +138,7 @@ export class ScreenSharingPeer extends Peer { private sendWebrtcScreenSharingSignal(data: unknown) { try { this.connection.sendWebrtcScreenSharingSignal(data, this.userId); - }catch (e) { + } catch (e) { console.error(`sendWebrtcScreenSharingSignal => ${this.userId}`, e); } } @@ -139,7 +147,7 @@ export class ScreenSharingPeer extends Peer { * Sends received stream to screen. */ private stream(stream?: MediaStream) { - if(!stream){ + if (!stream) { this.isReceivingStream = false; } else { this.isReceivingStream = true; @@ -152,8 +160,8 @@ export class ScreenSharingPeer extends Peer { public destroy(error?: Error): void { try { - this._connected = false - if(!this.toClose){ + this._connected = false; + if (!this.toClose) { return; } // FIXME: I don't understand why "Closing connection with" message is displayed TWICE before "Nb users in peerConnectionArray" @@ -162,24 +170,24 @@ export class ScreenSharingPeer extends Peer { super.destroy(error); //console.log('Nb users in peerConnectionArray '+this.PeerConnectionArray.size); } catch (err) { - console.error("ScreenSharingPeer::destroy", err) + console.error("ScreenSharingPeer::destroy", err); } } - _onFinish () { - if (this.destroyed) return + _onFinish() { + if (this.destroyed) return; const destroySoon = () => { this.destroy(); - } + }; if (this._connected) { destroySoon(); } else { - this.once('connect', destroySoon); + this.once("connect", destroySoon); } } public stopPushingScreenSharingToRemoteUser(stream: MediaStream) { this.removeStream(stream); - this.write(new Buffer(JSON.stringify({type: MESSAGE_TYPE_CONSTRAINT, streamEnded: true}))); + this.write(new Buffer(JSON.stringify({ type: MESSAGE_TYPE_CONSTRAINT, streamEnded: true }))); } } diff --git a/front/src/WebRtc/SimplePeer.ts b/front/src/WebRtc/SimplePeer.ts index ecc0e21b..affcacd7 100644 --- a/front/src/WebRtc/SimplePeer.ts +++ b/front/src/WebRtc/SimplePeer.ts @@ -2,26 +2,22 @@ import type { WebRtcDisconnectMessageInterface, WebRtcSignalReceivedMessageInterface, } from "../Connexion/ConnexionModels"; -import { - mediaManager, - StartScreenSharingCallback, - StopScreenSharingCallback, -} from "./MediaManager"; -import {ScreenSharingPeer} from "./ScreenSharingPeer"; -import {MESSAGE_TYPE_BLOCKED, MESSAGE_TYPE_CONSTRAINT, MESSAGE_TYPE_MESSAGE, VideoPeer} from "./VideoPeer"; -import type {RoomConnection} from "../Connexion/RoomConnection"; -import {blackListManager} from "./BlackListManager"; -import {get} from "svelte/store"; -import {localStreamStore, LocalStreamStoreValue, obtainedMediaConstraintStore} from "../Stores/MediaStore"; -import {screenSharingLocalStreamStore} from "../Stores/ScreenSharingStore"; -import {discussionManager} from "./DiscussionManager"; +import { mediaManager, StartScreenSharingCallback, StopScreenSharingCallback } from "./MediaManager"; +import { ScreenSharingPeer } from "./ScreenSharingPeer"; +import { MESSAGE_TYPE_BLOCKED, MESSAGE_TYPE_CONSTRAINT, MESSAGE_TYPE_MESSAGE, VideoPeer } from "./VideoPeer"; +import type { RoomConnection } from "../Connexion/RoomConnection"; +import { blackListManager } from "./BlackListManager"; +import { get } from "svelte/store"; +import { localStreamStore, LocalStreamStoreValue, obtainedMediaConstraintStore } from "../Stores/MediaStore"; +import { screenSharingLocalStreamStore } from "../Stores/ScreenSharingStore"; +import { discussionManager } from "./DiscussionManager"; -export interface UserSimplePeerInterface{ +export interface UserSimplePeerInterface { userId: number; name?: string; initiator?: boolean; - webRtcUser?: string|undefined; - webRtcPassword?: string|undefined; + webRtcUser?: string | undefined; + webRtcPassword?: string | undefined; } export type RemotePeer = VideoPeer | ScreenSharingPeer; @@ -45,36 +41,40 @@ export class SimplePeer { private readonly unsubscribers: (() => void)[] = []; private readonly peerConnectionListeners: Array = new Array(); private readonly userId: number; - private lastWebrtcUserName: string|undefined; - private lastWebrtcPassword: string|undefined; + private lastWebrtcUserName: string | undefined; + private lastWebrtcPassword: string | undefined; constructor(private Connection: RoomConnection, private enableReporting: boolean, private myName: string) { // We need to go through this weird bound function pointer in order to be able to "free" this reference later. this.sendLocalScreenSharingStreamCallback = this.sendLocalScreenSharingStream.bind(this); this.stopLocalScreenSharingStreamCallback = this.stopLocalScreenSharingStream.bind(this); - this.unsubscribers.push(localStreamStore.subscribe((streamResult) => { - this.sendLocalVideoStream(streamResult); - })); + this.unsubscribers.push( + localStreamStore.subscribe((streamResult) => { + this.sendLocalVideoStream(streamResult); + }) + ); - let localScreenCapture: MediaStream|null = null; + let localScreenCapture: MediaStream | null = null; - this.unsubscribers.push(screenSharingLocalStreamStore.subscribe((streamResult) => { - if (streamResult.type === 'error') { - // Let's ignore screen sharing errors, we will deal with those in a different way. - return; - } - - if (streamResult.stream !== null) { - localScreenCapture = streamResult.stream; - this.sendLocalScreenSharingStream(localScreenCapture); - } else { - if (localScreenCapture) { - this.stopLocalScreenSharingStream(localScreenCapture); - localScreenCapture = null; + this.unsubscribers.push( + screenSharingLocalStreamStore.subscribe((streamResult) => { + if (streamResult.type === "error") { + // Let's ignore screen sharing errors, we will deal with those in a different way. + return; } - } - })); + + if (streamResult.stream !== null) { + localScreenCapture = streamResult.stream; + this.sendLocalScreenSharingStream(localScreenCapture); + } else { + if (localScreenCapture) { + this.stopLocalScreenSharingStream(localScreenCapture); + localScreenCapture = null; + } + } + }) + ); this.userId = Connection.getUserId(); this.initialise(); @@ -92,7 +92,6 @@ export class SimplePeer { * permit to listen when user could start visio */ private initialise() { - //receive signal by gemer this.Connection.receiveWebrtcSignal((message: WebRtcSignalReceivedMessageInterface) => { this.receiveWebrtcSignal(message); @@ -122,12 +121,12 @@ export class SimplePeer { // This would be symmetrical to the way we handle disconnection. //start connection - if(!user.initiator){ + if (!user.initiator) { return; } const streamResult = get(localStreamStore); - let stream : MediaStream | null = null; - if (streamResult.type === 'success' && streamResult.stream) { + let stream: MediaStream | null = null; + if (streamResult.type === "success" && streamResult.stream) { stream = streamResult.stream; } @@ -137,15 +136,15 @@ export class SimplePeer { /** * create peer connection to bind users */ - private createPeerConnection(user : UserSimplePeerInterface, localStream: MediaStream | null) : VideoPeer | null { - const peerConnection = this.PeerConnectionArray.get(user.userId) + private createPeerConnection(user: UserSimplePeerInterface, localStream: MediaStream | null): VideoPeer | null { + const peerConnection = this.PeerConnectionArray.get(user.userId); if (peerConnection) { if (peerConnection.destroyed) { peerConnection.toClose = true; peerConnection.destroy(); const peerConnexionDeleted = this.PeerConnectionArray.delete(user.userId); if (!peerConnexionDeleted) { - throw 'Error to delete peer connection'; + throw "Error to delete peer connection"; } //return this.createPeerConnection(user, localStream); } else { @@ -167,23 +166,32 @@ export class SimplePeer { const peer = new VideoPeer(user, user.initiator ? user.initiator : false, name, this.Connection, localStream); //permit to send message - mediaManager.addSendMessageCallback(user.userId,(message: string) => { - peer.write(new Buffer(JSON.stringify({type: MESSAGE_TYPE_MESSAGE, name: this.myName.toUpperCase(), userId: this.userId, message: message}))); + mediaManager.addSendMessageCallback(user.userId, (message: string) => { + peer.write( + new Buffer( + JSON.stringify({ + type: MESSAGE_TYPE_MESSAGE, + name: this.myName.toUpperCase(), + userId: this.userId, + message: message, + }) + ) + ); }); peer.toClose = false; // When a connection is established to a video stream, and if a screen sharing is taking place, // the user sharing screen should also initiate a connection to the remote user! - peer.on('connect', () => { + peer.on("connect", () => { const streamResult = get(screenSharingLocalStreamStore); - if (streamResult.type === 'success' && streamResult.stream !== null) { + if (streamResult.type === "success" && streamResult.stream !== null) { this.sendLocalScreenSharingStreamToUser(user.userId, streamResult.stream); } }); //Create a notification for first user in circle discussion - if(this.PeerConnectionArray.size === 0){ - mediaManager.createNotification(user.name??''); + if (this.PeerConnectionArray.size === 0) { + mediaManager.createNotification(user.name ?? ""); } this.PeerConnectionArray.set(user.userId, peer); @@ -196,27 +204,30 @@ export class SimplePeer { private getName(userId: number): string { const userSearch = this.Users.find((userSearch: UserSimplePeerInterface) => userSearch.userId === userId); if (userSearch) { - return userSearch.name || ''; + return userSearch.name || ""; } else { - return ''; + return ""; } } /** * create peer connection to bind users */ - private createPeerScreenSharingConnection(user : UserSimplePeerInterface, stream: MediaStream | null) : ScreenSharingPeer | null{ + private createPeerScreenSharingConnection( + user: UserSimplePeerInterface, + stream: MediaStream | null + ): ScreenSharingPeer | null { const peerConnection = this.PeerScreenSharingConnectionArray.get(user.userId); - if(peerConnection){ - if(peerConnection.destroyed){ + if (peerConnection) { + if (peerConnection.destroyed) { peerConnection.toClose = true; peerConnection.destroy(); const peerConnexionDeleted = this.PeerScreenSharingConnectionArray.delete(user.userId); - if(!peerConnexionDeleted){ - throw 'Error to delete peer connection'; + if (!peerConnexionDeleted) { + throw "Error to delete peer connection"; } this.createPeerConnection(user, stream); - }else { + } else { peerConnection.toClose = false; } return null; @@ -230,7 +241,13 @@ export class SimplePeer { const name = this.getName(user.userId); - const peer = new ScreenSharingPeer(user, user.initiator ? user.initiator : false, name, this.Connection, stream); + const peer = new ScreenSharingPeer( + user, + user.initiator ? user.initiator : false, + name, + this.Connection, + stream + ); this.PeerScreenSharingConnectionArray.set(user.userId, peer); for (const peerConnectionListener of this.peerConnectionListeners) { @@ -242,11 +259,13 @@ export class SimplePeer { /** * This is triggered twice. Once by the server, and once by a remote client disconnecting */ - private closeConnection(userId : number) { + private closeConnection(userId: number) { try { const peer = this.PeerConnectionArray.get(userId); if (peer === undefined) { - console.warn("closeConnection => Tried to close connection for user "+userId+" but could not find user"); + console.warn( + "closeConnection => Tried to close connection for user " + userId + " but could not find user" + ); return; } //create temp perr to close @@ -257,18 +276,18 @@ export class SimplePeer { this.closeScreenSharingConnection(userId); - const userIndex = this.Users.findIndex(user => user.userId === userId); - if(userIndex < 0){ - throw 'Couldn\'t delete user'; + const userIndex = this.Users.findIndex((user) => user.userId === userId); + if (userIndex < 0) { + throw "Couldn't delete user"; } else { this.Users.splice(userIndex, 1); } } catch (err) { - console.error("closeConnection", err) + console.error("closeConnection", err); } //if user left discussion, clear array peer connection of sharing - if(this.Users.length === 0) { + if (this.Users.length === 0) { for (const userId of this.PeerScreenSharingConnectionArray.keys()) { this.closeScreenSharingConnection(userId); this.PeerScreenSharingConnectionArray.delete(userId); @@ -283,12 +302,16 @@ export class SimplePeer { /** * This is triggered twice. Once by the server, and once by a remote client disconnecting */ - private closeScreenSharingConnection(userId : number) { + private closeScreenSharingConnection(userId: number) { try { //mediaManager.removeActiveScreenSharingVideo("" + userId); const peer = this.PeerScreenSharingConnectionArray.get(userId); if (peer === undefined) { - console.warn("closeScreenSharingConnection => Tried to close connection for user "+userId+" but could not find user") + console.warn( + "closeScreenSharingConnection => Tried to close connection for user " + + userId + + " but could not find user" + ); return; } // FIXME: I don't understand why "Closing connection with" message is displayed TWICE before "Nb users in peerConnectionArray" @@ -301,7 +324,7 @@ export class SimplePeer { }*/ //console.log('Nb users in peerConnectionArray '+this.PeerConnectionArray.size); } catch (err) { - console.error("closeConnection", err) + console.error("closeConnection", err); } } @@ -328,10 +351,10 @@ export class SimplePeer { private receiveWebrtcSignal(data: WebRtcSignalReceivedMessageInterface) { try { //if offer type, create peer connection - if(data.signal.type === "offer"){ + if (data.signal.type === "offer") { const streamResult = get(localStreamStore); - let stream : MediaStream | null = null; - if (streamResult.type === 'success' && streamResult.stream) { + let stream: MediaStream | null = null; + if (streamResult.type === "success" && streamResult.stream) { stream = streamResult.stream; } @@ -341,7 +364,7 @@ export class SimplePeer { if (peer !== undefined) { peer.signal(data.signal); } else { - console.error('Could not find peer whose ID is "'+data.userId+'" in PeerConnectionArray'); + console.error('Could not find peer whose ID is "' + data.userId + '" in PeerConnectionArray'); } } catch (e) { console.error(`receiveWebrtcSignal => ${data.userId}`, e); @@ -352,22 +375,24 @@ export class SimplePeer { if (blackListManager.isBlackListed(data.userId)) return; console.log("receiveWebrtcScreenSharingSignal", data); const streamResult = get(screenSharingLocalStreamStore); - let stream : MediaStream | null = null; - if (streamResult.type === 'success' && streamResult.stream !== null) { + let stream: MediaStream | null = null; + if (streamResult.type === "success" && streamResult.stream !== null) { stream = streamResult.stream; } try { //if offer type, create peer connection - if(data.signal.type === "offer"){ + if (data.signal.type === "offer") { this.createPeerScreenSharingConnection(data, stream); } const peer = this.PeerScreenSharingConnectionArray.get(data.userId); if (peer !== undefined) { peer.signal(data.signal); } else { - console.error('Could not find peer whose ID is "'+data.userId+'" in receiveWebrtcScreenSharingSignal'); - console.info('Attempt to create new peer connexion'); + console.error( + 'Could not find peer whose ID is "' + data.userId + '" in receiveWebrtcScreenSharingSignal' + ); + console.info("Attempt to create new peer connexion"); if (stream) { this.sendLocalScreenSharingStreamToUser(data.userId, stream); } @@ -384,17 +409,19 @@ export class SimplePeer { try { const PeerConnection = this.PeerConnectionArray.get(userId); if (!PeerConnection) { - throw new Error('While adding media, cannot find user with ID ' + userId); + throw new Error("While adding media, cannot find user with ID " + userId); } - PeerConnection.write(new Buffer(JSON.stringify({type: MESSAGE_TYPE_CONSTRAINT, ...streamResult.constraints}))); + PeerConnection.write( + new Buffer(JSON.stringify({ type: MESSAGE_TYPE_CONSTRAINT, ...streamResult.constraints })) + ); - if (streamResult.type === 'error') { + if (streamResult.type === "error") { return; } const localStream: MediaStream | null = streamResult.stream; - if(!localStream){ + if (!localStream) { return; } @@ -404,7 +431,7 @@ export class SimplePeer { (track as any).added = true; // eslint-disable-line @typescript-eslint/no-explicit-any PeerConnection.addTrack(track, localStream); } - }catch (e) { + } catch (e) { console.error(`pushVideoToRemoteUser => ${userId}`, e); } } @@ -412,7 +439,7 @@ export class SimplePeer { private pushScreenSharingToRemoteUser(userId: number, localScreenCapture: MediaStream) { const PeerConnection = this.PeerScreenSharingConnectionArray.get(userId); if (!PeerConnection) { - throw new Error('While pushing screen sharing, cannot find user with ID ' + userId); + throw new Error("While pushing screen sharing, cannot find user with ID " + userId); } for (const track of localScreenCapture.getTracks()) { @@ -421,7 +448,7 @@ export class SimplePeer { return; } - public sendLocalVideoStream(streamResult: LocalStreamStoreValue){ + public sendLocalVideoStream(streamResult: LocalStreamStoreValue) { for (const user of this.Users) { this.pushVideoToRemoteUser(user.userId, streamResult); } @@ -455,9 +482,12 @@ export class SimplePeer { const screenSharingUser: UserSimplePeerInterface = { userId, - initiator: true + initiator: true, }; - const PeerConnectionScreenSharing = this.createPeerScreenSharingConnection(screenSharingUser, localScreenCapture); + const PeerConnectionScreenSharing = this.createPeerScreenSharingConnection( + screenSharingUser, + localScreenCapture + ); if (!PeerConnectionScreenSharing) { return; } @@ -466,7 +496,7 @@ export class SimplePeer { private stopLocalScreenSharingStreamToUser(userId: number, stream: MediaStream): void { const PeerConnectionScreenSharing = this.PeerScreenSharingConnectionArray.get(userId); if (!PeerConnectionScreenSharing) { - throw new Error('Weird, screen sharing connection to user ' + userId + 'not found') + throw new Error("Weird, screen sharing connection to user " + userId + "not found"); } console.log("updatedScreenSharing => destroy", PeerConnectionScreenSharing); diff --git a/front/src/WebRtc/VideoPeer.ts b/front/src/WebRtc/VideoPeer.ts index 5b5212b9..30328c75 100644 --- a/front/src/WebRtc/VideoPeer.ts +++ b/front/src/WebRtc/VideoPeer.ts @@ -1,22 +1,22 @@ import type * as SimplePeerNamespace from "simple-peer"; -import {mediaManager} from "./MediaManager"; -import {STUN_SERVER, TURN_PASSWORD, TURN_SERVER, TURN_USER} from "../Enum/EnvironmentVariable"; -import type {RoomConnection} from "../Connexion/RoomConnection"; -import {blackListManager} from "./BlackListManager"; -import type {Subscription} from "rxjs"; -import type {UserSimplePeerInterface} from "./SimplePeer"; -import {get, readable, Readable} from "svelte/store"; -import {obtainedMediaConstraintStore} from "../Stores/MediaStore"; -import {discussionManager} from "./DiscussionManager"; +import { mediaManager } from "./MediaManager"; +import { STUN_SERVER, TURN_PASSWORD, TURN_SERVER, TURN_USER } from "../Enum/EnvironmentVariable"; +import type { RoomConnection } from "../Connexion/RoomConnection"; +import { blackListManager } from "./BlackListManager"; +import type { Subscription } from "rxjs"; +import type { UserSimplePeerInterface } from "./SimplePeer"; +import { get, readable, Readable } from "svelte/store"; +import { obtainedMediaConstraintStore } from "../Stores/MediaStore"; +import { discussionManager } from "./DiscussionManager"; -const Peer: SimplePeerNamespace.SimplePeer = require('simple-peer'); +const Peer: SimplePeerNamespace.SimplePeer = require("simple-peer"); export type PeerStatus = "connecting" | "connected" | "error" | "closed"; -export const MESSAGE_TYPE_CONSTRAINT = 'constraint'; -export const MESSAGE_TYPE_MESSAGE = 'message'; -export const MESSAGE_TYPE_BLOCKED = 'blocked'; -export const MESSAGE_TYPE_UNBLOCKED = 'unblocked'; +export const MESSAGE_TYPE_CONSTRAINT = "constraint"; +export const MESSAGE_TYPE_MESSAGE = "message"; +export const MESSAGE_TYPE_BLOCKED = "blocked"; +export const MESSAGE_TYPE_UNBLOCKED = "unblocked"; /** * A peer connection used to transmit video / audio signals between 2 peers. */ @@ -31,116 +31,124 @@ export class VideoPeer extends Peer { private onUnBlockSubscribe: Subscription; public readonly streamStore: Readable; public readonly statusStore: Readable; - public readonly constraintsStore: Readable; + public readonly constraintsStore: Readable; - constructor(public user: UserSimplePeerInterface, initiator: boolean, public readonly userName: string, private connection: RoomConnection, localStream: MediaStream | null) { + constructor( + public user: UserSimplePeerInterface, + initiator: boolean, + public readonly userName: string, + private connection: RoomConnection, + localStream: MediaStream | null + ) { super({ initiator: initiator ? initiator : false, //reconnectTimer: 10000, config: { iceServers: [ { - urls: STUN_SERVER.split(',') + urls: STUN_SERVER.split(","), }, - TURN_SERVER !== '' ? { - urls: TURN_SERVER.split(','), - username: user.webRtcUser || TURN_USER, - credential: user.webRtcPassword || TURN_PASSWORD - } : undefined, - ].filter((value) => value !== undefined) - } + TURN_SERVER !== "" + ? { + urls: TURN_SERVER.split(","), + username: user.webRtcUser || TURN_USER, + credential: user.webRtcPassword || TURN_PASSWORD, + } + : undefined, + ].filter((value) => value !== undefined), + }, }); this.userId = user.userId; - this.uniqueId = 'video_'+this.userId; + this.uniqueId = "video_" + this.userId; - this.streamStore = readable(null, (set) => { - const onStream = (stream: MediaStream|null) => { + this.streamStore = readable(null, (set) => { + const onStream = (stream: MediaStream | null) => { set(stream); }; const onData = (chunk: Buffer) => { - this.on('data', (chunk: Buffer) => { - const message = JSON.parse(chunk.toString('utf8')); + this.on("data", (chunk: Buffer) => { + const message = JSON.parse(chunk.toString("utf8")); if (message.type === MESSAGE_TYPE_CONSTRAINT) { if (!message.video) { set(null); } } }); - } + }; - this.on('stream', onStream); - this.on('data', onData); + this.on("stream", onStream); + this.on("data", onData); return () => { - this.off('stream', onStream); - this.off('data', onData); + this.off("stream", onStream); + this.off("data", onData); }; }); - this.constraintsStore = readable(null, (set) => { + this.constraintsStore = readable(null, (set) => { const onData = (chunk: Buffer) => { - const message = JSON.parse(chunk.toString('utf8')); - if(message.type === MESSAGE_TYPE_CONSTRAINT) { + const message = JSON.parse(chunk.toString("utf8")); + if (message.type === MESSAGE_TYPE_CONSTRAINT) { set(message); } - } + }; - this.on('data', onData); + this.on("data", onData); return () => { - this.off('data', onData); + this.off("data", onData); }; }); this.statusStore = readable("connecting", (set) => { const onConnect = () => { - set('connected'); + set("connected"); }; const onError = () => { - set('error'); + set("error"); }; const onClose = () => { - set('closed'); + set("closed"); }; - this.on('connect', onConnect); - this.on('error', onError); - this.on('close', onClose); + this.on("connect", onConnect); + this.on("error", onError); + this.on("close", onClose); return () => { - this.off('connect', onConnect); - this.off('error', onError); - this.off('close', onClose); + this.off("connect", onConnect); + this.off("error", onError); + this.off("close", onClose); }; }); //start listen signal for the peer connection - this.on('signal', (data: unknown) => { + this.on("signal", (data: unknown) => { this.sendWebrtcSignal(data); }); - this.on('stream', (stream: MediaStream) => this.stream(stream)); + this.on("stream", (stream: MediaStream) => this.stream(stream)); - this.on('close', () => { + this.on("close", () => { this._connected = false; this.toClose = true; this.destroy(); }); // eslint-disable-next-line @typescript-eslint/no-explicit-any - this.on('error', (err: any) => { + this.on("error", (err: any) => { console.error(`error => ${this.userId} => ${err.code}`, err); mediaManager.isError("" + this.userId); }); - this.on('connect', () => { + this.on("connect", () => { this._connected = true; }); - this.on('data', (chunk: Buffer) => { - const message = JSON.parse(chunk.toString('utf8')); - if(message.type === MESSAGE_TYPE_CONSTRAINT) { + this.on("data", (chunk: Buffer) => { + const message = JSON.parse(chunk.toString("utf8")); + if (message.type === MESSAGE_TYPE_CONSTRAINT) { if (message.audio) { mediaManager.enabledMicrophoneByUserId(this.userId); } else { @@ -152,23 +160,23 @@ export class VideoPeer extends Peer { } else { mediaManager.disabledVideoByUserId(this.userId); } - } else if(message.type === MESSAGE_TYPE_MESSAGE) { + } else if (message.type === MESSAGE_TYPE_MESSAGE) { if (!blackListManager.isBlackListed(message.userId)) { mediaManager.addNewMessage(message.name, message.message); } - } else if(message.type === MESSAGE_TYPE_BLOCKED) { + } else if (message.type === MESSAGE_TYPE_BLOCKED) { //FIXME when A blacklists B, the output stream from A is muted in B's js client. This is insecure since B can manipulate the code to unmute A stream. // Find a way to block A's output stream in A's js client //However, the output stream stream B is correctly blocked in A client this.blocked = true; this.toggleRemoteStream(false); - } else if(message.type === MESSAGE_TYPE_UNBLOCKED) { + } else if (message.type === MESSAGE_TYPE_UNBLOCKED) { this.blocked = false; this.toggleRemoteStream(true); } }); - this.once('finish', () => { + this.once("finish", () => { this._onFinish(); }); @@ -187,23 +195,32 @@ export class VideoPeer extends Peer { }); if (blackListManager.isBlackListed(this.userId)) { - this.sendBlockMessage(true) + this.sendBlockMessage(true); } } private sendBlockMessage(blocking: boolean) { - this.write(new Buffer(JSON.stringify({type: blocking ? MESSAGE_TYPE_BLOCKED : MESSAGE_TYPE_UNBLOCKED, name: this.userName.toUpperCase(), userId: this.userId, message: ''}))); + this.write( + new Buffer( + JSON.stringify({ + type: blocking ? MESSAGE_TYPE_BLOCKED : MESSAGE_TYPE_UNBLOCKED, + name: this.userName.toUpperCase(), + userId: this.userId, + message: "", + }) + ) + ); } private toggleRemoteStream(enable: boolean) { - this.remoteStream.getTracks().forEach(track => track.enabled = enable); + this.remoteStream.getTracks().forEach((track) => (track.enabled = enable)); mediaManager.toggleBlockLogo(this.userId, !enable); } private sendWebrtcSignal(data: unknown) { try { this.connection.sendWebrtcSignal(data, this.userId); - }catch (e) { + } catch (e) { console.error(`sendWebrtcSignal => ${this.userId}`, e); } } @@ -217,7 +234,7 @@ export class VideoPeer extends Peer { if (blackListManager.isBlackListed(this.userId) || this.blocked) { this.toggleRemoteStream(false); } - }catch (err){ + } catch (err) { console.error(err); } } @@ -227,8 +244,8 @@ export class VideoPeer extends Peer { */ public destroy(error?: Error): void { try { - this._connected = false - if(!this.toClose){ + this._connected = false; + if (!this.toClose) { return; } this.onBlockSubscribe.unsubscribe(); @@ -238,34 +255,36 @@ export class VideoPeer extends Peer { // I do understand the method closeConnection is called twice, but I don't understand how they manage to run in parallel. super.destroy(error); } catch (err) { - console.error("VideoPeer::destroy", err) + console.error("VideoPeer::destroy", err); } } - _onFinish () { - if (this.destroyed) return + _onFinish() { + if (this.destroyed) return; const destroySoon = () => { this.destroy(); - } + }; if (this._connected) { destroySoon(); } else { - this.once('connect', destroySoon); + this.once("connect", destroySoon); } } private pushVideoToRemoteUser(localStream: MediaStream | null) { try { - this.write(new Buffer(JSON.stringify({type: MESSAGE_TYPE_CONSTRAINT, ...get(obtainedMediaConstraintStore)}))); + this.write( + new Buffer(JSON.stringify({ type: MESSAGE_TYPE_CONSTRAINT, ...get(obtainedMediaConstraintStore) })) + ); - if(!localStream){ + if (!localStream) { return; } for (const track of localStream.getTracks()) { this.addTrack(track, localStream); } - }catch (e) { + } catch (e) { console.error(`pushVideoToRemoteUser => ${this.userId}`, e); } } diff --git a/front/src/iframe_api.ts b/front/src/iframe_api.ts index 0c2a7fde..a1949106 100644 --- a/front/src/iframe_api.ts +++ b/front/src/iframe_api.ts @@ -3,10 +3,10 @@ import { IframeResponseEvent, IframeResponseEventMap, isIframeResponseEventWrapper, - TypedMessageEvent + TypedMessageEvent, } from "./Api/Events/IframeEvent"; import chat from "./Api/iframe/chat"; -import type { IframeCallback } from './Api/iframe/IframeApiContribution'; +import type { IframeCallback } from "./Api/iframe/IframeApiContribution"; import nav from "./Api/iframe/nav"; import controls from "./Api/iframe/controls"; import ui from "./Api/iframe/ui"; @@ -16,7 +16,7 @@ import player from "./Api/iframe/player"; import type { ButtonDescriptor } from "./Api/iframe/Ui/ButtonDescriptor"; import type { Popup } from "./Api/iframe/Ui/Popup"; import type { Sound } from "./Api/iframe/Sound/Sound"; -import {sendToWorkadventure} from "./Api/iframe/IframeApiContribution"; +import { sendToWorkadventure } from "./Api/iframe/IframeApiContribution"; const wa = { ui, @@ -34,7 +34,7 @@ const wa = { * @deprecated Use WA.chat.sendChatMessage instead */ sendChatMessage(message: string, author: string): void { - console.warn('Method WA.sendChatMessage is deprecated. Please use WA.chat.sendChatMessage instead'); + console.warn("Method WA.sendChatMessage is deprecated. Please use WA.chat.sendChatMessage instead"); chat.sendChatMessage(message, author); }, @@ -42,7 +42,9 @@ const wa = { * @deprecated Use WA.chat.disablePlayerControls instead */ disablePlayerControls(): void { - console.warn('Method WA.disablePlayerControls is deprecated. Please use WA.controls.disablePlayerControls instead'); + console.warn( + "Method WA.disablePlayerControls is deprecated. Please use WA.controls.disablePlayerControls instead" + ); controls.disablePlayerControls(); }, @@ -50,7 +52,9 @@ const wa = { * @deprecated Use WA.controls.restorePlayerControls instead */ restorePlayerControls(): void { - console.warn('Method WA.restorePlayerControls is deprecated. Please use WA.controls.restorePlayerControls instead'); + console.warn( + "Method WA.restorePlayerControls is deprecated. Please use WA.controls.restorePlayerControls instead" + ); controls.restorePlayerControls(); }, @@ -58,7 +62,7 @@ const wa = { * @deprecated Use WA.ui.displayBubble instead */ displayBubble(): void { - console.warn('Method WA.displayBubble is deprecated. Please use WA.ui.displayBubble instead'); + console.warn("Method WA.displayBubble is deprecated. Please use WA.ui.displayBubble instead"); ui.displayBubble(); }, @@ -66,7 +70,7 @@ const wa = { * @deprecated Use WA.ui.removeBubble instead */ removeBubble(): void { - console.warn('Method WA.removeBubble is deprecated. Please use WA.ui.removeBubble instead'); + console.warn("Method WA.removeBubble is deprecated. Please use WA.ui.removeBubble instead"); ui.removeBubble(); }, @@ -74,7 +78,7 @@ const wa = { * @deprecated Use WA.nav.openTab instead */ openTab(url: string): void { - console.warn('Method WA.openTab is deprecated. Please use WA.nav.openTab instead'); + console.warn("Method WA.openTab is deprecated. Please use WA.nav.openTab instead"); nav.openTab(url); }, @@ -82,7 +86,7 @@ const wa = { * @deprecated Use WA.sound.loadSound instead */ loadSound(url: string): Sound { - console.warn('Method WA.loadSound is deprecated. Please use WA.sound.loadSound instead'); + console.warn("Method WA.loadSound is deprecated. Please use WA.sound.loadSound instead"); return sound.loadSound(url); }, @@ -90,7 +94,7 @@ const wa = { * @deprecated Use WA.nav.goToPage instead */ goToPage(url: string): void { - console.warn('Method WA.goToPage is deprecated. Please use WA.nav.goToPage instead'); + console.warn("Method WA.goToPage is deprecated. Please use WA.nav.goToPage instead"); nav.goToPage(url); }, @@ -98,7 +102,7 @@ const wa = { * @deprecated Use WA.nav.goToRoom instead */ goToRoom(url: string): void { - console.warn('Method WA.goToRoom is deprecated. Please use WA.nav.goToRoom instead'); + console.warn("Method WA.goToRoom is deprecated. Please use WA.nav.goToRoom instead"); nav.goToRoom(url); }, @@ -106,7 +110,7 @@ const wa = { * @deprecated Use WA.nav.openCoWebSite instead */ openCoWebSite(url: string, allowApi: boolean = false, allowPolicy: string = ""): void { - console.warn('Method WA.openCoWebSite is deprecated. Please use WA.nav.openCoWebSite instead'); + console.warn("Method WA.openCoWebSite is deprecated. Please use WA.nav.openCoWebSite instead"); nav.openCoWebSite(url, allowApi, allowPolicy); }, @@ -114,7 +118,7 @@ const wa = { * @deprecated Use WA.nav.closeCoWebSite instead */ closeCoWebSite(): void { - console.warn('Method WA.closeCoWebSite is deprecated. Please use WA.nav.closeCoWebSite instead'); + console.warn("Method WA.closeCoWebSite is deprecated. Please use WA.nav.closeCoWebSite instead"); nav.closeCoWebSite(); }, @@ -122,28 +126,28 @@ const wa = { * @deprecated Use WA.controls.restorePlayerControls instead */ openPopup(targetObject: string, message: string, buttons: ButtonDescriptor[]): Popup { - console.warn('Method WA.openPopup is deprecated. Please use WA.ui.openPopup instead'); + console.warn("Method WA.openPopup is deprecated. Please use WA.ui.openPopup instead"); return ui.openPopup(targetObject, message, buttons); }, /** * @deprecated Use WA.chat.onChatMessage instead */ onChatMessage(callback: (message: string) => void): void { - console.warn('Method WA.onChatMessage is deprecated. Please use WA.chat.onChatMessage instead'); + console.warn("Method WA.onChatMessage is deprecated. Please use WA.chat.onChatMessage instead"); chat.onChatMessage(callback); }, /** * @deprecated Use WA.room.onEnterZone instead */ onEnterZone(name: string, callback: () => void): void { - console.warn('Method WA.onEnterZone is deprecated. Please use WA.room.onEnterZone instead'); + console.warn("Method WA.onEnterZone is deprecated. Please use WA.room.onEnterZone instead"); room.onEnterZone(name, callback); }, /** * @deprecated Use WA.room.onLeaveZone instead */ onLeaveZone(name: string, callback: () => void): void { - console.warn('Method WA.onLeaveZone is deprecated. Please use WA.room.onLeaveZone instead'); + console.warn("Method WA.onLeaveZone is deprecated. Please use WA.room.onLeaveZone instead"); room.onLeaveZone(name, callback); }, }; @@ -151,30 +155,32 @@ const wa = { export type WorkAdventureApi = typeof wa; declare global { - interface Window { - WA: WorkAdventureApi + WA: WorkAdventureApi; } - let WA: WorkAdventureApi + let WA: WorkAdventureApi; } window.WA = wa; -window.addEventListener('message', (message: TypedMessageEvent>) => { - if (message.source !== window.parent) { - return; // Skip message in this event listener - } - const payload = message.data; - console.debug(payload); - - if (isIframeResponseEventWrapper(payload)) { - const payloadData = payload.data; - - const callback = registeredCallbacks[payload.type] as IframeCallback | undefined - if (callback?.typeChecker(payloadData)) { - callback?.callback(payloadData) +window.addEventListener( + "message", + (message: TypedMessageEvent>) => { + if (message.source !== window.parent) { + return; // Skip message in this event listener } - } + const payload = message.data; + console.debug(payload); - // ... -}); + if (isIframeResponseEventWrapper(payload)) { + const payloadData = payload.data; + + const callback = registeredCallbacks[payload.type] as IframeCallback | undefined; + if (callback?.typeChecker(payloadData)) { + callback?.callback(payloadData); + } + } + + // ... + } +); diff --git a/front/tests/Phaser/Game/RoomTest.ts b/front/tests/Phaser/Game/RoomTest.ts index 80624d64..4bd4283a 100644 --- a/front/tests/Phaser/Game/RoomTest.ts +++ b/front/tests/Phaser/Game/RoomTest.ts @@ -1,57 +1,57 @@ import "jasmine"; -import {Room} from "../../../src/Connexion/Room"; +import { Room } from "../../../src/Connexion/Room"; describe("Room getIdFromIdentifier()", () => { it("should work with an absolute room id and no hash as parameter", () => { - const {roomId, hash} = Room.getIdFromIdentifier('/_/global/maps.workadventu.re/test2.json', '', ''); + const { roomId, hash } = Room.getIdFromIdentifier('/_/global/maps.workadventu.re/test2.json', '', ''); expect(roomId).toEqual('_/global/maps.workadventu.re/test2.json'); - expect(hash).toEqual(''); + expect(hash).toEqual(null); }); it("should work with an absolute room id and a hash as parameters", () => { - const {roomId, hash} = Room.getIdFromIdentifier('/_/global/maps.workadventu.re/test2.json#start', '', ''); + const { roomId, hash } = Room.getIdFromIdentifier('/_/global/maps.workadventu.re/test2.json#start', '', ''); expect(roomId).toEqual('_/global/maps.workadventu.re/test2.json'); expect(hash).toEqual("start"); }); it("should work with an absolute room id, regardless of baseUrl or instance", () => { - const {roomId, hash} = Room.getIdFromIdentifier('/_/global/maps.workadventu.re/test2.json', 'https://another.domain/_/global/test.json', 'lol'); + const { roomId, hash } = Room.getIdFromIdentifier('/_/global/maps.workadventu.re/test2.json', 'https://another.domain/_/global/test.json', 'lol'); expect(roomId).toEqual('_/global/maps.workadventu.re/test2.json'); - expect(hash).toEqual(''); + expect(hash).toEqual(null); }); - - + + it("should work with a relative file link and no hash as parameters", () => { - const {roomId, hash} = Room.getIdFromIdentifier('./test2.json', 'https://maps.workadventu.re/test.json', 'global'); + const { roomId, hash } = Room.getIdFromIdentifier('./test2.json', 'https://maps.workadventu.re/test.json', 'global'); expect(roomId).toEqual('_/global/maps.workadventu.re/test2.json'); - expect(hash).toEqual(''); + expect(hash).toEqual(null); }); it("should work with a relative file link with no dot", () => { - const {roomId, hash} = Room.getIdFromIdentifier('test2.json', 'https://maps.workadventu.re/test.json', 'global'); + const { roomId, hash } = Room.getIdFromIdentifier('test2.json', 'https://maps.workadventu.re/test.json', 'global'); expect(roomId).toEqual('_/global/maps.workadventu.re/test2.json'); - expect(hash).toEqual(''); + expect(hash).toEqual(null); }); it("should work with a relative file link two levels deep", () => { - const {roomId, hash} = Room.getIdFromIdentifier('../floor1/Floor1.json', 'https://maps.workadventu.re/floor0/Floor0.json', 'global'); + const { roomId, hash } = Room.getIdFromIdentifier('../floor1/Floor1.json', 'https://maps.workadventu.re/floor0/Floor0.json', 'global'); expect(roomId).toEqual('_/global/maps.workadventu.re/floor1/Floor1.json'); - expect(hash).toEqual(''); + expect(hash).toEqual(null); }); it("should work with a relative file link that rewrite the map domain", () => { - const {roomId, hash} = Room.getIdFromIdentifier('../../maps.workadventure.localhost/Floor1/floor1.json', 'https://maps.workadventu.re/floor0/Floor0.json', 'global'); + const { roomId, hash } = Room.getIdFromIdentifier('../../maps.workadventure.localhost/Floor1/floor1.json', 'https://maps.workadventu.re/floor0/Floor0.json', 'global'); expect(roomId).toEqual('_/global/maps.workadventure.localhost/Floor1/floor1.json'); - expect(hash).toEqual(''); + expect(hash).toEqual(null); }); it("should work with a relative file link that rewrite the map instance", () => { - const {roomId, hash} = Room.getIdFromIdentifier('../../../notglobal/maps.workadventu.re/Floor1/floor1.json', 'https://maps.workadventu.re/floor0/Floor0.json', 'global'); + const { roomId, hash } = Room.getIdFromIdentifier('../../../notglobal/maps.workadventu.re/Floor1/floor1.json', 'https://maps.workadventu.re/floor0/Floor0.json', 'global'); expect(roomId).toEqual('_/notglobal/maps.workadventu.re/Floor1/floor1.json'); - expect(hash).toEqual(''); + expect(hash).toEqual(null); }); it("should work with a relative file link that change the map type", () => { - const {roomId, hash} = Room.getIdFromIdentifier('../../../../@/tcm/is/great', 'https://maps.workadventu.re/floor0/Floor0.json', 'global'); + const { roomId, hash } = Room.getIdFromIdentifier('../../../../@/tcm/is/great', 'https://maps.workadventu.re/floor0/Floor0.json', 'global'); expect(roomId).toEqual('@/tcm/is/great'); - expect(hash).toEqual(''); + expect(hash).toEqual(null); }); - + it("should work with a relative file link and a hash as parameters", () => { - const {roomId, hash} = Room.getIdFromIdentifier('./test2.json#start', 'https://maps.workadventu.re/test.json', 'global'); + const { roomId, hash } = Room.getIdFromIdentifier('./test2.json#start', 'https://maps.workadventu.re/test.json', 'global'); expect(roomId).toEqual('_/global/maps.workadventu.re/test2.json'); expect(hash).toEqual("start"); }); diff --git a/front/tests/Phaser/Map/LayersIteratorTest.ts b/front/tests/Phaser/Map/LayersIteratorTest.ts index 6cc4c0ff..a3951a7a 100644 --- a/front/tests/Phaser/Map/LayersIteratorTest.ts +++ b/front/tests/Phaser/Map/LayersIteratorTest.ts @@ -1,150 +1,155 @@ import "jasmine"; -import {Room} from "../../../src/Connexion/Room"; -import {flattenGroupLayersMap} from "../../../src/Phaser/Map/LayersFlattener"; -import type {ITiledMapLayer} from "../../../src/Phaser/Map/ITiledMap"; +import { Room } from "../../../src/Connexion/Room"; +import { flattenGroupLayersMap } from "../../../src/Phaser/Map/LayersFlattener"; +import type { ITiledMapLayer } from "../../../src/Phaser/Map/ITiledMap"; describe("Layers flattener", () => { it("should iterate maps with no group", () => { - let flatLayers:ITiledMapLayer[] = []; + let flatLayers: ITiledMapLayer[] = []; flatLayers = flattenGroupLayersMap({ - "compressionlevel": -1, - "height": 2, - "infinite": false, - "layers": [ + compressionlevel: -1, + height: 2, + infinite: false, + layers: [ { - "data": [0, 0, 0, 0], - "height": 2, - "id": 1, - "name": "Tile Layer 1", - "opacity": 1, - "type": "tilelayer", - "visible": true, - "width": 2, - "x": 0, - "y": 0 + data: [0, 0, 0, 0], + height: 2, + id: 1, + name: "Tile Layer 1", + opacity: 1, + type: "tilelayer", + visible: true, + width: 2, + x: 0, + y: 0, }, { - "data": [0, 0, 0, 0], - "height": 2, - "id": 1, - "name": "Tile Layer 2", - "opacity": 1, - "type": "tilelayer", - "visible": true, - "width": 2, - "x": 0, - "y": 0 - }], - "nextlayerid": 2, - "nextobjectid": 1, - "orientation": "orthogonal", - "renderorder": "right-down", - "tiledversion": "2021.03.23", - "tileheight": 32, - "tilesets": [], - "tilewidth": 32, - "type": "map", - "version": 1.5, - "width": 2 - }) + data: [0, 0, 0, 0], + height: 2, + id: 1, + name: "Tile Layer 2", + opacity: 1, + type: "tilelayer", + visible: true, + width: 2, + x: 0, + y: 0, + }, + ], + nextlayerid: 2, + nextobjectid: 1, + orientation: "orthogonal", + renderorder: "right-down", + tiledversion: "2021.03.23", + tileheight: 32, + tilesets: [], + tilewidth: 32, + type: "map", + version: 1.5, + width: 2, + }); const layers = []; for (const layer of flatLayers) { layers.push(layer.name); } - expect(layers).toEqual(['Tile Layer 1', 'Tile Layer 2']); + expect(layers).toEqual(["Tile Layer 1", "Tile Layer 2"]); }); it("should iterate maps with recursive groups", () => { - let flatLayers:ITiledMapLayer[] = []; + let flatLayers: ITiledMapLayer[] = []; flatLayers = flattenGroupLayersMap({ - "compressionlevel": -1, - "height": 2, - "infinite": false, - "layers": [ + compressionlevel: -1, + height: 2, + infinite: false, + layers: [ { - "id": 6, - "layers": [ + id: 6, + layers: [ { - "id": 5, - "layers": [ + id: 5, + layers: [ { - "data": [0, 0, 0, 0], - "height": 2, - "id": 10, - "name": "Tile3", - "opacity": 1, - "type": "tilelayer", - "visible": true, - "width": 2, - "x": 0, - "y": 0 + data: [0, 0, 0, 0], + height: 2, + id: 10, + name: "Tile3", + opacity: 1, + type: "tilelayer", + visible: true, + width: 2, + x: 0, + y: 0, }, { - "data": [0, 0, 0, 0], - "height": 2, - "id": 9, - "name": "Tile2", - "opacity": 1, - "type": "tilelayer", - "visible": true, - "width": 2, - "x": 0, - "y": 0 - }], - "name": "Group 3", - "opacity": 1, - "type": "group", - "visible": true, - "x": 0, - "y": 0 + data: [0, 0, 0, 0], + height: 2, + id: 9, + name: "Tile2", + opacity: 1, + type: "tilelayer", + visible: true, + width: 2, + x: 0, + y: 0, + }, + ], + name: "Group 3", + opacity: 1, + type: "group", + visible: true, + x: 0, + y: 0, }, { - "id": 7, - "layers": [ + id: 7, + layers: [ { - "data": [0, 0, 0, 0], - "height": 2, - "id": 8, - "name": "Tile1", - "opacity": 1, - "type": "tilelayer", - "visible": true, - "width": 2, - "x": 0, - "y": 0 - }], - "name": "Group 2", - "opacity": 1, - "type": "group", - "visible": true, - "x": 0, - "y": 0 - }], - "name": "Group 1", - "opacity": 1, - "type": "group", - "visible": true, - "x": 0, - "y": 0 - }], - "nextlayerid": 11, - "nextobjectid": 1, - "orientation": "orthogonal", - "renderorder": "right-down", - "tiledversion": "2021.03.23", - "tileheight": 32, - "tilesets": [], - "tilewidth": 32, - "type": "map", - "version": 1.5, - "width": 2 - }) + data: [0, 0, 0, 0], + height: 2, + id: 8, + name: "Tile1", + opacity: 1, + type: "tilelayer", + visible: true, + width: 2, + x: 0, + y: 0, + }, + ], + name: "Group 2", + opacity: 1, + type: "group", + visible: true, + x: 0, + y: 0, + }, + ], + name: "Group 1", + opacity: 1, + type: "group", + visible: true, + x: 0, + y: 0, + }, + ], + nextlayerid: 11, + nextobjectid: 1, + orientation: "orthogonal", + renderorder: "right-down", + tiledversion: "2021.03.23", + tileheight: 32, + tilesets: [], + tilewidth: 32, + type: "map", + version: 1.5, + width: 2, + }); const layers = []; for (const layer of flatLayers) { layers.push(layer.name); } - expect(layers).toEqual(['Group 1/Group 3/Tile3', 'Group 1/Group 3/Tile2', 'Group 1/Group 2/Tile1']); + expect(layers).toEqual(["Group 1/Group 3/Tile3", "Group 1/Group 3/Tile2", "Group 1/Group 2/Tile1"]); }); }); diff --git a/maps/tests/Metadata/setTiles.json b/maps/tests/Metadata/setTiles.json index 5b281a15..7eb9791a 100644 --- a/maps/tests/Metadata/setTiles.json +++ b/maps/tests/Metadata/setTiles.json @@ -286,11 +286,6 @@ "name":"jitsiTrigger", "type":"string", "value":"onaction" - }, - { - "name":"jitsiUrl", - "type":"string", - "value":"meet.jit.si" }] }, { diff --git a/maps/tests/function_tiles.json b/maps/tests/function_tiles.json new file mode 100644 index 00000000..9bc374eb --- /dev/null +++ b/maps/tests/function_tiles.json @@ -0,0 +1,33 @@ +{ "columns":2, + "image":"function_tiles.png", + "imageheight":64, + "imagewidth":64, + "margin":0, + "name":"function_tiles", + "spacing":0, + "tilecount":4, + "tiledversion":"1.6.0", + "tileheight":32, + "tiles":[ + { + "id":0, + "properties":[ + { + "name":"start", + "type":"string", + "value":"S1" + }] + }, + { + "id":1, + "properties":[ + { + "name":"start", + "type":"string", + "value":"S2" + }] + }], + "tilewidth":32, + "type":"tileset", + "version":"1.6" +} \ No newline at end of file diff --git a/maps/tests/function_tiles.png b/maps/tests/function_tiles.png new file mode 100644 index 00000000..147eb619 Binary files /dev/null and b/maps/tests/function_tiles.png differ diff --git a/maps/tests/index.html b/maps/tests/index.html index 1a4e3590..38ee51ef 100644 --- a/maps/tests/index.html +++ b/maps/tests/index.html @@ -170,6 +170,22 @@ Test animated tiles + + + Success Failure Pending + + + Test start tile (S1) + + + + + Success Failure Pending + + + Test start tile (S2) + + Success Failure Pending diff --git a/maps/tests/start-tile.json b/maps/tests/start-tile.json new file mode 100644 index 00000000..4a65429a --- /dev/null +++ b/maps/tests/start-tile.json @@ -0,0 +1,101 @@ +{ "compressionlevel":-1, + "height":5, + "infinite":false, + "layers":[ + { + "data":[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4], + "height":5, + "id":4, + "name":"background", + "opacity":1, + "type":"tilelayer", + "visible":true, + "width":5, + "x":0, + "y":0 + }, + { + "data":[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 3, 0, 0, 2, 3, 3, 0, 2, 2], + "height":5, + "id":1, + "name":"start", + "opacity":1, + "type":"tilelayer", + "visible":true, + "width":5, + "x":0, + "y":0 + }, + { + "draworder":"topdown", + "id":3, + "name":"floorLayer", + "objects":[ + { + "height":66.6666666666667, + "id":1, + "name":"", + "rotation":0, + "text": + { + "fontfamily":"Sans Serif", + "pixelsize":11, + "text":"If URL contains hash #S1, player starts on S1.\nIf URL contains hash #S2, player starts on S2.", + "wrap":true + }, + "type":"", + "visible":true, + "width":155.104166666667, + "x":3.28125, + "y":2.5 + }], + "opacity":1, + "type":"objectgroup", + "visible":true, + "x":0, + "y":0 + }], + "nextlayerid":5, + "nextobjectid":2, + "orientation":"orthogonal", + "renderorder":"right-down", + "tiledversion":"2021.03.23", + "tileheight":32, + "tilesets":[ + { + "columns":2, + "firstgid":1, + "image":"function_tiles.png", + "imageheight":64, + "imagewidth":64, + "margin":0, + "name":"function_tiles", + "spacing":0, + "tilecount":4, + "tileheight":32, + "tiles":[ + { + "id":0, + "properties":[ + { + "name":"start", + "type":"string", + "value":"S1" + }] + }, + { + "id":1, + "properties":[ + { + "name":"start", + "type":"string", + "value":"S2" + }] + }], + "tilewidth":32 + }], + "tilewidth":32, + "type":"map", + "version":1.5, + "width":5 +} \ No newline at end of file