|
@@ -3,6 +3,7 @@ using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.Threading.Tasks;
|
|
using MafiaTelegramBot.Game;
|
|
using MafiaTelegramBot.Game;
|
|
using MafiaTelegramBot.Game.GameRooms;
|
|
using MafiaTelegramBot.Game.GameRooms;
|
|
|
|
+using MafiaTelegramBot.Models;
|
|
using MafiaTelegramBot.Resources;
|
|
using MafiaTelegramBot.Resources;
|
|
|
|
|
|
namespace MafiaTelegramBot.Controllers
|
|
namespace MafiaTelegramBot.Controllers
|
|
@@ -10,6 +11,7 @@ namespace MafiaTelegramBot.Controllers
|
|
public static class RoomController
|
|
public static class RoomController
|
|
{
|
|
{
|
|
private static readonly Dictionary<string, GameRoom> OpenedGames = new();
|
|
private static readonly Dictionary<string, GameRoom> OpenedGames = new();
|
|
|
|
+ private static readonly Dictionary<string, FastGameRoom> FastGames = new();
|
|
|
|
|
|
public static async Task<ResultCode> CreateNewGame(Player creator, string roomName, bool isExtended, bool isPrivate, bool isRanking)
|
|
public static async Task<ResultCode> CreateNewGame(Player creator, string roomName, bool isExtended, bool isPrivate, bool isRanking)
|
|
{
|
|
{
|
|
@@ -30,28 +32,95 @@ namespace MafiaTelegramBot.Controllers
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public static async Task<ResultCode> ConnectToFastGame(Player player)
|
|
|
|
+ {
|
|
|
|
+ return await Task.Run(async () =>
|
|
|
|
+ {
|
|
|
|
+ async Task<FastGameRoom> GetMax(List<FastGameRoom> rooms)
|
|
|
|
+ {
|
|
|
|
+ var maxIndex = 0;
|
|
|
|
+ for (var i = 0; i < rooms.Count; ++i)
|
|
|
|
+ {
|
|
|
|
+ var first = await rooms[maxIndex].GetCapacity();
|
|
|
|
+ var second = await rooms[i].GetCapacity();
|
|
|
|
+ if (first < second && !rooms[i].IsRunning) maxIndex = i;
|
|
|
|
+ }
|
|
|
|
+ return rooms[maxIndex];
|
|
|
|
+ }
|
|
|
|
+ FastGameRoom room;
|
|
|
|
+ if (FastGames.Count == 0)
|
|
|
|
+ {
|
|
|
|
+ room = new FastGameRoom { Owner = null, RoomName = "FastGame" + Utilities.Rnd.Next(10000) };
|
|
|
|
+ room.SetTimer();
|
|
|
|
+ FastGames.Add(RoomEncrypter.GetCode(room.RoomName), room);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ room = await GetMax(FastGames.Values.ToList());
|
|
|
|
+ if (room.IsRunning)
|
|
|
|
+ {
|
|
|
|
+ room = new FastGameRoom { Owner = null, RoomName = "FastGame" + Utilities.Rnd.Next(10000) };
|
|
|
|
+ room.SetTimer();
|
|
|
|
+ FastGames.Add(RoomEncrypter.GetCode(room.RoomName), room);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ player.SetRoomName(room.RoomName);
|
|
|
|
+ room.Players.Add(player.Id, player);
|
|
|
|
+ if (room.Players.Count > Constants.MEMORY_CLEANER_PLAYERS_COUNT) room.StopTimer();
|
|
|
|
+ await room.PlayersMessageChannel.SendExcept(player.Id, $"{player.NickName} {strings.connected_to_game}");
|
|
|
|
+ room.PlayersMessageChannel.AddPerson(player.ChatId);
|
|
|
|
+ await Bot.SendWithMarkdown2(player.ChatId, strings.you_connect_to_fast_game, Keyboard.PlayerGameMenu);
|
|
|
|
+ await Bot.SendHyperLink(player.ChatId, $"<a href='https://t.me/{AppSettings.Name}?start=fast_game={RoomEncrypter.GetCode(room.RoomName)}'>{strings.link}</a>");
|
|
|
|
+ if (room.Players.Count == Constants.PLAYERS_TO_START_FAST_GAME)
|
|
|
|
+ {
|
|
|
|
+ await room.Prepare();
|
|
|
|
+ room.Start();
|
|
|
|
+ }
|
|
|
|
+ return ResultCode.CodeOk;
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static async Task<ResultCode> ConnectToFastGame(Player player, string roomKey)
|
|
|
|
+ {
|
|
|
|
+ return await Task.Run(async () =>
|
|
|
|
+ {
|
|
|
|
+ var result = await ConnectToGame(player, roomKey);
|
|
|
|
+ if (result != ResultCode.CodeOk) return result;
|
|
|
|
+ var room = GetRoom(roomKey);
|
|
|
|
+ await Bot.SendWithMarkdown2(player.ChatId, strings.you_connect_to_fast_game, Keyboard.PlayerGameMenu);
|
|
|
|
+ await Bot.SendHyperLink(player.ChatId, $"<a href='https://t.me/{AppSettings.Name}?start=fast_game={RoomEncrypter.GetCode(room.RoomName)}'>{strings.link}</a>");
|
|
|
|
+ if (room.Players.Count == Constants.PLAYERS_TO_START_FAST_GAME)
|
|
|
|
+ {
|
|
|
|
+ await room.Prepare();
|
|
|
|
+ room.Start();
|
|
|
|
+ }
|
|
|
|
+ return ResultCode.CodeOk;
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
public static async Task<ResultCode> ConnectToGame(Player player, string roomKey)
|
|
public static async Task<ResultCode> ConnectToGame(Player player, string roomKey)
|
|
{
|
|
{
|
|
return await Task.Run(async () =>
|
|
return await Task.Run(async () =>
|
|
{
|
|
{
|
|
- if (!OpenedGames.ContainsKey(roomKey)) return ResultCode.RoomDoesNotExist;
|
|
|
|
|
|
+ var room = GetRoom(roomKey);
|
|
|
|
+ if (room == null) return ResultCode.RoomDoesNotExist;
|
|
var roomName = RoomEncrypter.GetName(roomKey);
|
|
var roomName = RoomEncrypter.GetName(roomKey);
|
|
- if (OpenedGames[roomKey].IsRunning) return ResultCode.GameAlreadyRunning;
|
|
|
|
- if (OpenedGames[roomKey].IsFilled()) return ResultCode.RoomIsFilled;
|
|
|
|
|
|
+ if (room.IsRunning) return ResultCode.GameAlreadyRunning;
|
|
|
|
+ if (room.IsFilled()) return ResultCode.RoomIsFilled;
|
|
if (!player.SetRoomName(roomName)) return ResultCode.UserAlreadyInGame;
|
|
if (!player.SetRoomName(roomName)) return ResultCode.UserAlreadyInGame;
|
|
- OpenedGames[roomKey].Players.Add(player.Id, player);
|
|
|
|
|
|
+ room.Players.Add(player.Id, player);
|
|
|
|
|
|
- if (OpenedGames[roomKey].Players.Count > Constants.MEMORY_CLEANER_PLAYERS_COUNT) OpenedGames[roomKey].StopTimer();
|
|
|
|
|
|
+ if (room.Players.Count > Constants.MEMORY_CLEANER_PLAYERS_COUNT) room.StopTimer();
|
|
|
|
|
|
- await OpenedGames[roomKey].PlayersMessageChannel.SendExcept(player.Id, $"{player.NickName} {strings.connected_to_game}");
|
|
|
|
- OpenedGames[roomKey].PlayersMessageChannel.AddPerson(player.ChatId);
|
|
|
|
|
|
+ await room.PlayersMessageChannel.SendExcept(player.Id, $"{player.NickName} {strings.connected_to_game}");
|
|
|
|
+ room.PlayersMessageChannel.AddPerson(player.ChatId);
|
|
return ResultCode.CodeOk;
|
|
return ResultCode.CodeOk;
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
public static List<Player> GetPlayers(string roomKey)
|
|
public static List<Player> GetPlayers(string roomKey)
|
|
{
|
|
{
|
|
- return OpenedGames[roomKey].Players.Values.ToList();
|
|
|
|
|
|
+ return GetRoom(roomKey).Players.Values.ToList();
|
|
}
|
|
}
|
|
|
|
|
|
public static async Task<ResultCode> LeaveFromGame(Player player)
|
|
public static async Task<ResultCode> LeaveFromGame(Player player)
|
|
@@ -61,13 +130,14 @@ namespace MafiaTelegramBot.Controllers
|
|
var roomName = player.GetRoomName();
|
|
var roomName = player.GetRoomName();
|
|
var roomKey = RoomEncrypter.GetCode(roomName);
|
|
var roomKey = RoomEncrypter.GetCode(roomName);
|
|
if (!player.RemoveGame()) return ResultCode.UserNotInGame;
|
|
if (!player.RemoveGame()) return ResultCode.UserNotInGame;
|
|
- await OpenedGames[roomKey].Leave(player);
|
|
|
|
- if (!OpenedGames[roomKey].IsRunning &&
|
|
|
|
- OpenedGames[roomKey].Players.Count <= Constants.MEMORY_CLEANER_INTERVAL)
|
|
|
|
- OpenedGames[roomKey].StartTimer();
|
|
|
|
- if (OpenedGames[roomKey].Players.Count >= 0) return ResultCode.CodeOk;
|
|
|
|
|
|
+ var room = GetRoom(roomKey);
|
|
|
|
+ if (room == null) return ResultCode.CodeOk;
|
|
|
|
+ await room.Leave(player);
|
|
|
|
+ if (!room.IsRunning && room.Players.Count <= Constants.MEMORY_CLEANER_INTERVAL) room.StartTimer();
|
|
|
|
+ if (room.Players.Count >= 0) return ResultCode.CodeOk;
|
|
RoomEncrypter.RemoveCode(roomName);
|
|
RoomEncrypter.RemoveCode(roomName);
|
|
OpenedGames.Remove(roomKey);
|
|
OpenedGames.Remove(roomKey);
|
|
|
|
+ FastGames.Remove(roomKey);
|
|
return ResultCode.CodeOk;
|
|
return ResultCode.CodeOk;
|
|
});
|
|
});
|
|
}
|
|
}
|
|
@@ -76,18 +146,20 @@ namespace MafiaTelegramBot.Controllers
|
|
{
|
|
{
|
|
await Task.Run(async () =>
|
|
await Task.Run(async () =>
|
|
{
|
|
{
|
|
- if (OpenedGames.ContainsKey(roomKey))
|
|
|
|
|
|
+ var room = GetRoom(roomKey);
|
|
|
|
+ if (room != null)
|
|
{
|
|
{
|
|
- foreach (var player in OpenedGames[roomKey].Players.Values)
|
|
|
|
|
|
+ foreach (var player in room.Players.Values)
|
|
{
|
|
{
|
|
- await OpenedGames[roomKey].PlayersMessageChannel.SendTo(player.Id, strings.room_dissolved,
|
|
|
|
|
|
+ await room.PlayersMessageChannel.SendTo(player.Id, strings.room_dissolved,
|
|
player.IsAdmin ? Keyboard.AdminMainMenu : Keyboard.MainMenu);
|
|
player.IsAdmin ? Keyboard.AdminMainMenu : Keyboard.MainMenu);
|
|
player.RemoveGame();
|
|
player.RemoveGame();
|
|
player.ResetState();
|
|
player.ResetState();
|
|
}
|
|
}
|
|
- RoomEncrypter.RemoveCode(OpenedGames[roomKey].RoomName);
|
|
|
|
- OpenedGames[roomKey].DeleteTimer();
|
|
|
|
|
|
+ RoomEncrypter.RemoveCode(room.RoomName);
|
|
|
|
+ room.DeleteTimer();
|
|
OpenedGames.Remove(roomKey);
|
|
OpenedGames.Remove(roomKey);
|
|
|
|
+ FastGames.Remove(roomKey);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}
|
|
@@ -138,7 +210,10 @@ namespace MafiaTelegramBot.Controllers
|
|
|
|
|
|
public static GameRoom GetRoom(string roomKey)
|
|
public static GameRoom GetRoom(string roomKey)
|
|
{
|
|
{
|
|
- return OpenedGames[roomKey];
|
|
|
|
|
|
+ return OpenedGames.ContainsKey(roomKey)
|
|
|
|
+ ? OpenedGames[roomKey]
|
|
|
|
+ : FastGames.ContainsKey(roomKey)
|
|
|
|
+ ? FastGames[roomKey] : null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|