game state can be read out by the client APIs

# Conflicts:
#	front/src/Api/IframeListener.ts
#	front/src/Phaser/Game/GameScene.ts
#	front/src/iframe_api.ts
This commit is contained in:
jonny 2021-04-21 15:51:01 +02:00
parent 5dc2f0ac47
commit 3836d5037c
5 changed files with 74 additions and 0 deletions

View file

@ -9,6 +9,7 @@ import {ClosePopupEvent} from "./Api/Events/ClosePopupEvent";
import {OpenTabEvent} from "./Api/Events/OpenTabEvent";
import {GoToPageEvent} from "./Api/Events/GoToPageEvent";
import {OpenCoWebSiteEvent} from "./Api/Events/OpenCoWebSiteEvent";
import { GameStateEvent, isGameStateEvent } from './Api/Events/ApiGameStateEvent';
interface WorkAdventureApi {
sendChatMessage(message: string, author: string): void;
@ -24,6 +25,7 @@ interface WorkAdventureApi {
restorePlayerControl() : void;
displayBubble() : void;
removeBubble() : void;
getGameState():Promise<unknown>
}
declare global {
@ -74,7 +76,23 @@ class Popup {
}
}
const stateResolvers:Array<(event:GameStateEvent)=>void> =[]
window.WA = {
getGameState(){
return new Promise<GameStateEvent>((resolver,thrower)=>{
stateResolvers.push(resolver);
window.parent.postMessage({
type:"getState"
},"*")
})
},
/**
* Send a message in the chat.
* Only the local user will receive this message.
@ -224,6 +242,10 @@ window.addEventListener('message', message => {
if (callback) {
callback(popup);
}
}else if(payload.type=="gameState" && isGameStateEvent(payloadData)){
stateResolvers.forEach(resolver=>{
resolver(payloadData);
})
}
}