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) {}
        }
Example #2
0
 /// <summary>
 /// Helper method for taking action upon a button press
 /// </summary>
 /// <param name="b">The button that has been pressed</param>
 private void ButtonAction(Button b)
 {
     Contract.Requires(b != null);
     Contract.Ensures(
         game.State == GameState.StartUp
         ||
         (PirateHost.IsValidGameName(serverName.Text)
              ? (game.Host != null && game.Client != null && game.PlayingGame != null
                 && game.State == GameState.InGame)
              : game.State == GameState.CreateGame));
     var str = b.Name;
     switch(str) {
         case "creategame":
             if(serverName.Text == "" || !PirateHost.IsValidGameName(serverName.Text)) {
                 return;
             }
             var players = numberOfPlayers.ParseInput();
             var sName = serverName.Text;
             var host = new PirateHost(4939);
             host.Start(sName, players);
             var client = new PirateClient(game.PlayerName, host.Ip, 4939);
             PirateClientCommands.SendPlayerInfo(client);
             var playingGame = new Game();
             game.GameName = sName;
             game.MaxPlayers = players;
             game.Host = host;
             game.Client = client;
             client.SetGame(playingGame);
             game.PlayingGame = playingGame;
             game.State = GameState.InGame;
             break;
         case "cancelcg":
             game.State = GameState.StartUp;
             break;
     }
 }
        /// <summary>
        /// Set player info.
        /// </summary>
        /// <param name="host">The host.</param>
        /// <param name="pclient">The client.</param>
        /// <param name="data">The data received from client.</param>
        public static void SetPlayerInfo(PirateHost host, PirateClient pclient, PirateMessage data)
        {
            Contract.Requires(host != null && pclient != null && data != null && data.Head == PirateMessageHead.Pnfo);
            var player = PirateMessage.GetPlayerName(data);
            if (player == null) return;

            if (!host.ContainsPlayer(player)) {
                if(host.Game.Contains(pclient)) host.Game.RemovePlayer(pclient);
                host.SetPlayerName(pclient, player);
                host.Game.ClearPlayers();
                host.Game.AddPlayers(host.GetPlayers());
                pclient.SetGame(host.Game);
                SendPlayerInfo(host);
            }else {
                var msg = new PirateMessage(PirateMessageHead.Erro, PirateError.NameAlreadyTaken.ToString());
                host.SendMessage(pclient, msg);
            }
        }
Example #4
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;
        }