Exemple #1
0
        public void FillSportsBar()
        {
            SortableObservableCollection <MatchResultVw> ResultMatches = new SortableObservableCollection <MatchResultVw>();

            Repository.FindResults(ResultMatches, SportBarMatchFilter, Comparison);
            Dispatcher.Invoke(() =>
            {
                try
                {
                    var sports = ResultMatches.Where(x => x.SportView != null).Select(x => x.SportView).Distinct().ToList();

                    SportBarItem allsports = SportsBarItems.FirstOrDefault(x => x.SportDescriptor == SportSr.ALL_SPORTS);
                    if (allsports != null)
                    {
                        allsports.SportName = TranslationProvider.Translate(MultistringTags.ALL_SPORTS) as string;
                    }

                    foreach (var group in sports)
                    {
                        if (SportsBarItems.Count(x => x.SportDescriptor == group.LineObject.GroupSport.SportDescriptor) == 0)
                        {
                            SportsBarItems.Add(new SportBarItem(group.DisplayName, group.LineObject.GroupSport.SportDescriptor));
                        }
                        else
                        {
                            SportsBarItems.First(x => x.SportDescriptor == @group.LineObject.GroupSport.SportDescriptor).SportName = @group.DisplayName;
                        }
                    }

                    for (int i = 1; i < SportsBarItems.Count;)
                    {
                        var item = SportsBarItems[i];

                        if (sports.Count(x => x.LineObject.GroupSport.SportDescriptor == item.SportDescriptor) == 0)
                        {
                            SportsBarItems.RemoveAt(i);
                        }
                        else
                        {
                            i++;
                        }
                    }

                    SportsBarItems.Sort(ComparisonSportsBar);

                    OnPropertyChanged("SportsBarVisibility");
                }
                catch (Exception ex)
                {
                }
            });
        }
        private void FillTournaments()
        {
            lock (_lockerTimer)
            {
                SortableObservableCollection <IMatchVw> matches = new SortableObservableCollection <IMatchVw>();

                matches = Repository.FindMatches(matches, "", SelectedLanguage, MatchFilter, Sort);

                bool showSeparateOutrights = SelectedTournament != null && SelectedTournament.IsOutrightGroup;

                if (!showSeparateOutrights)
                {
                    var outrights = matches.Where(x => x.IsOutright).OrderBy(o => o.TournamentView.LineObject.GroupId).ToList();

                    if (outrights.Count > 0)
                    {
                        TournamentVw tournamentVw = null;

                        if (Tournaments.Count > 0 && Tournaments[0].IsOutrightGroup)
                        {
                            tournamentVw      = Tournaments[0];
                            tournamentVw.Name = TranslationProvider.Translate(MultistringTags.OUTRIGHTS).ToString();
                        }
                        else
                        {
                            tournamentVw = new TournamentVw(int.MinValue, 0, TranslationProvider.Translate(MultistringTags.OUTRIGHTS).ToString(), 0, int.MinValue, null, "");
                            tournamentVw.IsOutrightGroup = true;

                            Tournaments.Insert(0, tournamentVw);
                        }
                        tournamentVw.TemporaryMatchesCount = outrights.Count;
                    }
                }

                var prematches = matches.Where(x => x.IsOutright == showSeparateOutrights).OrderBy(o => o.TournamentView.LineObject.GroupId).ToList();

                foreach (var matchVw in prematches)
                {
                    if (matchVw.TournamentView != null && matchVw.ExpiryDate > DateTime.Now)
                    {
                        TournamentVw tournamentVw = null;

                        int iTrmtIndex = Tournaments.IndexOf(new TournamentVw(matchVw.TournamentView.LineObject.GroupId));

                        if (iTrmtIndex >= 0)
                        {
                            tournamentVw      = Tournaments[iTrmtIndex];
                            tournamentVw.Name = matchVw.TournamentView.DisplayName;
                        }
                        else
                        {
                            long   countryId = -1;
                            string country   = "";
                            if (matchVw.CountryView != null)
                            {
                                countryId = matchVw.CountryView.LineObject.SvrGroupId;
                                country   = matchVw.CountryView.DisplayName;
                            }
                            //mv.TournamentView.LineObject.

                            string mincombination = null;
                            if (matchVw.TournamentView.LineObject.GroupTournament != null && matchVw.TournamentView.LineObject.GroupTournament.MinCombination.Value > 0)
                            {
                                mincombination = TranslationProvider.Translate(MultistringTags.TERMINAL_X_COMB, matchVw.TournamentView.LineObject.GroupTournament.MinCombination.Value);
                            }
                            tournamentVw = new TournamentVw(matchVw.TournamentView.LineObject.GroupId,
                                                            matchVw.TournamentView.LineObject.SvrGroupId,
                                                            matchVw.TournamentView.DisplayName,
                                                            countryId,
                                                            matchVw.TournamentView.LineObject.Sort.Value,
                                                            mincombination,
                                                            country);

                            if (matchVw.IsOutright)
                            {
                                tournamentVw.ContainsOutrights = true;
                            }

                            string id     = tournamentVw.Id.ToString();
                            string tourId = matchVw.IsOutright ? id + "*1" : id + "*0";

                            if (ChangeTracker.SelectedTournaments.Contains(tourId))
                            {
                                tournamentVw.IsSelected = true;
                            }

                            Tournaments.Add(tournamentVw);
                        }

                        tournamentVw.AddMatch();
                    }
                }



                foreach (TournamentVw tournament in Tournaments)
                {
                    tournament.ApplayTemporaryMatchesCount();
                }


                Tournaments.Sort(Comparison);
            }

            if (ChangeTracker.SelectedTournaments.Count == 0)
            {
                Mediator.SendMessage(true, MsgTag.ActivateForwardSelected);
            }
            else
            {
                Mediator.SendMessage(true, MsgTag.ActivateShowSelected);
            }
        }
        private void FillTournaments()
        {
            Tournaments.Clear();
            if (flag1)
            {
                lock (_itemsLock)
                {
                    flag1 = false;
                    SortableObservableCollection <IMatchVw> matches = new SortableObservableCollection <IMatchVw>();
                    int tournamentsAmount = ChangeTracker.IsLandscapeMode ? 10 : 8;
                    SyncObservableCollection <SportCategory> tempCategories = new SyncObservableCollection <SportCategory>();

                    matches = Repository.FindMatches(matches, "", SelectedLanguage, MatchFilter, Sort);

                    //lets see if we can start from categories, not tournaments
                    var groups = matches.Where(x => !x.IsOutright && x.CategoryView != null).Select(x => x.CategoryView).Distinct().ToList();

                    foreach (var group in groups)
                    {
                        SportCategory temp = new SportCategory(group.DisplayName, new SyncObservableCollection <TournamentVw>(), group.LineObject.GroupId);
                        temp.Sort = group.LineObject.Sort.Value;
                        tempCategories.Add(temp);
                    }
                    tempCategories = new SyncObservableCollection <SportCategory>(tempCategories.OrderBy(x => x.Sort).ToList());

                    foreach (SportCategory category in tempCategories)
                    {
                        //fill tournaments - not outrights

                        List <TournamentVw> Tournaments = new List <TournamentVw>();
                        var tours = matches.Where(x => !x.IsOutright && x.TournamentView != null && x.CategoryView != null && x.CategoryView.LineObject.GroupId == category.CategoryID).Select(x => x.TournamentView).Distinct().ToList();
                        int allTournamentsCount = tours.Count;

                        for (int i = 0; i < tours.Count; i++)
                        {
                            long   id             = tours[i].LineObject.GroupId;
                            long   svrId          = tours[i].LineObject.SvrGroupId;
                            string tournamentName = tours[i].DisplayName;
                            long   countryId      = 0;
                            string country        = "";

                            int    sort           = tours[i].LineObject.Sort.Value;
                            string minCombination = null;

                            if (tours[i].LineObject.GroupTournament != null && tours[i].LineObject.GroupTournament.MinCombination.Value > 0)
                            {
                                minCombination = TranslationProvider.Translate(MultistringTags.TERMINAL_X_COMB, tours[i].LineObject.GroupTournament.MinCombination.Value);
                            }

                            if (tours[i].TournamentCountryView != null)
                            {
                                countryId = tours[i].TournamentCountryView.LineObject.SvrGroupId;
                                country   = tours[i].TournamentCountryView.DisplayName;
                            }

                            long   sportId   = 0;
                            string sportName = "";

                            if (tours[i].TournamentSportView != null)
                            {
                                sportId   = tours[i].TournamentSportView.LineObject.SvrGroupId;
                                sportName = tours[i].TournamentSportView.DisplayName;
                            }

                            bool isOutright = false;
                            long categoryId = 0;

                            TournamentVw tour = new TournamentVw(id, svrId, tournamentName, countryId, sort, minCombination, country, sportId, isOutright, sportName, categoryId);
                            tour.TemporaryMatchesCount = matches.Where(x => !x.IsOutright && x.TournamentView != null && x.TournamentView.LineObject.GroupId == tour.Id).Count();
                            tour.ApplayTemporaryMatchesCount();

                            if (ChangeTracker.SelectedTournaments.Contains(tour.Id.ToString() + "*0"))
                            {
                                tour.IsSelected = true;
                            }

                            Tournaments.Add(tour);
                        }

                        Tournaments = new List <TournamentVw>(Tournaments.OrderBy(x => x.Sort));
                        Tournaments.Sort(Comparison);
                        //now i have all category tournaments

                        for (int i = 0; i < tournamentsAmount; i++)
                        {
                            if (i >= Tournaments.Count)
                            {
                                break;
                            }

                            category.Tournaments.Add(Tournaments[i]);
                        }

                        Tournaments.Clear();
                        //if there is only outrights, add them to category
                        int outrightCount = matches.Where(x => x.IsOutright && x.CategoryView != null && x.CategoryView.LineObject.GroupId == category.CategoryID).Count();
                        if (outrightCount > 0)
                        {
                            allTournamentsCount++;
                        }

                        if (category.Tournaments.Count == 0 || category.Tournaments.Count < tournamentsAmount)
                        {
                            if (outrightCount > 0)
                            {
                                TournamentVw outright = new TournamentVw(int.MinValue, 0, TranslationProvider.Translate(MultistringTags.OUTRIGHTS).ToString(), 0, int.MinValue, null, "", category.CategoryID, true, category.SportName);
                                outright.MatchesCount = outrightCount;
                                category.Tournaments.Add(outright);
                            }
                        }

                        //add all tournaments button
                        if (allTournamentsCount > category.Tournaments.Count)
                        {
                            category.Tournaments.RemoveAt(category.Tournaments.Count - 1);

                            TournamentVw allTournaments = new TournamentVw(int.MinValue, -999, "All tournaments", 0, int.MinValue, null, "", category.CategoryID, true, category.SportName);
                            allTournaments.MatchesCount = allTournamentsCount;
                            category.Tournaments.Add(allTournaments);
                        }
                    }


                    SetImageCategories(tempCategories);
                    Dispatcher.BeginInvoke((Action)(() =>
                    {
                        Categories.ApplyChanges(tempCategories);
                    }));
                }
            }
        }
        public void FillSportsBar()
        {
            SortableObservableCollection <IMatchVw> PreMatches = new SortableObservableCollection <IMatchVw>();

            Repository.FindMatches(PreMatches, "", SelectedLanguage, MatchFilterSportBar, delegate(IMatchVw m1, IMatchVw m2) { return(0); });

            try
            {
                var sports = PreMatches.Where(x => x.SportView != null).Select(x => x.SportView).Distinct().ToList();

                SportBarItem allsports = SportsBarItemsPreMatch.FirstOrDefault(x => x.SportDescriptor == SportSr.ALL_SPORTS);
                if (allsports != null)
                {
                    allsports.SportName = TranslationProvider.Translate(MultistringTags.ALL_SPORTS) as string;
                }
                else
                {
                    SportsBarItemsPreMatch.Insert(0, new SportBarItem(TranslationProvider.Translate(MultistringTags.ALL_SPORTS) as string, SportSr.ALL_SPORTS));
                }

                foreach (var group in sports)
                {
                    {
                        if (SportsBarItemsPreMatch.Count(x => x.SportDescriptor == group.LineObject.GroupSport.SportDescriptor) == 0)
                        {
                            SportsBarItemsPreMatch.Add(new SportBarItem(group.DisplayName, group.LineObject.GroupSport.SportDescriptor));
                        }
                        else
                        {
                            SportsBarItemsPreMatch.First(x => x.SportDescriptor == @group.LineObject.GroupSport.SportDescriptor).SportName = @group.DisplayName;
                        }
                    }
                }

                for (int i = 1; i < SportsBarItemsPreMatch.Count;)
                {
                    var item = SportsBarItemsPreMatch[i];

                    if (sports.Count(x => x.LineObject.GroupSport.SportDescriptor == item.SportDescriptor) == 0)
                    {
                        SportsBarItemsPreMatch.RemoveAt(i);
                    }
                    else
                    {
                        i++;
                    }
                }

                foreach (SportBarItem item in SportsBarItemsPreMatch)
                {
                    if (SelectedDescriptors.Contains(item.SportDescriptor))
                    {
                        item.IsChecked = true;
                    }
                    else
                    {
                        item.IsChecked = false;
                    }
                }

                SportsBarItemsPreMatch.Sort(ComparisonSportsBar);

                OnPropertyChanged("SportsBarVisibility");
            }
            catch (Exception ex)
            {
            }
        }
        private async void ProcessTmDbInfo()
        {
            try
            {
                IsBusy = true;

                BusyMessage = "Saving Config...";

                SaveConfig();

                if (string.IsNullOrWhiteSpace(Settings.Default.TMDbApiKey))
                {
                    // Prompt user for API key
                    // Prompt from : https://github.com/ramer/IPrompt
                    var prompt = IInputBox.Show("Enter your Movie Database API Key", "API Key Required");
                    if (string.IsNullOrWhiteSpace(prompt))
                    {
                        IMessageBox.Show("The Movie Database Key is required for this service. Signup for the API key (free) at www.themoviedb.org");
                        return;
                    }

                    Settings.Default.TMDbApiKey = prompt;
                    Settings.Default.Save();
                }

                // API using nuget package -- code here;  https://github.com/LordMike/TMDbLib
                var client = new TMDbClient(Settings.Default.TMDbApiKey);
                await client.AuthenticationRequestAutenticationTokenAsync();

                // API permits only 40 calls per 10 seconds. using this nuget package to limit it to 35.
                var timeconstraint = TimeLimiter.GetFromMaxCountByInterval(35, TimeSpan.FromSeconds(10));

                // loop over the folders and find the folders that dont have a metadata data file in them
                var foldersToProcess = _files.Where(x => !x.IsSelected && x.TheMovieDatabaseData == null).ToList();
                for (var index = 0; index < foldersToProcess.Count; index++)
                {
                    var f          = foldersToProcess[index];
                    var folderName = f.LocalFolderPath.Substring(1, f.LocalFolderPath.Length - 1);

                    if (folderName.Contains(DirectorySeperator))
                    {
                        continue;
                    }

                    var name = f.SingleDirectoryName.GetNameBeforeYear();
                    if (name.EndsWith("-"))
                    {
                        name = name.Substring(0, name.Length - 1).Trim();
                    }

                    if (Config.MovieOption)
                    {
                        // Currently dont support season info lookup. Just primary anme.
                        if (f.SingleDirectoryName.ToLower().StartsWith("season"))
                        {
                            continue;
                        }

                        var year = f.SingleDirectoryName.GetYear();
                        if (year.HasValue)
                        {
                            BusyMessage = $"Searching For Movie [{index + 1} / {foldersToProcess.Count}] '{name}' ({year.Value})";
                            await timeconstraint.Perform(async() =>
                            {
                                var result = await client.SearchMovieAsync(name, year: year.Value);
                                if (result.TotalResults >= 1)
                                {
                                    f.TheMovieDatabaseData = result.Results.OrderByDescending(x => x.VoteCount)
                                                             .FirstOrDefault();
                                    var json = JsonConvert.SerializeObject(f.TheMovieDatabaseData, Formatting.Indented);
                                    File.WriteAllText(f.TheMovieDatabaseFileName, json);
                                }
                            });
                        }
                    }
                    else if (Config.TvOption)
                    {
                        BusyMessage = $"Searching For Tv [{index + 1} / {foldersToProcess.Count}] '{name}')";
                        await timeconstraint.Perform(async() =>
                        {
                            var result = await client.SearchTvShowAsync(name);
                            if (result.TotalResults >= 1)
                            {
                                f.TheMovieDatabaseData =
                                    result.Results.OrderByDescending(x => x.VoteCount).FirstOrDefault();
                                var json = JsonConvert.SerializeObject(f.TheMovieDatabaseData, Formatting.Indented);
                                File.WriteAllText(f.TheMovieDatabaseFileName, json);
                            }
                        });
                    }
                }
            }
            catch (TaskCanceledException t)
            {
                // Ignore, Task was canceled.
            }
            catch (Exception ex)
            {
                IMessageBox.Show(ex.Message);
                if (ex.Message.Contains("unauthorized"))
                {
                    Settings.Default.TMDbApiKey = string.Empty;
                    Settings.Default.Save();
                }
            }
            finally
            {
                IsBusy = false;
            }
        }