From e6d4d2a8f22113060faec8a60c6610da5a8dd49e Mon Sep 17 00:00:00 2001 From: dank074 Date: Mon, 15 Nov 2021 20:28:08 -0600 Subject: [PATCH] added wordquiz handler --- .../messages/parser/poll/QuestionParser.ts | 2 +- src/nitro/session/RoomSessionManager.ts | 2 + .../events/RoomSessionWordQuizEvent.ts | 9 +- src/nitro/session/handler/WordQuizHandler.ts | 82 +++++++++++++++++++ 4 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 src/nitro/session/handler/WordQuizHandler.ts diff --git a/src/nitro/communication/messages/parser/poll/QuestionParser.ts b/src/nitro/communication/messages/parser/poll/QuestionParser.ts index 735768dd..24b76899 100644 --- a/src/nitro/communication/messages/parser/poll/QuestionParser.ts +++ b/src/nitro/communication/messages/parser/poll/QuestionParser.ts @@ -77,7 +77,7 @@ export class QuestionParser implements IMessageParser } } -interface IQuestion +export interface IQuestion { id: number; number: number; diff --git a/src/nitro/session/RoomSessionManager.ts b/src/nitro/session/RoomSessionManager.ts index d1e54169..6a9a8702 100644 --- a/src/nitro/session/RoomSessionManager.ts +++ b/src/nitro/session/RoomSessionManager.ts @@ -12,6 +12,7 @@ import { RoomPermissionsHandler } from './handler/RoomPermissionsHandler'; import { RoomPresentHandler } from './handler/RoomPresentHandler'; import { RoomSessionHandler } from './handler/RoomSessionHandler'; import { RoomUsersHandler } from './handler/RoomUsersHandler'; +import { WordQuizHandler } from './handler/WordQuizHandler'; import { IRoomHandlerListener } from './IRoomHandlerListener'; import { IRoomSession } from './IRoomSession'; import { IRoomSessionManager } from './IRoomSessionManager'; @@ -77,6 +78,7 @@ export class RoomSessionManager extends NitroManager implements IRoomSessionMana new RoomUsersHandler(connection, this), new RoomPresentHandler(connection, this), new GenericErrorHandler(connection, this), + new WordQuizHandler(connection, this), ); } diff --git a/src/nitro/session/events/RoomSessionWordQuizEvent.ts b/src/nitro/session/events/RoomSessionWordQuizEvent.ts index b287188e..9498cb1b 100644 --- a/src/nitro/session/events/RoomSessionWordQuizEvent.ts +++ b/src/nitro/session/events/RoomSessionWordQuizEvent.ts @@ -1,4 +1,5 @@ -import { IRoomSession } from '../IRoomSession'; +import { IQuestion } from '../../communication/messages/parser/poll/QuestionParser'; +import { IRoomSession } from '../IRoomSession'; import { RoomSessionEvent } from './RoomSessionEvent'; export class RoomSessionWordQuizEvent extends RoomSessionEvent @@ -12,7 +13,7 @@ export class RoomSessionWordQuizEvent extends RoomSessionEvent private _pollId: number = -1; private _questionId: number = -1; private _duration: number = -1; - private _question: string[] = null; + private _question: IQuestion = null; private _userId: number = -1; private _value: string; private _answerCounts: Map; @@ -69,12 +70,12 @@ export class RoomSessionWordQuizEvent extends RoomSessionEvent this._duration = k; } - public get question(): string[] + public get question(): IQuestion { return this._question; } - public set question(k: string[]) + public set question(k: IQuestion) { this._question = k; } diff --git a/src/nitro/session/handler/WordQuizHandler.ts b/src/nitro/session/handler/WordQuizHandler.ts new file mode 100644 index 00000000..57a1544e --- /dev/null +++ b/src/nitro/session/handler/WordQuizHandler.ts @@ -0,0 +1,82 @@ +import { IConnection } from '../../../core/communication/connections/IConnection'; +import { QuestionAnsweredEvent } from '../../communication/messages/incoming/poll/QuestionAnsweredEvent'; +import { QuestionEvent } from '../../communication/messages/incoming/poll/QuestionEvent'; +import { QuestionFinishedEvent } from '../../communication/messages/incoming/poll/QuestionFinishedEvent'; +import { RoomSessionWordQuizEvent } from '../events/RoomSessionWordQuizEvent'; +import { IRoomHandlerListener } from '../IRoomHandlerListener'; +import { BaseHandler } from './BaseHandler'; + +export class WordQuizHandler extends BaseHandler +{ + constructor(connection: IConnection, listener: IRoomHandlerListener) + { + super(connection, listener); + + connection.addMessageEvent(new QuestionEvent(this.onQuestionEvent.bind(this))); + connection.addMessageEvent(new QuestionAnsweredEvent(this.onQuestionAnsweredEvent.bind(this))); + connection.addMessageEvent(new QuestionFinishedEvent(this.onQuestionFinishedEvent.bind(this))); + } + + private onQuestionEvent(event: QuestionEvent): void + { + if(!this.listener) return; + + const session = this.listener.getSession(this.roomId); + + if(!session) return; + + const parser = event.getParser(); + + if(!parser) return; + + const quizEvent = new RoomSessionWordQuizEvent(RoomSessionWordQuizEvent.QUESTION, session, parser.pollId); + + quizEvent.question = parser.question; + quizEvent.duration = parser.duration; + quizEvent.pollType = parser.pollType; + quizEvent.questionId = parser.questionId; + quizEvent.pollId = parser.pollId; + + this.listener.events.dispatchEvent(quizEvent); + } + + private onQuestionAnsweredEvent(event: QuestionAnsweredEvent): void + { + if(!this.listener) return; + + const session = this.listener.getSession(this.roomId); + + if(!session) return; + + const parser = event.getParser(); + + if(!parser) return; + + const quizEvent = new RoomSessionWordQuizEvent(RoomSessionWordQuizEvent.ANSWERED, session, parser.userId); + + quizEvent.value = parser.value; + quizEvent.userId = parser.userId; + quizEvent.answerCounts = parser.answerCounts; + + this.listener.events.dispatchEvent(quizEvent); + } + + private onQuestionFinishedEvent(event: QuestionFinishedEvent): void + { + if(!this.listener) return; + + const session = this.listener.getSession(this.roomId); + + if(!session) return; + + const parser = event.getParser(); + + if(!parser) return; + + const quizEvent = new RoomSessionWordQuizEvent(RoomSessionWordQuizEvent.FINISHED, session); + quizEvent.questionId = parser.questionId; + quizEvent.answerCounts = parser.answerCounts; + + this.listener.events.dispatchEvent(quizEvent); + } +}