コード例 #1
0
        public async Task NewGame()
        {
            var random = new Random();
            var gameId = random.Next().ToString();
            await Groups.Add(Context.ConnectionId, gameId);

            var game = new Game(gameId);

            var player = new Player(Context.ConnectionId, "P1");
            GameManager.Instance.AddGame(game);
            GameManager.Instance.AddPlayerToGame(gameId, player);

            Clients.Group(gameId).getGameId(gameId);
        }
コード例 #2
0
        public Player Play(Player playerOne, Player playerTwo)
        {
            if (playerOne.SelectedShape == null || playerTwo.SelectedShape == null)
            {
                throw new Exception("Cannot play with no shapes selection");
            }

            if (playerOne.SelectedShape.ShapeType == playerTwo.SelectedShape.ShapeType)
            {
                return null;
            }

            if (playerOne.SelectedShape.Defeat(playerTwo.SelectedShape))
            {
                return playerOne;
            }

            return playerTwo;
        }
コード例 #3
0
        public void PlayerTwoWinsPlayerOne()
        {
            var playerOne = new Player("juan");
            var playerTwo = new Player("yagi");

            var shapeThatWins = new Mock<IShape>();
            shapeThatWins.Setup(s => s.Defeat(It.IsAny<IShape>())).Returns(true);
            shapeThatWins.Setup(s => s.ShapeType).Returns(ShapeType.Rock);

            var shapeThatLoses = new Mock<IShape>();
            shapeThatLoses.Setup(s => s.Defeat(It.IsAny<IShape>())).Returns(false);
            shapeThatLoses.Setup(s => s.ShapeType).Returns(ShapeType.Scissor);

            playerOne.SelectedShape = shapeThatLoses.Object;
            playerTwo.SelectedShape = shapeThatWins.Object;

            var game = new Game();
            var winner = game.Play(playerOne, playerTwo);

            Assert.AreEqual(playerTwo, winner);
        }
コード例 #4
0
        public void PlayerOneDrawPlayerTwo()
        {
            var playerOne = new Player("juan");
            var playerTwo = new Player("yagi");

            var shapeOne = new Mock<IShape>();
            shapeOne.Setup(s => s.Defeat(It.IsAny<IShape>())).Returns(true);
            shapeOne.Setup(s => s.ShapeType).Returns(ShapeType.Rock);

            var shapeTwo = new Mock<IShape>();
            shapeTwo.Setup(s => s.Defeat(It.IsAny<IShape>())).Returns(false);
            shapeTwo.Setup(s => s.ShapeType).Returns(ShapeType.Rock);

            playerOne.SelectedShape = shapeOne.Object;
            playerTwo.SelectedShape = shapeTwo.Object;

            var game = new Game();
            var winner = game.Play(playerOne, playerTwo);

            Assert.IsNull(winner);
        }
コード例 #5
0
        public async Task JoinGame(string gameId){

            var players = GameManager.Instance.GetPlayersFromGameId(gameId);

            if (players.Count > 2)
            {
                await Clients.Group(gameId).onError("Sorry, I'm a little bit lazy right now... so only two players will be able to play in one game");
            }

            var game = GameManager.Instance.Games.Single(g => g.Id == gameId);

            var player = new Player(Context.ConnectionId, "P2");

            GameManager.Instance.AddPlayerToGame(gameId, player);

            if (game == null)
            {
                Clients.Caller.playerConnected("No Game to Connect... Sorry!");
            }

            await Groups.Add(Context.ConnectionId, gameId);
            Clients.Group(gameId).playerConnected(gameId, "A new player is online!");
        }
コード例 #6
0
 public void AddPlayerToGame(string gameId, Player player)
 {
     _players[gameId].Add(player);
 }