public async Task <MatchResultDto> ReadMatchResultAsync(int matchId, string playerServiceId) { MatchResult matchResult = await dbContext.MatchResults .Include(matchResult1 => matchResult1.Warship) .ThenInclude(warship => warship.WarshipType) .Include(matchResult1 => matchResult1.Transaction) .ThenInclude(resource => resource.Increments) .Include(matchResult1 => matchResult1.Transaction) .ThenInclude(resource => resource.Decrements) .SingleOrDefaultAsync(matchResult1 => matchResult1.MatchId == matchId && matchResult1.Warship.Account.ServiceId == playerServiceId); //Такой матч существует? if (matchResult == null) { throw new NullReferenceException(nameof(matchResult)); return(null); } //Результат игрока записан? if (!matchResult.IsFinished) { throw new Exception("Игрок не закончил этот матч"); return(null); } int currentWarshipRating = await warshipRatingReaderService.ReadWarshipRatingAsync(matchResult.WarshipId); var lootboxPoints = new Dictionary <MatchRewardTypeEnum, int>(); if (matchResult.Transaction.Increments.Count == 0) { throw new Exception("Игрок ничего не заработал за бой"); } foreach (Increment increment in matchResult.Transaction.Increments) { if (increment.IncrementTypeId == IncrementTypeEnum.LootboxPoints) { if (increment.MatchRewardTypeId != null) { lootboxPoints.Add(increment.MatchRewardTypeId.Value, increment.Amount); } } } int warshipRatingIncrement = matchResult.Transaction.Increments .Where(increment => increment.IncrementTypeId == IncrementTypeEnum.WarshipRating) .Sum(increment => increment.Amount); int warshipRatingDecrement = matchResult.Transaction.Decrements .Where(decrement => decrement.DecrementTypeId == DecrementTypeEnum.WarshipRating) .Sum(decrement => decrement.Amount); int matchRatingDelta = warshipRatingIncrement - warshipRatingDecrement; MatchResultDto matchResultDto = new MatchResultDto { CurrentWarshipRating = currentWarshipRating, MatchRatingDelta = matchRatingDelta, LootboxPoints = lootboxPoints, SkinName = matchResult.Warship.WarshipType.Name }; return(matchResultDto); }
public async Task<bool> UpdatePlayerMatchResultInDbAsync(int accountId, int placeInBattle, int matchId) { Console.WriteLine($"placeInBattle = "+placeInBattle); Account account = await dbContext.Accounts.FindAsync(accountId); if (account == null) { throw new Exception("Аккаунта не существует"); } bool isPlayerInMatch = unfinishedMatchesSingletonService.IsPlayerInMatch(account.ServiceId, matchId); if (!isPlayerInMatch) { Console.WriteLine("Этот игрок не в бою UpdatePlayerMatchResultInDbAsync"); return false; } //Достать пустой результат боя из БД MatchResult matchResult = await dbContext.MatchResults .Where(matchResult1 => matchResult1.MatchId == matchId && matchResult1.Warship.AccountId == accountId) .SingleAsync(); //Прочитать текущий рейтинг корабля. Он нужен для вычисления награды за бой. int currentWarshipRating = await warshipRatingReaderService.ReadWarshipRatingAsync(matchResult.WarshipId); //Вычислить награду за бой MatchReward matchReward = battleRoyaleMatchRewardCalculatorService .Calculate(placeInBattle, currentWarshipRating); //Обновить место в бою matchResult.PlaceInMatch = placeInBattle; //ОБновить ресурсы var increments = new List<Increment>(); var decrements = new List<Decrement>(); if (matchReward.SoftCurrency > 0) { increments.Add( new Increment { Amount = matchReward.SoftCurrency, IncrementTypeId = IncrementTypeEnum.SoftCurrency, MatchRewardTypeId = MatchRewardTypeEnum.RankingReward }); } if (matchReward.LootboxPoints > 0) { increments.Add( new Increment { Amount = matchReward.LootboxPoints, IncrementTypeId = IncrementTypeEnum.LootboxPoints, MatchRewardTypeId = MatchRewardTypeEnum.RankingReward }); } if (matchReward.WarshipRatingDelta > 0) { increments.Add(new Increment { Amount = matchReward.WarshipRatingDelta, IncrementTypeId = IncrementTypeEnum.WarshipRating, WarshipId = matchResult.WarshipId, MatchRewardTypeId = MatchRewardTypeEnum.RankingReward }); } else if(matchReward.WarshipRatingDelta < 0) { decrements.Add(new Decrement { DecrementTypeId = DecrementTypeEnum.WarshipRating, WarshipId = matchResult.WarshipId, Amount = Math.Abs(matchReward.WarshipRatingDelta) }); } Transaction transaction = new Transaction { WasShown = false, DateTime = DateTime.UtcNow, Increments = increments, Decrements = decrements, TransactionTypeId = TransactionTypeEnum.MatchReward, AccountId = accountId }; matchResult.Transaction = transaction; //Пометить, что игрок окончил бой matchResult.IsFinished = true; //Сохранить результат боя в БД await dbContext.SaveChangesAsync(); //Удалить игрока из памяти bool success = unfinishedMatchesSingletonService.TryRemovePlayerFromMatch(account.ServiceId); if (!success) { throw new Exception("Не удалось удалить игрока из матча "); } return true; }