123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using MafiaTelegramBot.DataBase;
- using MafiaTelegramBot.Game;
- using MafiaTelegramBot.Resources;
- namespace MafiaTelegramBot.Controllers
- {
- public static class RoomController
- {
- private static readonly Dictionary<string, GameRoom> OpenedGames = new();
- public static async Task<int> CreateNewGame(User owner, string roomName, bool extended, string secretPhrase = null)
- {
- var gameExists = await Task.Run(() => OpenedGames.ContainsKey(roomName));
- return await Task.Run(async () =>
- {
- if (gameExists) return Constants.GAME_EXISTS;
- if (!await owner.SetCurrentGame(roomName)) return Constants.USER_ALREADY_IN_GAME;
- var room = secretPhrase == null
- ? new GameRoom {Creator = owner, RoomName = roomName, IsExtended = extended}
- : new PrivateGameRoom {Creator = owner, RoomName = roomName, IsExtended = extended};
- OpenedGames.Add(secretPhrase ?? roomName, room);
- room.Players.Add(owner.Username, owner);
- return Constants.CODE_OK;
- });
- }
- public static async Task<int> ConnectToGame(User player, string roomKey)
- {
- return await Task.Run(async () =>
- {
- if (OpenedGames[roomKey].IsFilled()) return Constants.ROOM_IS_FILLED;
- if (! await player.SetCurrentGame(OpenedGames[roomKey].RoomName)) return Constants.USER_ALREADY_IN_GAME;
- OpenedGames[roomKey].Players.Add(player.Username, player);
- return Constants.CODE_OK;
- });
- }
- public static async Task<List<User>> GetPlayers(string roomKey)
- {
- return await Task.Run( ()=>
- {
- var room = OpenedGames[roomKey];
- var users = room.Players.Values.ToList();
- return users;
- });
- }
-
- public static async Task<int> LeaveFromGame(User player)
- {
- return await Task.Run(async () =>
- {
- var current = player.GetCurrentGame();
- if (current == "") return Constants.USER_NOT_IN_GAME;
- await player.RemoveGame();
- try
- {
- OpenedGames[current].Players.Remove(player.Username);
- if (OpenedGames[current].Players.Count == 0) OpenedGames.Remove(current);
- }
- catch (Exception)
- {
- var pass = RoomEncrypter.NameToCode(current);
- OpenedGames[pass].Players.Remove(player.Username);
- if (OpenedGames[pass].Players.Count == 0)
- {
- OpenedGames.Remove(pass);
- RoomEncrypter.Remove(current);
- }
- }
- return Constants.CODE_OK;
- });
- }
- public static async Task<List<GameRoom>> GetPublicRooms()
- {
- return await Task.Run(() =>
- {
- var rooms = OpenedGames.Values.ToList();
- List<GameRoom> publicRooms = new List<GameRoom>();
- int countRooms = 0;
- foreach (var room in rooms)
- {
- if (room.IsPrivate) continue;
- if (room.IsFilled()) continue;
- publicRooms.Add(room);
- countRooms++;
- if (countRooms == 10) break;
- }
- return publicRooms;
- });
- }
-
- public static async Task<GameRoom> GetRoom(string roomKey)
- {
- return await Task.Run( ()=>
- {
- var room = OpenedGames[roomKey];
- return room;
- });
- }
- }
- }
|