static bool StartConnect(int ColClient, ref List <Socket> UsersSockets, ref List <User> Users) { bool result = true; try { //конечная локальная точка int Port = 11020; IPHostEntry ipHost = Dns.GetHostEntry("localhost"); IPAddress ipAddr = ipHost.AddressList[1]; IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, Port); //Сoздаем сокет Ncp/Ip Socket sListener = new Socket(ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp); sListener.Bind(ipEndPoint); sListener.Listen(ColClient); //Сокет для локальной точки и прослушивание входящих сокетов UsersSockets = new List <Socket>(); Users = new List <User>(); //tiemer Thread thread1 = new Thread(ListnerTimer); thread1.IsBackground = true; thread1.Start(sListener); //запускаем while ((UsersSockets.Count() < ColClient)) { //!!! Console.WriteLine("Ожидаем соединения через {0}", ipEndPoint); //Ожидаем соединения Socket handler = sListener.Accept(); //общаемся с клиентом Users.Add(JsonConvert.DeserializeObject <User>(JsonHandle.ReciveString(handler))); ///!!! //Console.WriteLine("Login {0}", user.login); //Отвечаем Port++; JsonHandle.SendObject(handler, Port); ipEndPoint = new IPEndPoint(ipAddr, Port); //close handler.Shutdown(SocketShutdown.Both); handler.Close(); //Сoздаем сокет Tcp/Ip Socket UserSender = new Socket(ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp); //Сокет подключение try { UserSender.Connect(ipEndPoint); UsersSockets.Add(UserSender); } catch { Port--; } } //close thread1.Abort(); sListener.Close(); sListener = null; //проверка на число пользователей if (UsersSockets.Count() < 2) { result = false; } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } return(result); }
static void Game(List <Socket> UsersSockets, ref List <Player> PlayerList) { TPokerAction pokerAction; bool stop = false; bool raise = true; do { raise = false; for (int i = 0; i < UsersSockets.Count(); i++) { if ((PlayerList[i].fold != true) && PlayerList[i].money != 0) { JsonHandle.SendObject(UsersSockets[i], false); //stop Thread.Sleep(100); JsonHandle.SendObject(UsersSockets[i], stop); pokerAction = JsonConvert.DeserializeObject <TPokerAction>(JsonHandle.ReciveString(UsersSockets[i])); switch (pokerAction) { case TPokerAction.Check: PlayerList[i].money = PlayerList[i].money + PlayerList[i].bet - MaxBet; if (PlayerList[i].money > 0) { AllMoney = AllMoney - PlayerList[i].bet + MaxBet; PlayerList[i].bet = MaxBet; } else { AllMoney = AllMoney - PlayerList[i].bet + MaxBet + PlayerList[i].money; PlayerList[i].bet = MaxBet + PlayerList[i].money; PlayerList[i].money = 0; } break; case TPokerAction.Rais: int DopBet = JsonConvert.DeserializeObject <int>(JsonHandle.ReciveString(UsersSockets[i])); if (PlayerList[i].bet + DopBet < MaxBet) { DopBet = MaxBet - PlayerList[i].bet; } PlayerList[i].money = PlayerList[i].money - DopBet; if (PlayerList[i].money > 0) { AllMoney += DopBet; PlayerList[i].bet += DopBet; } else { AllMoney = AllMoney + DopBet + PlayerList[i].money; PlayerList[i].bet = PlayerList[i].bet + DopBet + PlayerList[i].money; PlayerList[i].money = 0; } if (MaxBet < PlayerList[i].bet) { MaxBet = PlayerList[i].bet; raise = true; } break; default: PlayerList[i].fold = true; PlayerList[i].bet = 0; break; } PlayerList[i].MaxBet = MaxBet; } SendToPlayersSecret(UsersSockets, PlayerList); } //new circle raise = raise && (PlayerList[0].bet != MaxBet); stop = ((PlayerList[0].table.Length == 10) && (raise == false)); if ((raise == false) && !stop) { int num = rand.Next(0, cards.Deck.Count()); string TableCard = cards.Deck[num]; cards.Deck.Remove(TableCard); if (PlayerList[0].table == "") { num = rand.Next(0, cards.Deck.Count()); TableCard += cards.Deck[num]; cards.Deck.Remove(cards.Deck[num]); num = rand.Next(0, cards.Deck.Count()); TableCard += cards.Deck[num]; cards.Deck.Remove(cards.Deck[num]); } foreach (Player player in PlayerList) { player.table = player.table + TableCard; } } SendToPlayersSecret(UsersSockets, PlayerList); // stop game }while(!stop); for (int i = 0; i < UsersSockets.Count(); i++) { JsonHandle.SendObject(UsersSockets[i], false); //stop Thread.Sleep(200); JsonHandle.SendObject(UsersSockets[i], stop); } }