Beispiel #1
0
        public void ShouldDoDoNothingIfCurrentGameDoesntExistInWorkingContext()
        {
            // Arrange: Create current game
            var gameQueue = new GameQueue
            {
                Id    = Guid.NewGuid(),
                Users = new List <GameUser> {
                    new GameUser {
                        UserId = "johnny"
                    }
                },
                Status       = QueueStatus.Waiting,
                CreationTime = DateTime.Parse("01/01/2011 13:00:00")
            };

            int usersCount = gameQueue.Users.Count();

            // Arrange: Create mock dependencies
            //          Set current game in repository
            var gameRepository = new Mock <IGameRepository>();

            gameRepository.Setup(m => m.GetGameQueue(gameQueue.Id)).Returns(gameQueue);
            gameRepository.Setup(m => m.AddOrUpdateGameQueue(gameQueue));
            var workerContext = this.CreateMockWorkerContext();

            // Arrange: Intentionally we are NOT doing the next step
            // workerContext.Object.Context.TryAdd("currentGameQueueId", game.Id);

            // Arrange: Create the command
            TimeSpan timeout            = TimeSpan.FromSeconds(30);
            int      maxNumberOfPlayers = 3;
            var      command            = new GameQueueAutoStartCommand(gameRepository.Object, workerContext.Object, timeout, maxNumberOfPlayers);

            // Arrange: Set current date time so the game has timed out
            TimeProvider.Current = new FixedTimeProvider()
            {
                CurrentDateTime = gameQueue.CreationTime + timeout
            };

            // Act: Execute the command. It should do nothing as the current game isn't in the worker context
            command.Do(null);

            // Assert: Verify that the game wasn't modified
            Assert.AreEqual(gameQueue.Status, QueueStatus.Waiting);
            Assert.AreEqual(gameQueue.Users.Count(), usersCount);
        }
Beispiel #2
0
        public void ShouldDoNotAutoStartIfEnoughtPlayers()
        {
            // Arrange: Create current game and Mock Dependencies
            var johnnyId  = "johnny";
            var peterId   = "peter";
            var lanaId    = "lana";
            var gameQueue = new GameQueue
            {
                Id    = Guid.NewGuid(),
                Users = new List <GameUser> {
                    new GameUser {
                        UserId = johnnyId
                    }, new GameUser {
                        UserId = peterId
                    }, new GameUser {
                        UserId = lanaId
                    }
                },
                Status       = QueueStatus.Waiting,
                CreationTime = DateTime.Parse("01/01/2011 13:00:00")
            };

            var gameRepository    = this.CreateGameRepository(gameQueue);
            var workerContext     = this.CreateMockWorkerContext(gameQueue);
            var humanPlayersCount = gameQueue.Users.Count();

            // Arrange: Create the command
            TimeSpan timeout            = TimeSpan.FromSeconds(30);
            int      maxNumberOfPlayers = 3;
            var      command            = new GameQueueAutoStartCommand(gameRepository.Object, workerContext.Object, timeout, maxNumberOfPlayers);

            // Arrange: Set current date time to timeout the game
            this.SetCurrentDateTime(gameQueue.CreationTime + timeout);

            // Act: Execute the command
            command.Do(null);

            // Assert: Verify that the game was modified to start with bots but still has johnny and peter
            Assert.AreEqual(gameQueue.Status, QueueStatus.Waiting);
            Assert.AreEqual(gameQueue.Users.Count(), maxNumberOfPlayers);
            Assert.IsTrue(gameQueue.Users.Any(u => u.UserId == johnnyId));
            Assert.IsTrue(gameQueue.Users.Any(u => u.UserId == peterId));
            Assert.IsTrue(gameQueue.Users.Any(u => u.UserId == lanaId));
            Assert.AreEqual(this.BotsCount(gameQueue), 0);
        }
Beispiel #3
0
        public void ShouldCurrentGameQueueIdReturnSetValue()
        {
            // Arrange: We create the mock dependencies and the new command. We set the game ID.
            //          We initialize currentGame to any value
            var gameRepository = new Mock <IGameRepository>();
            var workerContext  = this.CreateMockWorkerContext();

            workerContext.Object.Context.TryAdd("currentGameQueueId", Guid.NewGuid());
            var command            = new GameQueueAutoStartCommand(gameRepository.Object, workerContext.Object);
            var currentGameQueueId = Guid.NewGuid();

            // Act: We set the game ID, and then we retrieve it
            command.CurrentGameQueueId = currentGameQueueId;
            var returnedGameID = command.CurrentGameQueueId;

            // Assert: We verify the returnedGameID is the game ID we set
            Assert.AreEqual(currentGameQueueId, returnedGameID);
        }