Making alone mode more robust

This fixes a number of issues where the game was attempting to access a non existing connection (in alone mode)
This commit is contained in:
David Négrier 2021-04-23 13:44:04 +02:00
parent 1251cbdc76
commit 4f4f499d47
3 changed files with 24 additions and 23 deletions

View File

@ -135,7 +135,7 @@ export class GameScene extends ResizableScene implements CenterListener {
pendingEvents: Queue<InitUserPositionEventInterface|AddPlayerEventInterface|RemovePlayerEventInterface|UserMovedEventInterface|GroupCreatedUpdatedEventInterface|DeleteGroupEventInterface> = new Queue<InitUserPositionEventInterface|AddPlayerEventInterface|RemovePlayerEventInterface|UserMovedEventInterface|GroupCreatedUpdatedEventInterface|DeleteGroupEventInterface>(); pendingEvents: Queue<InitUserPositionEventInterface|AddPlayerEventInterface|RemovePlayerEventInterface|UserMovedEventInterface|GroupCreatedUpdatedEventInterface|DeleteGroupEventInterface> = new Queue<InitUserPositionEventInterface|AddPlayerEventInterface|RemovePlayerEventInterface|UserMovedEventInterface|GroupCreatedUpdatedEventInterface|DeleteGroupEventInterface>();
private initPosition: PositionInterface|null = null; private initPosition: PositionInterface|null = null;
private playersPositionInterpolator = new PlayersPositionInterpolator(); private playersPositionInterpolator = new PlayersPositionInterpolator();
public connection!: RoomConnection; public connection: RoomConnection|undefined;
private simplePeer!: SimplePeer; private simplePeer!: SimplePeer;
private GlobalMessageManager!: GlobalMessageManager; private GlobalMessageManager!: GlobalMessageManager;
public ConsoleGlobalMessageManager!: ConsoleGlobalMessageManager; public ConsoleGlobalMessageManager!: ConsoleGlobalMessageManager;
@ -416,7 +416,7 @@ export class GameScene extends ResizableScene implements CenterListener {
//initialise list of other player //initialise list of other player
this.MapPlayers = this.physics.add.group({immovable: true}); this.MapPlayers = this.physics.add.group({immovable: true});
//create input to move //create input to move
mediaManager.setUserInputManager(this.userInputManager); mediaManager.setUserInputManager(this.userInputManager);
this.userInputManager = new UserInputManager(this); this.userInputManager = new UserInputManager(this);
@ -712,7 +712,7 @@ export class GameScene extends ResizableScene implements CenterListener {
if (JITSI_PRIVATE_MODE && !jitsiUrl) { if (JITSI_PRIVATE_MODE && !jitsiUrl) {
const adminTag = allProps.get("jitsiRoomAdminTag") as string|undefined; const adminTag = allProps.get("jitsiRoomAdminTag") as string|undefined;
this.connection.emitQueryJitsiJwtMessage(roomName, adminTag); this.connection?.emitQueryJitsiJwtMessage(roomName, adminTag);
} else { } else {
this.startJitsi(roomName, undefined); this.startJitsi(roomName, undefined);
} }
@ -735,9 +735,9 @@ export class GameScene extends ResizableScene implements CenterListener {
}); });
this.gameMap.onPropertyChange('silent', (newValue, oldValue) => { this.gameMap.onPropertyChange('silent', (newValue, oldValue) => {
if (newValue === undefined || newValue === false || newValue === '') { if (newValue === undefined || newValue === false || newValue === '') {
this.connection.setSilent(false); this.connection?.setSilent(false);
} else { } else {
this.connection.setSilent(true); this.connection?.setSilent(true);
} }
}); });
this.gameMap.onPropertyChange('playAudio', (newValue, oldValue, allProps) => { this.gameMap.onPropertyChange('playAudio', (newValue, oldValue, allProps) => {
@ -1167,7 +1167,7 @@ ${escapedMessage}
this.lastMoveEventSent = event; this.lastMoveEventSent = event;
this.lastSentTick = this.currentTick; this.lastSentTick = this.currentTick;
const camera = this.cameras.main; const camera = this.cameras.main;
this.connection.sharePosition(event.x, event.y, event.direction, event.moving, { this.connection?.sharePosition(event.x, event.y, event.direction, event.moving, {
left: camera.scrollX, left: camera.scrollX,
top: camera.scrollY, top: camera.scrollY,
right: camera.scrollX + camera.width, right: camera.scrollX + camera.width,
@ -1233,7 +1233,7 @@ ${escapedMessage}
* Put all the players on the map on map load. * Put all the players on the map on map load.
*/ */
private doInitUsersPosition(usersPosition: MessageUserPositionInterface[]): void { private doInitUsersPosition(usersPosition: MessageUserPositionInterface[]): void {
const currentPlayerId = this.connection.getUserId(); const currentPlayerId = this.connection?.getUserId();
this.removeAllRemotePlayers(); this.removeAllRemotePlayers();
// load map // load map
usersPosition.forEach((userPosition : MessageUserPositionInterface) => { usersPosition.forEach((userPosition : MessageUserPositionInterface) => {
@ -1377,7 +1377,7 @@ ${escapedMessage}
* Sends to the server an event emitted by one of the ActionableItems. * Sends to the server an event emitted by one of the ActionableItems.
*/ */
emitActionableEvent(itemId: number, eventName: string, state: unknown, parameters: unknown) { emitActionableEvent(itemId: number, eventName: string, state: unknown, parameters: unknown) {
this.connection.emitActionableEvent(itemId, eventName, state, parameters); this.connection?.emitActionableEvent(itemId, eventName, state, parameters);
} }
public onResize(): void { public onResize(): void {
@ -1385,7 +1385,7 @@ ${escapedMessage}
// Send new viewport to server // Send new viewport to server
const camera = this.cameras.main; const camera = this.cameras.main;
this.connection.setViewport({ this.connection?.setViewport({
left: camera.scrollX, left: camera.scrollX,
top: camera.scrollY, top: camera.scrollY,
right: camera.scrollX + camera.width, right: camera.scrollX + camera.width,
@ -1441,7 +1441,7 @@ ${escapedMessage}
const jitsiUrl = allProps.get("jitsiUrl") as string|undefined; const jitsiUrl = allProps.get("jitsiUrl") as string|undefined;
jitsiFactory.start(roomName, this.playerName, jwt, jitsiConfig, jitsiInterfaceConfig, jitsiUrl); jitsiFactory.start(roomName, this.playerName, jwt, jitsiConfig, jitsiInterfaceConfig, jitsiUrl);
this.connection.setSilent(true); this.connection?.setSilent(true);
mediaManager.hideGameOverlay(); mediaManager.hideGameOverlay();
//permit to stop jitsi when user close iframe //permit to stop jitsi when user close iframe

View File

@ -91,7 +91,7 @@ export class MenuScene extends Phaser.Scene {
this.menuElement.addListener('click'); this.menuElement.addListener('click');
this.menuElement.on('click', this.onMenuClick.bind(this)); this.menuElement.on('click', this.onMenuClick.bind(this));
worldFullWarningStream.stream.subscribe(() => this.showWorldCapacityWarning()); worldFullWarningStream.stream.subscribe(() => this.showWorldCapacityWarning());
} }
@ -118,7 +118,8 @@ export class MenuScene extends Phaser.Scene {
this.closeAll(); this.closeAll();
this.sideMenuOpened = true; this.sideMenuOpened = true;
this.menuButton.getChildByID('openMenuButton').innerHTML = 'X'; this.menuButton.getChildByID('openMenuButton').innerHTML = 'X';
if (gameManager.getCurrentGameScene(this).connection && gameManager.getCurrentGameScene(this).connection.isAdmin()) { const connection = gameManager.getCurrentGameScene(this).connection;
if (connection && connection.isAdmin()) {
const adminSection = this.menuElement.getChildByID('adminConsoleSection') as HTMLElement; const adminSection = this.menuElement.getChildByID('adminConsoleSection') as HTMLElement;
adminSection.hidden = false; adminSection.hidden = false;
} }
@ -134,7 +135,7 @@ export class MenuScene extends Phaser.Scene {
ease: 'Power3' ease: 'Power3'
}); });
} }
private showWorldCapacityWarning() { private showWorldCapacityWarning() {
if (!this.warningContainer) { if (!this.warningContainer) {
this.warningContainer = new WarningContainer(this); this.warningContainer = new WarningContainer(this);
@ -147,7 +148,7 @@ export class MenuScene extends Phaser.Scene {
this.warningContainer = null this.warningContainer = null
this.warningContainerTimeout = null this.warningContainerTimeout = null
}, 120000); }, 120000);
} }
private closeSideMenu(): void { private closeSideMenu(): void {

View File

@ -7,11 +7,11 @@ export const gameReportRessource = 'resources/html/gameReport.html';
export class ReportMenu extends Phaser.GameObjects.DOMElement { export class ReportMenu extends Phaser.GameObjects.DOMElement {
private opened: boolean = false; private opened: boolean = false;
private userId!: number; private userId!: number;
private userName!: string|undefined; private userName!: string|undefined;
private anonymous: boolean; private anonymous: boolean;
constructor(scene: Phaser.Scene, anonymous: boolean) { constructor(scene: Phaser.Scene, anonymous: boolean) {
super(scene, -2000, -2000); super(scene, -2000, -2000);
this.anonymous = anonymous; this.anonymous = anonymous;
@ -23,7 +23,7 @@ export class ReportMenu extends Phaser.GameObjects.DOMElement {
const textToHide = this.getChildByID('askActionP') as HTMLElement; const textToHide = this.getChildByID('askActionP') as HTMLElement;
textToHide.hidden = true; textToHide.hidden = true;
} }
scene.add.existing(this); scene.add.existing(this);
MenuScene.revealMenusAfterInit(this, gameReportKey); MenuScene.revealMenusAfterInit(this, gameReportKey);
@ -45,10 +45,10 @@ export class ReportMenu extends Phaser.GameObjects.DOMElement {
this.close(); this.close();
return; return;
} }
this.userId = userId; this.userId = userId;
this.userName = userName; this.userName = userName;
const mainEl = this.getChildByID('gameReport') as HTMLElement; const mainEl = this.getChildByID('gameReport') as HTMLElement;
this.x = this.getCenteredX(mainEl); this.x = this.getCenteredX(mainEl);
this.y = this.getHiddenY(mainEl); this.y = this.getHiddenY(mainEl);
@ -82,7 +82,7 @@ export class ReportMenu extends Phaser.GameObjects.DOMElement {
ease: 'Power3' ease: 'Power3'
}); });
} }
//todo: into a parent class? //todo: into a parent class?
private getCenteredX(mainEl: HTMLElement): number { private getCenteredX(mainEl: HTMLElement): number {
return window.innerWidth / 4 - mainEl.clientWidth / 2; return window.innerWidth / 4 - mainEl.clientWidth / 2;
@ -93,7 +93,7 @@ export class ReportMenu extends Phaser.GameObjects.DOMElement {
private getCenteredY(mainEl: HTMLElement): number { private getCenteredY(mainEl: HTMLElement): number {
return window.innerHeight / 4 - mainEl.clientHeight / 2; return window.innerHeight / 4 - mainEl.clientHeight / 2;
} }
private toggleBlock(): void { private toggleBlock(): void {
!blackListManager.isBlackListed(this.userId) ? blackListManager.blackList(this.userId) : blackListManager.cancelBlackList(this.userId); !blackListManager.isBlackListed(this.userId) ? blackListManager.blackList(this.userId) : blackListManager.cancelBlackList(this.userId);
this.close(); this.close();
@ -109,10 +109,10 @@ export class ReportMenu extends Phaser.GameObjects.DOMElement {
gamePError.style.display = 'block'; gamePError.style.display = 'block';
return; return;
} }
gameManager.getCurrentGameScene(this.scene).connection.emitReportPlayerMessage( gameManager.getCurrentGameScene(this.scene).connection?.emitReportPlayerMessage(
this.userId, this.userId,
gameTextArea.value gameTextArea.value
); );
this.close(); this.close();
} }
} }