Exemplo n.º 1
0
        public void TestMultiPlayerConnectDisconnect()
        {
            this.NewClient(out GameClient <ClientPlayer> client1, out ClientListener clientListener1);
            this.NewClient(out GameClient <ClientPlayer> client2, out ClientListener clientListener2);
            this.NewClient(out GameClient <ClientPlayer> client3, out ClientListener clientListener3);
            this.NewServer(out GameServer <ServerPlayer> server, out ServerListener serverListener);

            void UpdateAction()
            {
                server.Update();
                client1.Update();

                MainThreadDispatcher.Execute();

                server.Update();
                client2.Update();

                MainThreadDispatcher.Execute();

                server.Update();
                client3.Update();

                MainThreadDispatcher.Execute();
            }

            server.Start(5000);

            UpdateAction();

            client1.Connect(hostIp, 5000);

            UpdateAction();

            client2.Connect(hostIp, 5000);

            UpdateAction();

            client3.Connect(hostIp, 5000);

            UpdateAction();

            Assert.IsTrue(clientListener1.connectedCalled);
            Assert.IsTrue(clientListener2.connectedCalled);
            Assert.IsTrue(clientListener3.connectedCalled);

            Assert.IsNotNull(clientListener1.localPlayer);
            Assert.IsNotNull(clientListener2.localPlayer);
            Assert.IsNotNull(clientListener3.localPlayer);

            var player1 = client1.playerCollection.FindPlayer(player => player.isLocalPlayer);
            var player2 = client2.playerCollection.FindPlayer(player => player.isLocalPlayer);
            var player3 = client3.playerCollection.FindPlayer(player => player.isLocalPlayer);

            Assert.AreEqual(player1.playerId, serverListener.connectedPlayers[0].playerId);
            Assert.AreEqual(player2.playerId, serverListener.connectedPlayers[1].playerId);
            Assert.AreEqual(player3.playerId, serverListener.connectedPlayers[2].playerId);

            var playerId1 = player1.playerId;
            var playerId2 = player2.playerId;
            var playerId3 = player3.playerId;

            var serverPlayer1 = server.playerCollection.FindPlayer(playerId1);
            var serverPlayer2 = server.playerCollection.FindPlayer(playerId2);
            var serverPlayer3 = server.playerCollection.FindPlayer(playerId3);

            Assert.IsNotNull(serverPlayer1);
            Assert.IsNotNull(serverPlayer2);
            Assert.IsNotNull(serverPlayer3);

            UpdateAction();

            client3.Disconnect();

            UpdateAction();

            Assert.IsNotNull(server.playerCollection.FindPlayer(playerId1));
            Assert.IsNotNull(server.playerCollection.FindPlayer(playerId2));
            Assert.IsNull(server.playerCollection.FindPlayer(playerId3));

            Assert.IsFalse(clientListener1.disconnectCalled);
            Assert.IsFalse(clientListener2.disconnectCalled);
            Assert.IsTrue(clientListener3.disconnectCalled);

            Assert.AreEqual(1, clientListener1.disconnectedPlayers.Count);
            Assert.AreEqual(1, clientListener2.disconnectedPlayers.Count);
            Assert.AreEqual(0, clientListener3.disconnectedPlayers.Count);

            Assert.AreEqual(player3.playerId, clientListener1.disconnectedPlayers[0].playerId);
            Assert.AreEqual(player3.playerId, clientListener2.disconnectedPlayers[0].playerId);

            Assert.AreNotEqual(player1.playerId, player2.playerId);
            Assert.AreNotEqual(player1.playerId, player3.playerId);
            Assert.AreNotEqual(player2.playerId, player3.playerId);

            Assert.AreEqual(1, serverListener.disconnectedPlayers.Count);

            Assert.AreEqual(1, client1.playerCollection.values.FindAll(p => p.isLocalPlayer).Count);
            Assert.AreEqual(1, client2.playerCollection.values.FindAll(p => p.isLocalPlayer).Count);
            Assert.AreEqual(0, client3.playerCollection.values.FindAll(p => p.isLocalPlayer).Count);

            server.Stop();

            Thread.Sleep(2000);
        }
