tgd-telegram-service/src/bot/scenes/quiz.scene.ts
2024-11-21 00:18:19 +04:00

103 lines
3 KiB
TypeScript

import { Command, Ctx, Message, On, Scene, SceneEnter } from 'nestjs-telegraf';
import { QUIZ_SCENE } from './scenes.const';
import { GlobalCommands } from '../global-commands';
import {Inject, Logger} from '@nestjs/common';
import { Messages } from '../tg.text';
import {Context} from "../context.interface";
import AppConsts from "../../constants";
import {ClientProxy} from "@nestjs/microservices";
import {TGHandlers} from "../handlers";
@Scene(QUIZ_SCENE)
export class QuizScene {
private readonly logger = new Logger(QuizScene.name);
constructor(
private globalCmd: GlobalCommands,
@Inject(AppConsts.GameServiceName) private gameService: ClientProxy,
) {}
@SceneEnter()
async onSceneEnter(@Ctx() ctx: Context, @Message('text') text: string) {
if (ctx.session.__scenes.state.hasOwnProperty('answering')) {
return this.onText(text, ctx);
}
await ctx.reply(
'Ответы на вопросы будут появляться туть, кто первый тот победил',
);
}
@Command('leave')
async onLeaveScene(@Ctx() ctx: Context) {
await ctx.scene.leave();
}
@Command('cards')
async onCardCommand(@Ctx() ctx: Context) {
this.logger.verbose(`cards command (quiz)`);
this.gameService.emit({ cmd: 'GetCards'}, { user: ctx.from.id, inline: false});
}
@Command('next')
async onNextCommand(@Ctx() ctx: Context) {
if(ctx.from.id === 11178819) {
this.gameService.emit({ cmd: 'CompleteQueue'}, { user: ctx.from.id })
}
}
@Command('start')
async onCommandStart(@Ctx() ctx: Context) {
await ctx.scene.leave();
this.globalCmd.printCommands(ctx);
}
@On('callback_query')
async onInlineQuery(@Ctx() ctx: Context) {
try {
await TGHandlers.handleCallback(ctx, this.logger, this.gameService);
}catch (e) {
}
}
@On('text')
async onText(@Message('text') text: string, @Ctx() ctx: Context) {
console.log(text);
//console.log(JSON.stringify(ctx));
if(text.startsWith('/')) {
await ctx.scene.leave();
return;
}
if (text.includes(Messages.EMOJI_CARD)) {
this.gameService.emit({ cmd: 'CardPlayed'}, { text, user: ctx.message.from.id })
return;
}
if (text.includes(Messages.EMOJI_PLAYER)) {
this.gameService.emit({
cmd: 'PLayerSelected'
}, {
text, user: ctx.message.from.id
});
return;
}
this.logger.verbose(`Answer from: ${ctx.message.from.first_name}, validating`);
this.gameService.send(
{ cmd: 'ValidateAnswer'},
{ answer: text, user: ctx.message.from.id, name: ctx.message.from.first_name })
.subscribe(
(res) => {
if(res.valid) {
ctx.replyWithMarkdownV2('Верно', {
reply_markup: {
remove_keyboard: true,
},
});
return;
}
ctx.replyWithMarkdownV2('Ответ неверный', {
reply_markup: {
remove_keyboard: true,
},
});
}
)
}
}