Example #1
0
        internal void UpdateHerdSelectedState(string herdId, bool newState)
        {
            HerdEntity entity = Herds.Single(x => x.HerdId == herdId);

            entity.Selected = newState;
            grangerSimpleDb.Save();
            OnHerdsModified?.Invoke(this, new EventArgs());
        }
Example #2
0
        internal void DeleteHerd(string herdName)
        {
            HerdEntity herd = Herds.Single(x => x.HerdId == herdName);

            CreatureEntity[] creaturesInThisHerd = Creatures.Where(x => x.Herd == herdName).ToArray();
            foreach (var creatureEntity in creaturesInThisHerd)
            {
                grangerSimpleDb.Creatures.Remove(creatureEntity.Id);
            }
            grangerSimpleDb.Herds.Remove(herd.HerdId);
            grangerSimpleDb.Save();
            OnHerdsModified?.Invoke(this, new EventArgs());
        }
Example #3
0
        public void InsertHerd(string herdName)
        {
            HerdEntity newHerd = new HerdEntity {
                HerdId = herdName
            };

            if (grangerSimpleDb.Herds.ContainsKey(herdName))
            {
                throw new ApplicationException("There already exists a herd with id " + herdName);
            }
            grangerSimpleDb.Herds[herdName] = newHerd;
            grangerSimpleDb.Save();
            OnHerdsModified?.Invoke(this, new EventArgs());
        }
Example #4
0
        /// <exception cref="DuplicateCreatureIdentityException"></exception>
        internal void MergeHerds(string sourceHerdName, string destinationHerdName)
        {
            HerdEntity sourceHerd      = Herds.Single(x => x.HerdId == sourceHerdName);
            HerdEntity destinationHerd = Herds.Single(x => x.HerdId == destinationHerdName);

            CreatureEntity[] creaturesInSource      = Creatures.Where(x => x.Herd == sourceHerd.HerdId).ToArray();
            CreatureEntity[] creaturesInDestination = Creatures.Where(x => x.Herd == destinationHerd.HerdId).ToArray();

            List <CreatureEntity> nonUniqueIdentityCreatures = new List <CreatureEntity>();

            foreach (var sourceCreatures in creaturesInSource)
            {
                foreach (var destinationCreature in creaturesInDestination)
                {
                    if (sourceCreatures.IsUniquelyIdentifiableWhenComparedTo(destinationCreature) == false)
                    {
                        nonUniqueIdentityCreatures.Add(sourceCreatures);
                    }
                }
            }

            if (nonUniqueIdentityCreatures.Any())
            {
                throw new DuplicateCreatureIdentityException("target herd: "
                                                             + destinationHerd.HerdId
                                                             + " already contains creature(s) of same identity: "
                                                             + string.Join(", ", nonUniqueIdentityCreatures));
            }
            else
            {
                foreach (var creature in creaturesInSource)
                {
                    creature.Herd = destinationHerd.HerdId;
                }
                grangerSimpleDb.Herds.Remove(sourceHerd.HerdId);
                grangerSimpleDb.Save();
                OnHerdsModified?.Invoke(this, new EventArgs());
            }
        }
Example #5
0
        internal void RenameHerd(string renamingHerd, string newHerdName)
        {
            if (grangerSimpleDb.Herds.ContainsKey(newHerdName))
            {
                throw new ApplicationException("There already exists a herd with id " + newHerdName);
            }

            HerdEntity oldherd = Herds.Single(x => x.HerdId == renamingHerd);

            HerdEntity newHerd = oldherd.CloneMe(newHerdName);

            newHerd.HerdId = newHerdName;
            grangerSimpleDb.Herds[newHerdName] = newHerd;

            List <CreatureEntity> creaturesInThisHerd = Creatures.Where(x => x.Herd == renamingHerd).ToList();

            creaturesInThisHerd.ForEach(x => x.Herd = newHerdName);

            grangerSimpleDb.Herds.Remove(oldherd.HerdId);
            grangerSimpleDb.Save();
            OnHerdsModified?.Invoke(this, new EventArgs());
        }