Beispiel #1
0
    public void Register(TriggerCommandsType command, GameCommandHandler handler)
    {
        List <Action> callbacks = null;

        if (!_handlers.TryGetValue(command, out callbacks))
        {
            callbacks = _handlers[command] = new List <Action>();
        }
        callbacks.Add(handler.OnInteraction);
    }
        public async void GameCommandHandlerHandlesCreateGameCommand()
        {
            // Arrange
            var mockAggregateRepo = new Mock <IAggregateRepository>();
            var newGameGuid       = Guid.NewGuid();
            var fakeCommand       = new CreateGame(newGameGuid, 8);
            var sut = new GameCommandHandler(mockAggregateRepo.Object);

            // Act
            await sut.Handle(fakeCommand);

            //Assert
            mockAggregateRepo.Verify(m => m.Save(It.IsAny <Game>(), -1), Times.Once);
        }
        public async void GameCommandHandlerHandlesUpdatePlayerNameCommand()
        {
            // Arrange
            var mockAggregateRepo = new Mock <IAggregateRepository>();
            var fakeGame          = new Game();

            mockAggregateRepo.Setup(m => m.GetById <Game>(Guid.Empty)).Returns(Task.FromResult(fakeGame));
            var newCommandId = Guid.NewGuid();
            var fakeCommand  = new UpdatePlayerName(newCommandId, 0, Guid.Empty, "Dave", 1);
            var sut          = new GameCommandHandler(mockAggregateRepo.Object);

            // Act
            await sut.Handle(fakeCommand);

            //Assert
            mockAggregateRepo.Verify(m => m.GetById <Game>(Guid.Empty), Times.Once);
            mockAggregateRepo.Verify(m => m.Save(It.IsAny <Game>(), It.IsAny <int>()), Times.Once);
            Assert.Equal(1, fakeGame.GetUncommittedChanges().Count());
            Assert.Equal(typeof(PlayerNameUpdated), fakeGame.GetUncommittedChanges().First().GetType());
        }
Beispiel #4
0
 public void Remove(TriggerCommandsType command, GameCommandHandler handler)
 {
     _handlers[command].Remove(handler.OnInteraction);
 }
 public static void RemoveGameCmdHandler(GameCommandHandler h) {
     if(gameCmdHandlers.Contains(h))
         gameCmdHandlers.Remove(h);
 }
 public static void AddGameCmdHandler(GameCommandHandler h) {
     if(!gameCmdHandlers.Contains(h))
         gameCmdHandlers.Add(h);
 }
