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

@ -0,0 +1,11 @@
import * as tg from "generic-type-guard";
export const isGameStateEvent =
new tg.IsInterface().withProperties({
roomId: tg.isString,
data:tg.isObject
}).get();
/**
* A message sent from the game to the iFrame when a user enters or leaves a zone marked with the "zone" property.
*/
export type GameStateEvent = tg.GuardedType<typeof isGameStateEvent>;

View file

@ -12,6 +12,8 @@ import {ClosePopupEvent, isClosePopupEvent} from "./Events/ClosePopupEvent";
import {scriptUtils} from "./ScriptUtils";
import {GoToPageEvent, isGoToPageEvent} from "./Events/GoToPageEvent";
import {isOpenCoWebsite, OpenCoWebSiteEvent} from "./Events/OpenCoWebSiteEvent";
import { GameStateEvent } from './Events/ApiGameStateEvent';
import { deepFreezeClone as deepFreezeClone } from '../utility';
/**
@ -52,6 +54,10 @@ class IframeListener {
private readonly _removeBubbleStream: Subject<void> = new Subject();
public readonly removeBubbleStream = this._removeBubbleStream.asObservable();
private readonly _gameStateStream: Subject<void> = new Subject();
public readonly gameStateStream = this._gameStateStream.asObservable();
private readonly iframes = new Set<HTMLIFrameElement>();
private readonly scripts = new Map<string, HTMLIFrameElement>();
@ -103,6 +109,8 @@ class IframeListener {
}
else if (payload.type === 'removeBubble'){
this._removeBubbleStream.next();
}else if(payload.type=="getState"){
this._gameStateStream.next();
}
}
@ -111,6 +119,14 @@ class IframeListener {
}
sendFrozenGameStateEvent(gameStateEvent: GameStateEvent) {
this.postMessage({
'type': 'gameState',
'data': deepFreezeClone(gameStateEvent)
});
}
/**
* Allows the passed iFrame to send/receive messages via the API.
*/