Ejemplo n.º 1
0
 /// <summary>
 /// Remove a list of players from the game.
 /// </summary>
 /// <param name="players">IEnumerable Players list to be removed from the game.</param>
 public void RemovePlayers(IEnumerable <Player> players)
 {
     if (CurrentPhase.CurrentPhase == Constant.FIRST_GAME_PHASE)
     {
         if (null == JoinedPlayers)
         {
             throw new PlayerNotInGameException("The player could not be Removed from the Game. No players are joined.");
         }
         try
         {
             foreach (Player player in players)
             {
                 JoinedPlayers.Remove(player);
             }
         }
         catch (Exception)
         {
             throw new PlayerNotInGameException("The player could not be Removed from the Game. The player was not joined.");
         }
     }
     else
     {
         throw new ActiveGameException("The game is active. Players may not be removed from the game once it has started.");
     }
 }
Ejemplo n.º 2
0
        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();
                    }
                };
            }
        }