Beispiel #1
0
        private void ValidateFields(Forage forage, Result <Forage> result)
        {
            // No future dates.
            if (forage.Date > DateTime.Now)
            {
                result.AddMessage("Forage date cannot be in the future.");
            }

            if (forage.Kilograms <= 0M || forage.Kilograms > 250.0M)
            {
                result.AddMessage("Kilograms must be a positive number less than 250.0");
            }

            //can't have same forager+date+item...
            var query = forageRepository.FindByDate(forage.Date)
                        .Where(f => (f.Forager.Id == forage.Forager.Id) && (f.Item.Id == f.Item.Id))
                        .ToList();

            if (query.Count() > 0)
            {
                //... so update instead
                forage.Id         = query[0].Id;
                forage.Kilograms += query[0].Kilograms;

                forageRepository.Update(forage);
                result.AddMessage("A forage for this item & forager was already found for this date. Item amount updated.");
            }
        }
Beispiel #2
0
        public int Generate(DateTime start, DateTime end, int count)
        {
            if (start > end || count <= 0)
            {
                return(0);
            }

            count = Math.Min(count, 500);

            var dates = new List <DateTime>();

            while (start <= end)
            {
                dates.Add(start);
                start = start.AddDays(1);
            }

            List <Item>    items    = itemRepository.FindAll();
            List <Forager> foragers = foragerRepository.FindAll();
            Random         random   = new Random();

            for (int i = 0; i < count; i++)
            {
                Forage forage = new Forage();
                forage.Date      = dates[random.Next(dates.Count)];
                forage.Forager   = foragers[random.Next(foragers.Count)];
                forage.Item      = items[random.Next(items.Count)];
                forage.Kilograms = (decimal)(random.NextDouble() * 5.0 + 0.1);
                forageRepository.Add(forage);
            }

            return(count);
        }
        private void AddForage()
        {
            view.DisplayHeader(MainMenuOption.AddForage.ToLabel());
            Forager forager = GetForager();

            if (forager == null)
            {
                return;
            }
            Item item = GetItem();

            if (item == null)
            {
                return;
            }
            Forage          forage = view.MakeForage(forager, item);
            Result <Forage> result = forageService.Add(forage);

            if (!result.Success)
            {
                view.DisplayStatus(false, result.Messages);
            }
            else
            {
                string successMessage = $"Forage {result.Value.Id} created.";
                view.DisplayStatus(true, successMessage);
            }
        }
        public List <Forage> FindByDate(DateTime date)
        {
            var forages = new List <Forage>();
            var path    = GetFilePath(date);

            if (!File.Exists(path))
            {
                return(forages);
            }

            string[] lines = null;
            try
            {
                lines = File.ReadAllLines(path);
            }
            catch (IOException ex)
            {
                throw new RepositoryException("could not read forages", ex);
            }


            for (int i = 1; i < lines.Length; i++) // skip the header
            {
                string[] fields = lines[i].Split(",", StringSplitOptions.TrimEntries);
                Forage   forage = Deserialize(fields, date);
                if (forage != null)
                {
                    forages.Add(forage);
                }
            }
            return(forages);
        }
 private string Serialize(Forage item)
 {
     return(string.Format("{0},{1},{2},{3}",
                          item.Id,
                          item.Forager.Id,
                          item.Item.Id,
                          item.Kilograms));
 }
Beispiel #6
0
 public ForagedItem(Forage forageItem, int value, string description, bool givesDamage, bool isPsychodelic)
 {
     TypeofForage  = forageItem;
     Value         = value;
     Description   = description;
     GivesDamage   = givesDamage;
     IsPsychodelic = isPsychodelic; //if psychodelic, make a random choice?
 }
        public Forage Add(Forage forage)
        {
            List <Forage> all = FindByDate(forage.Date);

            forage.Id = Guid.NewGuid().ToString();
            all.Add(forage);
            Write(all, forage.Date);
            return(forage);
        }
        public ForageRepositoryDouble()
        {
            Forage forage = new Forage();

            forage.Id        = "498604db-b6d6-4599-a503-3d8190fda823";
            forage.Date      = date;
            forage.Forager   = ForagerRepositoryDouble.FORAGER;
            forage.Item      = ItemRepositoryDouble.ITEM;
            forage.Kilograms = 1.25M;
            forages.Add(forage);
        }
Beispiel #9
0
        public Forage MakeForage(Forager forager, Item item)
        {
            Forage forage = new Forage();

            forage.Forager = forager;
            forage.Item    = item;
            forage.Date    = io.ReadDate("Forage date [MM/dd/yyyy]: ");
            string message = $"Kilograms of {item.Name}: ";

            forage.Kilograms = io.ReadDecimal(message, 0.001M, 250.0M);
            return(forage);
        }
Beispiel #10
0
        private void ValidateChildrenExist(Forage forage, Result <Forage> result)
        {
            if (forage.Forager.Id == null ||
                foragerRepository.FindById(forage.Forager.Id) == null)
            {
                result.AddMessage("Forager does not exist.");
            }

            if (itemRepository.FindById(forage.Item.Id) == null)
            {
                result.AddMessage("Item does not exist.");
            }
        }
