// Обновляем информацию об игроках на столе private void SeatPlayers(List <ServerPlayerInfo> players) { // Заполняем стол for (int i = 0; i < Helper.maxPlayers; i++) { ServerPlayerInfo player = players.Find(p => p.seat == i); if (player != null) { UpdateTable(player); } else { CleanSeat(i); } } // Включаем/выключаем кнопки ServerPlayerInfo me = players.Find(p => p.endPoint == player.info.endPoint); if (me.yourMove) { SwitchButtons(true); DefineCallCheckButton(); } else { SwitchButtons(false); } }
private void UpdatePlayersFromInvolved(List <ServerPlayerInfo> involvedPlayers) { for (int i = 0; i < players.Count; i++) { int seat = players[i].seat; ServerPlayerInfo player = involvedPlayers.Find(p => p.seat == seat); if (player != null) { players[i] = player; } } }
public string GetJson() { var data = new Dictionary <string, PlayerInfo>(); var info = new ServerPlayerInfo(data, CurrentID); foreach (var p in _playerList) { if (p.Value.IsLogin || p.Value.HasPassword) { data.Add(p.Key, p.Value.GetPlayerInfo()); } } return(JsonConvert.SerializeObject(info)); }
private Image SetCard2(ServerPlayerInfo info) { string filename = ""; // Устанавливаем карты так, чтобы клиент мог видеть только свои if (info.card2 != null && info.endPoint.Equals(player.client.Client.LocalEndPoint.ToString())) { filename = "../../Images/Cards/" + info.card2.suit.ToString() + info.card2.quality.ToString() + ".png"; } else { filename = "../../Images/Cards/Back.png"; } return(Image.FromFile(filename)); }
public int ReceivePlayerInfo() { // Получаем информацию о новом подключившемся клиенте int seat = 0; byte[] bytes = new byte[1024]; tcpSocket.Receive(bytes); BinaryFormatter formatter = new BinaryFormatter(); using (MemoryStream memory = new MemoryStream(bytes)) { // TODO: В это месте упало в случайный момент времени почему-то PlayerInfo info = (PlayerInfo)formatter.Deserialize(memory); seat = FindSeat(); ServerPlayerInfo serverInfo = new ServerPlayerInfo(info.name, info.money, info.endPoint, seat); players.Add(serverInfo); Console.WriteLine($"Player {info.name} with ${info.money} seat on {seat}"); } return(seat); }
public GameServer(ServerGameInfo sgInfo) : base() { _playerCollection = new Dictionary <short, Player>(); _serverGameInfo = sgInfo; _simulation = new MockServerSimulation(); lock (((ICollection)sgInfo.Players).SyncRoot) { foreach (IPlayerID pid in sgInfo.Players.Values) { ServerPlayerInfo spi = pid as ServerPlayerInfo; lock (((ICollection)_playerCollection).SyncRoot) { _playerCollection.Add(new KeyValuePair <short, Player>(spi.Id, spi.Player)); _simulation.AddPlayer(spi.Id, (PlayerData)spi.Player.PlayerData.Clone()); } } } MessageHandler = new GameMessageHandler(this); MessageHandler.SetSender(_msgSender); InfoLog.WriteInfo("Game server for game: " + sgInfo.Name + "created successfully", EPrefix.ServerInformation); }
private void UpdateTable(ServerPlayerInfo info) { switch (info.seat) { case 0: try { // Для решения проблемы попытки обращения к элементу созданному в другом потоке используем MethodInvoker Invoke(new MethodInvoker(() => { lblPlayer1Name.Text = info.name; lblPlayer1Money.Text = info.money.ToString(); pbPlayer1Card1.Image = SetCard1(info); pbPlayer1Card2.Image = SetCard2(info); lblPlayer1Rate.Text = info.isPlaying ? info.currentRate.ToString() : ""; pbPlayer1.Image = Image.FromFile("../../Images/Players/Player1.png"); pbPlayer1Diller.Image = (info.diller ? Image.FromFile("../../Images/Diller.png") : null); })); } catch (Exception exc) { } break; case 1: try { // TODO: Тут было исключение с потоками при отключении одного из клиентов Invoke(new MethodInvoker(() => { lblPlayer2Name.Text = info.name; lblPlayer2Money.Text = info.money.ToString(); pbPlayer2Card1.Image = SetCard1(info); pbPlayer2Card2.Image = SetCard2(info); lblPlayer2Rate.Text = info.isPlaying ? info.currentRate.ToString() : ""; pbPlayer2.Image = Image.FromFile("../../Images/Players/Player2.png"); pbPlayer2Diller.Image = (info.diller ? Image.FromFile("../../Images/Diller.png") : null); })); } catch (Exception exc) { } break; case 2: try { Invoke(new MethodInvoker(() => { lblPlayer3Name.Text = info.name; lblPlayer3Money.Text = info.money.ToString(); pbPlayer3Card1.Image = SetCard1(info); pbPlayer3Card2.Image = SetCard2(info); lblPlayer3Rate.Text = info.isPlaying ? info.currentRate.ToString() : ""; pbPlayer3.Image = Image.FromFile("../../Images/Players/Player3.png"); pbPlayer3Diller.Image = (info.diller ? Image.FromFile("../../Images/Diller.png") : null); })); } catch (Exception exc) { } break; case 3: try { Invoke(new MethodInvoker(() => { lblPlayer4Name.Text = info.name; lblPlayer4Money.Text = info.money.ToString(); pbPlayer4Card1.Image = SetCard1(info); pbPlayer4Card2.Image = SetCard2(info); lblPlayer4Rate.Text = info.isPlaying ? info.currentRate.ToString() : ""; pbPlayer4.Image = Image.FromFile("../../Images/Players/Player4.png"); pbPlayer4Diller.Image = (info.diller ? Image.FromFile("../../Images/Diller.png") : null); })); } catch (Exception exc) { } break; case 4: try { Invoke(new MethodInvoker(() => { lblPlayer5Name.Text = info.name; lblPlayer5Money.Text = info.money.ToString(); pbPlayer5Card1.Image = SetCard1(info); pbPlayer5Card2.Image = SetCard2(info); lblPlayer5Rate.Text = info.isPlaying ? info.currentRate.ToString() : ""; pbPlayer5.Image = Image.FromFile("../../Images/Players/Player5.png"); pbPlayer5Diller.Image = (info.diller ? Image.FromFile("../../Images/Diller.png") : null); })); } catch (Exception exc) { } break; case 5: try { Invoke(new MethodInvoker(() => { lblPlayer6Name.Text = info.name; lblPlayer6Money.Text = info.money.ToString(); pbPlayer6Card1.Image = SetCard1(info); pbPlayer6Card2.Image = SetCard2(info); lblPlayer6Rate.Text = info.isPlaying ? info.currentRate.ToString() : ""; pbPlayer6.Image = Image.FromFile("../../Images/Players/Player6.png"); pbPlayer6Diller.Image = (info.diller ? Image.FromFile("../../Images/Diller.png") : null); })); } catch (Exception exc) { } break; default: break; } }
private void HandleClient(object socket) { // Присоединение нового клиента int seat = ReceivePlayerInfo(); //GiveCards(seat); SendServerPlayerInfoByQueue(); Console.WriteLine(tcpSocket.LocalEndPoint); Console.WriteLine(tcpSocket.RemoteEndPoint); Turn turn = new Turn(); // Обработка запросов клиента while (work) { // Получаем действия игрока (ходы) byte[] bytes = new byte[1024]; // TODO: Если закрыть последнего клиента, говорит удаленный хост принудительо разорвал соединеие if (((Socket)socket).Connected) { try { ((Socket)socket).Receive(bytes); } catch (Exception exc) { ((Socket)socket).Close(); return; } } BinaryFormatter formatter = new BinaryFormatter(); using (MemoryStream memory = new MemoryStream(bytes)) { turn = (Turn)formatter.Deserialize(memory); } switch (turn.turnType) { case TurnType.Fold: case TurnType.Check: case TurnType.Call: case TurnType.Raise: // Получаем обновленную информацию об участвовавших в раздаче игроках var involvedPlayers = game.MakeTurn(seat, turn); UpdatePlayersFromInvolved(involvedPlayers); break; case TurnType.Exit: game.involvedPlayers.Find(p => p.seat == seat).isPlaying = false; // Говорим, что игрок, который ушел, скинул карты var involvedPlayers2 = game.MakeTurn(seat, new Turn(TurnType.Fold)); UpdatePlayersFromInvolved(involvedPlayers2); ServerPlayerInfo info = players.Find(player => player.seat == seat); players.Remove(info); Console.WriteLine($"Player {info.name} leaves game"); break; default: break; } SendServerPlayerInfoByQueue(); } }