From 1cedce0a76fbdcd9794a5ced5a6b0ccce43aaa9d Mon Sep 17 00:00:00 2001 From: dank074 Date: Mon, 15 Nov 2021 23:51:24 -0600 Subject: [PATCH] add session poll handler --- src/nitro/session/RoomSessionManager.ts | 2 + .../session/events/RoomSessionPollEvent.ts | 9 ++- src/nitro/session/handler/PollHandler.ts | 81 +++++++++++++++++++ 3 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 src/nitro/session/handler/PollHandler.ts diff --git a/src/nitro/session/RoomSessionManager.ts b/src/nitro/session/RoomSessionManager.ts index 6a9a8702..6b2817c7 100644 --- a/src/nitro/session/RoomSessionManager.ts +++ b/src/nitro/session/RoomSessionManager.ts @@ -5,6 +5,7 @@ import { IRoomEngine } from '../room/IRoomEngine'; import { RoomSessionEvent } from './events/RoomSessionEvent'; import { BaseHandler } from './handler/BaseHandler'; import { GenericErrorHandler } from './handler/GenericErrorHandler'; +import { PollHandler } from './handler/PollHandler'; import { RoomChatHandler } from './handler/RoomChatHandler'; import { RoomDataHandler } from './handler/RoomDataHandler'; import { RoomDimmerPresetsHandler } from './handler/RoomDimmerPresetsHandler'; @@ -79,6 +80,7 @@ export class RoomSessionManager extends NitroManager implements IRoomSessionMana new RoomPresentHandler(connection, this), new GenericErrorHandler(connection, this), new WordQuizHandler(connection, this), + new PollHandler(connection, this), ); } diff --git a/src/nitro/session/events/RoomSessionPollEvent.ts b/src/nitro/session/events/RoomSessionPollEvent.ts index 5f9dff0d..2dd84d36 100644 --- a/src/nitro/session/events/RoomSessionPollEvent.ts +++ b/src/nitro/session/events/RoomSessionPollEvent.ts @@ -1,4 +1,5 @@ -import { IRoomSession } from '../IRoomSession'; +import { PollQuestion } from '../../communication/messages/parser/poll/PollQuestion'; +import { IRoomSession } from '../IRoomSession'; import { RoomSessionEvent } from './RoomSessionEvent'; @@ -14,7 +15,7 @@ export class RoomSessionPollEvent extends RoomSessionEvent private _numQuestions: number = 0; private _startMessage: string = ''; private _endMessage: string = ''; - private _questionArray: string[] = null; + private _questionArray: PollQuestion[] = null; private _npsPoll: boolean = false; constructor(k: string, _arg_2: IRoomSession, _arg_3: number) @@ -79,12 +80,12 @@ export class RoomSessionPollEvent extends RoomSessionEvent this._endMessage = k; } - public get questionArray(): string[] + public get questionArray(): PollQuestion[] { return this._questionArray; } - public set questionArray(k: string[]) + public set questionArray(k: PollQuestion[]) { this._questionArray = k; } diff --git a/src/nitro/session/handler/PollHandler.ts b/src/nitro/session/handler/PollHandler.ts new file mode 100644 index 00000000..bc83b052 --- /dev/null +++ b/src/nitro/session/handler/PollHandler.ts @@ -0,0 +1,81 @@ +import { IConnection } from '../../../core/communication/connections/IConnection'; +import { PollContentsEvent } from '../../communication/messages/incoming/poll/PollContentsEvent'; +import { PollErrorEvent } from '../../communication/messages/incoming/poll/PollErrorEvent'; +import { PollOfferEvent } from '../../communication/messages/incoming/poll/PollOfferEvent'; +import { RoomSessionPollEvent } from '../events/RoomSessionPollEvent'; +import { IRoomHandlerListener } from '../IRoomHandlerListener'; +import { BaseHandler } from './BaseHandler'; + +export class PollHandler extends BaseHandler +{ + constructor(connection: IConnection, listener: IRoomHandlerListener) + { + super(connection, listener); + + connection.addMessageEvent(new PollContentsEvent(this.onPollContentsEvent.bind(this))); + connection.addMessageEvent(new PollOfferEvent(this.onPollOfferEvent.bind(this))); + connection.addMessageEvent(new PollErrorEvent(this.onPollErrorEvent.bind(this))); + } + + private onPollContentsEvent(event: PollContentsEvent): void + { + if(!this.listener) return; + + const session = this.listener.getSession(this.roomId); + + if(!session) return; + + const parser = event.getParser(); + + if(!parser) return; + + const pollEvent = new RoomSessionPollEvent(RoomSessionPollEvent.CONTENT, session, parser.id); + + pollEvent.startMessage = parser.startMessage; + pollEvent.endMessage = parser.endMessage; + pollEvent.numQuestions = parser.numQuestions; + pollEvent.questionArray = parser.questionArray; + pollEvent.npsPoll = parser.npsPoll; + + this.listener.events.dispatchEvent(pollEvent); + } + + private onPollOfferEvent(event: PollOfferEvent): void + { + if(!this.listener) return; + + const session = this.listener.getSession(this.roomId); + + if(!session) return; + + const parser = event.getParser(); + + if(!parser) return; + + const pollEvent = new RoomSessionPollEvent(RoomSessionPollEvent.OFFER, session, parser.id); + + pollEvent.summary = parser.headline; + pollEvent.summary = parser.summary; + + this.listener.events.dispatchEvent(pollEvent); + } + + private onPollErrorEvent(event: PollErrorEvent): void + { + if(!this.listener) return; + + const session = this.listener.getSession(this.roomId); + + if(!session) return; + + const parser = event.getParser(); + + if(!parser) return; + + const pollEvent = new RoomSessionPollEvent(RoomSessionPollEvent.ERROR, session, -1); + pollEvent.headline = '???'; + pollEvent.summary = '???'; + + this.listener.events.dispatchEvent(pollEvent); + } +}