2020-10-19 19:32:47 +02:00
|
|
|
import * as TypeMessages from "./TypeMessage";
|
2021-01-25 14:10:16 +01:00
|
|
|
import {Banned} from "./TypeMessage";
|
2021-03-01 17:47:00 +01:00
|
|
|
import {adminMessagesService} from "../Connexion/AdminMessagesService";
|
2020-10-19 19:32:47 +02:00
|
|
|
|
2020-10-19 21:04:16 +02:00
|
|
|
export interface TypeMessageInterface {
|
|
|
|
showMessage(message: string): void;
|
|
|
|
}
|
2020-10-19 19:32:47 +02:00
|
|
|
|
2021-03-01 17:47:00 +01:00
|
|
|
class UserMessageManager {
|
2020-10-19 19:32:47 +02:00
|
|
|
|
2020-10-19 21:04:16 +02:00
|
|
|
typeMessages: Map<string, TypeMessageInterface> = new Map<string, TypeMessageInterface>();
|
2021-03-01 17:47:00 +01:00
|
|
|
receiveBannedMessageListener!: Function;
|
2020-10-19 19:32:47 +02:00
|
|
|
|
2021-03-01 17:47:00 +01:00
|
|
|
constructor() {
|
2020-10-19 21:04:16 +02:00
|
|
|
const valueTypeMessageTab = Object.values(TypeMessages);
|
2020-10-19 19:32:47 +02:00
|
|
|
Object.keys(TypeMessages).forEach((value: string, index: number) => {
|
2020-10-19 21:04:16 +02:00
|
|
|
const typeMessageInstance: TypeMessageInterface = (new valueTypeMessageTab[index]() as TypeMessageInterface);
|
2020-10-19 19:32:47 +02:00
|
|
|
this.typeMessages.set(value.toLowerCase(), typeMessageInstance);
|
|
|
|
});
|
2021-01-25 14:10:16 +01:00
|
|
|
|
2021-03-01 17:47:00 +01:00
|
|
|
adminMessagesService.messageStream.subscribe((event) => {
|
|
|
|
const typeMessage = this.showMessage(event.type, event.text);
|
2021-01-25 14:10:16 +01:00
|
|
|
if(typeMessage instanceof Banned) {
|
2021-03-01 17:47:00 +01:00
|
|
|
this.receiveBannedMessageListener();
|
2021-01-25 14:10:16 +01:00
|
|
|
}
|
2021-03-01 17:47:00 +01:00
|
|
|
})
|
2020-10-19 19:32:47 +02:00
|
|
|
}
|
|
|
|
|
2020-10-19 21:04:16 +02:00
|
|
|
showMessage(type: string, message: string) {
|
|
|
|
const classTypeMessage = this.typeMessages.get(type.toLowerCase());
|
|
|
|
if (!classTypeMessage) {
|
2020-10-19 19:32:47 +02:00
|
|
|
console.error('Message unknown');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
classTypeMessage.showMessage(message);
|
2021-01-25 14:10:16 +01:00
|
|
|
return classTypeMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
setReceiveBanListener(callback: Function){
|
2021-03-01 17:47:00 +01:00
|
|
|
this.receiveBannedMessageListener = callback;
|
2020-10-19 19:32:47 +02:00
|
|
|
}
|
2021-03-01 17:47:00 +01:00
|
|
|
}
|
|
|
|
export const userMessageManager = new UserMessageManager()
|