Exemple #1
0
        public override void ParseFromNetworkMessage(NetworkMessage message)
        {
            ChannelId   = message.ReadUInt16();
            ChannelName = message.ReadString();

            JoinedPlayers.Capacity = message.ReadUInt16();
            for (var i = 0; i < JoinedPlayers.Capacity; ++i)
            {
                JoinedPlayers.Add(message.ReadString());
            }

            InvitedPlayers.Capacity = message.ReadUInt16();
            for (var i = 0; i < InvitedPlayers.Capacity; ++i)
            {
                InvitedPlayers.Add(message.ReadString());
            }
        }
        public Launcher(bool startServers)
        {
            DataContext = this;

            InitializeComponent();

            CanResize = false;

            if (startServers)
            {
                //GameServer
                _serverThread = new Thread(() =>
                {
                    try
                    {
                        _logger.Info($"Running game server..");
                        _manager.StartServer();
                        _logger.Info($"Game server stopped.");
                    }
                    catch (Exception e)
                    {
                        _logger.Error(e, $"A critical error occurred on the game server.");
                    }
                });
                _serverThread.Start();

                //CoordServer
                _coordServer.OnPlayerJoin += (player) =>
                {
                    _logger.Info($"Played joined {player.Name} from address {player.Address}.");

                    var existing = Player.GetPlayerByAddressOrCreate(player.Address);
                    player.Player.Address = player.Address;
                    player.Player.ReIndex();
                    existing.ReplaceWith(player.Player);

                    JoinedPlayers.Add(player);
                    Dispatcher.UIThread.InvokeAsync(() =>
                    {
                        ShowHosting();
                    });
                };
                _coordServer.OnPlayerLeave += (player) =>
                {
                    _logger.Info($"Played left {player.Name} from address {player.Address}.");

                    JoinedPlayers.Remove(player);
                    if (JoinedPlayers.Count == 0)
                    {
                        Dispatcher.UIThread.InvokeAsync(() =>
                        {
                            ShowJoin();
                        });
                    }
                };
                _coordThread = new Thread(() =>
                {
                    try
                    {
                        _logger.Info($"Starting coordination server..");
                        _coordServer.Start();
                    }
                    catch (Exception e)
                    {
                        _logger.Error(e, $"A critical error occurred on the coordination server.");
                    }
                });
                _coordThread.Start();

                Closed += (a, b) =>
                {
                    _manager.StopServer();
                    _logger.Info($"Stopped game server.");
                    _coordServer.Stop();
                    _logger.Info($"Stopped coordination server.");
                    if (_serverThread != null)
                    {
                        _serverThread.Join();
                    }

                    if (_coordThread != null)
                    {
                        _coordThread.Join();
                    }
                };
            }
        }
        public GameTableState()
        {
            On((TableCreated e) =>
            {
                TableId       = e.Id;
                SmallBlind    = e.SmallBlind;
                BuyIn         = e.BuyIn;
                Dealer        = null;
                JoinedPlayers = new Dictionary <string, TablePlayer>();
                Players       = new Dictionary <int, GamePlayer>();
            });
            On((GameFinished e) =>
            {
                GameId         = null;
                MaxBid         = 0;
                CurrentBidding = null;

                foreach (var winner in e.Winners)
                {
                    JoinedPlayers[winner.UserId].Cash += winner.Amount;
                }
            });

            On((GameCreated e) =>
            {
                GameId = e.GameId;
                Pack   = new Pack(e.Cards);
                Deck   = new List <Card>();
                SitPlayers(e.Players);
                CurrentBidding = new BiddingInfo(e.Players.Count);
                MaxRaisedValue = 0;
            });
            On((DealerAssigned e) =>
            {
                Dealer = e.Dealer.Position;
            });
            On((PlayerJoined e) => JoinedPlayers.Add(e.UserId, new TablePlayer()
            {
                Cash     = e.Cash,
                Position = e.Position,
                UserId   = e.UserId,
            }));
            On((CardsDealed e) =>
            {
                foreach (var playerCard in e.Cards)
                {
                    Pack.Remove(playerCard.Card);
                    Players[playerCard.Position].Cards.Add(playerCard.Card);
                }
            });
            On((BidMade e) => AddBidEvent(e.Bid));
            On((BiddingFinished e) =>
            {
                MaxRaisedValue = 0;
            });
            On((NextPlayerTurned e) =>
            {
                CurrentPlayer  = e.Player.Position;
                MaxRaisedValue = e.MaxRaisedValue;
            });
            On((DeckDealed e) =>
            {
                foreach (var card in e.Cards)
                {
                    Pack.Remove(card);
                    Deck.Add(card);
                }
                CurrentBidding.NextStage();
            });
        }