Example #1
0
 // Moving hamsters from the exercise-cage back to the normal cages.
 public async Task EmptyExerciseCage(DateTime date)
 {
     await Task.Run(() =>
     {
         using (var dbContext = new DaycareContext())
         {
             var exerciseCage = dbContext.ExerciseCages.First();
             foreach (var hamster in exerciseCage.Hamsters)
             {
                 foreach (var cage in dbContext.Cages)
                 {
                     if (cage.Hamsters.Count < 3)
                     {
                         if (cage.Hamsters.Count == 0)
                         {
                             cage.HamstersGender = hamster.Gender;
                             exerciseCage.Hamsters.Remove(hamster);
                             cage.Hamsters.Add(hamster);
                             hamster.Logs.Add(new Log(Activity.Moving, Location.Cage, date));
                             break;
                         }
                         else if (cage.HamstersGender == hamster.Gender)
                         {
                             exerciseCage.Hamsters.Remove(hamster);
                             cage.Hamsters.Add(hamster);
                             hamster.Logs.Add(new Log(Activity.Moving, Location.Cage, date));
                             break;
                         }
                     }
                 }
             }
             dbContext.SaveChanges();
         }
     });
 }
Example #2
0
        // Clear all cages and logging departure.
        public async Task CheckOutHamster(DateTime date)
        {
            await Task.Run(() =>
            {
                using (var dbContext = new DaycareContext())
                {
                    var exerciseCage = dbContext.ExerciseCages.First();
                    foreach (var hamster in exerciseCage.Hamsters)
                    {
                        hamster.Logs.Add(new Log(Activity.Leaving, Location.Daycare, date));
                    }
                    exerciseCage.Hamsters.Clear();
                    exerciseCage.HamstersGender = Gender.Unspecified;

                    foreach (var cage in dbContext.Cages)
                    {
                        foreach (var hamster in cage.Hamsters)
                        {
                            hamster.Logs.Add(new Log(Activity.Leaving, Location.Daycare, date));
                        }
                        cage.Hamsters.Clear();
                        cage.HamstersGender = Gender.Unspecified;
                    }
                    dbContext.SaveChanges();
                }
            });
        }
Example #3
0
        private static void CreateExerciseCage() // Creates a exercisecage into the database.
        {
            var dbContext = new DaycareContext();

            if (dbContext.ExerciseCages.Count() == 0)
            {
                dbContext.ExerciseCages.Add(new ExerciseCage());
                dbContext.SaveChanges();
            }
        }
        public static void ClearLogs()
        {
            var dbContext = new DaycareContext();

            foreach (var log in dbContext.Logs)
            {
                dbContext.Logs.Remove(log);
            }
            dbContext.SaveChanges();
            dbContext.Database.ExecuteSqlRaw("DBCC CHECKIDENT('Logs', RESEED, 0)");
        }
Example #5
0
 internal void LogActivity(DateTime date)
 {
     using (var dbContext = new DaycareContext())
     {
         foreach (var hamster in Hamsters)
         {
             hamster.Logs.Add(new Log(Activity.Exercising, Location.ExerciseCage, date));
         }
         dbContext.SaveChanges();
     }
 }
Example #6
0
        private static void CreateCages() // Creates 10 cages into the database.
        {
            var dbContext = new DaycareContext();

            if (dbContext.Cages.Count() == 0)
            {
                for (int i = 0; i < 10; i++)
                {
                    dbContext.Cages.Add(new Cage());
                }
            }
            dbContext.SaveChanges();
        }