Beispiel #11
0
        public Result <Forage> Add(Forage forage)
        {
            Result <Forage> result = Validate(forage);

            if (!result.Success)
            {
                return(result);
            }

            result.Value = forageRepository.Add(forage);

            return(result);
        }
Beispiel #12
0
        public void ShouldAddInsteadOfDuplicatingSameForagerSameItem()
        {
            Forage forage = new Forage();

            forage.Date      = new DateTime(2020, 6, 26);
            forage.Forager   = ForagerRepositoryDouble.FORAGER;
            forage.Item      = ItemRepositoryDouble.ITEM;
            forage.Kilograms = 0.5M;

            Result <Forage> result = service.Add(forage);

            Assert.IsFalse(result.Success);
            //TODO: validate correct message
        }
        public bool Update(Forage forage)
        {
            List <Forage> all = FindByDate(forage.Date);

            for (int i = 0; i < all.Count; i++)
            {
                if (all[i].Id == forage.Id)
                {
                    all[i] = forage;
                    Write(all, forage.Date);
                    return(true);
                }
            }
            return(false);
        }
Beispiel #14
0
        public void ShouldNotAddWhenItemNotFound()
        {
            Item item = new Item(11, "Dandelion", Category.Edible, 0.05M);

            Forage forage = new Forage();

            forage.Date      = DateTime.Today;
            forage.Forager   = ForagerRepositoryDouble.FORAGER;
            forage.Item      = item;
            forage.Kilograms = 0.5M;

            Result <Forage> result = service.Add(forage);

            Assert.IsFalse(result.Success);
        }
Beispiel #15
0
        public void ShouldAdd()
        {
            Forage forage = new Forage();

            forage.Date      = DateTime.Today;
            forage.Forager   = ForagerRepositoryDouble.FORAGER;
            forage.Item      = ItemRepositoryDouble.ITEM;
            forage.Kilograms = 0.5M;

            Result <Forage> result = service.Add(forage);

            Assert.IsTrue(result.Success);
            Assert.NotNull(result.Value);
            Assert.AreEqual(36, result.Value.Id.Length);
        }
Beispiel #16
0
        private Result <Forage> Validate(Forage forage)
        {
            Result <Forage> result = ValidateNulls(forage);

            if (!result.Success)
            {
                return(result);
            }

            ValidateFields(forage, result);
            if (!result.Success)
            {
                return(result);
            }

            ValidateChildrenExist(forage, result);

            return(result);
        }
Beispiel #17
0
        public void ShouldNotAddWhenForagerNotFound()
        {
            Forager forager = new Forager();

            forager.Id        = "30816379-188d-4552-913f-9a48405e8c08";
            forager.FirstName = "Ermengarde";
            forager.LastName  = "Sansom";
            forager.State     = "NM";

            Forage forage = new Forage();

            forage.Date      = DateTime.Today;
            forage.Forager   = forager;
            forage.Item      = ItemRepositoryDouble.ITEM;
            forage.Kilograms = 0.5M;

            Result <Forage> result = service.Add(forage);

            Assert.IsFalse(result.Success);
        }
Beispiel #18
0
        public void ShouldAdd()
        {
            Forage forage = new Forage();

            forage.Date      = date;
            forage.Kilograms = 0.75M;

            Item item = new Item();

            item.Id     = 12;
            forage.Item = item;

            Forager forager = new Forager();

            forager.Id     = "AAAA-1111-2222-FFFF";
            forage.Forager = forager;

            forage = repository.Add(forage);

            Assert.AreEqual(36, forage.Id.Length);
        }
Beispiel #19
0
        private Result <Forage> ValidateNulls(Forage forage)
        {
            var result = new Result <Forage>();

            if (forage == null)
            {
                result.AddMessage("Nothing to save.");
                return(result);
            }

            if (forage.Forager == null)
            {
                result.AddMessage("Forager is required.");
            }

            if (forage.Item == null)
            {
                result.AddMessage("Item is required.");
            }

            return(result);
        }
Beispiel #20
0
 public void checkSpawnIntegrity()
 {
     for (int i = 0; i < Resource.Count; i++)
     {
         Resource[i].loadItem();
         if (!Resource[i].isValid)
         {
             Memory.instance.Monitor.Log("Resource Spawn invalid, removing.");
             Resource.RemoveAt(i);
             i--;
         }
     }
     for (int i = 0; i < Forage.Count; i++)
     {
         Forage[i].loadItem();
         if (!Forage[i].isValid)
         {
             Memory.instance.Monitor.Log("Forage Spawn invalid, removing.");
             Forage.RemoveAt(i);
             i--;
         }
     }
 }
        private Forage Deserialize(string[] fields, DateTime date)
        {
            if (fields.Length != 4)
            {
                return(null);
            }

            Forage result = new Forage();

            result.Id        = fields[0];
            result.Date      = date;
            result.Kilograms = decimal.Parse(fields[3]);

            Forager forager = new Forager();

            forager.Id     = fields[1];
            result.Forager = forager;

            Item item = new Item();

            item.Id     = int.Parse(fields[2]);
            result.Item = item;
            return(result);
        }
 public Forage Add(Forage forage)
 {
     forage.Id = Guid.NewGuid().ToString();
     forages.Add(forage);
     return(forage);
 }
 public bool Update(Forage forage)
 {
     return(false);
 }