示例#1
0
        public bool Equals(Game game)
        {
            bool result = this.Id == game.Id &&
                   this.CreationTime == game.CreationTime &&
                   new CollectionHaveSameElementsComparison<GameUser>(this.Users, game.Users).DoIt() &&
                   this.Status == game.Status;

            return result;
        }
        public void AddOrUpdateGame(Game game)
        {
            if (game == null)
            {
                throw new ArgumentNullException("game");
            }

            if (game.Id == Guid.Empty)
            {
                throw new ArgumentException("Game Id cannot be empty");
            }

            this.gameContainer.Save(game.Id.ToString(), game);
        }
        public void LeaveGameTest()
        {
            var testUserId = Guid.NewGuid().ToString();
            var testGame = new Game
            {
                Id = Guid.NewGuid(),
                Users = new List<GameUser>
                {
                    new GameUser { UserId = Guid.NewGuid().ToString() },
                    new GameUser { UserId = testUserId },
                    new GameUser { UserId = Guid.NewGuid().ToString() }
                }
            };

            var gameRepository = new Mock<IGameRepository>();
            gameRepository.Setup(m => m.GetGame(It.Is<Guid>(g => g == testGame.Id)))
                          .Returns(testGame)
                          .Verifiable();

            gameRepository.Setup(m => m.AddOrUpdateGame(It.Is<Game>(g => g.Id == testGame.Id && g.Users.Count == 2 && !g.Users.Any(u => u.UserId == testUserId))))
                          .Verifiable();

            var userRepository = new Mock<IUserRepository>();
            userRepository.Setup(m => m.AddOrUpdateUserSession(It.Is<UserSession>(s => s.UserId == testUserId && s.ActiveGameQueueId == Guid.Empty)))
                          .Verifiable();

            var command = new LeaveGameCommand(userRepository.Object, gameRepository.Object);
            var context = new Dictionary<string, object>
            {
                { "gameId", testGame.Id },
                { "userId", testUserId }
            };

            command.Do(context);

            gameRepository.VerifyAll();
            userRepository.VerifyAll();
        }
        public Game StartGame(Guid gameQueueId)
        {
            GameQueue queue = this.gameQueueContainer.Get(gameQueueId.ToString());

            if (queue == null)
            {
                throw new InvalidOperationException(string.Format("Game Queue does not exist: {0}", gameQueueId));
            }

            if (queue.Status != QueueStatus.Waiting)
            {
                throw new InvalidOperationException(string.Format("Game Queue Status is not Waiting: {0}", gameQueueId));
            }

            // Create game
            var game = new Game
            {
                Id = Guid.NewGuid(),
                Users = queue.Users,
                ActiveUser = queue.Users.First().UserId,
                Status = GameStatus.Waiting,
                Seed = new Random().Next(10000, int.MaxValue)
            };

            queue.Status = QueueStatus.Ready;
            queue.GameId = game.Id;

            this.gameContainer.Save(game.Id.ToString(), game);
            this.gameQueueContainer.Save(gameQueueId.ToString(), queue);

            return game;
        }
        private Game CreateNewGame(IGameRepository gameRepository, params string[] userIds)
        {
            Guid gameID = Guid.NewGuid();

            Game game = new Game() { Id = gameID, ActiveUser = "******", GameActions = new List<GameAction>(), Users = new List<GameUser>() };

            foreach (string userId in userIds)
            {
                GameUser user = new GameUser() { UserId = userId };
                game.Users.Add(user);
            }

            gameRepository.AddOrUpdateGame(game);

            return game;
        }
        public void Do(IDictionary<string, object> context)
        {
            if (this.workerContext.Context.ContainsKey("currentGameQueueId") && this.CurrentGameQueueId != Guid.Empty)
            {
                var currentGameQueue = this.gameRepository.GetGameQueue(this.CurrentGameQueueId);

                if (currentGameQueue != null &&
                    currentGameQueue.Users.Count() > 0 &&
                    currentGameQueue.Users.Count() < this.maxNumberOfPlayersPerGame &&
                    currentGameQueue.Status == QueueStatus.Waiting)
                {
                    if (currentGameQueue.TimeElapsed() >= this.gameQueueTimeoutWaiting.TotalSeconds)
                    {
                        if (currentGameQueue.Users.Count() == 1)
                        {
                            currentGameQueue.Status = QueueStatus.Timeout;
                        }
                        else
                        {
                            // Add bots
                            //// (Removed for test and integration)
                            ////while (currentGameQueue.Users.Count() < this.maxNumberOfPlayersPerGame)
                            ////{
                            ////    // TODO: Define special UserIds for bots
                            ////    var userId = ConfigurationConstants.BotUserIdPrefix + Guid.NewGuid();
                            ////    currentGameQueue.Users.Add(
                            ////        new GameUser
                            ////        {
                            ////            UserId = userId,
                            ////            UserName = userId
                            ////        });
                            ////}

                            // Create game
                            var game = new Game
                            {
                                Id = Guid.NewGuid(),
                                Users = currentGameQueue.Users,
                                ActiveUser = currentGameQueue.Users.First().UserId,
                                Status = GameStatus.Waiting,
                                Seed = new Random().Next(10000, int.MaxValue)
                            };

                            currentGameQueue.Status = QueueStatus.Ready;
                            currentGameQueue.GameId = game.Id;

                            this.gameRepository.AddOrUpdateGame(game);
                        }

                        this.gameRepository.AddOrUpdateGameQueue(currentGameQueue);
                    }
                }
            }
        }