cowebsite

navigation
sound
player
extraction
This commit is contained in:
jonny 2021-05-28 01:14:10 +02:00
parent ea04dd5303
commit 540e5783b6
8 changed files with 1297 additions and 113 deletions

View file

@ -1,13 +1,5 @@
import { Subject } from "rxjs";
import type { GoToPageEvent } from "./Api/Events/GoToPageEvent";
import { IframeResponseEventMap, isIframeResponseEventWrapper } from "./Api/Events/IframeEvent";
import type { LoadSoundEvent } from "./Api/Events/LoadSoundEvent";
import type { OpenCoWebSiteEvent } from "./Api/Events/OpenCoWebSiteEvent";
import type { OpenTabEvent } from "./Api/Events/OpenTabEvent";
import type { PlaySoundEvent } from "./Api/Events/PlaySoundEvent";
import type { StopSoundEvent } from "./Api/Events/StopSoundEvent";
import { isUserInputChatEvent, UserInputChatEvent } from "./Api/Events/UserInputChatEvent";
import SoundConfig = Phaser.Types.Sound.SoundConfig;
export const registeredCallbacks: { [K in keyof IframeResponseEventMap]?: {
typeChecker: Function
callback: Function
@ -16,9 +8,13 @@ export const registeredCallbacks: { [K in keyof IframeResponseEventMap]?: {
const importType = Promise.all([
import("./Api/iframe/popup"),
import("./Api/iframe/chatmessage"),
import("./Api/iframe/zone-events")
import("./Api/iframe/Sound"),
import("./Api/iframe/zone-events"),
import("./Api/iframe/Navigation"),
import("./Api/iframe/CoWebsite")
])
type PromiseReturnType<P> = P extends Promise<infer T> ? T : P
type WorkadventureCommandClasses = PromiseReturnType<typeof importType>[number]["default"];
@ -50,20 +46,12 @@ type WorkAdventureApiFiles = {
} & SubObjectTypes
export interface WorkAdventureApi extends WorkAdventureApiFiles {
openTab(url: string): void;
goToPage(url: string): void;
openCoWebSite(url: string): void;
closeCoWebSite(): void;
disablePlayerControls(): void;
restorePlayerControls(): void;
displayBubble(): void;
removeBubble(): void;
loadSound(url: string): Sound;
}
declare global {
interface Window {
@ -72,55 +60,9 @@ declare global {
let WA: WorkAdventureApi
}
const userInputChatStream: Subject<UserInputChatEvent> = new Subject();
export class Sound {
constructor(private url: string) {
window.parent.postMessage({
"type": 'loadSound',
"data": {
url: this.url,
} as LoadSoundEvent
}, '*');
}
public play(config: SoundConfig) {
window.parent.postMessage({
"type": 'playSound',
"data": {
url: this.url,
config
} as PlaySoundEvent
}, '*');
return this.url;
}
public stop() {
window.parent.postMessage({
"type": 'stopSound',
"data": {
url: this.url,
} as StopSoundEvent
}, '*');
return this.url;
}
}
window.WA = {
disablePlayerControls(): void {
window.parent.postMessage({ 'type': 'disablePlayerControls' }, '*');
},
restorePlayerControls(): void {
window.parent.postMessage({ 'type': 'restorePlayerControls' }, '*');
},
displayBubble(): void {
window.parent.postMessage({ 'type': 'displayBubble' }, '*');
@ -130,42 +72,7 @@ window.WA = {
window.parent.postMessage({ 'type': 'removeBubble' }, '*');
},
openTab(url: string): void {
window.parent.postMessage({
"type": 'openTab',
"data": {
url
} as OpenTabEvent
}, '*');
},
loadSound(url: string): Sound {
return new Sound(url);
},
goToPage(url: string): void {
window.parent.postMessage({
"type": 'goToPage',
"data": {
url
} as GoToPageEvent
}, '*');
},
openCoWebSite(url: string): void {
window.parent.postMessage({
"type": 'openCoWebSite',
"data": {
url
} as OpenCoWebSiteEvent
}, '*');
},
closeCoWebSite(): void {
window.parent.postMessage({
"type": 'closeCoWebSite'
}, '*');
},
...({} as WorkAdventureApiFiles),
}
@ -173,18 +80,16 @@ window.addEventListener('message', message => {
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;
if (payload.type === 'userInputChat' && isUserInputChatEvent(payloadData)) {
userInputChatStream.next(payloadData);
if (registeredCallbacks[payload.type] && registeredCallbacks[payload.type]?.typeChecker(payloadData)) {
registeredCallbacks[payload.type]?.callback(payloadData)
return
}
}
// ...