Example #7
0
 // Putting hamsters in the exercise-cage based on date and the least amount av exercised logs for each hamster.
 public async Task FillExerciseCage(DateTime date)
 {
     await Task.Run(() =>
     {
         using (var dbContext = new DaycareContext())
         {
             var exerciseCage = dbContext.ExerciseCages.First();
             if (exerciseCage.Hamsters.Count == 0)
             {
                 var HamstersExerciseQuery = dbContext.Hamsters.ToList()
                                             .GroupBy(h => h).Select(g => new
                 {
                     Hamster       = g.Key,
                     ExerciseCount = g.Key.Logs.Where(l => l.Activity == Activity.Exercising && l.Date.Day == date.Day).Count()
                 });
                 HamstersExerciseQuery = HamstersExerciseQuery.ToList().OrderBy(h => h.ExerciseCount);
                 Gender gender         = Gender.Unspecified;
                 int count             = 0;
                 foreach (var hamster in HamstersExerciseQuery)
                 {
                     if (count == 0)
                     {
                         gender = hamster.Hamster.Gender;
                     }
                     if (hamster.Hamster.Gender == gender)
                     {
                         foreach (var cage in dbContext.Cages)
                         {
                             if (cage.Hamsters.Contains(hamster.Hamster))
                             {
                                 cage.Hamsters.Remove(hamster.Hamster);
                                 break;
                             }
                         }
                         exerciseCage.Hamsters.Add(hamster.Hamster);
                         hamster.Hamster.Logs.Add(new Log(Activity.Moving, Location.ExerciseCage, date));
                         count++;
                     }
                     if (count == 6)
                     {
                         break;
                     }
                 }
                 dbContext.SaveChanges();
             }
         }
     });
 }
Example #8
0
        private static void ResetLogsAndHamsters() // Removes logs and if they are in cages they will get cleared from that.
        {
            var dbContext = new DaycareContext();

            if (dbContext.Logs.Count() > 0)
            {
                foreach (var log in dbContext.Logs)
                {
                    dbContext.Remove(log);
                }
            }
            dbContext.Cages.ToList().ForEach(c => c.Hamsters.Clear());
            dbContext.ExerciseCages.First().Hamsters.Clear();
            dbContext.Hamsters.ToList().ForEach(h => h.CheckedIn = null);
            dbContext.SaveChanges();
        }
Example #9
0
        // Method for clearing cages if program is exited before checkout.
        public void Clear()
        {
            using (var dbContext = new DaycareContext())
            {
                var exerciseCage = dbContext.ExerciseCages.First();
                exerciseCage.Hamsters.Clear();
                exerciseCage.HamstersGender = Gender.Unspecified;

                foreach (var cage in dbContext.Cages)
                {
                    cage.Hamsters.Clear();
                    cage.HamstersGender = Gender.Unspecified;
                }
                dbContext.SaveChanges();
            }
        }
Example #10
0
 internal void LogActivity(DateTime date)
 {
     using (var dbContext = new DaycareContext())
     {
         var activities = new List <Activity>();
         activities.Add(Activity.Eating);
         activities.Add(Activity.Sleeping);
         activities.Add(Activity.Playing);
         var random   = new Random();
         var activity = activities[random.Next(0, activities.Count)];
         foreach (var hamster in Hamsters)
         {
             hamster.Logs.Add(new Log(activity, Location.Cage, date));
         }
         dbContext.SaveChanges();
     }
 }
Example #11
0
 // The logic for each tick of the day.
 public async Task OnTick(int tickCount, DateTime date)
 {
     if (tickCount == 0)
     {
         await CheckInHamsters(date);
     }
     if (tickCount >= 0 && tickCount < 100)
     {
         // Exercise rotation.
         if (tickCount % 10 == 0)
         {
             if (tickCount != 0)
             {
                 await EmptyExerciseCage(date);
             }
             await FillExerciseCage(date);
         }
         // Logging the daily activity for all hamsters based on which cage they are in on two different threads.
         var operationList = new List <Task>();
         operationList.Add(new Task(() =>
         {
             using (var dbContext = new DaycareContext())
             {
                 foreach (var cage in dbContext.Cages)
                 {
                     cage.LogActivity(date);
                 }
                 dbContext.SaveChanges();
             }
         }));
         operationList.Add(new Task(() =>
         {
             using (var dbContext = new DaycareContext())
             {
                 dbContext.ExerciseCages.First().LogActivity(date);
                 dbContext.SaveChanges();
             }
         }));
         operationList.ForEach(t => t.Start());
         await Task.WhenAll(operationList);
     }
     if (tickCount == 100)
     {
         await CheckOutHamster(date);
     }
 }
