public void GameListReply_TestEverything()
        {
            ProcessInfo gm1 = new ProcessInfo()
                {
                    ProcessId=1,
                    Label="Game Manager 1",
                    EndPoint = new PublicEndPoint() { Host = "buzz.serv.usu.edu", Port = 20011 }
                };
            ProcessInfo gm2 = new ProcessInfo()
                {
                    ProcessId=2,
                    Label="Game Manager 2",
                    EndPoint = new PublicEndPoint() { Host = "buzz.serv.usu.edu", Port = 20022 }
                };

            GameInfo g1 = new GameInfo()
            {
                GameManager = gm1,
                GameId = 10,
                Label = "Test Game #1",
                MinPlayers = 3,
                MaxPlayers = 5,
                Status = GameInfo.StatusCode.Available
            };

            GameInfo g2 = new GameInfo()
            {
                GameManager = gm2,
                GameId = 12,
                Label = "Test Game #2",
                MinPlayers = 3,
                MaxPlayers = 5,
                Status = GameInfo.StatusCode.Available
            };

            GameListReply r1 = new GameListReply() { Success = true, Note = "Test note", GameInfo = new GameInfo[] { g1, g2 } };

            byte[] bytes = r1.Encode();
            string tmp = ASCIIEncoding.ASCII.GetString(bytes);

            GameListReply r2 = Message.Decode(bytes) as GameListReply;
            Assert.IsNotNull(r2);
        }
Exemple #2
0
 private void Receive(GameListReply reply)
 {
     logger.Debug("Received Game List Reply");
     if (reply.Success)
     {
         GamesList = reply.GameInfo;
     }
     else
         logger.Error(reply.Note);
 }
        public void TestGetGames()
        {
            // Create udp client to mock registry
            UdpClient mockClient = new UdpClient(0);
            int mockClientPort = ((IPEndPoint)mockClient.Client.LocalEndPoint).Port;
            PublicEndPoint mockClientEP = new PublicEndPoint()
            {
                Host = "127.0.0.1",
                Port = mockClientPort
            };

            // Create fake player
            TestablePlayer player = new TestablePlayer()
            {
                RegistryEndPoint = mockClientEP
            };
            player.CurrentState = new GetGamesState(player);

            // Run player
            Thread playerThread = new Thread(new ThreadStart(player.Start));
            playerThread.Start();

            // Get message registry client
            IPEndPoint senderEP = new IPEndPoint(IPAddress.Any, 0);
            byte[] bytes = mockClient.Receive(ref senderEP);

            // Assert on GameListRequest
            Message msg = Message.Decode(bytes);
            Assert.IsNotNull(msg);
            Assert.IsTrue(msg is GameListRequest);
            GameListRequest request = msg as GameListRequest;
            Assert.AreEqual(request.StatusFilter, (int)GameInfo.StatusCode.Available);

            // Send GameListReply from registry to player
            GameListReply reply = new GameListReply()
            {
                GameInfo = new GameInfo[] {
                    new GameInfo() {
                        GameId = 7,
                        Label = "Test Game",
                        Status = GameInfo.StatusCode.Available,
                        GameManager = new ProcessInfo()
                        {
                            EndPoint = new PublicEndPoint()
                            {
                                HostAndPort = senderEP.ToString()
                            }
                        }
                    }
                },
                Success = true
            };
            bytes = reply.Encode();
            mockClient.Send(bytes, bytes.Length, senderEP);

            // Assert on GameListReply, player state
            Thread.Sleep(2000);
            player.Stop();
            playerThread.Join();
            Assert.AreEqual(player.GamesList.Length, 1);
            GameInfo playerInfo = player.GamesList[0];
            GameInfo replyInfo = reply.GameInfo[0];
            Assert.AreEqual(playerInfo.GameId, replyInfo.GameId);
            Assert.AreEqual(playerInfo.Label, replyInfo.Label);
            Assert.AreEqual(playerInfo.Status, replyInfo.Status);
            Assert.AreEqual(playerInfo.GameManager.EndPoint.HostAndPort, replyInfo.GameManager.EndPoint.HostAndPort);
            Assert.IsTrue(player.CurrentState is JoinGameState);
        }