コード例 #1
0
        public void TestStroke()
        {
            UserAnimal userAnimal = new UserAnimal()
            {
                Animal = testAnimal
            };

            // Wait until happiness is at its lowest
            Thread.Sleep(4000);

            // Make sure that stroking reduces back to zero
            userAnimal.Stroke(1);
            Assert.AreEqual(0, userAnimal.Happiness);

            // Make sure that stroking can go up to 1
            userAnimal.Stroke(1);
            Assert.AreEqual(1, userAnimal.Happiness);

            // Make sure that stroking can go no further than 1
            userAnimal.Stroke(1);
            Assert.AreEqual(1, userAnimal.Happiness);

            // Wait until happiness is at its lowest
            Thread.Sleep(4000);

            // Make sure that stroking can go no further than 1
            userAnimal.Stroke(3);
            Assert.AreEqual(1, userAnimal.Happiness);
        }
コード例 #2
0
        public void TestStrokeInvalid()
        {
            UserAnimal userAnimal = new UserAnimal()
            {
                Animal = testAnimal
            };

            Assert.Catch <System.ArgumentException>(() => userAnimal.Stroke(-0.1m));
            Assert.Catch <System.ArgumentException>(() => userAnimal.Stroke(0));
        }
コード例 #3
0
        public ActionResult <UserAnimal> Stroke(uint userId, uint id)
        {
            UserAnimal userAnimal = service
                                    .FindAll()
                                    .FirstOrDefault(ua => ua.UserId == userId && ua.AnimalId == id)
            ;

            if (userAnimal == null)
            {
                return(new NotFoundResult());
            }

            // Stroke the animal and update the database
            userAnimal.Stroke(STROKE_AMOUNT);
            service.Update(userAnimal);

            return(Get(userId, id));
        }
コード例 #4
0
        public void TestUpdate()
        {
            User user = new User()
            {
                DisplayName = "Some display name"
            };

            Animal animal = new Animal()
            {
                TypeName         = "Test animal",
                HungerPerSecond  = 0.5m,
                SadnessPerSecond = 0.4m
            };

            UserAnimal userAnimal;

            using (ApiContext context = new ApiContext(dbOptions)) {
                context.Users.Add(user);
                context.Animals.Add(animal);

                userAnimal = new UserAnimal()
                {
                    UserId   = user.Id,
                    AnimalId = animal.Id,
                };
                context.UserAnimals.Add(userAnimal);
                context.SaveChanges();
            }

            // Check the initial state of the animal
            using (ApiContext context = new ApiContext(dbOptions)) {
                Assert.AreEqual(1, context.UserAnimals.Count());

                userAnimal = context.UserAnimals.Include(ua => ua.Animal).First();

                Assert.AreEqual(0, userAnimal.Hunger);
                Assert.AreEqual(0, userAnimal.Happiness);
            }

            // Add an update (at this stage it won't really update much)
            Thread.Sleep(1000);
            using (ApiContext context = new ApiContext(dbOptions)) {
                UserAnimalService service = new UserAnimalService(context);

                service.Update(userAnimal);
            }

            using (ApiContext context = new ApiContext(dbOptions)) {
                Assert.AreEqual(1, context.UserAnimals.Count());

                userAnimal = context.UserAnimals.Include(ua => ua.Animal).First();
                Assert.AreEqual(0.5, userAnimal.Hunger);
                Assert.AreEqual(-0.4, userAnimal.Happiness);
            }

            // Feed the animal and update again - this will cause the internal dates to update
            Thread.Sleep(1000);
            using (ApiContext context = new ApiContext(dbOptions)) {
                UserAnimalService service = new UserAnimalService(context);

                userAnimal.Feed(1);
                service.Update(userAnimal);
            }

            using (ApiContext context = new ApiContext(dbOptions)) {
                Assert.AreEqual(1, context.UserAnimals.Count());

                userAnimal = context.UserAnimals.Include(ua => ua.Animal).First();
                Assert.AreEqual(0, userAnimal.Hunger);
                Assert.AreEqual(-0.8, userAnimal.Happiness);
            }

            // Stroke the animal and update again - this will cause the internal dates to update
            Thread.Sleep(1000);
            using (ApiContext context = new ApiContext(dbOptions)) {
                UserAnimalService service = new UserAnimalService(context);

                userAnimal.Stroke(1);
                service.Update(userAnimal);
            }

            using (ApiContext context = new ApiContext(dbOptions)) {
                Assert.AreEqual(1, context.UserAnimals.Count());

                userAnimal = context.UserAnimals.Include(ua => ua.Animal).First();
                Assert.AreEqual(0.5, userAnimal.Hunger);
                Assert.AreEqual(0, userAnimal.Happiness);
            }
        }