public void CanGenerate()
        {
            using (var env = new TestEnvironment())
            {
                var service = env.GetService <IEncounterService>();

                var option = new EncounterOption
                {
                    PartyLevel = 1,
                    PartySize  = 5
                };

                var result = service.GenerateAsync(option).Result;

                result.ShouldNotBeNull();
            }
        }
예제 #2
0
        public void LoadEncounterStep(int pStep)
        {
            if (pStep == -1)
            {
                EndActiveEncounter();
            }
            if (activeEncounter == null)
            {
                return;
            }

            // Clear the old options
            foreach (RectTransform r in ActionUIParentRect.GetComponentsInChildren <RectTransform>())
            {
                if (r != ActionUIParentRect)
                {
                    Destroy(r.gameObject);
                }
            }

            MainText.text = activeEncounter.Steps[pStep].Text;

            for (int i = 0; i < activeEncounter.Steps[pStep].Options.Count; i++)
            {
                EncounterOption option = activeEncounter.Steps[pStep].Options[i];

                // spawn the option object
                // Instantiate a mercenary ui object in the container with the data at i.
                GameObject n = Instantiate(ActionUIObject, ActionUIParentRect.transform);
                n.GetComponent <EncounterOptionUIObject>().Initialize(option);
                RectTransform rect = n.GetComponent <RectTransform>();

                rect.anchoredPosition = new Vector2(rect.localPosition.x,
                                                    (ActionUIParentRect.rect.yMax - ActionUIObject.GetComponent <RectTransform>().rect.height / 2) - i * (rect.rect.height + 3));

                // Set up click listener
                n.GetComponent <Button>().onClick.AddListener(() => {
                    LoadEncounterStep(option.Action.Success(ActiveMercenary.Stats) ? option.SuccessStepIndex : option.FailureStepIndex);
                });
            }
        }
예제 #3
0
        private static void ValidateOption(EncounterOption option)
        {
            var exceptions = new List <ServiceException>();

            if (option == null)
            {
                exceptions.Add(new ServiceException(ServiceException.EntityNotFoundException));
            }
            if (option.PartyLevel == 0)
            {
                exceptions.Add(new ServiceException(ServiceException.RequiredValidation, "partyLevel"));
            }
            if (option.PartySize == 0)
            {
                exceptions.Add(new ServiceException(ServiceException.RequiredValidation, "partySize"));
            }

            if (exceptions.Any())
            {
                throw new ServiceAggregateException(exceptions);
            }
        }
        public void CanThrowException()
        {
            using (var env = new TestEnvironment())
            {
                var service = env.GetService <IEncounterService>();

                var option = new EncounterOption
                {
                    PartyLevel   = 1,
                    PartySize    = 1,
                    MonsterTypes = new List <MonsterType> {
                        MonsterType.Dragon
                    },
                    Difficulty = Difficulty.Easy
                };

                Should.Throw <ServiceException>(async() =>
                {
                    await service.GenerateAsync(option);
                });
            }
        }
        public void CanFilterWithMonsteryTypes()
        {
            using (var env = new TestEnvironment())
            {
                var service = env.GetService <IEncounterService>();

                var option = new EncounterOption
                {
                    PartyLevel   = 4,
                    MonsterTypes = new List <MonsterType> {
                        MonsterType.Beast, MonsterType.Humanoid
                    },
                    PartySize = 5
                };

                var result = service.GenerateAsync(option).Result;

                result.ShouldNotBeNull();
                result.Encounters.ShouldNotBeNull();
                result.Encounters.Any(e => !option.MonsterTypes.Select(m => m.ToString().ToLower()).Any(e.Type.ToLower().Equals)).ShouldBeFalse();
            }
        }
예제 #6
0
        public async Task <EncounterModel> GenerateAsync(EncounterOption option)
        {
            ValidateOption(option);
            PartyLevel = option.PartyLevel;
            PartySize  = option.PartySize;
            XpList     = new List <KeyValuePair <Difficulty, int> >()
            {
                new KeyValuePair <Difficulty, int>(Difficulty.Easy, _thresholds[PartyLevel, 0] * PartySize),
                new KeyValuePair <Difficulty, int>(Difficulty.Medium, _thresholds[PartyLevel, 1] * PartySize),
                new KeyValuePair <Difficulty, int>(Difficulty.Hard, _thresholds[PartyLevel, 2] * PartySize),
                new KeyValuePair <Difficulty, int>(Difficulty.Deadly, _thresholds[PartyLevel, 3] * PartySize)
            };
            try
            {
                Monsters = new List <Monster>(MonsterLoader.Instance.MonsterList);
                var sumxp      = CalcSumXP((int)Difficulty.Deadly);
                var monsterXps = new SortedSet <int>();

                if (option.MonsterTypes.Any())
                {
                    var selectedMonsters = option.MonsterTypes.Select(m => m.ToString().ToLower());
                    Monsters = Monsters.Where(m => selectedMonsters.Any(m.Type.ToLower().Equals)).ToList();
                }

                if (option.Difficulty.HasValue)
                {
                    sumxp = _thresholds[option.PartyLevel, (int)option.Difficulty.Value] * option.PartySize;
                }

                foreach (var monster in Monsters)
                {
                    monsterXps.Add(_challengeRatingXP[_challengeRating.IndexOf(monster.Challenge_Rating)]);
                }

                CheckPossible(sumxp, monsterXps);

                var result       = new EncounterModel();
                var maxTryNumber = 5000;
                await Task.Run(() =>
                {
                    while (result.Encounters.Count < option.Count && maxTryNumber > 0)
                    {
                        try
                        {
                            result.Encounters.Add(GetMonster(option.Difficulty));
                            maxTryNumber--;
                        }
                        catch (ServiceException)
                        {
                            maxTryNumber--;
                            continue;
                        }
                        catch (Exception)
                        {
                            throw;
                        }
                    }
                });

                result.Encounters.RemoveAll(ed => ed == null); // cleanup if needed
                result.SumXp = result.Encounters.Sum(ed => ed.XP);

                return(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Generate encounter failed.");
                throw new ServiceException(ex.Message, ex);
            }
        }
 public void Initialize(EncounterOption Data)
 {
     Text.text = Data.Text;
 }
예제 #8
0
 public async Task <EncounterModel> Generate([FromBody] EncounterOption option)
 {
     return(await _encounterService.GenerateAsync(option));
 }