public async Task <TViewModel> GetCurrentHeroResourcesViewModel <TViewModel>()
        {
            ResourcePouch resourcePouch = await this.GetResourcePouch();

            TViewModel viewModel = this.mapper.Map <TViewModel>(resourcePouch);

            return(viewModel);
        }
        public async Task <int> GetResource(string resourceName, int id = 0)
        {
            ResourcePouch resources = await this.GetResourcePouch(id);

            int currentAmount = (int)typeof(ResourcePouch).GetProperty(resourceName).GetValue(resources);

            return(currentAmount);
        }
Пример #3
0
        private async Task<string> DetermineFishingVessel(int id = 0)
        {
            ResourcePouch resources = await this.resourcePouchService.GetResourcePouch(id);

            return resources.Submarines > 0 ? ResourceNames.Submarines
                : resources.Ships > 0 ? ResourceNames.Ships
                : resources.Boats > 0 ? ResourceNames.Boats
                : string.Empty;
        }
        public async Task IncreaseResource(string resource, int amount, int id = 0)
        {
            ResourcePouch resources = await this.GetResourcePouch(id);

            int currentAmount = (int)typeof(ResourcePouch).GetProperty(resource).GetValue(resources);

            typeof(ResourcePouch).GetProperty(resource).SetValue(resources, currentAmount + amount);

            await this.context.SaveChangesAsync();
        }
        public async Task <ResourcePouch> GetResourcePouch(int id = 0)
        {
            if (id == 0)
            {
                id = (await this.heroService.GetHero()).ResourcePouchId;
            }

            ResourcePouch resourcePouch = await this.context.ResourcePouches.FindAsync(id);

            return(resourcePouch);
        }
        public async Task DecreaseResource(string resourceName, int amount, int id = 0)
        {
            ResourcePouch resources = await this.GetResourcePouch(id);

            this.CheckIfHeroHasEnoughResource(resources, resourceName, amount);

            int currentAmount = (int)typeof(ResourcePouch).GetProperty(resourceName).GetValue(resources);

            typeof(ResourcePouch).GetProperty(resourceName).SetValue(resources, currentAmount - amount);

            await this.context.SaveChangesAsync();
        }
        private void CheckIfHeroHasEnoughResource(ResourcePouch resources, string resourceName, int amount)
        {
            int currentAmount = (int)typeof(ResourcePouch).GetProperty(resourceName).GetValue(resources);

            if (currentAmount < amount)
            {
                throw new FarmHeroesException(
                          string.Format(ResourcePouchExceptionMessages.NotEnoughResourceMessage, this.SeparateWords(resourceName)),
                          string.Format(ResourcePouchExceptionMessages.NotEnoughResourceInstruction, this.SeparateWords(resourceName)),
                          string.Empty,
                          true);
            }
        }