public async Task <MatchmakerResponse> GetMatchDataAsync(string playerServiceId, int warshipId) { //Данные для окна ожидания боя MatchmakerResponse response = new MatchmakerResponse { NumberOfPlayersInQueue = queueSingletonService.GetNumberOfPlayers(), NumberOfPlayersInBattles = unfinishedMatchesService.GetNumberOfPlayersInBattles() }; //Игрок в очереди? if (queueSingletonService.Contains(playerServiceId)) { Console.WriteLine("PlayerInQueue"); response.PlayerInQueue = true; return(response); } //Игрок в бою? else if (unfinishedMatchesService.IsPlayerInMatch(playerServiceId)) { Console.WriteLine("IsPlayerInMatch"); BattleRoyaleMatchModel matchModel = unfinishedMatchesService.GetMatchModel(playerServiceId); response.PlayerInBattle = true; response.MatchModel = new BattleRoyaleClientMatchModel(matchModel, playerServiceId); return(response); } //Добавить в очередь else { Console.WriteLine("TryEnqueuePlayerAsync"); bool success = await queueExtenderService.TryEnqueuePlayerAsync(playerServiceId, warshipId); if (!success) { throw new Exception("Не удалось добавить игрока в очередь."); } response.PlayerHasJustBeenRegistered = true; return(response); } }
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; }