Utilities.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using MafiaTelegramBot.Models;
  5. using MafiaTelegramBot.Resources;
  6. using Telegram.Bot.Types;
  7. namespace MafiaTelegramBot
  8. {
  9. public static class Utilities
  10. {
  11. public static Task<Message> GetResultCodeMessage(ResultCode code, long chatId)
  12. {
  13. return code switch
  14. {
  15. ResultCode.TooManyPlayers => Bot.SendWithMarkdown2(chatId, strings.too_many_players),
  16. ResultCode.TooFewPlayers => Bot.SendWithMarkdown2(chatId, strings.too_few_players),
  17. ResultCode.GameAlreadyRunning => Bot.SendWithMarkdown2(chatId, strings.game_already_running),
  18. ResultCode.GameAlreadyExists => Bot.SendWithMarkdown2(chatId, strings.game_already_exists),
  19. ResultCode.UserAlreadyInGame => Bot.SendWithMarkdown2(chatId, strings.user_already_in_game),
  20. ResultCode.UserNotInGame => Bot.SendWithMarkdown2(chatId, strings.user_not_in_game),
  21. ResultCode.RoomIsFilled => Bot.SendWithMarkdown2(chatId, strings.room_is_filled),
  22. ResultCode.RoomDoesNotExist => Bot.SendWithMarkdown2(chatId, strings.room_does_not_exists),
  23. ResultCode.RolesNotEqualPlayers => Bot.SendWithMarkdown2(chatId, strings.players_not_equal_roles),
  24. _ => Bot.SendWithMarkdown2(chatId, strings.unexpected_error)
  25. };
  26. }
  27. public static async Task<string> ToMarkdownString(string src)
  28. {
  29. return src != null
  30. ? await Task.Run(()=>
  31. {
  32. var newStr = src
  33. .Replace(".", "\\.")
  34. .Replace("#", "\\#")
  35. .Replace("[", "\\[")
  36. .Replace("]", "\\]")
  37. .Replace("|", "\\|")
  38. .Replace("!", "\\!")
  39. .Replace("-", "\\-");
  40. return newStr;
  41. }) : "";
  42. }
  43. public static async Task<string> EscapeSpecific(string src)
  44. {
  45. return src != null
  46. ? await Task.Run(()=>
  47. {
  48. var newStr = src
  49. .Replace("_", "\\_")
  50. .Replace("*", "\\*")
  51. .Replace("(", "\\(")
  52. .Replace(")", "\\)")
  53. .Replace("~", "\\~")
  54. .Replace("`", "\\`")
  55. .Replace(">", "\\>")
  56. .Replace("+", "\\+")
  57. .Replace("=", "\\=")
  58. .Replace("{", "\\{")
  59. .Replace("}", "\\}")
  60. .Replace("@", "\\@")
  61. .Replace("'", "\\'");
  62. return newStr;
  63. }) :"";
  64. }
  65. }
  66. }