Fixing strict type checks

This commit is contained in:
David Négrier 2020-06-03 11:55:31 +02:00
parent a3ac782f17
commit a231024502
4 changed files with 66 additions and 47 deletions

View File

@ -98,10 +98,10 @@ export interface GroupCreatedUpdatedMessageInterface {
} }
export interface ConnectionInterface { export interface ConnectionInterface {
socket: any; socket: Socket|null;
token: string; token: string|null;
name: string; name: string|null;
userId: string; userId: string|null;
createConnection(name: string, characterSelected: string): Promise<any>; createConnection(name: string, characterSelected: string): Promise<any>;
@ -122,15 +122,15 @@ export interface ConnectionInterface {
} }
export class Connection implements ConnectionInterface { export class Connection implements ConnectionInterface {
socket: Socket; socket: Socket|null = null;
token: string; token: string|null = null;
name: string; // TODO: drop "name" storage here name: string|null = null; // TODO: drop "name" storage here
character: string; character: string|null = null;
userId: string; userId: string|null = null;
GameManager: GameManager; GameManager: GameManager;
lastPositionShared: PointInterface = null; lastPositionShared: PointInterface|null = null;
lastRoom: string|null = null; lastRoom: string|null = null;
constructor(GameManager: GameManager) { 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 * @param character
@ -171,7 +178,7 @@ export class Connection implements ConnectionInterface {
this.onUserLeft(); this.onUserLeft();
return new Promise<ConnectionInterface>((resolve, reject) => { return new Promise<ConnectionInterface>((resolve, reject) => {
this.socket.emit(EventMessage.SET_PLAYER_DETAILS, { this.getSocket().emit(EventMessage.SET_PLAYER_DETAILS, {
name: this.name, name: this.name,
character: this.character character: this.character
} as SetPlayerDetailsMessage, (id: string) => { } 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 { 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.GameManager.initUsersPosition(userPositions);
}); });
this.lastRoom = roomId; this.lastRoom = roomId;
@ -227,42 +234,42 @@ export class Connection implements ConnectionInterface {
} }
let point = new Point(x, y, direction, moving); let point = new Point(x, y, direction, moving);
this.lastPositionShared = point; this.lastPositionShared = point;
this.socket.emit(EventMessage.USER_POSITION, point); this.getSocket().emit(EventMessage.USER_POSITION, point);
} }
private onUserJoins(): void { private onUserJoins(): void {
this.socket.on(EventMessage.JOIN_ROOM, (message: MessageUserJoined) => { this.getSocket().on(EventMessage.JOIN_ROOM, (message: MessageUserJoined) => {
this.GameManager.onUserJoins(message); this.GameManager.onUserJoins(message);
}); });
} }
private onUserMoved(): void { private onUserMoved(): void {
this.socket.on(EventMessage.USER_MOVED, (message: MessageUserMovedInterface) => { this.getSocket().on(EventMessage.USER_MOVED, (message: MessageUserMovedInterface) => {
this.GameManager.onUserMoved(message); this.GameManager.onUserMoved(message);
}); });
} }
private onUserLeft(): void { private onUserLeft(): void {
this.socket.on(EventMessage.USER_LEFT, (userId: string) => { this.getSocket().on(EventMessage.USER_LEFT, (userId: string) => {
this.GameManager.onUserLeft(userId); this.GameManager.onUserLeft(userId);
}); });
} }
private groupUpdatedOrCreated(): void { 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) //console.log('Group ', groupCreateUpdateMessage.groupId, " position :", groupCreateUpdateMessage.position.x, groupCreateUpdateMessage.position.y)
this.GameManager.shareGroupPosition(groupCreateUpdateMessage); this.GameManager.shareGroupPosition(groupCreateUpdateMessage);
}) })
} }
private groupDeleted(): void { private groupDeleted(): void {
this.socket.on(EventMessage.GROUP_DELETE, (groupId: string) => { this.getSocket().on(EventMessage.GROUP_DELETE, (groupId: string) => {
this.GameManager.deleteGroup(groupId); this.GameManager.deleteGroup(groupId);
}) })
} }
sendWebrtcSignal(signal: any, roomId: string, userId? : string, receiverId? : string) { 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, userId: userId ? userId : this.userId,
receiverId: receiverId ? receiverId : this.userId, receiverId: receiverId ? receiverId : this.userId,
roomId: roomId, roomId: roomId,
@ -271,31 +278,34 @@ export class Connection implements ConnectionInterface {
} }
receiveWebrtcStart(callback: Function) { receiveWebrtcStart(callback: Function) {
this.socket.on(EventMessage.WEBRTC_START, callback); this.getSocket().on(EventMessage.WEBRTC_START, callback);
} }
receiveWebrtcSignal(callback: Function) { receiveWebrtcSignal(callback: Function) {
return this.socket.on(EventMessage.WEBRTC_SIGNAL, callback); return this.getSocket().on(EventMessage.WEBRTC_SIGNAL, callback);
} }
private errorMessage(): void { 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); console.error(EventMessage.MESSAGE_ERROR, message);
}) })
} }
private disconnectServer(): void { private disconnectServer(): void {
this.socket.on(EventMessage.CONNECT_ERROR, () => { this.getSocket().on(EventMessage.CONNECT_ERROR, () => {
this.GameManager.switchToDisconnectedScene(); this.GameManager.switchToDisconnectedScene();
}); });
this.socket.on(EventMessage.RECONNECT, () => { this.getSocket().on(EventMessage.RECONNECT, () => {
this.connectSocketServer(); this.connectSocketServer();
if (this.lastPositionShared === null) {
throw new Error('No last position shared found while reconnecting');
}
this.GameManager.reconnectToGameScene(this.lastPositionShared); this.GameManager.reconnectToGameScene(this.lastPositionShared);
}); });
} }
disconnectMessage(callback: Function): void { disconnectMessage(callback: Function): void {
this.socket.on(EventMessage.WEBRTC_DISCONNECT, callback); this.getSocket().on(EventMessage.WEBRTC_DISCONNECT, callback);
} }
} }

