Esempio n. 1
0
        private async Task TryRemoveFromExerciseArea(Hamster hamster, int activityId)
        {
            await Task.Run(() =>
            {
                using (HamsterDbContext hamsterDb = new HamsterDbContext())
                {
                    var theExerciseArea = hamsterDb.ExerciseAreas.First();

                    var theHamster = hamsterDb.Hamsters.Where(h => h.Id == hamster.Id).First();

                    hamster.ExerciseAreaId = null;
                    theExerciseArea.CageSize--;

                    var addCageActivityLogTask = CreateAddActivityLog(activityId, theHamster.Id);

                    theHamster.CageId = hamsterDb.Cages.Where(c => c.CageSize < 3 && c.CageSize > 0 &&
                                                              c.Hamsters.First().IsFemale == theHamster.IsFemale ||
                                                              c.CageSize == 0)
                                        .Select(c => c.Id).First();

                    var cage = hamsterDb.Cages.Where(c => c.Id == theHamster.CageId).First();

                    cage.CageSize++;

                    hamsterDb.SaveChanges();
                }
            });
        }
Esempio n. 2
0
        private async Task PickUpFromCages()
        {
            using (HamsterDbContext hamsterDb = new HamsterDbContext())
            {
                var taskOne = Task.Run(() => NullifyHamsters());
                var taskTwo = Task.Run(async() =>
                {
                    var hamsters   = hamsterDb.Hamsters.ToList();
                    var activityId = hamsterDb.Activities.Where(a => a.ActivityName == "Departure")
                                     .Select(a => a.Id).Single();
                    var taskList = new List <Task>();

                    for (int i = 0; i < hamsters.Count(); i++)
                    {
                        var addActivityLogTask = CreateAddActivityLog(activityId, hamsters[i].Id);
                        taskList.Add(addActivityLogTask);
                    }

                    await Task.WhenAll(taskList);
                    hamsterDb.SaveChanges();
                });

                var taskArray = new Task[] { taskOne, taskTwo };
                await Task.WhenAll(taskArray);
            }
        }
Esempio n. 3
0
 private async Task CreateAddSimulation()
 {
     await Task.Run(() =>
     {
         using (HamsterDbContext hamsterDb = new HamsterDbContext())
         {
             hamsterDb.Simulations.Add(new Simulation {
                 Name = "Temp"
             });
             hamsterDb.SaveChanges();
             var currentSimulation  = hamsterDb.Simulations.OrderBy(s => s.Id).Last();
             currentSimulation.Name = $"Simulation {currentSimulation.Id}- " + DateTime.Now.ToString();
             currentSimulationId    = currentSimulation.Id;
             hamsterDb.SaveChanges();
         }
     });
 }
Esempio n. 4
0
        private async Task ArrivalOfHamsters()
        {
            using (HamsterDbContext hamsterDb = new HamsterDbContext())
            {
                var hamstersWithNoCage = hamsterDb.Hamsters.ToList();
                hamstersWithNoCage = hamstersWithNoCage.Shuffle().ToList();
                var cages = hamsterDb.Cages.Include(h => h.Hamsters).ToList();

                var arrivalActivityId = hamsterDb.Activities.Where(a => a.ActivityName == "Arrived")
                                        .Select(a => a.Id).SingleOrDefault();
                var cageActivityId = hamsterDb.Activities.Where(a => a.ActivityName == "Cage")
                                     .Select(a => a.Id).SingleOrDefault();

                DateTime tempCurrentSimDate = new DateTime();
                tempCurrentSimDate = currentSimulationDate;

                var taskList = new List <Task>();

                for (int i = 0; i < hamstersWithNoCage.Count(); i++)
                {
                    hamstersWithNoCage[i].CageId = cages.Where(c => c.CageSize < 3 && c.CageSize > 0 &&
                                                               c.Hamsters.First().IsFemale == hamstersWithNoCage[i].IsFemale ||
                                                               c.CageSize == 0)
                                                   .Select(c => c.Id).FirstOrDefault();

                    var cage = cages.Where(c => c.Id == hamstersWithNoCage[i].CageId).Single();

                    cage.CageSize++;
                    hamstersWithNoCage[i].CheckedInTime = tempCurrentSimDate;

                    hamsterDb.SaveChanges();

                    var addArrivalActivityLogTask = CreateAddActivityLog(arrivalActivityId, hamstersWithNoCage[i].Id);
                    taskList.Add(addArrivalActivityLogTask);

                    var addCageActivityLogTask = CreateAddActivityLog(cageActivityId, hamstersWithNoCage[i].Id);
                    taskList.Add(addCageActivityLogTask);
                }

                await Task.WhenAll(taskList);

                hamsterDb.SaveChanges();
            }
        }
Esempio n. 5
0
 private async Task NullHamster()
 {
     await Task.Run(() =>
     {
         using (HamsterDbContext hamsterDb = new HamsterDbContext())
         {
             hamsterDb.Database.ExecuteSqlRaw("EXEC NullHamsterForNewSimulation"); // calling a stored procedure made in EF with migration
             hamsterDb.SaveChanges();
         }
     });
 } // calling stored procedure
