Пример #1
0
        public void PlayGame()
        {
            PirateHost host = new PirateHost(4939);
            host.Start();
            host.Broadcaster.Interval = 1.1;
            Assert.That(host.Broadcaster.Interval < 1.11 && host.Broadcaster.Interval > 1.09);

            PirateClient player1 = new PirateClient("Player1", "127.0.0.1", 4939);
            player1.SetGame(new Game());
            player1.BetRequested += OnBetRequest;
            player1.CardRequested += OnCardRequest;
            player1.Disconnected += OnDisconnect;
            player1.InitConnection();
            while(!host.ContainsPlayer(player1.Name)) {}

            Assert.That(PirateScanner.CheckIp(PirateScanner.GetIp("127.0.0.1"), 4939, 1000));

            PirateClient player2 = new PirateClient("Player2", "127.0.0.1", 4939);
            player2.SetGame(new Game());
            player2.BetRequested += OnBetRequest;
            player2.CardRequested += OnCardRequest;
            player2.Disconnected += OnDisconnect;
            player2.InitConnection();
            while(!host.ContainsPlayer(player2.Name)) {}
            Assert.That(host.ContainsPlayer(player1.Name));

            var ps = new PirateScanner();
            var gameinfos = ps.ScanForGames(4939, 2000);
            Assert.That(gameinfos.Count > 0);
            var gameinfo = gameinfos[0];

            PirateClient player3 = new PirateClient("Player3", gameinfo.Ip, 4939);
            player3.SetGame(new Game());
            player3.BetRequested += OnBetRequest;
            player3.CardRequested += OnCardRequest;
            player3.Disconnected += OnDisconnect;
            player3.InitConnection();
            while(!host.ContainsPlayer(player3.Name)) {}
            Assert.That(player1.Name == host.PlayerFromSocket(host.GetPlayers().First().Socket).Name);

            while(host.Game.Players.Count != 3) {}

            host.StartGame();

            while(!host.Game.Finished) {
                Assert.That(host.Game.Started);
            }

            Assert.That(host.Game.Finished);

            host.Stop();

            while(player1.Socket.Connected || player2.Socket.Connected || player3.Socket.Connected) {}
        }
Пример #2
0
        private static void Player()
        {
            Console.Write("IP to use (empty to scan): ");
            var strIp = Console.ReadLine();
            IPAddress ip = null;
            if(!string.IsNullOrEmpty(strIp)) {
                if(!IPAddress.TryParse(strIp, out ip)) {
                    Console.WriteLine("Invalid IP specified!");
                    Player();
                    return;
                }

                if(!PirateScanner.CheckIp(ip, 4939, 5000)) {
                    Console.WriteLine("No game is hosted at the specified ip!");
                    Player();
                    return;
                }
            }else {
                Console.WriteLine("Scanning for IPs...");
                /*var d = DateTime.Now;
                ip = new PirateScanner().ScanForIp(4939);
                Console.WriteLine("Scan took " + (DateTime.Now - d).TotalMilliseconds + " milliseconds");
                if (ip == null) {
                    Console.WriteLine("No IP found... Make sure there's a host!");
                    return;
                }
                Console.WriteLine("IP Found: " + ip);*/

                var games = new PirateScanner().ScanForGames(4939, 10000);
                if(games.Count > 0) {
                    for(var i = 0; i < games.Count; i++) {
                        Console.WriteLine("\t[" + i + "] " + games[i].Ip + " \"" + games[i].GameName + "\" (" + games[i].Players + "/" + games[i].MaxPlayers + ")");
                    }
                    Console.Write("Select IP (index): ");
                    var ipIndex = Console.ReadLine();
                    if(ipIndex == null || !Regex.IsMatch(ipIndex, "^[0-9]+$")) {
                        Console.WriteLine("Invalid index specified...");
                        Player();
                        return;
                    } else {
                        int index = int.Parse(ipIndex);
                        if (index >= games.Count) {
                            Console.WriteLine("Invalid index specified...");
                            Player();
                            return;
                        }
                        ip = games[index].Ip;
                    }
                }else {
                    Console.WriteLine("No IP found... Make sure there's a host!");
                    Player();
                    return;
                }
            }
            Console.WriteLine();

            var game = new Game();
            var pc = new PirateClient("", ip, 4939);
            pc.SetGame(game);
            pc.NameRequested += OnNameRequest;
            pc.Disconnected += OnDisconnect;
            pc.BetRequested += OnBetRequest;
            pc.CardRequested += OnCardRequest;
            Console.WriteLine("Initiating...");
            pc.InitConnection();

            while(pc.Socket.Connected) {
                Thread.Sleep(10);
            }
            return;
        }