Exemple #1
0
        public async Task DisconnectPlayerFromRoomAsync(int roomId, string playerId)
        {
            var room = await _roomsRepository.GetRoomByIdAsync(roomId);

            if (room == null)
            {
                throw new Exception($"Room {roomId} does not exist");
            }

            var user = await _userManager.FindByIdAsync(playerId);

            if (user == null)
            {
                throw new Exception($"User {playerId} does not exist");
            }

            user.RoomId = null;
            room.CurrentPlayersCount--;

            if (room.Status)
            {
                Game game = _gamesRepository.GetGameById(roomId);
                if (game.Players.Contains(playerId))
                {
                    game.Players.ToList().Remove(user.Id);
                    _gamesRepository.UpdateGame(game);
                }
            }

            if (room.CurrentPlayersCount == 0)
            {
                await _roomsRepository.DeleteRoomAsync(room.Id);
            }
            else
            {
                await _roomsRepository.UpdateRoomAsync(room);
            }

            await _userManager.UpdateAsync(user);
        }