Example #1
0
        public async Task <ActionResult <ApiResultViewModel <OneToOneBattleViewModel> > > StartRandomBattle([FromRoute] long accountId, [FromBody] OneToOneBattleInputModel inputModel, CancellationToken cancellationToken)
        {
            var currentPlayer = await _dataContext.Accounts.FirstOrDefaultAsync(q => q.Id == accountId, cancellationToken);

            if (currentPlayer == null)
            {
                return(BadRequest("invalid_accountId", "Player1 account not found"));
            }

            var activebattle = await _dataContext.OneToOneBattles.Where(x =>
                                                                        x.IsArchived == false && x.WinnerPlayerId == null &&
                                                                        (x.Player1Id == accountId || x.Player2Id == accountId)).ToListAsync(cancellationToken: cancellationToken);

            if (activebattle.Count() > 5)
            {
                return(BadRequest("Too many Active battle", "you can just allow to have 5 active battle"));
            }



            OneToOneBattle battle;

            if (inputModel.BattleInvitationId != null)
            {
                var battleInvitation = await _dataContext.BattleInvitations.FirstOrDefaultAsync(q => q.Id == inputModel.BattleInvitationId, cancellationToken);

                if (battleInvitation == null)
                {
                    return(BadRequest("invalid_battleInvitationId", "Invitation not found."));
                }

                battleInvitation.Status = BattleInvitationStatus.Accepted;


                var inviterPlayerStats = await _dataContext.AccountStatsSummaries.FirstAsync(q => q.IsArchived == false &&
                                                                                             q.AccountId == battleInvitation.InviterAccountId, cancellationToken);

                inviterPlayerStats.SuccessfulBattleInvitationsCount += 1;

                var account = await _accountManager.GetAsync(accountId, cancellationToken);

                await _achievementService.ProcessSuccessfulBattleInvitationAchievements(battleInvitation, inviterPlayerStats, cancellationToken);

                await _notificationManager.SaveAsync(
                    new Core.Entities.Notification
                {
                    AccountId          = accountId,
                    Body               = $"Accept Battle By  {account.Email}",
                    NotificationTypeId = NotificationTypeIds.General,
                    Title              = "Accept battle"
                }, cancellationToken);

                await _dataContext.SaveChangesAsync(cancellationToken);

                var inviterPlayer = await _dataContext.Accounts.FirstOrDefaultAsync(q => q.Id == battleInvitation.InviterAccountId, cancellationToken);

                battle = await _gamingService.StartRandomBattleAsync(inviterPlayer, currentPlayer, cancellationToken);
            }
            else
            {
                battle = await _gamingService.StartRandomBattleAsync(currentPlayer, null, cancellationToken);
            }

            return(OkData(await _battleMapper.MapAsync(battle, cancellationToken)));
        }