Beispiel #7
0
        public void TestInializer()
        {
            var rts = new Genre
            {
                Id = 1,
                Name = "RTS",
                ChildGenres = new Genre[0]
            };

            var strategy = new Genre
            {
                Id = 2,
                Name = "Strategy",
                ChildGenres = new[] { rts }
            };

            rts.ParentGenre = strategy;
            rts.ParentGenreId = 2;
            var genres = new[] { rts, strategy };
            _genreRepositoryMock = new Mock<IRepository<Genre, int>>();
            _genreRepositoryMock.Setup(x => x.Get()).Returns(genres);
            _genreRepositoryMock.Setup(x => x.Get(It.IsAny<Int32>())).Returns(
                (Int32 i) => genres.FirstOrDefault(g => g.Id == i));
            _genreRepositoryMock.Setup(x => x.GetSingle(It.IsAny<Expression<Func<Genre, Boolean>>>())).Returns(
                (Expression<Func<Genre, Boolean>> predicate) => genres.FirstOrDefault(predicate.Compile()));

            var desktop = new PlatformType
            {
                Id = 1,
                Name = "Desktop"
            };
            var web = new PlatformType
            {
                Id = 2,
                Name = "Web"
            };
            var platformTypes = new[] { desktop, web };
            _platformTypeRepositoryMock = new Mock<IRepository<PlatformType, int>>();
            _platformTypeRepositoryMock.Setup(x => x.Get()).Returns(platformTypes);
            _platformTypeRepositoryMock.Setup(x => x.Get(It.IsAny<Int32>())).Returns(
                (Int32 i) => platformTypes.FirstOrDefault(g => g.Id == i));
            _platformTypeRepositoryMock.Setup(x => x.GetSingle(It.IsAny<Expression<Func<PlatformType, Boolean>>>())).Returns(
                (Expression<Func<PlatformType, Boolean>> predicate) => platformTypes.FirstOrDefault(predicate.Compile()));
            _platformTypeRepositoryMock.Setup(x => x.Get(It.IsAny<Expression<Func<PlatformType, Boolean>>>())).Returns(
                (Expression<Func<PlatformType, Boolean>> predicate) => platformTypes.Where(predicate.Compile()));

            _newGameRightCommand = new CreateGameCommand
            {
                Name = "GTA 5",
                Description = "5 part",
                Key = "gta-5",
                GenreIds = new[] { 1 },
                PlatformTypeIds = new[] { 1 }
            };

            _editGameRightCommand = new EditGameCommand
            {
                Id = 1,
                Name = "New name",
                Description = "New description",
                Key = "new-key",
                GenreIds = new[] { 2 },
                PlatformTypeIds = new[] { 2 }
            };

            _dota = new Game
            {
                Id = 1,
                Name = "Dota 2",
                Description = "Just try it",
                Key = "dota-2",
                Genres = new[] { rts },
                PlatformTypes = new[] { desktop, web }
            };

            _witcher = new Game
            {
                Id = 2,
                Name = "Witcher 3",
                Description = "3d part of trilogy",
                Key = "witcher-3",
                Genres = new[] { strategy },
                PlatformTypes = new[] { desktop }
            };
            _games = new[] { _dota, _witcher };
            _gameRepositoryMock = new Mock<IRepository<Game, int>>();
            _gameRepositoryMock.Setup(x => x.Get()).Returns(_games);
            _gameRepositoryMock.Setup(x => x.Get(It.Is<Int32>(i => i == 1))).Returns(_dota);
            _gameRepositoryMock.Setup(x => x.Get(It.Is<Int32>(i => i == 2))).Returns(_witcher);
            _gameRepositoryMock.Setup(x => x.Get(It.IsAny<Expression<Func<Game, Boolean>>>())).Returns(
                (Expression<Func<Game, Boolean>> predicate) => _games.Where(predicate.Compile()));
            _gameRepositoryMock.Setup(x => x.GetSingle(It.IsAny<Expression<Func<Game, Boolean>>>())).Returns(
                (Expression<Func<Game, Boolean>> predicate) => _games.FirstOrDefault(predicate.Compile()));

            _unitOfWorkMock = new Mock<IGameStoreUnitOfWork>();
            _unitOfWorkMock.Setup(x => x.Games).Returns(_gameRepositoryMock.Object);
            _unitOfWorkMock.Setup(x => x.Genres).Returns(_genreRepositoryMock.Object);
            _unitOfWorkMock.Setup(x => x.PlatformTypes).Returns(_platformTypeRepositoryMock.Object);

            var logger = new Mock<ILogger>();
            _commandHandler = new GameCommandHandler(_unitOfWorkMock.Object, logger.Object);
            _queryHandler = new GameQueryHandler(_unitOfWorkMock.Object, logger.Object);
        }
