/// <summary>
        /// Deal card.
        /// </summary>
        /// <param name="host">The host.</param>
        /// <param name="data">Data received from client.</param>
        public static void DealCard(PirateHost host, PirateMessage data)
        {
            Contract.Requires(host != null && data != null && data.Head == PirateMessageHead.Xcrd);
            var player = PirateMessage.GetPlayerName(data);
            if(player == null) {
                return;
            }

            var pclient = host.PlayerFromString(player);
            if(pclient == null) {
                return;
            }

            var card = Card.FromString(data.Body);
            if(card == null) {
                return;
            }

            pclient.GetCard(card);

            Console.WriteLine("Host: Sending card " + card.ToShortString() + " to " + pclient);

            var msg = new PirateMessage(PirateMessageHead.Xcrd, card.ToString());
            host.SendMessage(pclient, msg);

            if(host.Game.Round.CardsDealt == host.Game.Round.TotalCards) {
                RequestBets(host);
            }
        }
 public void GetBytes366()
 {
     PirateMessage pirateMessage;
     byte[] bs;
     pirateMessage =
       new PirateMessage(PirateMessageHead.Fail, "\0\0\0\0\0\0\udc00\0\0\0\0");
     bs = this.GetBytes(pirateMessage);
     Assert.IsNotNull((object)bs);
     Assert.AreEqual<int>(21, bs.Length);
     Assert.AreEqual<byte>((byte)48, bs[0]);
     Assert.AreEqual<byte>((byte)48, bs[1]);
     Assert.AreEqual<byte>((byte)49, bs[2]);
     Assert.AreEqual<byte>((byte)55, bs[3]);
     Assert.AreEqual<byte>((byte)70, bs[4]);
     Assert.AreEqual<byte>((byte)65, bs[5]);
     Assert.AreEqual<byte>((byte)73, bs[6]);
     Assert.AreEqual<byte>((byte)76, bs[7]);
     Assert.AreEqual<byte>((byte)0, bs[8]);
     Assert.AreEqual<byte>((byte)0, bs[9]);
     Assert.AreEqual<byte>((byte)0, bs[10]);
     Assert.AreEqual<byte>((byte)0, bs[11]);
     Assert.AreEqual<byte>((byte)0, bs[12]);
     Assert.AreEqual<byte>((byte)0, bs[13]);
     Assert.AreEqual<byte>((byte)239, bs[14]);
     Assert.AreEqual<byte>((byte)191, bs[15]);
     Assert.AreEqual<byte>((byte)189, bs[16]);
     Assert.AreEqual<byte>((byte)0, bs[17]);
     Assert.AreEqual<byte>((byte)0, bs[18]);
     Assert.AreEqual<byte>((byte)0, bs[19]);
     Assert.AreEqual<byte>((byte)0, bs[20]);
     Assert.IsNotNull((object)pirateMessage);
     Assert.AreEqual<PirateMessageHead>(PirateMessageHead.Fail, pirateMessage.Head);
     Assert.AreEqual<string>("\0\0\0\0\0\0\udc00\0\0\0\0", pirateMessage.Body);
 }
 public void GetPlayerName816()
 {
     PirateMessage pirateMessage;
     string s;
     pirateMessage = new PirateMessage(PirateMessageHead.Fail, "player_name: 000");
     s = this.GetPlayerName(pirateMessage);
     Assert.AreEqual<string>("000", s);
 }
 public void GetGameName787()
 {
     PirateMessage pirateMessage;
     string s;
     pirateMessage = new PirateMessage(PirateMessageHead.Fail, "game_name: A");
     s = this.GetGameName(pirateMessage);
     Assert.AreEqual<string>("A", s);
 }
 public void GetWinner103()
 {
     PirateMessage pirateMessage;
     string s;
     pirateMessage = new PirateMessage(PirateMessageHead.Fail, "winning_player: a0");
     s = this.GetWinner(pirateMessage);
     Assert.AreEqual<string>("a0", s);
 }
 public void GetDealer695()
 {
     PirateMessage pirateMessage;
     string s;
     pirateMessage = new PirateMessage(PirateMessageHead.Fail, "dealer: 00000000");
     s = this.GetDealer(pirateMessage);
     Assert.AreEqual<string>("00000000", s);
 }
 public void GetRound92()
 {
     PirateMessage pirateMessage;
     int i;
     pirateMessage = new PirateMessage(PirateMessageHead.Fail, "round: 8");
     i = this.GetRound(pirateMessage);
     Assert.AreEqual<int>(8, i);
 }
 public void GetPlayersInGame777()
 {
     PirateMessage pirateMessage;
     int i;
     pirateMessage = new PirateMessage(PirateMessageHead.Fail, "players_ingame: 0");
     i = this.GetPlayersInGame(pirateMessage);
     Assert.AreEqual<int>(0, i);
 }
 public void GetStartingPlayer6001()
 {
     PirateMessage pirateMessage;
     string s;
     pirateMessage = new PirateMessage(PirateMessageHead.Fail, "starting_player: 00");
     s = this.GetStartingPlayer(pirateMessage);
     Assert.AreEqual<string>("00", s);
 }
 public void GetWinner424()
 {
     PirateMessage pirateMessage;
     string s;
     pirateMessage =
       new PirateMessage(PirateMessageHead.Fail, "winning_player: 0\u00f6\u803c");
     s = this.GetWinner(pirateMessage);
     Assert.AreEqual<string>("", s);
 }
 public void InitConnection(
     PirateHost host,
     PirateClient pclient,
     PirateMessage data
 )
 {
     PirateHostCommands.InitConnection(host, pclient, data);
     // TODO: add assertions to method PirateHostCommandsTest.InitConnection(PirateHost, PirateClient, PirateMessage)
 }
 public void GetPlayerNames138()
 {
     PirateMessage pirateMessage;
     HashSet<string> hashSet;
     pirateMessage = new PirateMessage(PirateMessageHead.Fail, "");
     hashSet = this.GetPlayerNames(pirateMessage);
     Assert.IsNotNull((object)hashSet);
     Assert.AreEqual<int>(0, hashSet.Count);
     Assert.IsNotNull(hashSet.Comparer);
 }
 public void GetPlayerScores544()
 {
     PirateMessage pirateMessage;
     Dictionary<string, int> dictionary;
     pirateMessage = new PirateMessage(PirateMessageHead.Fail, new string('\0', 14));
     dictionary = this.GetPlayerScores(pirateMessage);
     Assert.IsNotNull((object)dictionary);
     Assert.IsNotNull(dictionary.Comparer);
     Assert.AreEqual<int>(0, dictionary.Count);
 }
 public void GetPlayerScores440()
 {
     PirateMessage pirateMessage;
     Dictionary<string, int> dictionary;
     pirateMessage = new PirateMessage(PirateMessageHead.Fail, "player_score: a;-0");
     dictionary = this.GetPlayerScores(pirateMessage);
     Assert.IsNotNull((object)dictionary);
     Assert.IsNotNull(dictionary.Comparer);
     Assert.AreEqual<int>(1, dictionary.Count);
 }
 public void GetPlayerTricks393()
 {
     PirateMessage pirateMessage;
     Dictionary<string, int> dictionary;
     pirateMessage = new PirateMessage(PirateMessageHead.Fail, "player_tricks: Z");
     dictionary = this.GetPlayerTricks(pirateMessage);
     Assert.IsNotNull((object)dictionary);
     Assert.IsNotNull(dictionary.Comparer);
     Assert.AreEqual<int>(0, dictionary.Count);
 }
        /// <summary>
        /// Deal card to player.
        /// </summary>
        /// <param name="pclient">The client.</param>
        /// <param name="receiver">Receiving player.</param>
        /// <param name="card">Card dealt.</param>
        public static void DealCard(PirateClient pclient, Player receiver, Card card)
        {
            Contract.Requires(pclient != null && receiver != null && card != null);
            var body = PirateMessage.ConstructBody(PirateMessage.ConstructPlayerName(receiver.Name), card.ToString());
            var msg = new PirateMessage(PirateMessageHead.Xcrd, body);

            if(!pclient.VirtualPlayer)
                Console.WriteLine(pclient.Name + ": Dealing " + card.ToShortString() + " to " + receiver.Name);
            pclient.SendMessage(msg);
        }
