partey_workadventure/front/src/Administration/UserMessageManager.ts

43 lines
1.5 KiB
TypeScript
Raw Normal View History

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