Beispiel #8
0
        void StartGame(List<PlayerConnection> connections, List<PlayerInfo> players, List<DeckItem> decks, 
            string[] passwords, GameType gameType, bool modal)
        {
            List<PlayerAccountData> allPlayers = new List<PlayerAccountData>();
              for(int i = 0; i < players.Count; i++)
            allPlayers.Add(new PlayerAccountData() { Info = players[i], Password = passwords[i], Deck = decks[i] });

              var gameModel = new GameModel(logicStarter.GameItem);
              var gameView = logicStarter.ViewFactory.CreateGameView();

              //using(GameCommandHandler gameCommandHandler = new GameCommandHandler(gameModel, allPlayers.First().Player.UniqueID))
              //{
              GameCommandHandler gameCommandHandler = new GameCommandHandler(gameModel, allPlayers.First().Info.NickName, logicStarter.ServicesProvider);

              for(int i = 0; i < connections.Count; i++)
              gameCommandHandler.AddChannel(connections[i].NetClient, players[i + 1].NickName);
            var gameController = new GameController(gameCommandHandler, gameModel, gameView, allPlayers, gameType, logicStarter.ServicesProvider, true);
            gameController.ShowOptionsRequested += new EventHandler(gameController_ShowOptionsRequested);
            if(modal)
              gameView.ShowModal();
            else
              gameView.Show();
              //}
        }
        void StartGame(List<PlayerAccountData> players, GameModel gameModel, GameType gameType)
        {
            bool savedGame = gameModel != null;
              if(savedGame)
            gameModel.Console.Messages.Clear(); // clients must not see saved server's log
              else
            gameModel = new GameModel(gameItem);

              bool exitGame = false;
              while(!exitGame)
              {
            var gameView = viewFactory.CreateGameView();
            using(var commandHandler = new GameCommandHandler(gameModel, players.First().Info.NickName, servicesProvider))
            {
              foreach(var player in players.Where(e => e.Channel != null))
            commandHandler.AddChannel(player.Channel, player.Info.NickName);
              var gameController = new GameController(commandHandler, gameModel, gameView, players, gameType, servicesProvider, !savedGame);
              gameController.ShowOptionsRequested += new EventHandler(gameController_ShowOptionsRequested);
              gameView.ShowModal();
            }
            exitGame = !gameModel.IsRestartNotified;
            if(!exitGame)
            {
              players = players.Where(e => gameModel.Players.Any(p => p.Key == e.Info.NickName)).ToList();
              exitGame = RunRestartGame(players, gameType);
              if(!exitGame)
              {
            gameModel = new GameModel(gameItem);
            savedGame = false;
              }
            }
              }
        }
        public void StartSolitaire()
        {
            PlayerAccountData thisPlayer = new PlayerAccountData();
              DeckRoom deckRoomLogic = new DeckRoom(this);
              thisPlayer.Deck = deckRoomLogic.Run();
              if(thisPlayer.Deck != null)
              {
            List<PlayerAccountData> players = new List<PlayerAccountData>();

            thisPlayer.Info = CreateLocalPlayerInfo();
            thisPlayer.Deck.MainCards.Shuffle();
            thisPlayer.Password = thisPlayer.Info.NickName;
            players.Add(thisPlayer);

            PlayerAccountData opponentPlayer = new PlayerAccountData();
            opponentPlayer.Info = (PlayerInfo)thisPlayer.Info.Clone();
            opponentPlayer.Info.NickName = "Opponent";
            opponentPlayer.Deck = ServicesProvider.DecksService.LoadDeck(thisPlayer.Deck.Category, thisPlayer.Deck.Name);
            opponentPlayer.Deck.MainCards.Shuffle();
            opponentPlayer.Password = opponentPlayer.Info.NickName;
            players.Add(opponentPlayer);

            #if DEBUG
            PlayerAccountData debugPlayer = new PlayerAccountData();
            debugPlayer.Info = (PlayerInfo)thisPlayer.Info.Clone();
            debugPlayer.Info.NickName = "Debug";
            debugPlayer.Deck = ServicesProvider.DecksService.LoadDeck(thisPlayer.Deck.Category, thisPlayer.Deck.Name);
            debugPlayer.Deck.MainCards.Shuffle();
            debugPlayer.Password = debugPlayer.Info.NickName;
            players.Add(debugPlayer);
            #endif

            var gameModel = new GameModel(gameItem);
            var gameView = viewFactory.CreateGameView();
            using(GameCommandHandler gameCommandHandler = new GameCommandHandler(gameModel, thisPlayer.Info.NickName, servicesProvider))
            {
              var gameController = new GameController(gameCommandHandler, gameModel, gameView, players, GameType.Solitaire, servicesProvider, true);
              gameController.ShowOptionsRequested += new EventHandler(gameController_ShowOptionsRequested);
              gameView.ShowModal();
            }
              }
        }