correction from code review
This commit is contained in:
parent
d4bc999c54
commit
a5cb93541a
@ -1,26 +1,13 @@
|
||||
import * as tg from "generic-type-guard";
|
||||
|
||||
/*export const isPositionState = new tg.IsInterface().withProperties({
|
||||
x: tg.isNumber,
|
||||
y: tg.isNumber
|
||||
}).get()
|
||||
export const isPlayerState = new tg.IsInterface()
|
||||
.withStringIndexSignature(
|
||||
new tg.IsInterface().withProperties({
|
||||
position: isPositionState,
|
||||
pusherId: tg.isUnion(tg.isNumber, tg.isUndefined)
|
||||
}).get()
|
||||
).get()
|
||||
|
||||
export type PlayerStateObject = tg.GuardedType<typeof isPlayerState>;*/
|
||||
|
||||
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)
|
||||
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 got by the script
|
||||
|
@ -4,7 +4,7 @@ import * as tg from "generic-type-guard";
|
||||
|
||||
export const isHasPlayerMovedEvent =
|
||||
new tg.IsInterface().withProperties({
|
||||
direction: tg.isString,
|
||||
direction: tg.isElementOf('right', 'left', 'up', 'down'),
|
||||
moving: tg.isBoolean,
|
||||
x: tg.isNumber,
|
||||
y: tg.isNumber
|
||||
|
@ -15,8 +15,6 @@ import type { UserInputChatEvent } from './UserInputChatEvent';
|
||||
import type { DataLayerEvent } from "./DataLayerEvent";
|
||||
import type { LayerEvent } from './LayerEvent';
|
||||
import type { SetPropertyEvent } from "./setPropertyEvent";
|
||||
import type { TagEvent } from "./TagEvent";
|
||||
import type { TilesetEvent } from "./TilesetEvent";
|
||||
|
||||
export interface TypedMessageEvent<T> extends MessageEvent {
|
||||
data: T
|
||||
@ -24,7 +22,6 @@ export interface TypedMessageEvent<T> extends MessageEvent {
|
||||
|
||||
export type IframeEventMap = {
|
||||
getState: GameStateEvent,
|
||||
// updateTile: UpdateTileEvent
|
||||
registerMenuCommand: MenuItemRegisterEvent
|
||||
chat: ChatEvent,
|
||||
openPopup: OpenPopupEvent
|
||||
@ -42,7 +39,6 @@ export type IframeEventMap = {
|
||||
hideLayer: LayerEvent
|
||||
setProperty: SetPropertyEvent
|
||||
getDataLayer: undefined
|
||||
getTag: undefined
|
||||
//tilsetEvent: TilesetEvent
|
||||
}
|
||||
export interface IframeEvent<T extends keyof IframeEventMap> {
|
||||
@ -63,7 +59,6 @@ export interface IframeResponseEventMap {
|
||||
hasPlayerMoved: HasPlayerMovedEvent
|
||||
dataLayer: DataLayerEvent
|
||||
menuItemClicked: MenuItemClickedEvent
|
||||
tagList: TagEvent
|
||||
}
|
||||
export interface IframeResponseEvent<T extends keyof IframeResponseEventMap> {
|
||||
type: T;
|
||||
|
@ -1,10 +0,0 @@
|
||||
import * as tg from "generic-type-guard";
|
||||
|
||||
export const isTagEvent =
|
||||
new tg.IsInterface().withProperties({
|
||||
list: tg.isArray(tg.isString),
|
||||
}).get();
|
||||
/**
|
||||
* A message sent from the iFrame to the game to show/hide a layer.
|
||||
*/
|
||||
export type TagEvent = tg.GuardedType<typeof isTagEvent>;
|
@ -1,15 +0,0 @@
|
||||
import * as tg from "generic-type-guard";
|
||||
|
||||
export const isTilesetEvent =
|
||||
new tg.IsInterface().withProperties({
|
||||
name : tg.isString,
|
||||
imgUrl : tg.isString,
|
||||
tilewidth : tg.isNumber,
|
||||
tileheight : tg.isNumber,
|
||||
margin : tg.isNumber,
|
||||
spacing : tg.isNumber,
|
||||
}).get();
|
||||
/**
|
||||
* A message sent from the iFrame to the game to show/hide a layer.
|
||||
*/
|
||||
export type TilesetEvent = tg.GuardedType<typeof isTilesetEvent>;
|
@ -19,7 +19,6 @@ import { Math } from 'phaser';
|
||||
import type { DataLayerEvent } from "./Events/DataLayerEvent";
|
||||
import { isMenuItemRegisterEvent } from './Events/MenuItemRegisterEvent';
|
||||
import type { MenuItemClickedEvent } from './Events/MenuItemClickedEvent';
|
||||
import type { TagEvent } from "./Events/TagEvent";
|
||||
//import { isTilesetEvent, TilesetEvent } from "./Events/TilesetEvent";
|
||||
|
||||
|
||||
@ -80,9 +79,6 @@ class IframeListener {
|
||||
private readonly _registerMenuCommandStream: Subject<string> = new Subject();
|
||||
public readonly registerMenuCommandStream = this._registerMenuCommandStream.asObservable();
|
||||
|
||||
private readonly _tagListStream: Subject<void> = new Subject();
|
||||
public readonly tagListStream = this._tagListStream.asObservable();
|
||||
|
||||
/* private readonly _tilesetLoaderStream: Subject<TilesetEvent> = new Subject();
|
||||
public readonly tilesetLoaderStream = this._tilesetLoaderStream.asObservable();*/
|
||||
|
||||
@ -154,9 +150,7 @@ class IframeListener {
|
||||
this._dataLayerChangeStream.next();
|
||||
} else if (payload.type == "registerMenuCommand" && isMenuItemRegisterEvent(payload.data)) {
|
||||
this._registerMenuCommandStream.next(payload.data.menutItem)
|
||||
} else if (payload.type == "getTag") {
|
||||
this._tagListStream.next();
|
||||
/* } else if (payload.type == "tilsetEvent" && isTilesetEvent(payload.data)) {
|
||||
/* } else if (payload.type == "tilsetEvent" && isTilesetEvent(payload.data)) {
|
||||
this._tilesetLoaderStream.next(payload.data);*/
|
||||
}
|
||||
}
|
||||
@ -164,13 +158,6 @@ class IframeListener {
|
||||
|
||||
}
|
||||
|
||||
sendUserTagList(tagList: TagEvent){
|
||||
this.postMessage({
|
||||
'type' : 'tagList',
|
||||
'data' : tagList
|
||||
})
|
||||
}
|
||||
|
||||
sendDataLayerEvent(dataLayerEvent: DataLayerEvent) {
|
||||
this.postMessage({
|
||||
'type' : 'dataLayer',
|
||||
|
@ -169,9 +169,9 @@ export class RoomConnection implements RoomConnection {
|
||||
} else if (message.hasWorldfullmessage()) {
|
||||
worldFullMessageStream.onMessage();
|
||||
this.closed = true;
|
||||
// // } else if (message.hasWorldconnexionmessage()) {
|
||||
// worldFullMessageStream.onMessage(message.getWorldconnexionmessage()?.getMessage());
|
||||
// this.closed = true;
|
||||
} else if (message.hasWorldconnexionmessage()) {
|
||||
worldFullMessageStream.onMessage(message.getWorldconnexionmessage()?.getMessage());
|
||||
this.closed = true;
|
||||
} else if (message.hasWebrtcsignaltoclientmessage()) {
|
||||
this.dispatch(EventMessage.WEBRTC_SIGNAL, message.getWebrtcsignaltoclientmessage());
|
||||
} else if (message.hasWebrtcscreensharingsignaltoclientmessage()) {
|
||||
@ -600,9 +600,6 @@ export class RoomConnection implements RoomConnection {
|
||||
}
|
||||
|
||||
public getAllTag() : string[] {
|
||||
this.tags.push('TEST');
|
||||
this.tags.push('TEST 2');
|
||||
this.tags.push('TEST 3');
|
||||
return this.tags;
|
||||
}
|
||||
}
|
||||
|
@ -864,15 +864,6 @@ ${escapedMessage}
|
||||
this.iframeSubscriptionList.push(iframeListener.enablePlayerControlStream.subscribe(() => {
|
||||
this.userInputManager.restoreControls();
|
||||
}));
|
||||
this.iframeSubscriptionList.push(iframeListener.gameStateStream.subscribe(() => {
|
||||
iframeListener.sendFrozenGameStateEvent({
|
||||
mapUrl: this.MapUrlFile,
|
||||
startLayerName: this.startLayerName,
|
||||
uuid: localUserStore.getLocalUser()?.uuid,
|
||||
nickname: localUserStore.getName(),
|
||||
roomId: this.RoomId,
|
||||
})
|
||||
}));
|
||||
|
||||
let scriptedBubbleSprite: Sprite;
|
||||
this.iframeSubscriptionList.push(iframeListener.displayBubbleStream.subscribe(() => {
|
||||
@ -886,12 +877,10 @@ ${escapedMessage}
|
||||
}));
|
||||
|
||||
this.iframeSubscriptionList.push(iframeListener.showLayerStream.subscribe((layerEvent)=>{
|
||||
console.log('show');
|
||||
this.setLayerVisibility(layerEvent.name, true);
|
||||
}));
|
||||
|
||||
this.iframeSubscriptionList.push(iframeListener.hideLayerStream.subscribe((layerEvent)=>{
|
||||
console.log('hide');
|
||||
this.setLayerVisibility(layerEvent.name, false);
|
||||
}));
|
||||
|
||||
@ -903,12 +892,16 @@ ${escapedMessage}
|
||||
iframeListener.sendDataLayerEvent({data: this.gameMap.getMap()});
|
||||
}))
|
||||
|
||||
this.iframeSubscriptionList.push(iframeListener.tagListStream.subscribe(()=> {
|
||||
if (this.connection === undefined) {
|
||||
return;
|
||||
}
|
||||
iframeListener.sendUserTagList({list: this.connection.getAllTag()});
|
||||
}))
|
||||
this.iframeSubscriptionList.push(iframeListener.gameStateStream.subscribe(() => {
|
||||
iframeListener.sendFrozenGameStateEvent({
|
||||
mapUrl: this.MapUrlFile,
|
||||
startLayerName: this.startLayerName,
|
||||
uuid: localUserStore.getLocalUser()?.uuid,
|
||||
nickname: localUserStore.getName(),
|
||||
roomId: this.RoomId,
|
||||
tags: this.connection ? this.connection.getAllTag() : []
|
||||
})
|
||||
}));
|
||||
|
||||
/* this.iframeSubscriptionList.push(iframeListener.tilesetLoaderStream.subscribe((tileset) => {
|
||||
//this.load.tilemapTiledJSON('logo', tileset.imgUrl);
|
||||
|
@ -17,8 +17,6 @@ import { DataLayerEvent, isDataLayerEvent } from "./Api/Events/DataLayerEvent";
|
||||
import type { ITiledMap } from "./Phaser/Map/ITiledMap";
|
||||
import type { MenuItemRegisterEvent } from "./Api/Events/MenuItemRegisterEvent";
|
||||
import { isMenuItemClickedEvent } from "./Api/Events/MenuItemClickedEvent";
|
||||
import {TagEvent, isTagEvent} from "./Api/Events/TagEvent";
|
||||
import type { TilesetEvent } from "./Api/Events/TilesetEvent";
|
||||
|
||||
interface WorkAdventureApi {
|
||||
sendChatMessage(message: string, author: string): void;
|
||||
@ -42,18 +40,26 @@ interface WorkAdventureApi {
|
||||
displayBubble(): void;
|
||||
removeBubble(): void;
|
||||
registerMenuCommand(commandDescriptor: string, callback: (commandDescriptor: string) => void): void
|
||||
getMapUrl(): Promise<string>;
|
||||
getUuid(): Promise<string | undefined>;
|
||||
getRoomId(): Promise<string>;
|
||||
getStartLayerName(): Promise<string | null>;
|
||||
getNickName(): Promise<string | null>;
|
||||
getTagUser(): Promise<string[]>;
|
||||
getMap(): Promise<ITiledMap>
|
||||
getCurrentUser(): Promise<User>
|
||||
getCurrentRoom(): Promise<Room>
|
||||
//loadTileset(name: string, imgUrl : string, tilewidth : number, tileheight : number, margin : number, spacing : number): void;
|
||||
|
||||
onPlayerMove(callback: (playerMovedEvent: HasPlayerMovedEvent) => void): void
|
||||
}
|
||||
|
||||
interface User {
|
||||
id: string | undefined
|
||||
nickName: string | null
|
||||
tags: string[]
|
||||
}
|
||||
|
||||
interface Room {
|
||||
id: string
|
||||
mapUrl: string
|
||||
map: ITiledMap
|
||||
startLayer: string | null
|
||||
}
|
||||
|
||||
declare global {
|
||||
// eslint-disable-next-line no-var
|
||||
var WA: WorkAdventureApi
|
||||
@ -101,12 +107,14 @@ class Popup {
|
||||
}, '*');
|
||||
}
|
||||
}
|
||||
function uuidv4() {
|
||||
|
||||
/*function uuidv4() {
|
||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
||||
const r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
|
||||
return v.toString(16);
|
||||
});
|
||||
}
|
||||
}*/
|
||||
|
||||
function getGameState(): Promise<GameStateEvent> {
|
||||
if (immutableData) {
|
||||
return Promise.resolve(immutableData);
|
||||
@ -131,34 +139,21 @@ function getDataLayer(): Promise<DataLayerEvent> {
|
||||
})
|
||||
}
|
||||
|
||||
function getTag(): Promise<TagEvent> {
|
||||
return new Promise<TagEvent>((resolver, thrower) => {
|
||||
tagResolver.push((resolver));
|
||||
postToParent({
|
||||
type: "getTag",
|
||||
data: undefined
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const gameStateResolver: Array<(event: GameStateEvent) => void> = []
|
||||
const dataLayerResolver: Array<(event: DataLayerEvent) => void> = []
|
||||
const tagResolver: Array<(event : TagEvent) => void> = []
|
||||
let immutableData: GameStateEvent;
|
||||
|
||||
const callbackPlayerMoved: { [type: string]: HasPlayerMovedEventCallback | ((arg?: HasPlayerMovedEvent | never) => void) } = {}
|
||||
|
||||
//const callbackPlayerMoved: { [type: string]: HasPlayerMovedEventCallback | ((arg?: HasPlayerMovedEvent | never) => void) } = {}
|
||||
const callbackPlayerMoved: Array<(event: HasPlayerMovedEvent) => void> = []
|
||||
|
||||
function postToParent(content: IframeEvent<keyof IframeEventMap>) {
|
||||
window.parent.postMessage(content, "*")
|
||||
}
|
||||
let playerUuid: string | undefined;
|
||||
|
||||
window.WA = {
|
||||
|
||||
onPlayerMove(callback: HasPlayerMovedEventCallback): void {
|
||||
playerUuid = uuidv4();
|
||||
callbackPlayerMoved[playerUuid] = callback;
|
||||
callbackPlayerMoved.push(callback);
|
||||
postToParent({
|
||||
type: "onPlayerMove",
|
||||
data: undefined
|
||||
@ -179,45 +174,17 @@ window.WA = {
|
||||
})
|
||||
},*/
|
||||
|
||||
getTagUser(): Promise<string[]> {
|
||||
return getTag().then((res) => {
|
||||
return res.list;
|
||||
getCurrentUser(): Promise<User> {
|
||||
return getGameState().then((gameState) => {
|
||||
return {id: gameState.uuid, nickName: gameState.nickname, tags: gameState.tags};
|
||||
})
|
||||
},
|
||||
|
||||
getMap(): Promise<ITiledMap> {
|
||||
return getDataLayer().then((res) => {
|
||||
return res.data as ITiledMap;
|
||||
})
|
||||
},
|
||||
|
||||
getNickName(): Promise<string | null> {
|
||||
return getGameState().then((res) => {
|
||||
return res.nickname;
|
||||
})
|
||||
},
|
||||
|
||||
getMapUrl(): Promise<string> {
|
||||
return getGameState().then((res) => {
|
||||
return res.mapUrl;
|
||||
})
|
||||
},
|
||||
|
||||
getUuid(): Promise<string | undefined> {
|
||||
return getGameState().then((res) => {
|
||||
return res.uuid;
|
||||
})
|
||||
},
|
||||
|
||||
getRoomId(): Promise<string> {
|
||||
return getGameState().then((res) => {
|
||||
return res.roomId;
|
||||
})
|
||||
},
|
||||
|
||||
getStartLayerName(): Promise<string | null> {
|
||||
return getGameState().then((res) => {
|
||||
return res.startLayerName;
|
||||
getCurrentRoom(): Promise<Room> {
|
||||
return getGameState().then((gameState) => {
|
||||
return getDataLayer().then((mapJson) => {
|
||||
return {id: gameState.roomId, map: mapJson.data as ITiledMap, mapUrl: gameState.mapUrl, startLayer: gameState.startLayerName};
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
@ -411,22 +378,18 @@ window.addEventListener('message', message => {
|
||||
resolver(payloadData);
|
||||
})
|
||||
immutableData = payloadData;
|
||||
} else if (payload.type == "hasPlayerMoved" && isHasPlayerMovedEvent(payloadData) && playerUuid) {
|
||||
callbackPlayerMoved[playerUuid](payloadData)
|
||||
} else if (payload.type == "hasPlayerMoved" && isHasPlayerMovedEvent(payloadData)) {
|
||||
callbackPlayerMoved.forEach(callback => {
|
||||
callback(payloadData);
|
||||
})
|
||||
} else if (payload.type == "dataLayer" && isDataLayerEvent(payloadData)) {
|
||||
dataLayerResolver.forEach(resolver => {
|
||||
resolver(payloadData);
|
||||
})
|
||||
} else if (payload.type == "menuItemClicked" && isMenuItemClickedEvent(payload.data)) {
|
||||
const callback = menuCallbacks.get(payload.data.menuItem);
|
||||
} else if (payload.type == "menuItemClicked" && isMenuItemClickedEvent(payloadData)) {
|
||||
const callback = menuCallbacks.get(payloadData.menuItem);
|
||||
if (callback) {
|
||||
callback(payload.data.menuItem)
|
||||
}
|
||||
} else {
|
||||
if (payload.type == "tagList" && isTagEvent(payloadData)) {
|
||||
tagResolver.forEach(resolver => {
|
||||
resolver(payloadData);
|
||||
})
|
||||
callback(payloadData.menuItem)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user