private static void OnCardRequest(PirateClient pclient) { var c = pclient.GetPlayableCard(); Assert.That(pclient.HasCard(c)); pclient.PlayCard(pclient.GetPlayableCard()); Assert.That(!pclient.HasCard(c)); }
private static void OnBetRequest(PirateClient pclient) { var bet = CollectionFnc.PickRandom(0, pclient.Hand.Count); Assert.That(bet >= 0 && bet <= pclient.Game.CardsToDeal); pclient.SetBet(bet); Assert.That(pclient.Bet == bet); }
public void DealCard( PirateClient pclient, Player receiver, Card card ) { PirateClientCommands.DealCard(pclient, receiver, card); // TODO: add assertions to method PirateClientCommandsTest.DealCard(PirateClient, Player, Card) }
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 ErrorMessage( PirateHost host, PirateClient pclient, PirateError error ) { PirateHostCommands.ErrorMessage(host, pclient, error); // TODO: add assertions to method PirateHostCommandsTest.ErrorMessage(PirateHost, PirateClient, PirateError) }
public PirateClient Constructor02( string name, string ip, int port ) { PirateClient target = new PirateClient(name, ip, port); return target; // TODO: add assertions to method PirateClientTest.Constructor02(String, String, Int32) }
/// <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); }
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) {} }
/// <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; } }
public void GameStarted(PirateClient pclient, PirateMessage data) { PirateClientCommands.GameStarted(pclient, data); // TODO: add assertions to method PirateClientCommandsTest.GameStarted(PirateClient, PirateMessage) }
public void ErrorMessage(PirateClient pclient, PirateMessage msg) { PirateClientCommands.ErrorMessage(pclient, msg); // TODO: add assertions to method PirateClientCommandsTest.ErrorMessage(PirateClient, PirateMessage) }
public void FinishRound(PirateClient pclient, PirateMessage data) { PirateClientCommands.FinishRound(pclient, data); // TODO: add assertions to method PirateClientCommandsTest.FinishRound(PirateClient, PirateMessage) }
/// <summary> /// Verify connection with host. /// </summary> /// <param name="pclient">The client.</param> /// <param name="data">Data received from host.</param> public static void VerifyConnection(PirateClient pclient, PirateMessage data) { Contract.Requires(pclient != null && data != null && data.Head == PirateMessageHead.Init); var msg = new PirateMessage(PirateMessageHead.Verf, data.Body); pclient.SendMessage(msg); }
/// <summary> /// Set the bet. /// </summary> /// <param name="pclient">The client.</param> /// <param name="bet">Bet to use.</param> public static void SetBet(PirateClient pclient, int bet) { Contract.Requires(pclient != null & bet >= 0); var msg = new PirateMessage(PirateMessageHead.Pbet, bet.ToString()); pclient.SendMessage(msg); }
/// <summary> /// Disconnect a client. /// </summary> /// <param name="pclient">The client to disconnect.</param> private void SocketDisconnect(PirateClient pclient) { Contract.Requires(pclient != null); if (pclient.Socket != null) { this.RemoveClient(pclient); if (pclient.Socket.RemoteEndPoint != null) { var clientIp = pclient.Socket.RemoteEndPoint.ToString(); if (DebugMode) Console.WriteLine("Client disconnected " + (!string.IsNullOrEmpty(pclient.Name) ? "[" + pclient.Name + "]" : "") + ": " + clientIp); } if (pclient.Socket.Connected) { pclient.Socket.Close(); } if(Started && !string.IsNullOrEmpty(pclient.Name)) { PirateHostCommands.SendPlayerInfo(this); } } }
/// <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; } } }
/// <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); } }
/// <summary> /// Checks whether or not the specified player is contained in the host player list. /// </summary> /// <param name="pclient">Player to check for.</param> /// <returns>True if the player is within, false if not.</returns> public bool ContainsPlayer(PirateClient pclient) { Contract.Requires(pclient != null); return ContainsPlayer(pclient.Socket); }
public void GetPlayersInGame(PirateClient pclient, PirateMessage data) { PirateClientCommands.GetPlayersInGame(pclient, data); // TODO: add assertions to method PirateClientCommandsTest.GetPlayersInGame(PirateClient, PirateMessage) }
public void InitConnection(PirateClient pclient) { PirateClientCommands.InitConnection(pclient); // TODO: add assertions to method PirateClientCommandsTest.InitConnection(PirateClient) }
public void NewPile(PirateClient pclient, PirateMessage data) { PirateClientCommands.NewPile(pclient, data); // TODO: add assertions to method PirateClientCommandsTest.NewPile(PirateClient, PirateMessage) }
/// <summary> /// Remove a client. /// </summary> /// <param name="pclient">Client to be removed.</param> public void RemoveClient(PirateClient pclient) { Contract.Requires(pclient != null); if (!this.Clients.ContainsKey(pclient.Socket)) { return; } lock (Players) { if (!String.IsNullOrEmpty(this.Clients[pclient.Socket].Name)) { if (this.Players.ContainsKey(this.Clients[pclient.Socket].Name)) { this.Players.Remove(this.Clients[pclient.Socket].Name); } } } lock (Clients) { this.Clients.Remove(pclient.Socket); } UpdateBroadcastInfo(); }
public void PlayCard(PirateClient pclient, Card card) { PirateClientCommands.PlayCard(pclient, card); // TODO: add assertions to method PirateClientCommandsTest.PlayCard(PirateClient, Card) }
/// <summary> /// Set the name of the specified player. /// </summary> /// <param name="pclient">The player to set the name for.</param> /// <param name="name">The name </param> public void SetPlayerName(PirateClient pclient, string name) { Contract.Requires(pclient != null && name != null && this.Clients.ContainsKey(pclient.Socket)); lock (Players) { if (!String.IsNullOrEmpty(this.Clients[pclient.Socket].Name)) { if (this.Players.ContainsKey(this.Clients[pclient.Socket].Name)) { this.Players.Remove(this.Clients[pclient.Socket].Name); } } this.Players.Add(name, pclient.Socket); } pclient.SetName(name); Console.WriteLine("Set name for " + pclient.Socket.RemoteEndPoint + " to " + name); UpdateBroadcastInfo(); }
public void SendPlayerInfo(PirateClient pclient) { PirateClientCommands.SendPlayerInfo(pclient); // TODO: add assertions to method PirateClientCommandsTest.SendPlayerInfo(PirateClient) }
/// <summary> /// New connection established. /// </summary> /// <param name="ar">AsyncResult containing information about the asynchronous operation.</param> private void SocketConnected(IAsyncResult ar) { Contract.Requires(ar != null && ar.AsyncState is PirateHost); try { var host = (PirateHost)ar.AsyncState; if (Started) { var client = host.Listener.EndAcceptSocket(ar); var pclient = new PirateClient(client); if(AcceptNewConnections) { if (DebugMode) Console.WriteLine("Client connected: " + client.RemoteEndPoint.ToString()); host.WaitForSocket(); // Wait for more if (!host.Clients.ContainsKey(pclient.Socket)) { host.Clients.Add(pclient.Socket, pclient); SocketMessageReceive(pclient); } else { PirateHostCommands.ErrorMessage(this, pclient, PirateError.AlreadyConnected); } } else { PirateHostCommands.ErrorMessage(this, pclient, PirateError.NoNewConnections); } } } catch (SocketException ex) { if(!IgnoreSocketErrors.Contains(ex.SocketErrorCode)) Console.WriteLine("SocketException: " + ex); } catch (Exception ex) { Console.WriteLine("Exception: " + ex); } }
public void SetBet(PirateClient pclient, int bet) { PirateClientCommands.SetBet(pclient, bet); // TODO: add assertions to method PirateClientCommandsTest.SetBet(PirateClient, Int32) }
/// <summary> /// Start receiving messages from a client. /// </summary> /// <param name="pclient">The client to receive messages from.</param> private void SocketMessageReceive(PirateClient pclient) { Contract.Requires(pclient != null); var mobj = new PirateMessageObj(pclient); pclient.Socket.BeginReceive( mobj.Buffer, 0, mobj.Buffer.Length, SocketFlags.None, SocketMessageReceived, mobj ); }
public void VerifyConnection(PirateClient pclient, PirateMessage data) { PirateClientCommands.VerifyConnection(pclient, data); // TODO: add assertions to method PirateClientCommandsTest.VerifyConnection(PirateClient, PirateMessage) }