2020-09-20 17:12:27 +02:00
|
|
|
import {HtmlUtils} from "./../WebRtc/HtmlUtils";
|
|
|
|
import {AUDIO_TYPE, MESSAGE_TYPE} from "./ConsoleGlobalMessageManager";
|
2021-03-31 16:00:14 +02:00
|
|
|
import {PUSHER_URL, UPLOADER_URL} from "../Enum/EnvironmentVariable";
|
2021-05-12 09:13:25 +02:00
|
|
|
import type {RoomConnection} from "../Connexion/RoomConnection";
|
|
|
|
import type {PlayGlobalMessageInterface} from "../Connexion/ConnexionModels";
|
2020-09-16 18:38:50 +02:00
|
|
|
|
|
|
|
export class GlobalMessageManager {
|
|
|
|
|
2020-10-01 14:11:34 +02:00
|
|
|
constructor(private Connection: RoomConnection) {
|
2020-09-16 18:38:50 +02:00
|
|
|
this.initialise();
|
|
|
|
}
|
|
|
|
|
|
|
|
initialise(){
|
|
|
|
//receive signal to show message
|
2020-10-01 14:11:34 +02:00
|
|
|
this.Connection.receivePlayGlobalMessage((message: PlayGlobalMessageInterface) => {
|
2020-09-20 17:12:27 +02:00
|
|
|
this.playMessage(message);
|
2020-09-16 18:38:50 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
//receive signal to close message
|
2020-10-01 14:11:34 +02:00
|
|
|
this.Connection.receiveStopGlobalMessage((messageId: string) => {
|
|
|
|
this.stopMessage(messageId);
|
2020-09-16 18:38:50 +02:00
|
|
|
});
|
2020-10-15 11:51:24 +02:00
|
|
|
|
|
|
|
//receive signal to close message
|
|
|
|
this.Connection.receiveTeleportMessage((map: string) => {
|
|
|
|
console.log('map to teleport user', map);
|
|
|
|
//TODO teleport user on map
|
|
|
|
});
|
2020-09-16 18:38:50 +02:00
|
|
|
}
|
|
|
|
|
2020-10-01 14:11:34 +02:00
|
|
|
private playMessage(message : PlayGlobalMessageInterface){
|
2020-09-23 18:44:24 +02:00
|
|
|
const previousMessage = document.getElementById(this.getHtmlMessageId(message.id));
|
2020-09-19 01:08:56 +02:00
|
|
|
if(previousMessage){
|
|
|
|
previousMessage.remove();
|
|
|
|
}
|
|
|
|
|
2020-09-20 17:12:27 +02:00
|
|
|
if(AUDIO_TYPE === message.type){
|
|
|
|
this.playAudioMessage(message.id, message.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(MESSAGE_TYPE === message.type){
|
|
|
|
this.playTextMessage(message.id, message.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-01 14:11:34 +02:00
|
|
|
private playAudioMessage(messageId : string, urlMessage: string){
|
2020-09-21 15:00:39 +02:00
|
|
|
//delete previous elements
|
|
|
|
const previousDivAudio = document.getElementsByClassName('audio-playing');
|
|
|
|
for(let i = 0; i < previousDivAudio.length; i++){
|
|
|
|
previousDivAudio[i].remove();
|
2020-09-20 17:12:27 +02:00
|
|
|
}
|
2020-09-21 15:00:39 +02:00
|
|
|
|
|
|
|
//create new element
|
|
|
|
const divAudio : HTMLDivElement = document.createElement('div');
|
|
|
|
divAudio.id = `audio-playing-${messageId}`;
|
|
|
|
divAudio.classList.add('audio-playing');
|
|
|
|
const imgAudio : HTMLImageElement = document.createElement('img');
|
|
|
|
imgAudio.src = '/resources/logos/megaphone.svg';
|
|
|
|
const pAudio : HTMLParagraphElement = document.createElement('p');
|
|
|
|
pAudio.textContent = 'Message audio'
|
|
|
|
divAudio.appendChild(imgAudio);
|
|
|
|
divAudio.appendChild(pAudio);
|
|
|
|
|
2020-09-20 17:12:27 +02:00
|
|
|
const mainSectionDiv = HtmlUtils.getElementByIdOrFail<HTMLDivElement>('main-container');
|
2020-09-21 15:00:39 +02:00
|
|
|
mainSectionDiv.appendChild(divAudio);
|
2020-09-20 17:12:27 +02:00
|
|
|
|
2020-09-21 15:00:39 +02:00
|
|
|
const messageAudio : HTMLAudioElement = document.createElement('audio');
|
|
|
|
messageAudio.id = this.getHtmlMessageId(messageId);
|
|
|
|
messageAudio.autoplay = true;
|
|
|
|
messageAudio.style.display = 'none';
|
|
|
|
messageAudio.onended = () => {
|
|
|
|
divAudio.classList.remove('active');
|
|
|
|
messageAudio.remove();
|
|
|
|
setTimeout(() => {
|
|
|
|
divAudio.remove();
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
messageAudio.onplay = () => {
|
|
|
|
divAudio.classList.add('active');
|
|
|
|
}
|
|
|
|
const messageAudioSource : HTMLSourceElement = document.createElement('source');
|
2020-12-09 21:29:29 +01:00
|
|
|
messageAudioSource.src = `${UPLOADER_URL}${urlMessage}`;
|
2020-09-21 15:00:39 +02:00
|
|
|
messageAudio.appendChild(messageAudioSource);
|
|
|
|
mainSectionDiv.appendChild(messageAudio);
|
2020-09-20 17:12:27 +02:00
|
|
|
}
|
|
|
|
|
2020-10-01 14:11:34 +02:00
|
|
|
private playTextMessage(messageId : string, htmlMessage: string){
|
2020-09-19 01:08:56 +02:00
|
|
|
//add button to clear message
|
2020-09-20 17:12:27 +02:00
|
|
|
const buttonText = document.createElement('p');
|
2020-09-23 18:07:31 +02:00
|
|
|
buttonText.id = 'button-clear-message';
|
2020-09-19 01:08:56 +02:00
|
|
|
buttonText.innerText = 'Clear';
|
|
|
|
|
|
|
|
const buttonMainConsole = document.createElement('div');
|
2020-09-23 18:07:31 +02:00
|
|
|
buttonMainConsole.classList.add('clear');
|
2020-09-19 01:08:56 +02:00
|
|
|
buttonMainConsole.appendChild(buttonText);
|
|
|
|
buttonMainConsole.addEventListener('click', () => {
|
|
|
|
messageContainer.style.top = '-80%';
|
|
|
|
setTimeout(() => {
|
|
|
|
messageContainer.remove();
|
|
|
|
buttonMainConsole.remove();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-09-23 18:07:31 +02:00
|
|
|
//create content message
|
|
|
|
const messageCotent = document.createElement('div');
|
|
|
|
messageCotent.innerHTML = htmlMessage;
|
|
|
|
messageCotent.className = "content-message";
|
|
|
|
|
2020-09-19 01:08:56 +02:00
|
|
|
//add message container
|
|
|
|
const messageContainer = document.createElement('div');
|
|
|
|
messageContainer.id = this.getHtmlMessageId(messageId);
|
|
|
|
messageContainer.className = "message-container";
|
2020-09-23 18:07:31 +02:00
|
|
|
messageContainer.appendChild(messageCotent);
|
2020-09-19 01:08:56 +02:00
|
|
|
messageContainer.appendChild(buttonMainConsole);
|
2020-09-16 18:38:50 +02:00
|
|
|
|
|
|
|
const mainSectionDiv = HtmlUtils.getElementByIdOrFail<HTMLDivElement>('main-container');
|
2020-09-19 01:08:56 +02:00
|
|
|
mainSectionDiv.appendChild(messageContainer);
|
2020-09-16 18:38:50 +02:00
|
|
|
}
|
|
|
|
|
2020-10-01 14:11:34 +02:00
|
|
|
private stopMessage(messageId: string){
|
2020-09-16 18:38:50 +02:00
|
|
|
HtmlUtils.removeElementByIdOrFail<HTMLDivElement>(this.getHtmlMessageId(messageId));
|
|
|
|
}
|
|
|
|
|
2020-10-01 14:11:34 +02:00
|
|
|
private getHtmlMessageId(messageId: string) : string{
|
2020-09-16 18:38:50 +02:00
|
|
|
return `message-${messageId}`;
|
|
|
|
}
|
|
|
|
}
|