Beispiel #1
0
        public async Task <ServiceResponse <AttackResultDTO> > SpellAttack(SpellAttackDTO attack)
        {
            var response = new ServiceResponse <AttackResultDTO>();
            var attacker = await context.Characters
                           .Include(c => c.CharacterSpells).ThenInclude(cs => cs.Spell)
                           .FirstOrDefaultAsync(c => c.Id == attack.AttackerId);

            var opponent = await context.Characters
                           .FirstOrDefaultAsync(c => c.Id == attack.OpponentId);

            var characterSpell = attacker.CharacterSpells.FirstOrDefault(cs => cs.Spell.Id == attack.SpellId);

            if (characterSpell != null)
            {
                int damage = DoSpellAttack(attacker, opponent, characterSpell);

                if (opponent.HitPoint <= 0)
                {
                    response.Message = $"{opponent.Name} has been defeatead";
                }

                context.Characters.Update(opponent);
                await context.SaveChangesAsync();

                response.Data = new AttackResultDTO
                {
                    Attacker   = attacker.Name,
                    AttackerHP = attacker.HitPoint,
                    Opponent   = opponent.Name,
                    OpponentHP = opponent.HitPoint,
                    Damage     = damage
                };
            }
            else
            {
                response.Message = $"{attacker.Name} doesn't know that skill";
                response.Success = false;
            }


            return(response);
        }
Beispiel #2
0
 public async Task <IActionResult> Spell(SpellAttackDTO attack)
 {
     return(Ok(await this.fightService.SpellAttack(attack)));
 }