From 2852f204f506caa38b438caeac7c92338c584840 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Mon, 12 Oct 2020 17:42:37 +0200 Subject: [PATCH] Improving error handling upon unknown URL --- front/src/Connexion/ConnectionManager.ts | 2 +- front/src/Phaser/Game/GameManager.ts | 17 ++++++++++---- .../src/Phaser/Reconnecting/FourOFourScene.ts | 23 +++++++++++++++---- front/src/Url/UrlManager.ts | 2 +- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/front/src/Connexion/ConnectionManager.ts b/front/src/Connexion/ConnectionManager.ts index 0fea50b5..615f75b9 100644 --- a/front/src/Connexion/ConnectionManager.ts +++ b/front/src/Connexion/ConnectionManager.ts @@ -53,7 +53,7 @@ class ConnectionManager { return Promise.reject('Could not find a user in localstorage'); } } - return Promise.reject('ConnexionManager initialization failed'); + return Promise.reject('ConnexionManager initialization failed: invalid URL'); } public initBenchmark(): void { diff --git a/front/src/Phaser/Game/GameManager.ts b/front/src/Phaser/Game/GameManager.ts index 10277e20..bed098ae 100644 --- a/front/src/Phaser/Game/GameManager.ts +++ b/front/src/Phaser/Game/GameManager.ts @@ -1,6 +1,7 @@ import {GameScene} from "./GameScene"; import {connectionManager} from "../../Connexion/ConnectionManager"; import {Room} from "../../Connexion/Room"; +import {FourOFourSceneName} from "../Reconnecting/FourOFourScene"; export interface HasMovedEvent { direction: string; @@ -14,10 +15,16 @@ export class GameManager { private characterLayers!: string[]; private startRoom!:Room; private sceneManager!: Phaser.Scenes.SceneManager; - + public async init(sceneManager: Phaser.Scenes.SceneManager) { this.sceneManager = sceneManager; - this.startRoom = await connectionManager.initGameConnexion(); + try { + this.startRoom = await connectionManager.initGameConnexion(); + } catch (e) { + this.sceneManager.start(FourOFourSceneName, { + url: window.location.pathname.toString() + }); + } this.loadMap(this.startRoom.url, this.startRoom.ID); } @@ -40,8 +47,8 @@ export class GameManager { getCharacterSelected(): string[] { return this.characterLayers; } - - + + public loadMap(mapUrl: string, roomID: string): void { console.log('Loading map '+roomID+' at url '+mapUrl); const gameIndex = this.sceneManager.getIndex(roomID); @@ -57,7 +64,7 @@ export class GameManager { const endPos = mapUrlStart.indexOf(".json"); return mapUrlStart.substring(startPos, endPos); } - + public async goToStartingMap() { this.sceneManager.start(this.startRoom.ID, {startLayerName: 'global'}); } diff --git a/front/src/Phaser/Reconnecting/FourOFourScene.ts b/front/src/Phaser/Reconnecting/FourOFourScene.ts index 0c91a5bc..3e84b7e9 100644 --- a/front/src/Phaser/Reconnecting/FourOFourScene.ts +++ b/front/src/Phaser/Reconnecting/FourOFourScene.ts @@ -15,7 +15,8 @@ export class FourOFourScene extends Phaser.Scene { private fileNameField!: Text; private logo!: Image; private cat!: Sprite; - private file!: string; + private file: string|undefined; + private url: string|undefined; constructor() { super({ @@ -23,8 +24,9 @@ export class FourOFourScene extends Phaser.Scene { }); } - init({ file }: { file: string }) { + init({ file, url }: { file?: string, url?: string }) { this.file = file; + this.url = url; } preload() { @@ -45,11 +47,22 @@ export class FourOFourScene extends Phaser.Scene { this.mapNotFoundField = new TextField(this, this.game.renderer.width / 2, this.game.renderer.height / 2, "404 - File not found"); this.mapNotFoundField.setOrigin(0.5, 0.5).setCenterAlign(); - this.couldNotFindField = new TextField(this, this.game.renderer.width / 2, this.game.renderer.height / 2 + 24, "Could not load file"); + let text: string = ''; + if (this.file !== undefined) { + text = "Could not load map" + } + if (this.url !== undefined) { + text = "Invalid URL" + } + + this.couldNotFindField = new TextField(this, this.game.renderer.width / 2, this.game.renderer.height / 2 + 24, text); this.couldNotFindField.setOrigin(0.5, 0.5).setCenterAlign(); - this.fileNameField = this.add.text(this.game.renderer.width / 2, this.game.renderer.height / 2 + 38, this.file, { fontFamily: 'Georgia, "Goudy Bookletter 1911", Times, serif', fontSize: '10px' }); - this.fileNameField.setOrigin(0.5, 0.5); + const url = this.file ? this.file : this.url; + if (url !== undefined) { + this.fileNameField = this.add.text(this.game.renderer.width / 2, this.game.renderer.height / 2 + 38, url, { fontFamily: 'Georgia, "Goudy Bookletter 1911", Times, serif', fontSize: '10px' }); + this.fileNameField.setOrigin(0.5, 0.5); + } this.cat = this.physics.add.sprite(this.game.renderer.width / 2, this.game.renderer.height / 2 - 32, 'cat', 6); this.cat.flipY=true; diff --git a/front/src/Url/UrlManager.ts b/front/src/Url/UrlManager.ts index 39f5667d..876e258e 100644 --- a/front/src/Url/UrlManager.ts +++ b/front/src/Url/UrlManager.ts @@ -16,7 +16,7 @@ class UrlManager { return GameConnexionTypes.anonymous; } else if (url.indexOf('@/') > -1) { return GameConnexionTypes.organization; - } else if(url.indexOf('register/')) { + } else if(url.indexOf('register/') > -1) { return GameConnexionTypes.register } else { return GameConnexionTypes.unknown