示例#17
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="port">The port to communicate over.</param>
        public PirateHost(int port)
        {
            Contract.Requires(port > 0 && port <= 65535);
            this.Ip = PirateScanner.GetLocalIpV4();
            this.Port = port;

            this.Clients = new OrderedDictionary<Socket, PirateClient>();
            this.Players = new Dictionary<string, Socket>();
            this.Listener = new TcpListener(new IPEndPoint(IPAddress.Any, this.Port));

            var msg = new PirateMessage(PirateMessageHead.Bcst, Ip.ToString());
            this.Broadcaster = new PirateBroadcaster(this.Port, 6250);
        }
 public void GetHostIpThrowsContractException240()
 {
     try
     {
       PirateMessage pirateMessage;
       IPAddress iPAddress;
       pirateMessage = new PirateMessage(PirateMessageHead.Fail, "host_ip: .");
       iPAddress = this.GetHostIp(pirateMessage);
       throw
     new AssertFailedException("expected an exception of type ContractException");
     }
     catch(Exception ex)
     {
       if (!PexContract.IsContractException(ex))
     throw ex;
     }
 }
 public void GetDealerThrowsContractException313()
 {
     try
     {
       PirateMessage pirateMessage;
       string s;
       pirateMessage = new PirateMessage(PirateMessageHead.Fail, "");
       s = this.GetDealer(pirateMessage);
       throw
     new AssertFailedException("expected an exception of type ContractException");
     }
     catch(Exception ex)
     {
       if (!PexContract.IsContractException(ex))
     throw ex;
     }
 }
 public void GetGameNameThrowsContractException331()
 {
     try
     {
       PirateMessage pirateMessage;
       string s;
       pirateMessage = new PirateMessage(PirateMessageHead.Fail, "game_name: \0\0\0");
       s = this.GetGameName(pirateMessage);
       throw
     new AssertFailedException("expected an exception of type ContractException");
     }
     catch(Exception ex)
     {
       if (!PexContract.IsContractException(ex))
     throw ex;
     }
 }
 public void GetMaxPlayersInGameThrowsContractException371()
 {
     try
     {
       PirateMessage pirateMessage;
       int i;
       pirateMessage = new PirateMessage(PirateMessageHead.Fail, "");
       i = this.GetMaxPlayersInGame(pirateMessage);
       throw
     new AssertFailedException("expected an exception of type ContractException");
     }
     catch(Exception ex)
     {
       if (!PexContract.IsContractException(ex))
     throw ex;
     }
 }
        /// <summary>
        /// Begin round.
        /// </summary>
        /// <param name="host">The host.</param>
        public static void BeginRound(PirateHost host)
        {
            Contract.Requires(host != null && host.Game.Round.BetsDone);

            var bets = new HashSet<string>();
            foreach(var player in host.GetPlayers()) {
                bets.Add(PirateMessage.ConstructPlayerBet(player));
            }
            bets.Add(PirateMessage.ConstructRoundNumber(host.Game.CurrentRound));

            var msg = new PirateMessage(PirateMessageHead.Bgrn, PirateMessage.ConstructBody(bets));

            foreach(var player in host.GetPlayers()) {
                host.SendMessage(player, msg);
            }
            lock (host.Game.Round) {
                host.Game.Round.Begin();
            }
        }
 public void GetBytes641()
 {
     PirateMessage pirateMessage;
     byte[] bs;
     pirateMessage = new PirateMessage(PirateMessageHead.Fail, "");
     bs = this.GetBytes(pirateMessage);
     Assert.IsNotNull((object)bs);
     Assert.AreEqual<int>(8, bs.Length);
     Assert.AreEqual<byte>((byte)48, bs[0]);
     Assert.AreEqual<byte>((byte)48, bs[1]);
     Assert.AreEqual<byte>((byte)48, bs[2]);
     Assert.AreEqual<byte>((byte)52, bs[3]);
     Assert.AreEqual<byte>((byte)70, bs[4]);
     Assert.AreEqual<byte>((byte)65, bs[5]);
     Assert.AreEqual<byte>((byte)73, bs[6]);
     Assert.AreEqual<byte>((byte)76, bs[7]);
     Assert.IsNotNull((object)pirateMessage);
     Assert.AreEqual<PirateMessageHead>(PirateMessageHead.Fail, pirateMessage.Head);
     Assert.AreEqual<string>("", pirateMessage.Body);
 }
        /// <summary>
        /// A round has begun.
        /// </summary>
        /// <param name="pclient">The client.</param>
        /// <param name="data">Data received from host.</param>
        public static void BeginRound(PirateClient pclient, PirateMessage data)
        {
            Contract.Requires(pclient != null && data != null && data.Head == PirateMessageHead.Bgrn);
            var bets = PirateMessage.GetPlayerBets(data);
            var round = PirateMessage.GetRound(data);

            if (pclient.Game.CurrentRound != round) return;

            if(!pclient.VirtualPlayer)
                Console.WriteLine("Player bets:");
            foreach(var kvp in bets) {
                pclient.Game.Round.PlayerBet(kvp.Key, kvp.Value);
                if(!pclient.VirtualPlayer)
                    Console.WriteLine("\t" + kvp.Key + ": " + kvp.Value);
            }

            if (!pclient.Game.Round.BetsDone) return;
            if(!pclient.VirtualPlayer)
                Console.WriteLine("Round " + round + " has begun.");

            pclient.Game.Round.Begin();
        }
 /// <summary>
 /// Receive an error message.
 /// </summary>
 /// <param name="pclient">The client.</param>
 /// <param name="msg">Message to send.</param>
 public static void ErrorMessage(PirateClient pclient, PirateMessage msg)
 {
     Contract.Requires(pclient != null && msg != null && msg.Head == PirateMessageHead.Erro);
     var err = PirateMessage.GetError(msg.Body);
     switch(err) {
         case PirateError.AlreadyConnected:
             Console.WriteLine("You're already conncted!");
             break;
         case PirateError.InvalidBet:
             Console.WriteLine("Invalid bet specified!");
             break;
         case PirateError.NameAlreadyTaken:
             Console.WriteLine("Name is already taken!");
             pclient.NameNotAvailable();
             break;
         case PirateError.NoNewConnections:
             Console.WriteLine("No more players can connect!");
             break;
         case PirateError.Unknown:
             Console.WriteLine("Unknown error happened!");
             break;
     }
 }
