RoomController.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using MafiaTelegramBot.DataBase;
  6. using MafiaTelegramBot.Game;
  7. using MafiaTelegramBot.Resources;
  8. namespace MafiaTelegramBot.Controllers
  9. {
  10. public static class RoomController
  11. {
  12. private static readonly Dictionary<string, GameRoom> OpenedGames = new();
  13. public static async Task<int> CreateNewGame(User owner, string roomName, bool extended, string secretPhrase = null)
  14. {
  15. var gameExists = await Task.Run(() => OpenedGames.ContainsKey(roomName));
  16. return await Task.Run(async () =>
  17. {
  18. if (gameExists) return Constants.GAME_EXISTS;
  19. if (!await owner.SetCurrentGame(roomName)) return Constants.USER_ALREADY_IN_GAME;
  20. var room = secretPhrase == null
  21. ? new GameRoom {Creator = owner, RoomName = roomName, IsExtended = extended}
  22. : new PrivateGameRoom {Creator = owner, RoomName = roomName, IsExtended = extended};
  23. OpenedGames.Add(secretPhrase ?? roomName, room);
  24. room.Players.Add(owner.Username, owner);
  25. return Constants.CODE_OK;
  26. });
  27. }
  28. public static async Task<int> ConnectToGame(User player, string roomKey)
  29. {
  30. return await Task.Run(async () =>
  31. {
  32. if (OpenedGames[roomKey].IsFilled()) return Constants.ROOM_IS_FILLED;
  33. if (! await player.SetCurrentGame(OpenedGames[roomKey].RoomName)) return Constants.USER_ALREADY_IN_GAME;
  34. OpenedGames[roomKey].Players.Add(player.Username, player);
  35. return Constants.CODE_OK;
  36. });
  37. }
  38. public static async Task<List<User>> GetPlayers(string roomKey)
  39. {
  40. return await Task.Run( ()=>
  41. {
  42. var room = OpenedGames[roomKey];
  43. var users = room.Players.Values.ToList();
  44. return users;
  45. });
  46. }
  47. public static async Task<int> LeaveFromGame(User player)
  48. {
  49. return await Task.Run(async () =>
  50. {
  51. var current = player.GetCurrentGame();
  52. if (current == "") return Constants.USER_NOT_IN_GAME;
  53. await player.RemoveGame();
  54. try
  55. {
  56. OpenedGames[current].Players.Remove(player.Username);
  57. if (OpenedGames[current].Players.Count == 0) OpenedGames.Remove(current);
  58. }
  59. catch (Exception)
  60. {
  61. var pass = RoomEncrypter.NameToCode(current);
  62. OpenedGames[pass].Players.Remove(player.Username);
  63. if (OpenedGames[pass].Players.Count == 0)
  64. {
  65. OpenedGames.Remove(pass);
  66. RoomEncrypter.Remove(current);
  67. }
  68. }
  69. return Constants.CODE_OK;
  70. });
  71. }
  72. public static async Task<List<GameRoom>> GetPublicRooms()
  73. {
  74. return await Task.Run(() =>
  75. {
  76. var rooms = OpenedGames.Values.ToList();
  77. List<GameRoom> publicRooms = new List<GameRoom>();
  78. int countRooms = 0;
  79. foreach (var room in rooms)
  80. {
  81. if (room.IsPrivate) continue;
  82. if (room.IsFilled()) continue;
  83. publicRooms.Add(room);
  84. countRooms++;
  85. if (countRooms == 10) break;
  86. }
  87. return publicRooms;
  88. });
  89. }
  90. public static async Task<GameRoom> GetRoom(string roomKey)
  91. {
  92. return await Task.Run( ()=>
  93. {
  94. var room = OpenedGames[roomKey];
  95. return room;
  96. });
  97. }
  98. }
  99. }