Esempio n. 1
0
        public async Task <IZGameProcess> CreateCoOpAsync(ZCoopParams args)
        {
            ZConnectionHelper.MakeSureConnection();

            if (args.Mode != ZPlayMode.CooperativeHost && args.Mode != ZPlayMode.CooperativeClient)
            {
                throw new ArgumentException("Mode contains wrong value. Allowed values is CooperativeHost or CooperativeClient.");
            }

            if (ZPlayMode.CooperativeClient == args.Mode && args.FriendId == null)
            {
                throw new ArgumentException($"For this {args.Mode} mode need to specify {nameof(args.FriendId)} value.");
            }
            else if (ZPlayMode.CooperativeHost == args.Mode && (args.Difficulty == null || args.Level == null))
            {
                throw new ArgumentException($"For this {args.Mode} mode need to specify {nameof(args.Difficulty)}, {nameof(args.Level)} value.");
            }

            var installedGames = await _installedGamesService.GetInstalledGamesAsync();

            if (installedGames == null)
            {
                throw new Exception("Installed games not received. Check your ZClient connection.");
            }

            var architecture = args.PreferredArchitecture ??
                               (installedGames.IsX64 ? ZGameArchitecture.x64 : ZGameArchitecture.x32);
            var compatibleGames = installedGames.InstalledGames
                                  .Where(insGame => insGame.EnumGame == ZGame.BF3)
                                  .ToArray();
            var targetGame = compatibleGames.Length > 1
                ? compatibleGames.FirstOrDefault(insGame => insGame.RunnableName.EndsWith(architecture.ToString()))
                : compatibleGames.FirstOrDefault();

            if (targetGame == null)
            {
                throw new InvalidOperationException($"The target game {ZGame.BF3} not found.");
            }

            string commandRun;

            if (args.Mode == ZPlayMode.CooperativeHost)
            {
                commandRun = __runStrings[_CoopHostKey].ToObject <string>();
                commandRun = commandRun.Replace(_levelReplaceable, args.Level.ToString().ToUpper());
                commandRun = commandRun.Replace(_difficultyReplaceable, args.Difficulty.ToString().ToUpper());
                commandRun = commandRun.Replace(_personaRefReplaceable, __userContext.UserId.ToString());
            }
            else
            {
                commandRun = __runStrings[_CoopJoinKey].ToObject <string>();
                commandRun = commandRun.Replace(_friendIdReplaceable, args.FriendId.ToString());
                commandRun = commandRun.Replace(_personaRefReplaceable, __userContext.UserId.ToString());
            }

            var runGame = _createRunGame(targetGame, commandRun, ZGame.BF3, architecture);

            return(runGame);
        }
Esempio n. 2
0
        public async Task RunCoop(CreateCoopParameters 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 ZCoopParams
                {
                    PreferredArchitecture = settings.PreferredArchitecture,
                    Difficulty            = parameters.CoopMission?.Difficulty,
                    Level    = parameters.CoopMission?.Level,
                    Mode     = parameters.Mode,
                    FriendId = parameters.FriendId
                };

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

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

                    // pass game created event
                    _OnCreated(worker, "Coop", parameters.Mode == ZPlayMode.CooperativeClient ? "Client" : "Host");

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

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