Beispiel #1
0
        public async Task <Tuple <bool, List <string> > > FightAsync(CharacterDto character, MonsterDto monster, QuestDto quest)
        {
            var log = new List <string>();

            _random = new Random();
            if (character.Sickness > DateTime.Now)
            {
                var diff = Math.Abs(Math.Floor((character.Sickness - DateTime.Now).Value.TotalSeconds));
                log.Add($"You can't fight at the moment. You are too tired and will not be able to fight for {diff} seconds.");
                return(new Tuple <bool, List <string> >(false, log));
            }

            if (character.CurrentHp <= 0)
            {
                log.Add("You can't fight at the moment. You should get some rest, because you are too devasteted.");
                return(new Tuple <bool, List <string> >(false, log));
            }

            var characterAttr = character.CalculateAttributes();

            characterAttr.Hp = character.CurrentHp / 10; //setting hp to current value
            var combat = Task.Run(() => Combat(characterAttr, monster.Attributes, log));
            var result = await combat;

            using (var uow = UnitOfWorkProvider.Create())
            {
                var updatedUser = new CharacterUpdateDto()
                {
                    Name        = character.Name,
                    CurrentHp   = result.Item2,
                    Experience  = character.Experience,
                    Gold        = character.Gold,
                    Id          = character.Id,
                    SkillPoints = character.SkillPoints,
                    Sickness    = DateTime.Now.AddMinutes(10)
                };
                if (result.Item1)
                {
                    updatedUser.Experience += quest.Experience;
                    updatedUser.Gold       += quest.Gold;
                }

                await _characterService.Update(updatedUser);

                await uow.Commit();
            }
            return(new Tuple <bool, List <string> >(result.Item1, log));
        }
Beispiel #2
0
        public async Task <Tuple <bool, List <string> > > FightAsync(CharacterDto character, CharacterDto character2)
        {
            var log = new List <string>();

            _random = new Random();
            if (character.Sickness > DateTime.Now)
            {
                var diff = Math.Abs(Math.Floor((character.Sickness - DateTime.Now).Value.TotalSeconds));
                log.Add($"You can't fight at the moment. You are too tired and will not be able to fight for {diff} seconds.");
                return(new Tuple <bool, List <string> >(false, log));
            }

            if (character.CurrentHp <= 0)
            {
                log.Add("You can't fight at the moment. You should get some rest, because you are too devasteted.");
                return(new Tuple <bool, List <string> >(false, log));
            }

            if (character2.CurrentHp <= 0)
            {
                log.Add("It wouldn't be very nice if you attacked your opponent right now. He needs to lick his wounds first.");
                return(new Tuple <bool, List <string> >(false, log));
            }


            var player1 = character.CalculateAttributes();
            var player2 = character2.CalculateAttributes();

            player1.Hp = character.CurrentHp / 10; //setting hp to current value
            player2.Hp = character.CurrentHp / 10; //setting hp to current value
            var combat = Task.Run(() => Combat(player1, player2, log));
            var result = await combat;

            using (var uow = UnitOfWorkProvider.Create())
            {
                var updatedUser1 = new CharacterUpdateDto()
                {
                    Name        = character.Name,
                    CurrentHp   = result.Item2,
                    Experience  = character.Experience,
                    Gold        = character.Gold,
                    Id          = character.Id,
                    SkillPoints = character.SkillPoints,
                    Sickness    = DateTime.Now.AddMinutes(10)
                };

                var updatedUser2 = new CharacterUpdateDto()
                {
                    Name        = character2.Name,
                    CurrentHp   = result.Item3,
                    Experience  = character2.Experience,
                    Gold        = character2.Gold,
                    Id          = character2.Id,
                    SkillPoints = character2.SkillPoints,
                    Sickness    = character2.Sickness
                };

                if (result.Item1)
                {
                    updatedUser2.Gold -= (int)Math.Floor((float)updatedUser2.Gold / 100) * 10;
                    updatedUser1.Gold += (int)Math.Floor((float)updatedUser2.Gold / 100) * 10;
                }
                else
                {
                    updatedUser1.Gold -= (int)Math.Floor((float)updatedUser1.Gold / 100) * 10;
                    updatedUser2.Gold += (int)Math.Floor((float)updatedUser1.Gold / 100) * 10;
                }

                await _characterService.Update(updatedUser1);

                await _characterService.Update(updatedUser2);

                await uow.Commit();
            }
            return(new Tuple <bool, List <string> >(result.Item1, log));
        }