示例#26
0
 /// <summary>
 /// Update broadcast information.
 /// </summary>
 private void UpdateBroadcastInfo()
 {
     var msg = new PirateMessage(PirateMessageHead.Bcst, PirateMessage.ConstructHostInfo(this));
     this.Broadcaster.Message = msg.GetBytes();
 }
示例#27
0
 /// <summary>
 /// Handle message received.
 /// </summary>
 /// <param name="pclient">Client that send the message.</param>
 /// <param name="msg">The message.</param>
 private void HandleMessage(PirateClient pclient, PirateMessage msg)
 {
     Contract.Requires(pclient != null && msg != null);
     if (!Players.ContainsKey(pclient.Name)) {
         switch (msg.Head) {
             case PirateMessageHead.Knck:
                 PirateHostCommands.KnockKnock(this, pclient);
                 break;
             case PirateMessageHead.Init:
                 PirateHostCommands.InitConnection(this, pclient, msg);
                 break;
             case PirateMessageHead.Verf:
                 PirateHostCommands.VerifyConnection(this, pclient, msg);
                 break;
             case PirateMessageHead.Pnfo:
                 PirateHostCommands.SetPlayerInfo(this, pclient, msg);
                 break;
         }
     } else {
         switch (msg.Head) {
             case PirateMessageHead.Xcrd:
                 PirateHostCommands.DealCard(this, msg);
                 break;
             case PirateMessageHead.Pcrd:
                 PirateHostCommands.PlayCard(this, msg);
                 break;
             case PirateMessageHead.Pbet:
                 PirateHostCommands.ReceiveBet(this, pclient, msg);
                 break;
         }
     }
 }
