From a23102450276cf0be031e0456b8fceb238a0f1d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Wed, 3 Jun 2020 11:55:31 +0200 Subject: [PATCH] Fixing strict type checks --- front/src/Connection.ts | 60 ++++++++++++++++------------ front/src/Logger/MessageUI.ts | 8 ++-- front/src/Phaser/Login/LoginScene.ts | 16 ++++---- front/src/WebRtc/MediaManager.ts | 29 +++++++++----- 4 files changed, 66 insertions(+), 47 deletions(-) diff --git a/front/src/Connection.ts b/front/src/Connection.ts index fc8fa1ed..08ffb871 100644 --- a/front/src/Connection.ts +++ b/front/src/Connection.ts @@ -98,10 +98,10 @@ export interface GroupCreatedUpdatedMessageInterface { } export interface ConnectionInterface { - socket: any; - token: string; - name: string; - userId: string; + socket: Socket|null; + token: string|null; + name: string|null; + userId: string|null; createConnection(name: string, characterSelected: string): Promise; @@ -122,15 +122,15 @@ export interface ConnectionInterface { } export class Connection implements ConnectionInterface { - socket: Socket; - token: string; - name: string; // TODO: drop "name" storage here - character: string; - userId: string; + socket: Socket|null = null; + token: string|null = null; + name: string|null = null; // TODO: drop "name" storage here + character: string|null = null; + userId: string|null = null; GameManager: GameManager; - lastPositionShared: PointInterface = null; + lastPositionShared: PointInterface|null = null; lastRoom: string|null = null; constructor(GameManager: GameManager) { @@ -156,6 +156,13 @@ export class Connection implements ConnectionInterface { }); } + private getSocket(): Socket { + if (this.socket === null) { + throw new Error('Socket not initialized while using Connection') + } + return this.socket; + } + /** * * @param character @@ -171,7 +178,7 @@ export class Connection implements ConnectionInterface { this.onUserLeft(); return new Promise((resolve, reject) => { - this.socket.emit(EventMessage.SET_PLAYER_DETAILS, { + this.getSocket().emit(EventMessage.SET_PLAYER_DETAILS, { name: this.name, character: this.character } as SetPlayerDetailsMessage, (id: string) => { @@ -215,7 +222,7 @@ export class Connection implements ConnectionInterface { } joinARoom(roomId: string, startX: number, startY: number, direction: string, moving: boolean): void { - this.socket.emit(EventMessage.JOIN_ROOM, { roomId, position: {x: startX, y: startY, direction, moving }}, (userPositions: MessageUserPositionInterface[]) => { + this.getSocket().emit(EventMessage.JOIN_ROOM, { roomId, position: {x: startX, y: startY, direction, moving }}, (userPositions: MessageUserPositionInterface[]) => { this.GameManager.initUsersPosition(userPositions); }); this.lastRoom = roomId; @@ -227,42 +234,42 @@ export class Connection implements ConnectionInterface { } let point = new Point(x, y, direction, moving); this.lastPositionShared = point; - this.socket.emit(EventMessage.USER_POSITION, point); + this.getSocket().emit(EventMessage.USER_POSITION, point); } private onUserJoins(): void { - this.socket.on(EventMessage.JOIN_ROOM, (message: MessageUserJoined) => { + this.getSocket().on(EventMessage.JOIN_ROOM, (message: MessageUserJoined) => { this.GameManager.onUserJoins(message); }); } private onUserMoved(): void { - this.socket.on(EventMessage.USER_MOVED, (message: MessageUserMovedInterface) => { + this.getSocket().on(EventMessage.USER_MOVED, (message: MessageUserMovedInterface) => { this.GameManager.onUserMoved(message); }); } private onUserLeft(): void { - this.socket.on(EventMessage.USER_LEFT, (userId: string) => { + this.getSocket().on(EventMessage.USER_LEFT, (userId: string) => { this.GameManager.onUserLeft(userId); }); } private groupUpdatedOrCreated(): void { - this.socket.on(EventMessage.GROUP_CREATE_UPDATE, (groupCreateUpdateMessage: GroupCreatedUpdatedMessageInterface) => { + this.getSocket().on(EventMessage.GROUP_CREATE_UPDATE, (groupCreateUpdateMessage: GroupCreatedUpdatedMessageInterface) => { //console.log('Group ', groupCreateUpdateMessage.groupId, " position :", groupCreateUpdateMessage.position.x, groupCreateUpdateMessage.position.y) this.GameManager.shareGroupPosition(groupCreateUpdateMessage); }) } private groupDeleted(): void { - this.socket.on(EventMessage.GROUP_DELETE, (groupId: string) => { + this.getSocket().on(EventMessage.GROUP_DELETE, (groupId: string) => { this.GameManager.deleteGroup(groupId); }) } sendWebrtcSignal(signal: any, roomId: string, userId? : string, receiverId? : string) { - return this.socket.emit(EventMessage.WEBRTC_SIGNAL, { + return this.getSocket().emit(EventMessage.WEBRTC_SIGNAL, { userId: userId ? userId : this.userId, receiverId: receiverId ? receiverId : this.userId, roomId: roomId, @@ -271,31 +278,34 @@ export class Connection implements ConnectionInterface { } receiveWebrtcStart(callback: Function) { - this.socket.on(EventMessage.WEBRTC_START, callback); + this.getSocket().on(EventMessage.WEBRTC_START, callback); } receiveWebrtcSignal(callback: Function) { - return this.socket.on(EventMessage.WEBRTC_SIGNAL, callback); + return this.getSocket().on(EventMessage.WEBRTC_SIGNAL, callback); } private errorMessage(): void { - this.socket.on(EventMessage.MESSAGE_ERROR, (message: string) => { + this.getSocket().on(EventMessage.MESSAGE_ERROR, (message: string) => { console.error(EventMessage.MESSAGE_ERROR, message); }) } private disconnectServer(): void { - this.socket.on(EventMessage.CONNECT_ERROR, () => { + this.getSocket().on(EventMessage.CONNECT_ERROR, () => { this.GameManager.switchToDisconnectedScene(); }); - this.socket.on(EventMessage.RECONNECT, () => { + this.getSocket().on(EventMessage.RECONNECT, () => { this.connectSocketServer(); + if (this.lastPositionShared === null) { + throw new Error('No last position shared found while reconnecting'); + } this.GameManager.reconnectToGameScene(this.lastPositionShared); }); } disconnectMessage(callback: Function): void { - this.socket.on(EventMessage.WEBRTC_DISCONNECT, callback); + this.getSocket().on(EventMessage.WEBRTC_DISCONNECT, callback); } } diff --git a/front/src/Logger/MessageUI.ts b/front/src/Logger/MessageUI.ts index a73c2418..6011fb73 100644 --- a/front/src/Logger/MessageUI.ts +++ b/front/src/Logger/MessageUI.ts @@ -3,18 +3,18 @@ export class MessageUI { static warningMessage(text: string){ this.removeMessage(); let body = document.getElementById("body"); - body.insertAdjacentHTML('afterbegin', ` + body?.insertAdjacentHTML('afterbegin', `
${text}
`); } - static removeMessage(id : string = null) { + static removeMessage(id : string|null = null) { if(!id){ let messages = document.getElementsByClassName("message-info"); for (let i = 0; i < messages.length; i++){ - messages.item(i).remove(); + messages.item(i)?.remove(); } return; } @@ -24,4 +24,4 @@ export class MessageUI { } previousElement.remove(); } -} \ No newline at end of file +} diff --git a/front/src/Phaser/Login/LoginScene.ts b/front/src/Phaser/Login/LoginScene.ts index da684da2..f00b3879 100644 --- a/front/src/Phaser/Login/LoginScene.ts +++ b/front/src/Phaser/Login/LoginScene.ts @@ -16,12 +16,12 @@ enum LoginTextures { } export class LoginScene extends Phaser.Scene { - private nameInput: TextInput; - private textField: TextField; - private infoTextField: TextField; - private pressReturnField: TextField; - private logo: Image; - private name: string; + private nameInput: TextInput|null = null; + private textField: TextField|null = null; + private infoTextField: TextField|null = null; + private pressReturnField: TextField|null = null; + private logo: Image|null = null; + private name: string = ''; constructor() { super({ @@ -82,9 +82,9 @@ export class LoginScene extends Phaser.Scene { update(time: number, delta: number): void { if (this.name == '') { - this.pressReturnField.setVisible(false); + this.pressReturnField?.setVisible(false); } else { - this.pressReturnField.setVisible(!!(Math.floor(time / 500) % 2)); + this.pressReturnField?.setVisible(!!(Math.floor(time / 500) % 2)); } } diff --git a/front/src/WebRtc/MediaManager.ts b/front/src/WebRtc/MediaManager.ts index 1e183559..a80cb6af 100644 --- a/front/src/WebRtc/MediaManager.ts +++ b/front/src/WebRtc/MediaManager.ts @@ -4,26 +4,25 @@ const videoConstraint: {width : any, height: any, facingMode : string} = { facingMode: "user" }; export class MediaManager { - localStream: MediaStream; + localStream: MediaStream|null = null; remoteVideo: Array = new Array(); - myCamVideo: any; + myCamVideo: HTMLVideoElement; cinemaClose: any = null; cinema: any = null; microphoneClose: any = null; microphone: any = null; - webrtcInAudio: any; + webrtcInAudio: HTMLAudioElement; constraintsMedia : {audio : any, video : any} = { audio: true, video: videoConstraint }; - getCameraPromise : Promise = null; updatedLocalStreamCallBack : Function; constructor(updatedLocalStreamCallBack : Function) { this.updatedLocalStreamCallBack = updatedLocalStreamCallBack; - this.myCamVideo = document.getElementById('myCamVideo'); - this.webrtcInAudio = document.getElementById('audio-webrtc-in'); + this.myCamVideo = this.getElementByIdOrFail('myCamVideo'); + this.webrtcInAudio = this.getElementByIdOrFail('audio-webrtc-in'); this.webrtcInAudio.volume = 0.2; this.microphoneClose = document.getElementById('microphone-close'); @@ -56,7 +55,7 @@ export class MediaManager { } activeVisio(){ - let webRtc = document.getElementById('webRtc'); + let webRtc = this.getElementByIdOrFail('webRtc'); webRtc.classList.add('active'); } @@ -130,7 +129,7 @@ export class MediaManager { } catch (e) { promise = Promise.reject(false); } - return this.getCameraPromise = promise; + return promise; } /** @@ -139,7 +138,7 @@ export class MediaManager { */ addActiveVideo(userId : string, userName: string = ""){ this.webrtcInAudio.play(); - let elementRemoteVideo = document.getElementById("activeCam"); + let elementRemoteVideo = this.getElementByIdOrFail("activeCam"); userName = userName.toUpperCase(); let color = this.getColorByString(userName); elementRemoteVideo.insertAdjacentHTML('beforeend', ` @@ -247,4 +246,14 @@ export class MediaManager { } return color; } -} \ No newline at end of file + + private getElementByIdOrFail(id: string): T { + let elem = document.getElementById("activeCam"); + if (elem === null) { + throw new Error("Cannot find HTML element with id '"+id+"'"); + } + // FIXME: does not check the type of the returned type + return elem as T; + } + +}