Exemple #1
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));
        }
        public DeckManagerViewModel(
            IBackgroundWorkInterface backgroundWorkInterface,
            ICardInfoProvider cardInfoProvider,
            IDeckImportInterface deckImportInterface,
            ISerializer <Models.Client.Decklist> decklistSerializer,
            IDeckRepository deckRepository,
            IEventStreamFactory eventStreamFactory,
            IEventDispatcher gameEventDispatcher,
            IGameResultRepository gameResultRepository,
            ILogger logger,
            IPlayerDeckTrackerInterface playerDeckTrackerInterface,
            IReplaceDeckInterface replaceDeckInterface,
            IUserConfirmationInterface userConfirmationInterface,
            IEventDispatcher viewEventDispatcher)
        {
            _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));
            _eventStreamFactory      = eventStreamFactory.Require(nameof(eventStreamFactory));
            _gameEventDispatcher     = gameEventDispatcher.Require(nameof(gameEventDispatcher));
            _gameResultRepository    = gameResultRepository.Require(nameof(gameResultRepository));
            _logger = logger.Require(nameof(logger));
            _playerDeckTrackerInterface = playerDeckTrackerInterface.Require(nameof(playerDeckTrackerInterface));
            _replaceDeckInterface       = replaceDeckInterface.Require(nameof(replaceDeckInterface));
            _userConfirmationInterface  = userConfirmationInterface.Require(nameof(userConfirmationInterface));
            _viewEventDispatcher        = viewEventDispatcher.Require(nameof(viewEventDispatcher));

            // Start loading pre-existing decks
            Task.Run(
                async() =>
            {
                IEnumerable <Models.Client.Deck> decks = await _deckRepository.GetAllDecks();

                _deckViewModels =
                    new ObservableCollection <DeckViewModel>(
                        decks
                        .OrderBy(__deck => __deck.Position)
                        .Select(
                            __deck =>
                            CreateDeckViewModel(
                                __deck.DeckID,
                                DeserializeDecklist(__deck.DeckString),
                                __deck.Title,
                                __deck.GameResults)));

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

            Task.Run(
                async() =>
            {
                using (IEventStream eventStream = _eventStreamFactory.Create())
                {
                    eventStream.SeekEnd();

                    while (true)
                    {
                        try { _gameEventDispatcher.DispatchEvent(await eventStream.ReadNext()); }
                        catch (Exception ex) { await _logger.Write($"{ex.Message}{Environment.NewLine}{ex.StackTrace}"); }
                    }
                }
            });

            _viewEventDispatcher.RegisterHandler(
                new DelegateEventHandler <ViewCommands.DeleteDeck>(
                    async __event =>
            {
                DeckViewModel deckViewModel =
                    _deckViewModels
                    .FirstOrDefault(__deckViewModel => __deckViewModel.DeckID == __event.DeckID);
                if (deckViewModel == null)
                {
                    return;
                }

                _deckViewModels.Remove(deckViewModel);

                await SaveDecks();
            }));

            _viewEventDispatcher.RegisterHandler(
                new DelegateEventHandler <ViewCommands.MoveDeck>(
                    async __event =>
            {
                ushort sourcePosition = __event.SourcePosition;
                if (sourcePosition > _deckViewModels.Count)
                {
                    return;
                }

                DeckViewModel sourceDeck = _deckViewModels[sourcePosition];
                _deckViewModels.RemoveAt(sourcePosition);

                ushort targetPosition = __event.TargetPosition;
                _deckViewModels.Insert(targetPosition, sourceDeck);

                await SaveDecks();
            }));

            Models.Client.Decklist DeserializeDecklist(
                string deckstring)
            {
                using (var stream = new MemoryStream(Encoding.Default.GetBytes(deckstring)))
                    return(_decklistSerializer.Deserialize(stream).Result);
            }
        }