Exemplo n.º 1
0
 private DeckViewModel CreateDeckViewModel(
     Guid deckID,
     Models.Client.Decklist decklist,
     string title,
     IEnumerable <Models.Client.GameResult> gameResults = null)
 {
     return
         (new DeckViewModel(
              _backgroundWorkInterface,
              _cardInfoProvider,
              _deckImportInterface,
              _decklistSerializer,
              _deckRepository,
              _gameEventDispatcher,
              _gameResultRepository,
              _playerDeckTrackerInterface,
              _replaceDeckInterface,
              _userConfirmationInterface,
              _viewEventDispatcher,
              deckID,
              decklist,
              __deckViewModel => (ushort)_deckViewModels.IndexOf(__deckViewModel),
              title,
              gameResults));
 }
Exemplo n.º 2
0
        public DeckViewModel(
            IBackgroundWorkInterface backgroundWorkInterface,
            ICardInfoProvider cardInfoProvider,
            IDeckImportInterface deckImportInterface,
            ISerializer <Models.Client.Decklist> decklistSerializer,
            IDeckRepository deckRepository,
            IEventDispatcher gameEventDispatcher,
            IGameResultRepository gameResultRepository,
            IPlayerDeckTrackerInterface playerDeckTrackerInterface,
            IReplaceDeckInterface replaceDeckInterface,
            IUserConfirmationInterface userConfirmationInterface,
            IEventDispatcher viewEventDispatcher,
            Guid deckID,
            Models.Client.Decklist decklist,
            Func <DeckViewModel, ushort> getPosition,
            string title,
            IEnumerable <Models.Client.GameResult> gameResults = null)
        {
            _backgroundWorkInterface    = backgroundWorkInterface.Require(nameof(backgroundWorkInterface));
            _cardInfoProvider           = cardInfoProvider.Require(nameof(cardInfoProvider));
            _deckImportInterface        = deckImportInterface.Require(nameof(deckImportInterface));
            _decklistSerializer         = decklistSerializer.Require(nameof(decklistSerializer));
            _deckRepository             = deckRepository.Require(nameof(deckRepository));
            _gameEventDispatcher        = gameEventDispatcher.Require(nameof(gameEventDispatcher));
            _gameResultRepository       = gameResultRepository.Require(nameof(gameResultRepository));
            _playerDeckTrackerInterface = playerDeckTrackerInterface.Require(nameof(playerDeckTrackerInterface));
            _replaceDeckInterface       = replaceDeckInterface.Require(nameof(replaceDeckInterface));
            _userConfirmationInterface  = userConfirmationInterface.Require(nameof(userConfirmationInterface));
            _viewEventDispatcher        = viewEventDispatcher.Require(nameof(viewEventDispatcher));

            DeckID       = deckID;
            Decklist     = decklist;
            _gameResults = (gameResults ?? Enumerable.Empty <Models.Client.GameResult>()).ToList();
            _getPosition = getPosition.Require(nameof(getPosition));
            Title        = title ?? string.Empty;

            _viewEventHandlers.Add(
                new DelegateEventHandler <ViewEvents.DeckTracking>(
                    __event =>
            {
                IsTracking = __event.DeckViewModel == this;

                _deleteCanExecuteChanged?.Invoke();
                _replaceCanExecuteChanged?.Invoke();
            }));

            _viewEventHandlers.ForEach(__handler => _viewEventDispatcher.RegisterHandler(__handler));
        }
        void IPlayerDeckTrackerInterface.TrackDeck(
            Models.Client.Decklist decklist)
        {
            Reset();

            IEventDispatcher eventDispatcher = _eventDispatcherFactory.Create();

            _viewModel =
                new PlayerDeckTrackerViewModel(
                    _cardInfoProvider,
                    _configurationSource,
                    eventDispatcher,
                    _viewEventDispatcher,
                    decklist);

            _cancellation = new CancellationTokenSource();

            Task.Run(
                async() =>
            {
                using (IEventStream eventStream = _eventStreamFactory.Create())
                {
                    while (true)
                    {
                        object @event = await eventStream.ReadNext(_cancellation.Token);
                        if (@event == null)
                        {
                            return;
                        }

                        eventDispatcher.DispatchEvent(@event);
                    }
                }
            });

            _view = new PlayerDeckTrackerView(_viewModel);

            _view.Show();
        }
Exemplo n.º 4
0
        public PlayerDeckTrackerViewModel(
            ICardInfoProvider cardInfoProvider,
            IConfigurationSource configurationSource,
            IEventDispatcher gameEventDispatcher,
            IEventDispatcher viewEventDispatcher,
            Models.Client.Decklist decklist)
        {
            _cardInfoProvider    = cardInfoProvider.Require(nameof(cardInfoProvider));
            _configurationSource = configurationSource.Require(nameof(configurationSource));
            _gameEventDispatcher = gameEventDispatcher.Require(nameof(gameEventDispatcher));
            _viewEventDispatcher = viewEventDispatcher.Require(nameof(viewEventDispatcher));

            _decklist = decklist;

            Reset();

            _gameEventHandlers.Add(
                new DelegateEventHandler <GameEvents.CardAddedToDeck>(
                    __event =>
            {
                if (!_playerID.Equals(__event.PlayerID))
                {
                    return;
                }

                if (_trackedCardViewModels.Any(__trackedCardViewModel => __trackedCardViewModel.CardID.Eq(__event.CardID)))
                {
                    return;
                }

                int?playerID = _trackedCardViewModels.First()?.PlayerID;

                _trackedCardViewModels.Add(CreateCardViewModel(__event.CardID, 1, playerID));

                _trackedCardViewModels =
                    _trackedCardViewModels
                    .OrderBy(__trackedCardViewModel => __trackedCardViewModel.Cost)
                    .ThenBy(__trackedCardViewModel => __trackedCardViewModel.Name)
                    .ToList();

                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(TrackedCardViewModels)));
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Count)));
            }));

            _gameEventHandlers.Add(
                new DelegateEventHandler <GameEvents.GameStarted>(
                    __ => Reset()));

            _gameEventHandlers.Add(
                new DelegateEventHandler <GameEvents.OpponentCoinLost>(
                    __ => OpponentCoinStatus = false));

            _gameEventHandlers.Add(
                new DelegateEventHandler <GameEvents.OpponentReceivedCoin>(
                    __ => OpponentCoinStatus = true));

            _gameEventHandlers.Add(
                new DelegateEventHandler <GameEvents.PlayerDetermined>(
                    __event => _playerID = __event.PlayerID));

            _gameEventHandlers.ForEach(__handler => _gameEventDispatcher.RegisterHandler(__handler));

            _viewEventHandlers.Add(
                new DelegateEventHandler <ViewEvents.ConfigurationSettingsSaved>(
                    __ => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(FontSize)))));

            _viewEventHandlers.ForEach(__handler => _viewEventDispatcher.RegisterHandler(__handler));
        }