Exemplo n.º 2
0
        public void TestClientPingBroadcast()
        {
            this.NewClient(out GameClient <ClientPlayer> client1, out _);
            this.NewClient(out GameClient <ClientPlayer> client2, out _);
            this.NewServer(out GameServer <ServerPlayer> server, out _);

            void Update()
            {
                server.Update();
                client1.Update();

                MainThreadDispatcher.Execute();

                server.Update();
                client2.Update();

                MainThreadDispatcher.Execute();
            }

            server.Start(5000);

            Update();

            client1.Connect(hostIp, 5000);

            Update();

            client2.Connect(hostIp, 5000);

            Update();
            Update();
            Update();
            Update();

            var player1       = client1.playerCollection.FindPlayer(player => player.isLocalPlayer);
            var player2       = client2.playerCollection.FindPlayer(player => player.isLocalPlayer);
            var serverPlayer1 = server.playerCollection.FindPlayer(player1.playerId);
            var serverPlayer2 = server.playerCollection.FindPlayer(player2.playerId);

            var serverPing1 = serverPlayer1.mostRecentPingValue;
            var serverPing2 = serverPlayer2.mostRecentPingValue;

            Assert.AreNotEqual(player1.mostRecentPingValue, 0F);
            Assert.AreNotEqual(player2.mostRecentPingValue, 0F);
            Assert.Less(MathF.Abs(serverPing1 - player1.mostRecentPingValue), 0.5F);
            Assert.Less(MathF.Abs(serverPing2 - player2.mostRecentPingValue), 0.5F);

            Update();
            Update();
            Update();

            var client1client2Ping = client1.playerCollection[player2.playerId].mostRecentPingValue;
            var client2client1Ping = client2.playerCollection[player1.playerId].mostRecentPingValue;

            Assert.AreNotEqual(client1client2Ping, 0);
            Assert.AreNotEqual(client2client1Ping, 0);
            Assert.Less(MathF.Abs(player1.mostRecentPingValue - client2client1Ping), 0.5F);
            Assert.Less(MathF.Abs(player2.mostRecentPingValue - client1client2Ping), 0.5F);

            server.Stop();

            Thread.Sleep(2000);
        }
Exemplo n.º 3
0
        public void TestConnectDisconnect()
        {
            this.NewServer(out GameServer <ServerPlayer> server, out ServerListener serverListener);
            this.NewClient(out GameClient <ClientPlayer> client, out ClientListener clientListener);

            server.Start(5000);

            server.Update();

            client.Connect(hostIp, 5000);

            server.Update();
            client.Update();

            MainThreadDispatcher.Execute();

            server.Update();
            client.Update();

            MainThreadDispatcher.Execute();

            server.Update();
            client.Update();

            MainThreadDispatcher.Execute();

            Assert.IsTrue(clientListener.connectedCalled);

            var player = client.playerCollection.FindPlayer(player => player.isLocalPlayer);

            Assert.IsNotNull(player);
            Assert.IsNotNull(clientListener.localPlayer);
            Assert.AreEqual(player.playerId, serverListener.connectedPlayers[0].playerId);

            var playerId     = player.playerId;
            var serverPlayer = server.playerCollection.FindPlayer(playerId);

            Assert.IsNotNull(serverPlayer);

            client.Disconnect();

            client.Update();
            server.Update();

            MainThreadDispatcher.Execute();

            client.Update();
            server.Update();

            MainThreadDispatcher.Execute();

            var notServerPlayer = server.playerCollection.FindPlayer(playerId);

            Assert.IsTrue(clientListener.disconnectCalled);
            Assert.IsNull(notServerPlayer);

            Assert.AreEqual(player.playerId, serverListener.disconnectedPlayers[0].playerId);

            Assert.AreEqual(0, clientListener.disconnectedPlayers.Count);

            server.Stop();

            Thread.Sleep(2000);
        }