You've already forked Nitro_Render_V3
mirror of
https://github.com/duckietm/Nitro_Render_V3.git
synced 2026-06-19 15:06:20 +00:00
80 lines
2.5 KiB
TypeScript
80 lines
2.5 KiB
TypeScript
import { IConnection, IRoomHandlerListener } from '@nitrots/api';
|
|
import { QuestionAnsweredEvent, QuestionEvent, QuestionFinishedEvent } from '@nitrots/communication';
|
|
import { GetEventDispatcher, RoomSessionWordQuizEvent } from '@nitrots/events';
|
|
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;
|
|
|
|
GetEventDispatcher().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;
|
|
|
|
GetEventDispatcher().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;
|
|
|
|
GetEventDispatcher().dispatchEvent(quizEvent);
|
|
}
|
|
}
|