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 {
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<any>;
@ -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<ConnectionInterface>((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);
}
}

View File

@ -3,18 +3,18 @@ export class MessageUI {
static warningMessage(text: string){
this.removeMessage();
let body = document.getElementById("body");
body.insertAdjacentHTML('afterbegin', `
body?.insertAdjacentHTML('afterbegin', `
<div id="message-reconnect" class="message-info warning">
${text}
</div>
`);
}
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();
}
}
}

View File

@ -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));
}
}

View File

@ -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<any> = new Array<any>();
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<any> = 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<HTMLVideoElement>('myCamVideo');
this.webrtcInAudio = this.getElementByIdOrFail<HTMLAudioElement>('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;
}
}
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;
}
}