Beispiel #1
0
        internal static async Task _MultiplayerHandler(ZGame game)
        {
            // build the server list service instance
            var serverListService = _zloApi.CreateServersListService(game);
            var resetEvent        = new ManualResetEvent(false);

            // configure server list service
            serverListService.InitialSizeReached += (s, e) =>
            {
                resetEvent.Set();
                serverListService.StopReceiving();
            };
            serverListService.StartReceiving();

            // wait to server list full load
            resetEvent.WaitOne();

            // draw servers table
            const string straightLine = "_______________________________________________________________________________________";

            // configure console
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine("\n {0,88} \n {1,5} {2,50}| {3,30}| \n {4,88}", straightLine, "ID:", "ServerName", "Map:", straightLine);

            foreach (var item in serverListService.ServersCollection)
            {
                Console.WriteLine("{0,5}| {1,50}| {2,30}|", item.Id, item.Name, item.MapRotation.Current.Name);
            }

            Console.Write($"{straightLine} \n \nTo join, Enter a server ID: ");

            #region Get target server Id

            // get user input
            var serverIdUserInput = Console.ReadLine();

            // validate input
            if (!uint.TryParse(serverIdUserInput, out var targetServerId))
            {
                throw new InvalidOperationException("Invalid input!");
            }

            #endregion

            // add some space between user input and game log
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine();

            // create game
            var gameProcess = await _gameFactory.CreateMultiAsync(new ZMultiParams { Game = game, ServerId = targetServerId });

            // run and track game pipe
            await _RunAndTrack(gameProcess);
        }
Beispiel #2
0
        public async Task RunMultiplayer(CreateMultiplayerParameters parameters)
        {
            // check possibility to run a game
            var possibleToRun = _IsAlreadyHasRunGame();

            if (!possibleToRun || !_canRunNewGame)
            {
                _OnCreationError("You already have an active game");
            }
            else
            {
                // set service state
                _canRunNewGame = false;

                // get game setting
                var settings = _GetGameSettings(parameters.Game);

                // create run params
                var runParams = new ZMultiParams
                {
                    Game = parameters.Game,
                    PreferredArchitecture = settings.PreferredArchitecture,
                    Role     = parameters.PlayerRole,
                    ServerId = parameters.ServerModel.Id
                };

                try
                {
                    // create game to run
                    var gameProcess = await _gameFactory.CreateMultiAsync(runParams);

                    // create worker
                    var worker = (IGameWorker)_kernel.Get <MultiplayerGameWorker>();
                    worker.Complete += (sender, e) =>
                    {
                        _canRunNewGame = true;
                    };

                    // pass game created event
                    _OnCreated(worker, "Multiplayer", parameters.ServerModel.Name);

                    // run game
                    await worker.Begin(gameProcess, settings, parameters);
                }
                catch (Exception exc)
                {
                    // raise error event
                    _OnCreationError(exc.Message);

                    // reset service state
                    _canRunNewGame = true;
                }
            }
        }