View File

@ -3,18 +3,18 @@ export class MessageUI {
static warningMessage(text: string){ static warningMessage(text: string){
this.removeMessage(); this.removeMessage();
let body = document.getElementById("body"); let body = document.getElementById("body");
body.insertAdjacentHTML('afterbegin', ` body?.insertAdjacentHTML('afterbegin', `
<div id="message-reconnect" class="message-info warning"> <div id="message-reconnect" class="message-info warning">
${text} ${text}
</div> </div>
`); `);
} }
static removeMessage(id : string = null) { static removeMessage(id : string|null = null) {
if(!id){ if(!id){
let messages = document.getElementsByClassName("message-info"); let messages = document.getElementsByClassName("message-info");
for (let i = 0; i < messages.length; i++){ for (let i = 0; i < messages.length; i++){
messages.item(i).remove(); messages.item(i)?.remove();
} }
return; return;
} }
@ -24,4 +24,4 @@ export class MessageUI {
} }
previousElement.remove(); previousElement.remove();
} }
} }

View File

@ -16,12 +16,12 @@ enum LoginTextures {
} }
export class LoginScene extends Phaser.Scene { export class LoginScene extends Phaser.Scene {
private nameInput: TextInput; private nameInput: TextInput|null = null;
private textField: TextField; private textField: TextField|null = null;
private infoTextField: TextField; private infoTextField: TextField|null = null;
private pressReturnField: TextField; private pressReturnField: TextField|null = null;
private logo: Image; private logo: Image|null = null;
private name: string; private name: string = '';
constructor() { constructor() {
super({ super({
@ -82,9 +82,9 @@ export class LoginScene extends Phaser.Scene {
update(time: number, delta: number): void { update(time: number, delta: number): void {
if (this.name == '') { if (this.name == '') {
this.pressReturnField.setVisible(false); this.pressReturnField?.setVisible(false);
} else { } else {
this.pressReturnField.setVisible(!!(Math.floor(time / 500) % 2)); this.pressReturnField?.setVisible(!!(Math.floor(time / 500) % 2));
} }
} }

View File

@ -4,26 +4,25 @@ const videoConstraint: {width : any, height: any, facingMode : string} = {
facingMode: "user" facingMode: "user"
}; };
export class MediaManager { export class MediaManager {
localStream: MediaStream; localStream: MediaStream|null = null;
remoteVideo: Array<any> = new Array<any>(); remoteVideo: Array<any> = new Array<any>();
myCamVideo: any; myCamVideo: HTMLVideoElement;
cinemaClose: any = null; cinemaClose: any = null;
cinema: any = null; cinema: any = null;
microphoneClose: any = null; microphoneClose: any = null;
microphone: any = null; microphone: any = null;
webrtcInAudio: any; webrtcInAudio: HTMLAudioElement;
constraintsMedia : {audio : any, video : any} = { constraintsMedia : {audio : any, video : any} = {
audio: true, audio: true,
video: videoConstraint video: videoConstraint
}; };
getCameraPromise : Promise<any> = null;
updatedLocalStreamCallBack : Function; updatedLocalStreamCallBack : Function;
constructor(updatedLocalStreamCallBack : Function) { constructor(updatedLocalStreamCallBack : Function) {
this.updatedLocalStreamCallBack = updatedLocalStreamCallBack; this.updatedLocalStreamCallBack = updatedLocalStreamCallBack;
this.myCamVideo = document.getElementById('myCamVideo'); this.myCamVideo = this.getElementByIdOrFail<HTMLVideoElement>('myCamVideo');
this.webrtcInAudio = document.getElementById('audio-webrtc-in'); this.webrtcInAudio = this.getElementByIdOrFail<HTMLAudioElement>('audio-webrtc-in');
this.webrtcInAudio.volume = 0.2; this.webrtcInAudio.volume = 0.2;
this.microphoneClose = document.getElementById('microphone-close'); this.microphoneClose = document.getElementById('microphone-close');
@ -56,7 +55,7 @@ export class MediaManager {
} }
activeVisio(){ activeVisio(){
let webRtc = document.getElementById('webRtc'); let webRtc = this.getElementByIdOrFail('webRtc');
webRtc.classList.add('active'); webRtc.classList.add('active');
} }
@ -130,7 +129,7 @@ export class MediaManager {
} catch (e) { } catch (e) {
promise = Promise.reject(false); promise = Promise.reject(false);
} }
return this.getCameraPromise = promise; return promise;
} }
/** /**
@ -139,7 +138,7 @@ export class MediaManager {
*/ */
addActiveVideo(userId : string, userName: string = ""){ addActiveVideo(userId : string, userName: string = ""){
this.webrtcInAudio.play(); this.webrtcInAudio.play();
let elementRemoteVideo = document.getElementById("activeCam"); let elementRemoteVideo = this.getElementByIdOrFail("activeCam");
userName = userName.toUpperCase(); userName = userName.toUpperCase();
let color = this.getColorByString(userName); let color = this.getColorByString(userName);
elementRemoteVideo.insertAdjacentHTML('beforeend', ` elementRemoteVideo.insertAdjacentHTML('beforeend', `
@ -247,4 +246,14 @@ export class MediaManager {
} }
return color; return color;
} }
}
private getElementByIdOrFail<T extends HTMLElement>(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;
}
}