/// <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); } }
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; } } }
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 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> /// 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(); } }
/// <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); } }
/// <summary> /// Round finished. /// </summary> /// <param name="host">The host.</param> public static void RoundFinished(PirateHost host) { Contract.Requires(host != null); var body = PirateMessage.ConstructBody( PirateMessage.ContstructPlayerScores(host.Game.GetRoundScoreTotal(host.Game.CurrentRound))); var msg = new PirateMessage(PirateMessageHead.Frnd, body); foreach (var player in host.GetPlayers()) { host.SendMessage(player, msg); } }
/// <summary> /// Request bets from players. /// </summary> /// <param name="host">The host.</param> public static void RequestBets(PirateHost host) { Contract.Requires(host != null); var msg = new PirateMessage(PirateMessageHead.Breq, ""); foreach(var pclient in host.GetPlayers()) { host.SendMessage(pclient, msg); } }
public static string ConstructHostInfo(PirateHost host) { Contract.Requires(host != null); Contract.Ensures(Contract.Result<string>() != null); return ConstructBody( ConstructHostIp(host), ConstructGameName(host), ConstructPlayersInGame(host.PlayerCount), ConstructMaxPlayersInGame(host.MaxPlayers)); }
public static string ConstructGameName(PirateHost host) { Contract.Requires(host != null); Contract.Ensures(Contract.Result<string>() != null); return "game_name: " + host.GameName; }
public string ConstructHostIp(PirateHost host) { string result = PirateMessage.ConstructHostIp(host); return result; // TODO: add assertions to method PirateMessageTest.ConstructHostIp(PirateHost) }
public void PlayCard(PirateHost host, PirateMessage data) { PirateHostCommands.PlayCard(host, data); // TODO: add assertions to method PirateHostCommandsTest.PlayCard(PirateHost, PirateMessage) }
public void NewRound(PirateHost host) { PirateHostCommands.NewRound(host); // TODO: add assertions to method PirateHostCommandsTest.NewRound(PirateHost) }
/// <summary> /// Play card from player. /// </summary> /// <param name="host">The host.</param> /// <param name="data">Data received from client.</param> public static void PlayCard(PirateHost host, PirateMessage data) { Contract.Requires(host != null && data != null && data.Head == PirateMessageHead.Pcrd); var playerName = PirateMessage.GetPlayerName(data); var player = host.PlayerFromString(playerName); var card = Card.FromString(data.Body); if(!player.CardPlayable(card, host.Game.Round.BoardCards.FirstCard)) { ErrorMessage(host, player, PirateError.CardNotPlayable); var returnCard = new PirateMessage(PirateMessageHead.Xcrd, card.ToString()); host.SendMessage(player, returnCard); RequestCard(host, player); return; } Console.WriteLine(player.Name + " plays " + card.ToShortString()); var msg = new PirateMessage( PirateMessageHead.Pcrd, PirateMessage.ConstructBody(player.ToString(), card.ToString())); foreach(var pclient in host.GetPlayers()) { host.SendMessage(pclient, msg); } player.PlayCard(card); //host.Game.Round.PlayCard(player, card); if(!host.Game.Round.Finished) { RequestCard(host, host.PlayerFromIndex(host.Game.Round.CurrentPlayer)); }else { host.Game.NewRound(); } }
/// <summary> /// Receive bet from player. /// </summary> /// <param name="host">The host.</param> /// <param name="player">The client.</param> /// <param name="msg">The data received from the client.</param> public static void ReceiveBet(PirateHost host, PirateClient player, PirateMessage msg) { Contract.Requires(host != null && player != null && msg != null && msg.Head == PirateMessageHead.Pbet && host.Game.Round.AwaitingBets); lock (host.Game.Round) { var bet = 0; if (int.TryParse(msg.Body, out bet)) { player.SetBet(bet); } else { ErrorMessage(host, player, PirateError.InvalidBet); } if (host.Game.Round.BetsDone) { BeginRound(host); } } }
public static string ConstructHostIp(PirateHost host) { Contract.Requires(host != null); Contract.Ensures(Contract.Result<string>() != null); return "host_ip: " + host.Ip; }
/// <summary> /// Request card from player. /// </summary> /// <param name="host">The host.</param> /// <param name="pclient">The player.</param> public static void RequestCard(PirateHost host, PirateClient pclient) { Contract.Requires(host != null && pclient != null); Console.WriteLine("Sending card request to " + pclient.Name); var msg = new PirateMessage(PirateMessageHead.Creq, ""); host.SendMessage(pclient, msg); }
/// <summary> /// Game finished. /// </summary> /// <param name="host">The host.</param> public static void GameFinished(PirateHost host) { Contract.Requires(host != null); var body = PirateMessage.ConstructBody( PirateMessage.ContstructPlayerScores(host.Game.GetTotalScores())); body = PirateMessage.AppendBody(body, PirateMessage.ConstructWinner(host.Game.Leader)); var msg = new PirateMessage(PirateMessageHead.Gfin, body); foreach (var player in host.GetPlayers()) { host.SendMessage(player, msg); } }
/// <summary> /// Send player info. /// </summary> /// <param name="host">The host.</param> public static void SendPlayerInfo(PirateHost host) { Contract.Requires(host != null); var msg = new PirateMessage(PirateMessageHead.Pigm, PirateMessage.ConstructBody(host.GetPlayers().Select(player => player.ToString()).ToArray())); if (host.PlayerCount > 0) { if(host.DebugMode) Console.WriteLine("Host: Players in game:"); foreach (var player in host.GetPlayers()) { if(host.DebugMode) Console.WriteLine("\t" + player.Name); host.SendMessage(player, msg); } } }
/// <summary> /// Get player info. /// </summary> /// <param name="host">The host.</param> /// <param name="pclient">The client.</param> public static void GetPlayerInfo(PirateHost host, PirateClient pclient) { Contract.Requires(host != null && pclient != null); var msg = new PirateMessage(PirateMessageHead.Pnfo, ""); host.SendMessage(pclient, msg); }
/// <summary> /// Start game. /// </summary> /// <param name="host">The game.</param> public static void StartGame(PirateHost host) { Contract.Requires(host != null && host.PlayerCount >= 2); host.StopAccepting(); var dealerIndex = CollectionFnc.PickRandom(0, host.Game.Players.Count - 1); Console.WriteLine("Starting player is: " + host.Game.Players[dealerIndex].Name); var msg = new PirateMessage(PirateMessageHead.Gstr, PirateMessage.ConstructStartingPlayer(host.Game.Players[dealerIndex])); foreach(var pclient in host.GetPlayers()) { host.SendMessage(pclient, msg); } host.Game.Start(true, dealerIndex); }
/// <summary> /// Reply to a connection initialization. /// </summary> /// <param name="host">The host.</param> /// <param name="pclient">The client.</param> /// <param name="data">The data received from the client.</param> public static void InitConnection(PirateHost host, PirateClient pclient, PirateMessage data) { Contract.Requires(host != null && pclient != null && data != null && data.Head == PirateMessageHead.Init); var msg = new PirateMessage(PirateMessageHead.Init, WelcomePhrase); host.SendMessage(pclient, msg); }
/// <summary> /// Client verification. /// </summary> /// <param name="host">The host.</param> /// <param name="pclient">The client.</param> /// <param name="data">The data received from the client.</param> public static void VerifyConnection(PirateHost host, PirateClient pclient, PirateMessage data) { Contract.Requires(host != null && pclient != null && data != null && data.Head == PirateMessageHead.Verf); if(data.Body == WelcomePhrase) { GetPlayerInfo(host, pclient); } }
/// <summary> /// Reply to a knock. /// </summary> /// <param name="host">The host.</param> /// <param name="pclient">The client.</param> public static void KnockKnock(PirateHost host, PirateClient pclient) { Contract.Requires(host != null && pclient != null); var msg = new PirateMessage(PirateMessageHead.Knck, ""); host.SendMessage(pclient, msg); }
/// <summary> /// Send error message. /// </summary> /// <param name="host">The host.</param> /// <param name="pclient">The client.</param> /// <param name="error">The error.</param> public static void ErrorMessage(PirateHost host, PirateClient pclient, PirateError error) { Contract.Requires(host != null && pclient != null && error != PirateError.Unknown); var msg = new PirateMessage(PirateMessageHead.Erro, error.ToString()); host.SendMessage(pclient, msg); }
/// <summary> /// New pile. /// </summary> /// <param name="host">The host.</param> public static void NewPile(PirateHost host) { Contract.Requires(host != null); var body = PirateMessage.ConstructBody(PirateMessage.ConstructPlayerTricks(host.Game.Round)); body = PirateMessage.AppendBody(body, PirateMessage.ConstructWinner(host.Game.Round.LastTrick.Winner)); var msg = new PirateMessage(PirateMessageHead.Trdn, body); foreach(var player in host.GetPlayers()) { host.SendMessage(player, msg); } }
public PirateHost Constructor(int port) { PirateHost target = new PirateHost(port); return target; // TODO: add assertions to method PirateHostTest.Constructor(Int32) }
/// <summary> /// New round. /// </summary> /// <param name="host">The host.</param> public static void NewRound(PirateHost host) { Contract.Requires(host != null); var body = PirateMessage.ConstructBody( PirateMessage.ConstructDealer(host.Game.Players[host.Game.Round.Dealer].Name), PirateMessage.ConstructRoundNumber(host.Game.CurrentRound)); var msg = new PirateMessage(PirateMessageHead.Nrnd, body); foreach (var pclient in host.GetPlayers()) { host.SendMessage(pclient, msg); } Console.WriteLine("Starting new round: " + host.Game.CurrentRound); }