public ScrapedMatches GetScrapedMatch(int id)
        {
            ScrapedMatches result = new ScrapedMatches();

            result = _db.ScrapedMatches.Single(p => p.Id == id);

            return(result);
        }
        // GET: LoadCompare
        public ActionResult LoadCompare(int id, int MinFTR = 0)
        {
            CompareStatisticModel model = new CompareStatisticModel();
            ScrapedMatches        match = new ScrapedMatches();
            // Create Cachekey from parameters
            var CACHEKEY = $"cacheKey:MatchId={id}-MinFTR={MinFTR}";

            // If we have object in cache, return it
            if (Cache.Exists(CACHEKEY))
            {
                model = (CompareStatisticModel)Cache.Get(CACHEKEY);
            }
            else
            {
                match = _dataWorker.GetScrapedMatch(id);

                if (match != null)
                {
                    if (MinFTR == 5)
                    {
                        model = JsonConvert.DeserializeObject <CompareStatisticModel>(match.Json5MinFTR);
                    }
                    else if (MinFTR == 4)
                    {
                        model = JsonConvert.DeserializeObject <CompareStatisticModel>(match.Json4MinFTR);
                    }
                    else
                    {
                        model = JsonConvert.DeserializeObject <CompareStatisticModel>(match.Json);
                    }
                }

                // Save in cache
                if (!string.IsNullOrEmpty(CACHEKEY) && !Cache.Exists(CACHEKEY))
                {
                    int storeTime = 1000 * 3600 * 24 * 2; // store 2 days
                    Cache.Store(CACHEKEY, model, storeTime);
                }
            }

            model.ScrapeMatchId      = id;
            model.MinFullTeamRanking = MinFTR;
            return(View(model));
        }
        public int AddScrapedMatch(ScrapedMatches scrapedMatch, int MinFTR)
        {
            var  model      = new ScrapedMatches();
            bool bOldRecord = false;

            if (_db.ScrapedMatches.Any(p => p.MatchId == scrapedMatch.MatchId))
            {
                bOldRecord        = true;
                model             = _db.ScrapedMatches.Single(p => p.MatchId == scrapedMatch.MatchId);
                scrapedMatch.Id   = model.Id;
                model.MatchId     = scrapedMatch.MatchId;
                model.MatchUrl    = scrapedMatch.MatchUrl;
                model.Name        = scrapedMatch.Name;
                model.SportName   = scrapedMatch.SportName;
                model.Start       = scrapedMatch.Start;
                model.Team1Id     = scrapedMatch.Team1Id;
                model.Team1Name   = scrapedMatch.Team1Name;
                model.Team1Logo   = scrapedMatch.Team1Logo;
                model.Team2Id     = scrapedMatch.Team2Id;
                model.Team2Name   = scrapedMatch.Team2Name;
                model.Team2Logo   = scrapedMatch.Team2Logo;
                model.Json        = MinFTR == 0 || MinFTR == -1? scrapedMatch.Json : model.Json;
                model.Json4MinFTR = MinFTR == 4 || MinFTR == -1 ? scrapedMatch.Json4MinFTR : model.Json4MinFTR;
                model.Json5MinFTR = MinFTR == 5 || MinFTR == -1 ? scrapedMatch.Json5MinFTR : model.Json5MinFTR;
            }
            else
            {
                _db.ScrapedMatches.Add(scrapedMatch);
            }
            _db.SaveChanges();


            if (bOldRecord && MinFTR == -1 && scrapedMatch.Id > 0)
            {
                int storeTime = 1000 * 3600 * 24 * 2; // store 2 days
                // Remove all cachekeys, since we got new data
                var CACHEKEY = $"cacheKey:MatchId={scrapedMatch.Id}-MinFTR=0";
                if (Cache.Exists(CACHEKEY))
                {
                    Cache.Update(CACHEKEY, JsonConvert.DeserializeObject <CompareStatisticModel>(model.Json));
                }
                else
                {
                    Cache.Store(CACHEKEY, JsonConvert.DeserializeObject <CompareStatisticModel>(model.Json), storeTime);
                }
                CACHEKEY = $"cacheKey:MatchId={scrapedMatch.Id}-MinFTR=4";
                if (Cache.Exists(CACHEKEY))
                {
                    Cache.Update(CACHEKEY, JsonConvert.DeserializeObject <CompareStatisticModel>(model.Json4MinFTR));
                }
                else
                {
                    Cache.Store(CACHEKEY, JsonConvert.DeserializeObject <CompareStatisticModel>(model.Json4MinFTR), storeTime);
                }
                CACHEKEY = $"cacheKey:MatchId={scrapedMatch.Id}-MinFTR=5";
                if (Cache.Exists(CACHEKEY))
                {
                    Cache.Update(CACHEKEY, JsonConvert.DeserializeObject <CompareStatisticModel>(model.Json5MinFTR));
                }
                else
                {
                    Cache.Store(CACHEKEY, JsonConvert.DeserializeObject <CompareStatisticModel>(model.Json5MinFTR), storeTime);
                }
            }

            return(scrapedMatch.Id);
        }
        public async Task <CompareStatisticModel> runCompare(CompareStatisticModel model)
        {
            try
            {
                if (model != null)
                {
                    if (!string.IsNullOrEmpty(model.MatchUrl) || model.Team1Id > 0 || model.Team2Id > 0)
                    {
                        if (!string.IsNullOrEmpty(model.MatchUrl))
                        {
                            Program.MatchUrl = model.MatchUrl;
                            var result = _program.GetTeamIdsFromUrl(model.MatchUrl);

                            model.Team1Id        = result.Item1;
                            model.Team2Id        = result.Item2;
                            model.ExpectedLineUp = _program.GetTeamLineup(model.MatchUrl, model.Team1Id, model.Team2Id);
                        }
                        else
                        {
                            model.ExpectedLineUp = new ExpectedLineUp();
                        }
                        if (model.Team1Id > 0)
                        {
                            var secondaryTeamId = _dataWorker.GetSecondaryTeamId(model.Team1Id);

                            if (model.Scrape)
                            {
                                await _program.GetTeamDetails(model.Team1Id);
                            }

                            var logo = CheckIfLogoExist(model.Team1Id);
                            model.Teams.Add(await _dataWorker.GetTeamPeriodStatistics(model.Team1Id, model.PeriodSelection, model.ExpectedLineUp, secondaryTeamId, model.NoCache, model.MinFullTeamRanking, _program.Team1Rank, logo));
                        }
                        if (model.Team2Id > 0)
                        {
                            var secondaryTeamId = _dataWorker.GetSecondaryTeamId(model.Team2Id);

                            if (model.Scrape)
                            {
                                await _program.GetTeamDetails(model.Team2Id);
                            }
                            var logo = CheckIfLogoExist(model.Team2Id);
                            model.Teams.Add(await _dataWorker.GetTeamPeriodStatistics(model.Team2Id, model.PeriodSelection, model.ExpectedLineUp, secondaryTeamId, model.NoCache, model.MinFullTeamRanking, _program.Team2Rank, logo));
                        }

                        if (model.Teams != null && model.Teams.Count > 0)
                        {
                            _dataWorker.GenerateSuggestedMaps(ref model);
                        }


                        if (model != null)
                        {
                            ScrapedMatches scrapedMatch = new ScrapedMatches();
                            scrapedMatch.SportName = "CS:GO";
                            scrapedMatch.Event     = model.ExpectedLineUp.EventName;
                            scrapedMatch.MatchId   = model.ExpectedLineUp.MatchId;
                            scrapedMatch.Start     = model.ExpectedLineUp.Start;
                            scrapedMatch.MatchUrl  = model.MatchUrl;
                            scrapedMatch.Name      = $"{model.Teams[0].TeamName} - {model.Teams[1].TeamName}";
                            if (model.MinFullTeamRanking == 4)
                            {
                                scrapedMatch.Json4MinFTR = JsonConvert.SerializeObject(model);
                            }
                            else if (model.MinFullTeamRanking == 5)
                            {
                                scrapedMatch.Json5MinFTR = JsonConvert.SerializeObject(model);
                            }
                            else
                            {
                                scrapedMatch.Json = JsonConvert.SerializeObject(model);
                            }
                            _dataWorker.AddScrapedMatch(scrapedMatch, model.MinFullTeamRanking);
                        }
                    }
                }


                return(model);
            }
            catch (Exception ex)
            {
                // just redisplay the form if something failed.
                ModelState.AddModelError("", ex.Message);
                return(new CompareStatisticModel());
            }
        }
        public async Task <ActionResult> SendToCompare(string url)
        {
            var scrapedmatchid = 0;

            try
            {
                var PeriodSelection = new List <string>();
                PeriodSelection.Add("3");
                PeriodSelection.Add("6");
                var model = new CompareStatisticModel();

                if (!url.Contains("http://www.hltv.org/"))
                {
                    model.MatchUrl = "http://www.hltv.org/" + url;
                }
                else
                {
                    model.MatchUrl = url;
                }


                model.Scrape          = true;
                model.PeriodSelection = PeriodSelection;

                // ******* Get Ids and teamlineup *******
                if (url.Length > 0)
                {
                    var result = _program.GetTeamIdsFromUrl(model.MatchUrl);
                    model.Team1Id        = result.Item1;
                    model.Team2Id        = result.Item2;
                    model.ExpectedLineUp = _program.GetTeamLineup(model.MatchUrl, model.Team1Id, model.Team2Id);
                }

                // ******* Scrape *******
                if (model.Team1Id > 0)
                {
                    if (model.Scrape)
                    {
                        await _program.GetTeamDetails(model.Team1Id);
                    }
                }
                if (model.Team2Id > 0)
                {
                    if (model.Scrape)
                    {
                        await _program.GetTeamDetails(model.Team2Id);
                    }
                }

                if (model.Team1Id > 0 && model.Team2Id > 0)
                {
                    var secondaryTeam1Id = _dataWorker.GetSecondaryTeamId(model.Team1Id);
                    var secondaryTeam2Id = _dataWorker.GetSecondaryTeamId(model.Team2Id);
                    model.HeadToHead = _dataWorker.GetHeadToHeadMatches(model.Team1Id, model.Team2Id);
                    var Team1Form = _dataWorker.GetTeamForm(model.Team1Id);
                    var Team2Form = _dataWorker.GetTeamForm(model.Team2Id);

                    List <int> FTR = new List <int> {
                        0, 4, 5
                    };
                    List <Tuple <int, string> > jsonlist = new List <Tuple <int, string> >();

                    // ******* Create model result for each MinFtr and save as json *******
                    foreach (int ftr in FTR)
                    {
                        model.Teams = new List <TeamStatisticPeriodModel>();
                        var logo = CheckIfLogoExist(model.Team1Id);
                        model.Teams.Add(await _dataWorker.GetTeamPeriodStatistics(model.Team1Id, model.PeriodSelection, model.ExpectedLineUp, secondaryTeam1Id, model.NoCache, ftr, _program.Team1Rank, logo));
                        var logo2 = CheckIfLogoExist(model.Team2Id);
                        model.Teams.Add(await _dataWorker.GetTeamPeriodStatistics(model.Team2Id, model.PeriodSelection, model.ExpectedLineUp, secondaryTeam2Id, model.NoCache, ftr, _program.Team2Rank, logo2));
                        if (model.Teams != null && model.Teams.Count > 0)
                        {
                            _dataWorker.GenerateSuggestedMaps(ref model);
                            if (model.Teams.Count > 1)
                            {
                                model.Teams[0].Form = Team1Form;
                                model.Teams[1].Form = Team2Form;
                            }
                        }
                        if (model != null)
                        {
                            jsonlist.Add(new Tuple <int, string>(ftr, JsonConvert.SerializeObject(model)));
                        }
                    }

                    // ******* If we have any json result we create ScrapedMatch and fill in info *******
                    if (jsonlist.Count > 0)
                    {
                        ScrapedMatches scrapedMatch = new ScrapedMatches();
                        scrapedMatch.SportName = "CS:GO";
                        scrapedMatch.Event     = model.ExpectedLineUp.EventName;
                        scrapedMatch.MatchId   = model.ExpectedLineUp.MatchId;
                        scrapedMatch.Start     = model.ExpectedLineUp.Start;
                        scrapedMatch.MatchUrl  = model.MatchUrl;
                        scrapedMatch.Team1Id   = model.Teams[0].TeamId;
                        scrapedMatch.Team1Name = model.Teams[0].TeamName;
                        scrapedMatch.Team1Logo = CheckIfLogoExist(model.Teams[0].TeamId);
                        scrapedMatch.Team2Id   = model.Teams[1].TeamId;
                        scrapedMatch.Team2Name = model.Teams[1].TeamName;
                        scrapedMatch.Team2Logo = CheckIfLogoExist(model.Teams[1].TeamId);
                        scrapedMatch.Name      = $"{model.Teams[0].TeamName} - {model.Teams[1].TeamName}";
                        if (jsonlist.Any(p => p.Item1 == 4))
                        {
                            scrapedMatch.Json4MinFTR = jsonlist.Single(p => p.Item1 == 4).Item2;
                        }
                        if (jsonlist.Any(p => p.Item1 == 5))
                        {
                            scrapedMatch.Json5MinFTR = jsonlist.Single(p => p.Item1 == 5).Item2;
                        }
                        if (jsonlist.Any(p => p.Item1 == 0))
                        {
                            scrapedMatch.Json = jsonlist.Single(p => p.Item1 == 0).Item2;
                        }
                        scrapedmatchid = _dataWorker.AddScrapedMatch(scrapedMatch, -1);
                    }
                }
            }
            catch (Exception ex)
            {
                // Do nothing
            }

            return(RedirectToAction("LoadCompare", new { id = scrapedmatchid, MinFTR = 0 }));
        }