string csvAlreadyExist(string region, string rank) { string regionFolderPath = StaticValue.getRegionRankDirectory(region, rank); Directory.CreateDirectory(regionFolderPath); DirectoryInfo di = new DirectoryInfo(regionFolderPath); bool overallDataExisting = false, championCounterDataExisting = false; foreach (FileInfo file in di.GetFiles()) { if (file.Name == StaticValue.csv_overallChampionsInfo) { overallDataExisting = true; } if (file.Name == StaticValue.csv_championCounter) { championCounterDataExisting = true; } } if (overallDataExisting && championCounterDataExisting) { return("Csv file already exists."); } else { myBrowser.ExecuteScriptAsync("$('.overlay').toggleClass('scale-out');"); return(startCrawler(region, rank)); } }
string startNewGame(string[] ourLane, string[] enemylane, bool firstMove) { string msg = ""; bool dataPersisted = StaticValue.allChampions.Count > 0; if (dataPersisted) { cancelRunningGameTask(); game = new Game(); game.FirstMove = firstMove; game.ChampionPool = StaticValue.getCopyOfAllChampions(); game.OurTeam.UpdateLane(ourLane); game.EnemyTeam.UpdateLane(enemylane); rotateBPDecision(game); TeammateRule teammateRule = new TeammateRule(game); teammateRule.Validate(); UpdateChampionPoolWeightRule updateChampionPoolWeightRule = new UpdateChampionPoolWeightRule(game); updateChampionPoolWeightRule.Validate(); RecommendRule recommendRule = new RecommendRule(game); recommendRule.Validate(); game_t = Task.Run(BP); var jsonresponse = new JavaScriptSerializer().Serialize(game.instruction); return(jsonresponse); } else { msg = "Data has not been persisted yet."; } return(msg); }
string persistCsvData(string region, string rank) { string msg = ""; string regionFolderPath = StaticValue.getRegionRankDirectory(region, rank), overallDataPath = string.Format("{0}\\{1}", regionFolderPath, StaticValue.csv_overallChampionsInfo), championCounterDataPath = string.Format("{0}\\{1}", regionFolderPath, StaticValue.csv_championCounter); if (File.Exists(overallDataPath) && File.Exists(championCounterDataPath)) { StaticValue.allChampions.Clear(); FileStream stream_overall = new FileStream(overallDataPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); TextFieldParser csvReader_overall = new TextFieldParser(stream_overall); csvReader_overall.SetDelimiters(new string[] { "," }); FileStream stream_counter = new FileStream(championCounterDataPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); TextFieldParser csvReader_counter = new TextFieldParser(stream_counter); csvReader_counter.SetDelimiters(new string[] { "," }); csvReader_overall.ReadFields(); //header csvReader_counter.ReadFields(); //header string[] filed_counter = null; while (!csvReader_overall.EndOfData) { string[] filed = csvReader_overall.ReadFields(); Champion champion = new Champion(); champion.Name = filed[0]; string championName = champion.Name; string[] lanes = filed[2].Split(';'); for (int i = 0; i < lanes.Count(); i++) { champion.Lanes.Add(StaticValue.mapLane(lanes[i])); } champion.WinRate = Convert.ToDouble(filed[3]); champion.BanRate = Convert.ToDouble(filed[4]); CounterInfo counterInfo = new CounterInfo(); if (filed_counter != null) { CounterRelation cf = new CounterRelation(); cf.ChampionName = filed_counter[2]; cf.Lane = StaticValue.mapLane(filed_counter[3]); cf.Rate = Convert.ToDouble(filed_counter[4]); switch (filed_counter[1]) { case "best against": counterInfo.BestAgainst[cf.ChampionName] = cf; break; case "is countered by": counterInfo.IsCounteredBy[cf.ChampionName] = cf; break; case "best with": counterInfo.BestWith[cf.ChampionName] = cf; break; } } while (!csvReader_counter.EndOfData && championName == champion.Name) { filed_counter = csvReader_counter.ReadFields(); championName = filed_counter[0]; if (championName == champion.Name) { CounterRelation cf = new CounterRelation(); cf.ChampionName = filed_counter[2]; cf.Lane = StaticValue.mapLane(filed_counter[3]); cf.Rate = Convert.ToDouble(filed_counter[4]); switch (filed_counter[1]) { case "best against": counterInfo.BestAgainst[cf.ChampionName] = cf; break; case "is countered by": counterInfo.IsCounteredBy[cf.ChampionName] = cf; break; case "best with": counterInfo.BestWith[cf.ChampionName] = cf; break; } filed_counter = null; } } champion.counterInfo = counterInfo; StaticValue.allChampions.Add(champion); } msg = "Data has been persisted into memory"; } else { msg = "No CSV file has been crawlered for the selected rank and region."; } return(msg); }
string startCrawler(string region, string rank) { string msg = ""; try { string regionFolderPath = StaticValue.getRegionRankDirectory(region, rank), overallDataPath = string.Format("{0}\\{1}", regionFolderPath, StaticValue.csv_overallChampionsInfo), championCounterDataPath = string.Format("{0}\\{1}", regionFolderPath, StaticValue.csv_championCounter); Directory.CreateDirectory(regionFolderPath); DirectoryInfo di = new DirectoryInfo(regionFolderPath); foreach (FileInfo file in di.GetFiles()) { file.Delete(); } // Load website HtmlWeb web = new HtmlWeb(); web.UserAgent = StaticValue.user_agent; //change the user agent of htmlweb object. ChangeUserAgent(); //change the user agent of webbrowser(.net control). var doc = new HtmlAgilityPack.HtmlDocument(); string rank_url = StaticValue.mapRegionNameAndRankNameToUrlPara(rank), region_url = StaticValue.mapRegionNameAndRankNameToUrlPara(region); string websiteSource = StaticValue.getOverallbyRegionRankUrl(region_url, rank_url); doc = web.Load(websiteSource); var div = doc.GetElementbyId("mainContent"); HtmlNodeCollection rows_hero = div.SelectNodes("//table[@class = 'data_table with_sortable_column']//tr"); //overall data using (StreamWriter sw_overall = File.CreateText(overallDataPath), sw_champion = File.CreateText(championCounterDataPath)) { sw_overall.WriteLine(string.Format("champion name,url,lane,win rate,ban rate")); sw_champion.WriteLine("champion name, relationship, agent champion name, lane, win rate"); //counter data for every individual champion for (int i = 0; i < rows_hero.Count; i++) { HtmlNode node = rows_hero[i]; if (node.Attributes.Count > 0) //table header, skip { continue; } HtmlNodeCollection championAttributes = node.SelectNodes("td"); if (championAttributes.Count > 1) { HtmlNode attribute1 = championAttributes[1].SelectSingleNode("a[1]"); string name = attribute1.SelectSingleNode("div/div[@class='txt']/span").InnerText.Replace("\r\n", "").Replace(" ", String.Empty), alias = attribute1.Attributes["href"].Value.Split('/')[3].ToLower(); string lane = attribute1.SelectSingleNode("div/div[@class='txt']/i").InnerText.Replace("\r\n", "").Replace(" ", String.Empty).Replace(",", ";"); string winRate = championAttributes[3].SelectSingleNode("progressbar[1]").Attributes["data-value"].Value; string banRate = championAttributes[4].SelectSingleNode("progressbar[1]").Attributes["data-value"].Value; string championCounterURL = StaticValue.getChampionCounterbyRegionRankUrl(alias, region_url, rank_url); sw_overall.WriteLine(string.Format("{0},{1},{2},{3},{4}", name, championCounterURL, lane, winRate, banRate)); var imgClient = new WebClient();//champion avatar downloader myBrowser.ExecuteScriptAsync("M.toast({html: 'Crawling data of " + alias + "', classes: 'rounded'});"); this.Invoke(((Action)(() => { Dictionary <int, string> rankDic = new Dictionary <int, string>(); rankDic[0] = Rank.IRON; rankDic[1] = Rank.BRONZE; rankDic[2] = Rank.GOLD; rankDic[3] = Rank.SILVER; rankDic[4] = Rank.PLATINUM; rankDic[5] = Rank.DIAMOND; rankDic[6] = Rank.MASTER; //the website clears and restatistic data from servers. //so some times the data is not available for the slected rank and server. //in this case, craw data from lower rank and all server. string[] tableNmae = new string[3] { "best against", "is countered by", "best with" }; bool[] dataGot = new bool[] { false, false, false }; int rank_i = rankDic.FirstOrDefault(x => x.Value == rank_url).Key; while (!dataGot[0] || !dataGot[1] || !dataGot[2]) //while not all counter data are retrieved { if (rank_i < 0) { break; } var doc_champion = web.LoadFromBrowser(championCounterURL, html => { // WAIT until the dynamic text is set return(!html.Contains("<button type='button' class='see_more_button'>See more</button>")); }); var content = doc_champion.DocumentNode; var avarta_url = content.SelectSingleNode("//div[@class='pageBanner img-align-block']/div[@class='img']/img").Attributes["src"].Value; //download champion avatar string avartaPath = @"../debug/web res/IMG/champion-avatar/" + name + ".jpg"; if (!File.Exists(avartaPath)) { imgClient.DownloadFile("https:" + avarta_url, avartaPath); } var winRateTables = content.SelectNodes("//table[@class= 'data_table sortable_table']"); //best with, best against, is countered by for (int j = 0; j < winRateTables.Count; j++) { HtmlNodeCollection agentChampions = winRateTables[j].SelectNodes("tbody/tr"); if (agentChampions.Count > 1 && !dataGot[j]) { dataGot[j] = true; for (int k = 1; k < agentChampions.Count; k++) { HtmlNode button = agentChampions[k].SelectSingleNode("td/button[@class='see_more_button']"); if (button == null) { HtmlNodeCollection agentChampionInfo = agentChampions[k].SelectNodes("td"); string agent_name = agentChampionInfo[0].SelectSingleNode("a/div/div[@class = 'txt']/span").InnerText.Replace("\r\n", "").Replace(" ", String.Empty), agent_lane = agentChampionInfo[0].SelectSingleNode("a/div/div[@class = 'txt']/i").InnerText.Replace("\r\n", "").Replace(" ", String.Empty).Replace(",", ";"), winRate_agent = agentChampionInfo[1].SelectSingleNode("progressbar").Attributes["data-value"].Value; sw_champion.WriteLine(string.Format("{0},{1},{2},{3},{4}", name, tableNmae[j], agent_name, agent_lane, winRate_agent)); } } } } championCounterURL = StaticValue.getChampionCounterbyRegionRankUrl(alias, ISS_MS_RS_BpHelper.Region.All, rankDic[--rank_i]); } })), null); } } } msg = "Crawler has finished, csv has been saved to " + regionFolderPath; } catch (Exception e) { msg = "error " + e.Message; } return(msg); }