示例#28
0
 /// <summary>
 /// Send message to a client.
 /// </summary>
 /// <param name="pclient">Client to send to.</param>
 /// <param name="msg">Message to send.</param>
 public void SendMessage(PirateClient pclient, PirateMessage msg)
 {
     Contract.Requires(pclient != null && msg != null);
     try {
         byte[] buffer = msg.GetBytes();
         pclient.Socket.BeginSend(
             buffer, 0, buffer.Length, SocketFlags.None, MessageSent, new PirateMessageObj(pclient, msg));
     } catch(SocketException ex) {
         if(!IgnoreSocketErrors.Contains(ex.SocketErrorCode)) Console.WriteLine("SocketException:" + ex);
         this.SocketDisconnect(pclient);
     }catch(Exception ex){
         Console.WriteLine(ex);
     }
 }
 public void GetPlayersInGame(PirateClient pclient, PirateMessage data)
 {
     PirateClientCommands.GetPlayersInGame(pclient, data);
     // TODO: add assertions to method PirateClientCommandsTest.GetPlayersInGame(PirateClient, PirateMessage)
 }
 public void GameStarted(PirateClient pclient, PirateMessage data)
 {
     PirateClientCommands.GameStarted(pclient, data);
     // TODO: add assertions to method PirateClientCommandsTest.GameStarted(PirateClient, PirateMessage)
 }