added wordquiz handler

This commit is contained in:
dank074 2021-11-15 20:28:08 -06:00
parent 2cc9598141
commit e6d4d2a8f2
4 changed files with 90 additions and 5 deletions

View File

@ -77,7 +77,7 @@ export class QuestionParser implements IMessageParser
}
}
interface IQuestion
export interface IQuestion
{
id: number;
number: number;

View File

@ -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),
);
}

View File

@ -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<string, number>;
@ -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;
}

View File

@ -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);
}
}