Esempio n. 6
0
 private async Task CreateAddActivityLog(int activityId, int hamsterId)
 {
     await Task.Run(() =>
     {
         using (HamsterDbContext hamsterDb = new HamsterDbContext())
         {
             hamsterDb.ActivityLogs.Add(new ActivityLog {
                 ActivityId   = activityId, HamsterId = hamsterId,
                 SimulationId = currentSimulationId, TimeStamp = currentSimulationDate
             });
             hamsterDb.SaveChanges();
         }
     });
 }
Esempio n. 7
0
        } // calling stored procedure

        private async Task NullCageSize <T>(T cageType)
        {
            await Task.Run(() =>
            {
                using (HamsterDbContext hamsterDb = new HamsterDbContext())
                {
                    if (cageType.GetType() == typeof(Cage))
                    {
                        hamsterDb.Database.ExecuteSqlRaw("EXEC NullCageCageSize"); // calling a stored procedure made in EF with migration
                    }
                    else if (cageType.GetType() == typeof(ExerciseArea))
                    {
                        hamsterDb.Database.ExecuteSqlRaw("EXEC NullExerciseAreaCageSize"); // calling a stored procedure made in EF with migration
                    }
                    hamsterDb.SaveChanges();
                }
            });
        } // calling stored procedure
Esempio n. 8
0
        private async Task MoveToExerciseArea()
        {
            using (HamsterDbContext hamsterDb = new HamsterDbContext())
            {
                var exerciseArea = hamsterDb.ExerciseAreas.First();
                var activityId   = hamsterDb.Activities.Where(a => a.ActivityName == "Cage")
                                   .Select(a => a.Id).First();

                if (exerciseArea.CageSize != 0)
                {
                    var hamstersInExerciseArea = hamsterDb.Hamsters.Where(h => h.ExerciseAreaId != null).Include(h => h.ExerciseArea).ToList();

                    TimeSpan timeUntilEndOfDay = new DateTime(currentSimulationDate.Year, currentSimulationDate.Month,
                                                              currentSimulationDate.Day, 17, 0, 0) - currentSimulationDate;

                    TimeSpan timeSpan = currentSimulationDate - hamstersInExerciseArea.First().LastExerciseTime.Value;

                    if (timeSpan.TotalMinutes >= 60)
                    {
                        if (timeUntilEndOfDay.TotalMinutes >= 60 || timeUntilEndOfDay.TotalMinutes == 6)
                        {
                            for (int i = 0; i < hamstersInExerciseArea.Count(); i++)
                            {
                                await TryRemoveFromExerciseArea(hamstersInExerciseArea[i], activityId);
                            }
                            hamsterDb.SaveChanges();

                            await TryAddToExerciseArea();
                        }
                    }
                }
                else
                {
                    await TryAddToExerciseArea();
                }
            }
        }
Esempio n. 9
0
        private async Task TryAddToExerciseArea()
        {
            using (HamsterDbContext hamsterDb = new HamsterDbContext())
            {
                TimeSpan timeUntilEndOfDay = new DateTime(currentSimulationDate.Year, currentSimulationDate.Month,
                                                          currentSimulationDate.Day, 17, 0, 0) - currentSimulationDate;

                if (timeUntilEndOfDay.TotalMinutes != 6)
                {
                    var exerciseArea = hamsterDb.ExerciseAreas.First();
                    var activityId   = hamsterDb.Activities.Where(a => a.ActivityName == "Exercise")
                                       .Select(a => a.Id).First();

                    var hamstersNotInExerciseArea = hamsterDb.Hamsters.Where(c => c.ExerciseAreaId == null)
                                                    .OrderBy(t => t.LastExerciseTime)
                                                    .ThenBy(t => t.LastExerciseTime.HasValue)
                                                    .Select(c => c).ToList();

                    List <Hamster> hamstersToAdd = new List <Hamster>();

                    var hamstersNeverExercised = hamstersNotInExerciseArea.Where(t => t.LastExerciseTime.HasValue == false).ToList();

                    var amountNeverExercised = 0;
                    if (hamstersNeverExercised != null)
                    {
                        amountNeverExercised = hamstersNeverExercised.Count();
                    }
                    else
                    {
                        amountNeverExercised = 0;
                    }

                    if (amountNeverExercised == hamsterDb.Hamsters.Count())
                    {
                        hamstersNeverExercised = hamstersNeverExercised.Shuffle().ToList();
                        hamstersToAdd          = hamstersNeverExercised.Where(c => c.IsFemale == hamstersNeverExercised.First().IsFemale)
                                                 .Take(6).ToList();
                    }
                    else
                    {
                        hamstersToAdd = hamstersNotInExerciseArea.Where(c => c.IsFemale == hamstersNotInExerciseArea.First().IsFemale)
                                        .Take(6).ToList();
                    }

                    var taskList = new List <Task>();

                    for (int i = 0; i < hamstersToAdd.Count(); i++)
                    {
                        var cage = hamsterDb.Cages.Where(c => c.Id == hamstersToAdd[i].CageId).First();

                        hamstersToAdd[i].CageId = null;
                        cage.CageSize--;

                        hamstersToAdd[i].ExerciseAreaId = exerciseArea.Id;
                        exerciseArea.CageSize++;

                        var addActivityLogTask = CreateAddActivityLog(activityId, hamstersToAdd[i].Id);
                        taskList.Add(addActivityLogTask);

                        hamstersToAdd[i].LastExerciseTime = currentSimulationDate;

                        hamsterDb.SaveChanges();
                    }

                    await Task.WhenAll(taskList);

                    hamsterDb.SaveChanges();
                }
            }
        }