示例#1
0
        private static void Host()
        {
            Console.WriteLine("Creating host");
            var host = new PirateHost(4939) { DebugMode = true };
            host.Start();
            Console.WriteLine("Host started");
            Console.Title = "Host";
            while (host.Started) {
                var cmd = Console.ReadLine();
                switch(cmd) {
                    case "start":
                    case "s":
                        if (!host.Game.Started) {
                            if (host.Game.Players.Count >= 2) {
                                host.StartGame();
                                host.Game.GameFinished += GameFinished;
                            } else {
                                Console.WriteLine("Not enough players to start the game!");
                            }
                        }else {
                            Console.WriteLine("Game already started!");
                        }
                        break;

                    case "exit":
                    case "e":
                        host.Stop();
                        break;
                }
            }
        }
示例#2
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) {}
        }
示例#3
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;
     }
 }