Exemplo n.º 1
0
        public async Task <VoidAppResult> Attack(Guid gameId, DtoAttackMove move, CancellationToken cancellationToken = default(CancellationToken))
        {
            var game = await this.dbContext.Games.SingleOrDefaultAsync(_ => _.Id == gameId, cancellationToken);

            var attacker = await this.dbContext.GameCards.IncludeCards().SingleOrDefaultAsync(_ => _.Id == move.AttackerId);

            var target = await this.dbContext.GameCards.IncludeCards().SingleOrDefaultAsync(_ => _.Id == move.TargetId);

            if (game == null || attacker == null || target == null)
            {
                return(VoidAppResult.Error(ErrorPreset.OnLoadingData));
            }

            var attackerDtoCardResult = await this.GetDtoCardsFromGameCards(new List <GameCard> {
                attacker
            }, gameId);

            if (attackerDtoCardResult.IsErrorResult)
            {
                return(attackerDtoCardResult.GetVoidAppResult());
            }
            if (!attackerDtoCardResult.SuccessReturnValue.First().CanAttack)
            {
                return(VoidAppResult.Error(AppActionResultType.RuleError, "This card has already attacked in this turn."));
            }

            var dbMove = new Move
            {
                Date           = DateTime.Now,
                Game           = game,
                Type           = MoveType.Attack,
                SourceEntity   = attacker,
                TargetEntities = new List <MoveTargetEntity>
                {
                    new MoveTargetEntity
                    {
                        Entity = target
                    }
                }
            };

            target.Health   -= attacker.Card.Attack + attacker.StatModifiersCount;
            attacker.Health -= target.Card.Attack + target.StatModifiersCount;

            if (attacker.Health <= 0)
            {
                attacker.CardLocation = CardLocation.OutOfPlay;
            }

            if (target.Health <= 0)
            {
                target.CardLocation = CardLocation.OutOfPlay;
            }

            await this.dbContext.Moves.AddAsync(dbMove, cancellationToken);

            await this.dbContext.SaveChangesAsync(cancellationToken);

            return(VoidAppResult.Success());
        }
        public async Task <IActionResult> Attack(Guid gameId, [FromBody] DtoAttackMove move)
        {
            var result = await this.gamesService.Attack(gameId, move).ConfigureAwait(false);

            return(this.utility.GetActionResult(result));
        }