simplified mapUrl parsing

This commit is contained in:
arp 2020-10-08 18:51:24 +02:00
parent f542b117a8
commit 4af46b1b3f
6 changed files with 93 additions and 73 deletions

View File

@ -36,6 +36,7 @@ export class AuthenticateController extends BaseController {
//todo: what to do if the organizationMemberToken is already used? //todo: what to do if the organizationMemberToken is already used?
const organizationMemberToken:string|null = param.organizationMemberToken; const organizationMemberToken:string|null = param.organizationMemberToken;
const mapSlug:string|null = param.mapSlug;
try { try {
let userUuid; let userUuid;
@ -48,10 +49,14 @@ export class AuthenticateController extends BaseController {
userUuid = data.userUuid; userUuid = data.userUuid;
mapUrlStart = data.mapUrlStart; mapUrlStart = data.mapUrlStart;
newUrl = this.getNewUrlOnAdminAuth(data) newUrl = this.getNewUrlOnAdminAuth(data)
} else if (mapSlug !== null) {
userUuid = uuid();
mapUrlStart = mapSlug;
newUrl = null;
} else { } else {
userUuid = uuid(); userUuid = uuid();
mapUrlStart = host.replace('api.', 'maps.') + URL_ROOM_STARTED; mapUrlStart = host.replace('api.', 'maps.') + URL_ROOM_STARTED;
newUrl = null; newUrl = '_/global/'+mapUrlStart;
} }
const authToken = Jwt.sign({userUuid: userUuid}, SECRET_KEY, {expiresIn: '24h'}); const authToken = Jwt.sign({userUuid: userUuid}, SECRET_KEY, {expiresIn: '24h'});

View File

@ -17,10 +17,20 @@ class ConnectionManager {
private authToken:string|null = null; private authToken:string|null = null;
private userUuid: string|null = null; private userUuid: string|null = null;
//todo: get map infos from url in anonym case
public async init(): Promise<void> { public async init(): Promise<void> {
let organizationMemberToken = null;
let teamSlug = null;
let mapSlug = null;
const match = /\/register\/(.+)/.exec(window.location.toString()); const match = /\/register\/(.+)/.exec(window.location.toString());
const organizationMemberToken = match ? match[1] : null; if (match) {
this.initPromise = Axios.post(`${API_URL}/login`, {organizationMemberToken}).then(res => res.data); organizationMemberToken = match[1];
} else {
const match = /\/_\/(.+)\/(.+)/.exec(window.location.toString());
teamSlug = match ? match[1] : null;
mapSlug = match ? match[2] : null;
}
this.initPromise = Axios.post(`${API_URL}/login`, {organizationMemberToken, teamSlug, mapSlug}).then(res => res.data);
const data = await this.initPromise const data = await this.initPromise
this.authToken = data.authToken; this.authToken = data.authToken;
this.userUuid = data.userUuid; this.userUuid = data.userUuid;

View File

@ -0,0 +1,6 @@
export class GameSceneDescriptor {
constructor(MapKey : string, MapUrlFile: string, instance: string, key: string) {
this.roomId = '';//
}
}

View File

@ -1,4 +1,4 @@
import {GameScene} from "./GameScene"; import {GameScene, GameSceneInitInterface} from "./GameScene";
import { import {
StartMapInterface StartMapInterface
} from "../../Connexion/ConnexionModels"; } from "../../Connexion/ConnexionModels";
@ -13,6 +13,11 @@ export interface HasMovedEvent {
y: number; y: number;
} }
export interface loadMapResponseInterface {
key: string,
startLayerName: string;
}
export class GameManager { export class GameManager {
private playerName!: string; private playerName!: string;
private characterLayers!: string[]; private characterLayers!: string[];
@ -29,15 +34,6 @@ export class GameManager {
this.characterLayers = layers; this.characterLayers = layers;
} }
loadStartMap() : Promise<StartMapInterface> {
return connectionManager.getMapUrlStart().then(mapUrlStart => {
return {
mapUrlStart: mapUrlStart,
startInstance: "global", //todo: is this property still usefull?
}
});
}
getPlayerName(): string { getPlayerName(): string {
return this.playerName; return this.playerName;
} }
@ -46,8 +42,47 @@ export class GameManager {
return this.characterLayers; return this.characterLayers;
} }
loadMap(mapUrl: string, scene: Phaser.Scenes.ScenePlugin, instance: string): string { /**
const sceneKey = GameScene.getMapKeyByUrl(mapUrl); * Returns the map URL and the instance from the current URL
*/
private findMapUrl(): [string, string]|null {
const path = window.location.pathname;
if (!path.startsWith('/_/')) {
return null;
}
const instanceAndMap = path.substr(3);
const firstSlash = instanceAndMap.indexOf('/');
if (firstSlash === -1) {
return null;
}
const instance = instanceAndMap.substr(0, firstSlash);
return [window.location.protocol+'//'+instanceAndMap.substr(firstSlash+1), instance];
}
public loadStartingMap(scene: Phaser.Scenes.ScenePlugin): Promise<loadMapResponseInterface> {
// Do we have a start URL in the address bar? If so, let's redirect to this address
const instanceAndMapUrl = this.findMapUrl();
if (instanceAndMapUrl !== null) {
const [mapUrl, instance] = instanceAndMapUrl;
const key = gameManager.loadMap(mapUrl, scene, instance);
const startLayerName = window.location.hash ? window.location.hash.substr(1) : '';
return Promise.resolve({key, startLayerName});
} else {
// If we do not have a map address in the URL, let's ask the server for a start map.
return connectionManager.getMapUrlStart().then((mapUrlStart: string) => {
const key = gameManager.loadMap(window.location.protocol + "//" + mapUrlStart, scene, 'global');
return {key, startLayerName: ''}
}).catch((err) => {
console.error(err);
throw err;
});
}
}
public loadMap(mapUrl: string, scene: Phaser.Scenes.ScenePlugin, instance: string): string {
const sceneKey = this.getMapKeyByUrl(mapUrl);
const gameIndex = scene.getIndex(sceneKey); const gameIndex = scene.getIndex(sceneKey);
if(gameIndex === -1){ if(gameIndex === -1){
@ -56,6 +91,13 @@ export class GameManager {
} }
return sceneKey; return sceneKey;
} }
public getMapKeyByUrl(mapUrlStart: string) : string {
// FIXME: the key should be computed from the full URL of the map.
const startPos = mapUrlStart.indexOf('://')+3;
const endPos = mapUrlStart.indexOf(".json");
return mapUrlStart.substring(startPos, endPos);
}
} }
export const gameManager = new GameManager(); export const gameManager = new GameManager();

View File

@ -138,17 +138,17 @@ export class GameScene extends Phaser.Scene implements CenterListener {
private outlinedItem: ActionableItem|null = null; private outlinedItem: ActionableItem|null = null;
private userInputManager!: UserInputManager; private userInputManager!: UserInputManager;
static createFromUrl(mapUrlFile: string, instance: string, key: string|null = null): GameScene { static createFromUrl(mapUrlFile: string, instance: string, gameSceneKey: string|null = null): GameScene {
const mapKey = GameScene.getMapKeyByUrl(mapUrlFile); const mapKey = gameManager.getMapKeyByUrl(mapUrlFile);
if (key === null) { if (gameSceneKey === null) {
key = mapKey; gameSceneKey = mapKey;
} }
return new GameScene(mapKey, mapUrlFile, instance, key); return new GameScene(mapKey, mapUrlFile, instance, gameSceneKey);
} }
constructor(MapKey : string, MapUrlFile: string, instance: string, key: string) { constructor(MapKey : string, MapUrlFile: string, instance: string, gameSceneKey: string) {
super({ super({
key: key key: gameSceneKey
}); });
this.GameManager = gameManager; this.GameManager = gameManager;
@ -588,9 +588,9 @@ export class GameScene extends Phaser.Scene implements CenterListener {
this.simplePeer.closeAllConnections(); this.simplePeer.closeAllConnections();
this.simplePeer.unregister(); this.simplePeer.unregister();
const key = 'somekey'+Math.round(Math.random()*10000); const gameSceneKey = 'somekey'+Math.round(Math.random()*10000);
const game : Phaser.Scene = GameScene.createFromUrl(this.MapUrlFile, this.instance, key); const game : Phaser.Scene = GameScene.createFromUrl(this.MapUrlFile, this.instance, gameSceneKey);
this.scene.add(key, game, true, this.scene.add(gameSceneKey, game, true,
{ {
initPosition: { initPosition: {
x: this.CurrentPlayer.x, x: this.CurrentPlayer.x,
@ -1136,12 +1136,7 @@ export class GameScene extends Phaser.Scene implements CenterListener {
this.groups.delete(groupId); this.groups.delete(groupId);
} }
public static getMapKeyByUrl(mapUrlStart: string) : string {
// FIXME: the key should be computed from the full URL of the map.
const startPos = mapUrlStart.indexOf('://')+3;
const endPos = mapUrlStart.indexOf(".json");
return mapUrlStart.substring(startPos, endPos);
}
/** /**
* Sends to the server an event emitted by one of the ActionableItems. * Sends to the server an event emitted by one of the ActionableItems.

View File

@ -94,7 +94,7 @@ export class EnableCameraScene extends Phaser.Scene {
this.add.existing(this.logo); this.add.existing(this.logo);
this.input.keyboard.on('keyup-ENTER', () => { this.input.keyboard.on('keyup-ENTER', () => {
return this.login(); this.login();
}); });
this.getElementByIdOrFail<HTMLDivElement>('webRtcSetup').classList.add('active'); this.getElementByIdOrFail<HTMLDivElement>('webRtcSetup').classList.add('active');
@ -258,7 +258,7 @@ export class EnableCameraScene extends Phaser.Scene {
this.soundMeterSprite.setVolume(this.soundMeter.getVolume()); this.soundMeterSprite.setVolume(this.soundMeter.getVolume());
} }
private async login(): Promise<StartMapInterface> { private async login(): Promise<void> {
this.getElementByIdOrFail<HTMLDivElement>('webRtcSetup').style.display = 'none'; this.getElementByIdOrFail<HTMLDivElement>('webRtcSetup').style.display = 'none';
this.soundMeter.stop(); this.soundMeter.stop();
window.removeEventListener('resize', this.repositionCallback); window.removeEventListener('resize', this.repositionCallback);
@ -266,46 +266,8 @@ export class EnableCameraScene extends Phaser.Scene {
mediaManager.stopCamera(); mediaManager.stopCamera();
mediaManager.stopMicrophone(); mediaManager.stopMicrophone();
// Do we have a start URL in the address bar? If so, let's redirect to this address let {key, startLayerName} = await gameManager.loadStartingMap(this.scene);
const instanceAndMapUrl = this.findMapUrl(); this.scene.start(key, {startLayerName});
if (instanceAndMapUrl !== null) {
const [mapUrl, instance] = instanceAndMapUrl;
const key = gameManager.loadMap(mapUrl, this.scene, instance);
this.scene.start(key, {
startLayerName: window.location.hash ? window.location.hash.substr(1) : undefined
} as GameSceneInitInterface);
return {
mapUrlStart: mapUrl,
startInstance: instance
};
} else {
// If we do not have a map address in the URL, let's ask the server for a start map.
return gameManager.loadStartMap().then((startMap: StartMapInterface) => {
const key = gameManager.loadMap(window.location.protocol + "//" + startMap.mapUrlStart, this.scene, startMap.startInstance);
this.scene.start(key);
return startMap;
}).catch((err) => {
console.error(err);
throw err;
});
}
}
/**
* Returns the map URL and the instance from the current URL
*/
private findMapUrl(): [string, string]|null {
const path = window.location.pathname;
if (!path.startsWith('/_/')) {
return null;
}
const instanceAndMap = path.substr(3);
const firstSlash = instanceAndMap.indexOf('/');
if (firstSlash === -1) {
return null;
}
const instance = instanceAndMap.substr(0, firstSlash);
return [window.location.protocol+'//'+instanceAndMap.substr(firstSlash+1), instance];
} }
private async getDevices() { private async getDevices() {