示例#1
0
        protected override void InitializeCommands()
        {
            base.InitializeCommands();

            LoadCommand = ReactiveCommand.Create(() => Load());

            var canDelete = this.WhenAny(x => x.SelectedPlayerType, x => x.Value != null);

            DeleteCommand = ReactiveCommand.Create(() =>
            {
                if (SelectedPlayerType != null)
                {
                    PlayerTypes.Remove(SelectedPlayerType);
                    SelectedPlayerType = PlayerTypes.FirstOrDefault();
                }
            }, canDelete);

            ResetCommand = ReactiveCommand.Create(() =>
            {
                var defaultPlayerTypes = playerTypeService.CreateDefaultPlayerTypes(viewModelInfo.TableType);

                var defaultPlayerType = defaultPlayerTypes.FirstOrDefault(p => p.Name == SelectedPlayerType.Name);

                if (defaultPlayerType == null)
                {
                    return;
                }

                SelectedPlayerType.StatsToMerge = defaultPlayerType.Stats;
            }, canDelete);

            ExportCommand    = ReactiveCommand.Create(() => Export(new[] { SelectedPlayerType }), canDelete);
            ExportAllCommand = ReactiveCommand.Create(() => Export(playerTypes));
            ImportCommand    = ReactiveCommand.Create(() => Import());
        }
        /// <summary>
        /// Builds report data
        /// </summary>
        private void BuildReportData(CancellationToken cancellationToken)
        {
            LogProvider.Log.Info("Building population report.");

            lock (syncLock)
            {
                try
                {
                    var tableType = GetTableType();

                    var playerTypes = hudPlayerTypeService.CreateDefaultPlayerTypes(tableType);

                    var players = dataService.GetPlayersList();

                    var populationIndicators = playerTypes.Select(x => new PopulationReportIndicators
                    {
                        PlayerTypeName = x.Name,
                        PlayerType     = x
                    }).ToDictionary(x => x.PlayerTypeName);

                    var allPlayersType = CreateAllPlayersType();

                    var allPlayersIndicator = new PopulationReportIndicators
                    {
                        PlayerTypeName = allPlayersType.Name,
                        PlayerType     = allPlayersType,
                        CanAddHands    = true
                    };

                    var filterPredicate = storageModel.CashFilterPredicate.Compile();
                    var heroId          = storageModel.PlayerSelectedItem?.PlayerId ?? 0;

                    Parallel.ForEach(players, (player, loopState) =>
                    {
                        if (cancellationToken.IsCancellationRequested)
                        {
                            loopState.Break();
                        }

                        if (player.PlayerId == heroId)
                        {
                            return;
                        }

                        var playerIndicators = new LightIndicators();

                        playerStatisticRepository.GetPlayerStatistic(player.PlayerId)
                        .Where(x => !x.IsTourney && FilterStatistic(x) && filterPredicate(x))
                        .ForEach(x =>
                        {
                            if (cancellationToken.IsCancellationRequested)
                            {
                                return;
                            }

                            playerIndicators.AddStatistic(x);

                            if (filterPredicate(x))
                            {
                                lock (allPlayersIndicator)
                                {
                                    allPlayersIndicator.AddStatistic(x);
                                }
                            }
                        }
                                 );

                        var playerType = hudPlayerTypeService.Match(playerIndicators, playerTypes, true);

                        if (playerType != null)
                        {
                            if (cancellationToken.IsCancellationRequested)
                            {
                                return;
                            }

                            playerStatisticRepository.GetPlayerStatistic(player.PlayerId)
                            .Where(x => !x.IsTourney && FilterStatistic(x) && filterPredicate(x))
                            .ForEach(x =>
                            {
                                if (cancellationToken.IsCancellationRequested)
                                {
                                    return;
                                }

                                lock (populationIndicators[playerType.Name])
                                {
                                    populationIndicators[playerType.Name].AddStatistic(x);
                                }
                            }
                                     );
                        }
                    });

                    populationIndicators.Add(allPlayersIndicator.PlayerTypeName, allPlayersIndicator);

                    populationIndicators.Values.ForEach(x => x.PrepareHands());

                    populationData.Report = populationIndicators.Values.ToList();

                    if (cancellationToken.IsCancellationRequested)
                    {
                        LogProvider.Log.Info("Population report has been cancelled.");
                        populationData.Report = new List <PopulationReportIndicators>();
                        return;
                    }

                    SaveCachedData();

                    LogProvider.Log.Info("Population report has been built.");
                }
                catch (Exception e)
                {
                    LogProvider.Log.Error(this, "Could build population report data.", e);
                }
            }
        }