12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- #nullable enable
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using MafiaTelegramBot.Controllers;
- using MafiaTelegramBot.DataBase.EntityDao;
- using MafiaTelegramBot.Game;
- using MafiaTelegramBot.Models.Commands.CustomMessageHandlers;
- using Telegram.Bot.Types;
- using Telegram.Bot.Types.Enums;
- namespace MafiaTelegramBot.Models.Commands
- {
- public abstract class Command : UpdateModel<string>
- {
- private static readonly Dictionary<string, Command> CustomHandlers = new()
- {
- {"UsersThatChangesNickname", new ChangeNicknameHandler()},
- {"UsersThatCreatesRoom", new CreateRoomHandler()},
- {"UserThatEntersPrivateCode", new EnterCodeHandler()},
- };
- protected override bool IsMatches(string command)
- {
- return command == Name;
- }
- public static async Task<Message> Update(Update update)
- {
- var chatId = update.Message.Chat.Id;
- await Bot.Get().SendChatActionAsync(chatId, ChatAction.Typing);
- var userId = update.Message.From.Id;
- var commands = Bot.Commands;
- var message = update.Message.Text;
- var startCommand = new StartCommand();
- //TODO refactor later
- if (message.Contains(startCommand.Name)) return await ((Command?) startCommand.Clone(chatId, userId))!.Execute(update);
- var user = await UserDao.GetPlayerById(userId);
- if (user.IsPlaying)
- {
- var roomKey = RoomEncrypter.GetCode(user.GetRoomName());
- var room = RoomController.GetRoom(roomKey);
- return await room.MHandler.Handle(update);
- }
- var command = FirstOrDefault(commands, message);
- if(command != null) return await ((Command?) command.Clone(chatId, userId))!.Execute(update);
- if (Bot.UsersThatChangesNickname.Remove(userId))
- return await ((Command?) CustomHandlers["UsersThatChangesNickname"].Clone(chatId, userId))!.Execute(update);
- if (Bot.UsersThatCreatesRoom.ContainsKey(userId))
- return await ((Command?) CustomHandlers["UsersThatCreatesRoom"].Clone(chatId, userId))!.Execute(update);
- if (Bot.UserThatEntersPrivateCode.Remove(userId))
- return await ((Command?) CustomHandlers["UserThatEntersPrivateCode"].Clone(chatId, userId))!.Execute(update);
- return await Bot.SendWithMarkdown2(chatId, $"{strings.command_not_found} _*\\({message}\\)*_");
- }
- }
- }
|