Example #1
0
    public void Game_DestroyHexagons()
    {
        Game   game    = new Game();
        Client client1 = TestObjects.BuildClient();
        Client client2 = TestObjects.BuildClient();

        game.Players.Add(client1);
        game.Players.Add(client2);
        Schema.BoardState originalBoardState = game.Board.GetBoardState();
        game.Board.DestroyHexagon(0);
        Assert.IsTrue(game.Board.Hexagons[0].IsDestroyed);
        Assert.ThrowsException <System.ArgumentException>(() => { game.Board.DestroyHexagon(420); });
        game.Update(DateTime.Now);

        foreach (Client client in game.Players)
        {
            Assert.IsTrue(client.MessageLog.Last.Value.Is(Schema.BoardState.Descriptor));
            Schema.BoardState boardState = client.MessageLog.Last.Value.Unpack <Schema.BoardState>();
            Assert.AreEqual(1, boardState.HexagonSets[0].Hexagons.Count);
            Assert.AreEqual(2, boardState.HexagonSets.Count);
            Assert.AreEqual(0, boardState.HexagonSets[0].Hexagons[0].Id);
            Assert.IsTrue(boardState.HexagonSets[0].Hexagons[0].IsDestroyed);
            Assert.AreEqual(0, boardState.HexagonSets[1].Hexagons.Count);
        }

        game.Board.DestroyHexagon(0);
        Schema.BoardState emptyBoardState = game.Board.GetBoardState();
        Assert.AreEqual(0, emptyBoardState.HexagonSets[0].Hexagons.Count);
    }
Example #2
0
    public void Game_InitialBoardStateMessage()
    {
        Game   game    = new Game();
        Client client1 = TestObjects.BuildClient();
        Client client2 = TestObjects.BuildClient();

        game.AddClient(client1);
        game.AddClient(client2);
        game.Update(game.LastUpdateTime);

        foreach (Client player in game.Players)
        {
            Schema.BoardState boardState     = player.MessageLog.First.Value.Unpack <Schema.BoardState>();
            HashSet <Hexagon> uniqueHexagons = new HashSet <Hexagon>();
            foreach (Schema.HexagonSet hexSet in boardState.HexagonSets)
            {
                foreach (Schema.Hexagon hexagon in hexSet.Hexagons)
                {
                    Assert.AreEqual(game.Board.Hexagons[hexagon.Id].Id, hexagon.Id);
                    Assert.AreEqual(game.Board.Hexagons[hexagon.Id].Position.X, hexagon.Position.X);
                    Assert.AreEqual(game.Board.Hexagons[hexagon.Id].Position.Y, hexagon.Position.Y);
                    Assert.AreEqual(game.Board.Hexagons[hexagon.Id].PlayerId, hexSet.PlayerId);
                    Assert.AreEqual(game.Board.Hexagons[hexagon.Id].IsDestroyed, hexagon.IsDestroyed);
                }
            }
        }

        Schema.BoardState emptyBoardState = game.Board.GetBoardState();
        Assert.AreEqual(2, emptyBoardState.HexagonSets.Count);
        Assert.AreEqual(0, emptyBoardState.HexagonSets[0].Hexagons.Count);
        Assert.AreEqual(0, emptyBoardState.HexagonSets[1].Hexagons.Count);
    }
Example #3
0
    public void Game_CreateProjectile()
    {
        Game   game   = new Game();
        Client client = TestObjects.BuildClient();

        game.Players.Add(client);
        Vector2 position = new Vector2(1, 2);
        Vector2 velocity = new Vector2(-1, -1);

        game.Board.CreateProjectile(0, position, velocity, (int)ProjectileType.BouncingBall);
        game.Update(game.LastUpdateTime);
        Assert.IsTrue(game.Players[0].MessageLog.First.Value.Is(Schema.ProjectileCreated.Descriptor));
        Schema.ProjectileCreated createdMessage = game.Players[0].MessageLog.First.Value.Unpack <Schema.ProjectileCreated>();
        Projectile projectile = game.Board.Projectiles[0];

        Assert.AreEqual(position, projectile.Position);
        Assert.AreEqual(velocity, projectile.Velocity);

        Assert.AreEqual(1, game.Board.Projectiles.Count);
        CompareVectors(projectile.Position, createdMessage.Position);
        CompareVectors(projectile.Velocity, createdMessage.Velocity);
        Assert.AreEqual(projectile.Mass, createdMessage.Mass);
        Assert.AreEqual(projectile.Radius, createdMessage.Radius);
        Assert.AreEqual(0, createdMessage.Id);
        game.Board.CreateProjectile(0, new Vector2(1, 1), new Vector2(4, 5), (int)ProjectileType.BouncingBall);
        Assert.AreEqual(1, game.Board.Projectiles.Last().Id);

        game.Update(game.LastUpdateTime.AddSeconds(1));
        Assert.AreEqual(position + velocity, projectile.Position);

        game.Update(game.LastUpdateTime.AddSeconds(1));
        Assert.AreEqual(position + velocity * 2, projectile.Position);
    }
Example #4
0
    public void Client_TCPConnection()
    {
        Client client = TestObjects.BuildClient();

        Assert.IsNotNull(client.TCPConnection);
        Assert.IsNotNull(client.TCPConnection.ReceiveBuffer);
        Assert.AreEqual(Constants.DEFAULT_BUFFER_SIZE, client.TCPConnection.ReceiveBuffer.Length);
        Assert.IsNotNull(client.TCPConnection.ReceivedData);
    }
