|
@@ -1,4 +1,3 @@
|
|
|
-using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Threading.Tasks;
|
|
@@ -12,30 +11,31 @@ namespace MafiaTelegramBot.Controllers
|
|
|
{
|
|
|
private static readonly Dictionary<string, GameRoom> OpenedGames = new();
|
|
|
|
|
|
- public static async Task<int> CreateNewGame(User owner, string roomName, bool extended, string secretPhrase = null)
|
|
|
+ public static async Task<ResultCode> 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;
|
|
|
+ if (gameExists) return ResultCode.GameAlreadyExists;
|
|
|
+ if (!await owner.SetRoomKey(secretPhrase ?? roomName)) return ResultCode.UserAlreadyInGame;
|
|
|
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;
|
|
|
+ return ResultCode.CodeOk;
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- public static async Task<int> ConnectToGame(User player, string roomKey)
|
|
|
+ public static async Task<ResultCode> 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;
|
|
|
+ if (OpenedGames[roomKey].IsFilled()) return ResultCode.RoomIsFilled;
|
|
|
+ if (OpenedGames.ContainsKey(roomKey)) return ResultCode.RoomIsFilled;
|
|
|
+ if (! await player.SetRoomKey(OpenedGames[roomKey].RoomName)) return ResultCode.UserAlreadyInGame;
|
|
|
OpenedGames[roomKey].Players.Add(player.Username, player);
|
|
|
- return Constants.CODE_OK;
|
|
|
+ return ResultCode.CodeOk;
|
|
|
});
|
|
|
}
|
|
|
|
|
@@ -49,29 +49,15 @@ namespace MafiaTelegramBot.Controllers
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- public static async Task<int> LeaveFromGame(User player)
|
|
|
+ public static async Task<ResultCode> 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;
|
|
|
+ var roomKey = player.GetRoomKey();
|
|
|
+ if (!await player.RemoveGame()) return ResultCode.UserNotInGame;
|
|
|
+ OpenedGames[roomKey].Players.Remove(player.Username);
|
|
|
+ if (OpenedGames[roomKey].Players.Count == 0) OpenedGames.Remove(roomKey);
|
|
|
+ return ResultCode.CodeOk;
|
|
|
});
|
|
|
}
|
|
|
|