public WindowedPlayerDeckTrackerInterface( ICardInfoProvider cardInfoProvider, IConfigurationSource configurationSource, IEventDispatcherFactory eventDispatcherFactory, IEventStreamFactory eventStreamFactory, IEventDispatcher viewEventDispatcher) { _cardInfoProvider = cardInfoProvider.Require(nameof(cardInfoProvider)); _configurationSource = configurationSource.Require(nameof(configurationSource)); _eventDispatcherFactory = eventDispatcherFactory.Require(nameof(eventDispatcherFactory)); _eventStreamFactory = eventStreamFactory.Require(nameof(eventStreamFactory)); _viewEventDispatcher = viewEventDispatcher.Require(nameof(viewEventDispatcher)); }
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); } }