Example #5
0
    public static Game BuildFullRunningGame()
    {
        Game   game    = new Game();
        Client client1 = TestObjects.BuildClient();
        Client client2 = TestObjects.BuildClient();

        game.AddClient(client1);
        game.AddClient(client2);
        return(game);
    }
Example #6
0
    public void Client_ExtractDataFromConnection_InvalidData()
    {
        Client client = TestObjects.BuildClient();

        byte[] bytes = Encoding.ASCII.GetBytes("Invalid data");
        client.TCPConnection.ReceiveBuffer = bytes;

        client.TCPConnection.ExtractDataFromBuffer(bytes.Length);

        Assert.IsNull(client.TCPConnection.ReceivedData.Message);
    }
Example #7
0
    public void Client_HandlePlayerLookingForGame()
    {
        Client client = TestObjects.BuildClient();

        Schema.LookingForGame playerLookingForGame = client.BuildLookingForGame();
        client.ClientSendToServer(Any.Pack(playerLookingForGame));

        Assert.IsNotNull(client.Game);
        CollectionAssert.Contains(client.Game.Players, client);
        Game originalGame = client.Game;

        client.ClientSendToServer(Any.Pack(playerLookingForGame));
        Assert.AreEqual(1, client.Game.Players.Count);
        Assert.AreEqual(originalGame, client.Game);
        Assert.AreEqual(1, client.Server.OpenGames.Count);
    }
Example #8
0
    public void Client_ExtractDataFromConnection()
    {
        Client client = TestObjects.BuildClient();

        Schema.LookingForGame player = new Schema.LookingForGame()
        {
            Username = "******"
        };
        byte[] bytes = Any.Pack(player).ToByteArray();
        client.TCPConnection.ReceiveBuffer = bytes;
        Assert.AreEqual(Constants.DEFAULT_BUFFER_SIZE, client.TCPConnection.ReceiveBuffer.Length);

        client.TCPConnection.ExtractDataFromBuffer(bytes.Length);

        Schema.LookingForGame readJoinedGame = client.TCPConnection.ReceivedData.Message.Unpack <Schema.LookingForGame>();
        Assert.AreEqual(player, readJoinedGame);
    }
Example #9
0
    public void Server_ClientLookingForGame()
    {
        Server server = new Server(8080);
        Client client = TestObjects.BuildClient(server);

        client.ClientSendToServer(Any.Pack(client.BuildLookingForGame()));

        Assert.IsTrue(client.MessageLog.First.Value.Is(Schema.LookingForGame.Descriptor));

        Schema.JoinedGame joinedGameResponse = client.MessageLog.First.Next.Value.Unpack <Schema.JoinedGame>();
        Assert.AreEqual(client.Username, joinedGameResponse.Username);

        Assert.IsTrue(client.MessageLog.Last.Value.Is(Schema.BoardState.Descriptor));
        Schema.BoardState boardState = client.MessageLog.Last.Value.Unpack <Schema.BoardState>();
        byte[]            bytes      = boardState.ToByteArray();
        Assert.IsTrue(bytes.Length < Constants.DEFAULT_BUFFER_SIZE);
        int totalHexagonCount = boardState.HexagonSets[0].Hexagons.Count + boardState.HexagonSets[1].Hexagons.Count;

        Assert.AreEqual(Constants.RowsPerPlayer * Constants.HexagonsPerRow * 2, totalHexagonCount);
    }
Example #10
0
    public void Client_HandleMultiplePlayersLookingForGame()
    {
        Server server  = new Server(8080);
        Client client1 = TestObjects.BuildClient(server);
        Client client2 = TestObjects.BuildClient(server);
        Client client3 = TestObjects.BuildClient(server);

        client1.ClientSendToServer(Any.Pack(client1.BuildLookingForGame()));
        client2.ClientSendToServer(Any.Pack(client2.BuildLookingForGame()));
        client3.ClientSendToServer(Any.Pack(client3.BuildLookingForGame()));

        Assert.AreEqual(1, server.OpenGames.Count);
        Assert.AreEqual(1, server.RunningGames.Count);
        CollectionAssert.Contains(server.RunningGames.First.Value.Players, client1);
        CollectionAssert.Contains(server.RunningGames.First.Value.Players, client2);
        CollectionAssert.DoesNotContain(server.RunningGames.First.Value.Players, client3);
        CollectionAssert.Contains(server.OpenGames.First.Value.Players, client3);
        Assert.AreEqual(0, client1.PlayerId);
        Assert.AreEqual(1, client2.PlayerId);
        Assert.AreEqual(0, client3.PlayerId);
    }
Example #11
0
    public void Server_UpdatesCalledOnGame()
    {
        Server server = new Server(8080);
        Client p1     = TestObjects.BuildClient(server);
        Client p2     = TestObjects.BuildClient(server);
        Game   game   = new Game();

        server.RunningGames.AddLast(game);
        game.Players.Add(p1);
        game.Players.Add(p2);

        Vector2 projectilePosition = new Vector2(0, 0);
        Vector2 velocity           = new Vector2(2, 3);

        game.Board.CreateProjectile(0, projectilePosition, velocity, (int)ProjectileType.BouncingBall);

        server.UpdateIteration(game.LastUpdateTime);
        Assert.AreEqual(projectilePosition, game.Board.Projectiles[0].Position);

        server.UpdateIteration(game.LastUpdateTime.AddMilliseconds(500));
        Assert.AreEqual(projectilePosition + velocity / 2, game.Board.Projectiles[0].Position);
    }