Example #12
0
        // Putting all hamsters in to their normal cages based on gender and space. As well as logging it.
        public async Task CheckInHamsters(DateTime date)
        {
            await Task.Run(() =>
            {
                using (var dbContext = new DaycareContext())
                {
                    var hamsterQueue = new Queue <Hamster>();
                    var hamsterList  = dbContext.Hamsters.ToList();
                    foreach (var hamster in hamsterList)
                    {
                        hamsterQueue.Enqueue(hamster);
                    }

                    foreach (var cage in dbContext.Cages)
                    {
                        if (cage.Hamsters.Count < 3)
                        {
                            for (int i = 0; i < 3; i++)
                            {
                                var hamster = hamsterQueue.Dequeue();
                                if (cage.Hamsters.Count == 0)
                                {
                                    cage.Hamsters.Add(hamster);
                                    hamster.Logs.Add(new Log(Activity.Arriving, Location.Daycare, date));
                                    cage.HamstersGender = hamster.Gender;
                                }
                                else if (cage.HamstersGender == hamster.Gender)
                                {
                                    cage.Hamsters.Add(hamster);
                                    hamster.Logs.Add(new Log(Activity.Arriving, Location.Daycare, date));
                                }
                                else
                                {
                                    hamsterQueue.Enqueue(hamster);
                                    i--;
                                }
                            }
                        }
                    }
                    dbContext.SaveChanges();
                }
            });
        }
Example #13
0
 public static void ResetHamsterValues()
 {
     using (var dbContext = new DaycareContext())
     {
         if (dbContext.Cages.Count() > 0)
         {
             foreach (var cage in dbContext.Cages)
             {
                 cage.Hamsters.Clear();
                 cage.HamstersGender = Gender.Unspecified;
             }
         }
         if (dbContext.ExerciseCages.Count() > 0)
         {
             dbContext.ExerciseCages.First().Hamsters.Clear();
             dbContext.ExerciseCages.First().HamstersGender = Gender.Unspecified;
         }
         dbContext.SaveChanges();
     }
 }
Example #14
0
        private static void ImportHamsters() // Imports data from a CSV-file into the datebase.
        {
            var dbContext = new DaycareContext();

            if (dbContext.Hamsters.Count() == 0)
            {
                var file = new DirectoryInfo(Directory.GetCurrentDirectory())
                           .Parent.Parent.Parent.Parent.GetDirectories()
                           .ToList().First(d => d.Name == "SeedFromFile").GetFiles()
                           .First(f => f.Name == "Hamsterlista30.csv").FullName;

                var csvLines = File.ReadAllLines(file);

                foreach (var csvLine in csvLines)
                {
                    var hamster = new Hamster();

                    string[] values = csvLine.Split(";");
                    hamster.Name = (values[0]);
                    hamster.Age  = Convert.ToInt32(values[1]);
                    if (values[2] == "M")
                    {
                        hamster.Gender = Gender.Male;
                    }
                    else if (values[2] == "K")
                    {
                        hamster.Gender = Gender.Female;
                    }
                    hamster.Owner = (values[3]);

                    dbContext.Hamsters.Add(new Hamster()
                    {
                        Name = hamster.Name, Age = hamster.Age, Gender = hamster.Gender, Owner = hamster.Owner
                    });
                    dbContext.SaveChanges();
                }
            }
        }
Example #15
0
        public static void SetupDaycareDatabase()
        {
            using (var dbContext = new DaycareContext())
            {
                dbContext.Database.EnsureCreated();

                if (dbContext.Hamsters.Count() == 0)
                {
                    var file = new DirectoryInfo(Directory.GetCurrentDirectory())
                               .Parent.Parent.Parent.Parent.GetDirectories()
                               .ToList().First(d => d.Name == "LoadFiles").GetFiles()
                               .First(f => f.Name == "Hamsterlist.csv").FullName;
                    var lines    = File.ReadAllLines(file);
                    var hamsters = lines.ToList().Select(l => new Hamster(
                                                             l.Split(',')[0],
                                                             int.Parse(l.Split(',')[1]),
                                                             (Gender)int.Parse(l.Split(',')[2]),
                                                             l.Split(',')[3])).ToList();

                    hamsters.ToList().ForEach(h => dbContext.Hamsters.Add(h));
                }
                if (dbContext.Cages.Count() == 0)
                {
                    for (int i = 0; i < 10; i++)
                    {
                        dbContext.Cages.Add(new Cage());
                    }
                }
                if (dbContext.ExerciseCages.Count() == 0)
                {
                    dbContext.ExerciseCages.Add(new ExerciseCage());
                }
                ResetHamsterValues();
                ClearLogs();
                dbContext.SaveChanges();
            }
        }