[C #] DOTA2 hero Matching Assistant of WinForm (webpage capturing + online Green Edition + open source code ),
Private string GetWebContent (string Url) {string strResult = ""; try {HttpWebRequest request = (HttpWebRequest) WebRequest. create (Url); // declare an HttpWebRequest request. timeout = 30000; // set the connection Timeout request. headers. set ("Pragma", "no-cache"); HttpWebResponse response = (HttpWebResponse) request. getResponse (); Stream streamReceive = response. getResponseStream (); Encoding encoding = Encoding. getEncoding ("UTF-8"); StreamReader streamReader = new StreamReader (streamReceive, encoding); strResult = streamReader. readToEnd ();} catch {MessageBox. show ("failed to get information, please check network connection") ;}return strResult ;}
The following are the capture steps. As I did this kind of application for the first time, please point it out in time.
A. Capture hero Information
First, capture the hero information.
Target URL: http://dotamax.com/hero/
PAGE analysis:
<Div id = "centroid" class = "hero-list-hero Unused-Hero" onclick = "DoNav ('/hero/detail/centaur /') "> <div class =" hero-top-list-bar "> <span style = "position: absolute; left: 5px; color: rgb (87,186, 53); bottom:-4px; "> </span> </div> <div c Lass = "hero-list-bar"> <span style = "color: # ccc! Important; text-align: center; "> centroid </span> </div>
From this label, it should be easy to find
- Chinese hero name
- English hero name
- Hero Avatar
After these fields are available, you can create a local cache or add a row of records. The Code is as follows:
Private void getHeros () {heroDataTable = new DataTable ("heros"); heroDataTable. columns. add ("Hero name", typeof (string); // URL to be crawled string Url =" http://www.dotamax.com/hero/ "; // Get the source code of the specified Url string html = GetWebContent (Url); string EnName, ChName; string key; int index = 0; // string output = ""; int count = 0; do {key = "onclick = \" DoNav ('/hero/detail/"; int I = html. indexOf (key, index); if (I =-1) break; I + = key. length; int j = html. indexOf ("/", I); EnName = html. substring (I, j-I); key = "<span style = \" color: # ccc! Important; text-align: center; \ ">"; I = html. indexOf (key, j + 1); I + = key. length; j = html. indexOf ("</span>", I); ChName = html. substring (I, j-I); Ch2En. add (ChName, EnName); heroList. add (ChName); DataRow dr = heroDataTable. newRow (); dr ["Hero name"] = ChName; heroDataTable. rows. add (dr); count ++; index = j;} while (true );}
B. Capture the restraint Index
Based on the hero name, go to the specified URL to capture the list of heroes restrained by the hero.
Target URL: http://dotamax.com/hero/detail/match_up_anti/english hero name/
PAGE analysis:
<Tr> <td> <a href = "/hero/detail/phantom_assassin"> </a> <span class = "hero-name-list"> phantom assassin </span> </td> <div style = "height: 10px "> 3.19% </div> <div class =" segment-green "style =" width: 33.997677256%; "> </div> </td> <div style =" height: 10px "> 56.96% </div> <div class =" segment-gold "style =" width: 56.9584024346%; "> </div> </td> <div style =" height: 10px "> 292445 </div> <div class =" segment-green "style =" width: %; "> </div> </td> </tr>
There are too many comments on the page (don't Know If You Want To set don't care). Remember to skip the page.
<!-- <td><div>-->
Find
- Restrained hero name
- Restraint Index
In this way, if you select this restraint hero, you will add the corresponding restraint index. Then sort the values in order to give recommendations. Code:
Private void addAntiIndex (string hero, int no) {no ++; string CurEnName = Ch2En [hero]; string CurChName = hero; string Url =" http://www.dotamax.com/hero/detail/match_up_anti/ "+ CurEnName +"/"; // obtain the source code of the specified Url: html = GetWebContent (Url); string AntiName, AntiValue, WinRate, UsedTime; string key; int index = 0 ;; do {key = "<span class = \" hero-name-list \ ">"; int I = html. indexOf (key, index); if (I =-1) {autoSorting (); return;} I + = key. length; int j = html. indexOf ("</span>", I); AntiName = html. substring (I, j-I); key = "<div style = \" height: 10px \ ">"; I = html. indexOf (key, j + 1); I + = key. length; j = html. indexOf ("</div>", I); AntiValue = html. substring (I, j-I); // remove anti-crawling j = html. indexOf ("-->", j); key = "<div style = \" height: 10px \ ">"; I = html. indexOf (key, j + 3); I + = key. length; j = html. indexOf ("</div>", I); WinRate = html. substring (I, j-I); key = "<div style = \" height: 10px \ ">"; I = html. indexOf (key, j + 1); I + = key. length; j = html. indexOf ("</div>", I); UsedTime = html. substring (I, j-I); index = j; AntiValue = AntiValue. substring (0, AntiValue. length-1); double value = Convert. toDouble (AntiValue); int t_no = findHero (AntiName); heroDataTable. rows [t_no] [no] =-value; double sum = 0; for (int h = 2; h <12; h ++) {sum + = (double) heroDataTable. rows [t_no] [h];} heroDataTable. rows [t_no] [1] = sum;} while (true );}
C. Capture the matching index
Based on the hero name, go to the specified URL to capture the list of heroes matching the hero.
Target URL: http://dotamax.com/hero/detail/match_up_comb//
There is basically no difference between the page and the previous step. I will not repeat it here.
The purpose is to find
- Hero name
- Coordination Index
In this way, on the basis of restraint, we will continue to combine the hero selected by our teammates to select the most XXX lineup. Code:
Private void addCombIndex (string hero, int no) {no ++; string CurEnName = Ch2En [hero]; string CurChName = hero; string Url =" http://www.dotamax.com/hero/detail/match_up_comb/ "+ CurEnName +"/"; // obtain the source code of the specified Url: html = GetWebContent (Url); string CombName, CombValue, WinRate, UsedTime; string key; int index = 0 ;; do {key = "<span class = \" hero-name-list \ ">"; int I = html. indexOf (key, index); if (I =-1) {autoSorting (); return;} I + = key. length; int j = html. indexOf ("</span>", I); CombName = html. substring (I, j-I); key = "<div style = \" height: 10px \ ">"; I = html. indexOf (key, j + 1); I + = key. length; j = html. indexOf ("</div>", I); CombValue = html. substring (I, j-I); // remove anti-crawling j = html. indexOf ("-->", j); key = "<div style = \" height: 10px \ ">"; I = html. indexOf (key, j + 3); I + = key. length; j = html. indexOf ("</div>", I); WinRate = html. substring (I, j-I); key = "<div style = \" height: 10px \ ">"; I = html. indexOf (key, j + 1); I + = key. length; j = html. indexOf ("</div>", I); UsedTime = html. substring (I, j-I); index = j; CombValue = CombValue. substring (0, CombValue. length-1); double value = Convert. toDouble (CombValue); int t_no = findHero (CombName); heroDataTable. rows [t_no] [no] = value; double sum = 0; for (int h = 2; h <12; h ++) {sum + = (double) heroDataTable. rows [t_no] [h];} heroDataTable. rows [t_no] [1] = sum;} while (true );}
D. Simple search and sorting
This seems like nothing to say.
Conclusion
I have used txt files, MS-SQL (VS more convenient) cache web data, but in order to simplify the installation and convenience of the Foundation test or write an online green version, that is: edge analysis and display, all data is stored only with variables. It only occupies part of the memory and does not generate any cached files. The entire program has only one exe file.
Of course, the C # program still needs to have the. net framework in advance. However, as a CS professional partner, there is actually no. NET installation, and I am no longer able to speak out.
However, there is also a problem in this way, that is, the response speed is slow, because in addition to importing all the hero information into the memory when starting the program, each time a hero is added, it also needs to capture a restraint/cooperation information. Anyway, I must use an offline version to pull data. Otherwise, I would have time to select statistics from the hero. However, everyone really wants to change the data and watch the crawled data fill up the database, feeling good: P
In fact, the biggest problem is that it is too difficult to manually add a hero, but DOTA2 seems that only the RPG version can be used as the Lua plug-in... I really don't know how the DOTA plug-ins interact with games. So can only be made into "DOTA2 hero with assistant" instead of "the dirtiest DOTA2 plug-in... Please tell me if anyone knows ~ Well, I still want to build the XXXX plug-in. (Hello, pattern recognition. Goodbye !)
Dota2Aid online Green Edition: http://files.cnblogs.com/files/KC-Mei/Dota2Aid.zip (do not know that help write understand, should be able to use it)
Well, good open source, VS2012 project file: https://github.com/Blz-Galaxy/Dota2Aid_OnlineVersion
Because I use the local database version, some bugs may not be fixed in the online version. Forgive me for being too lazy to change it... Anyway, I don't feel like I am actually using this response speed.
Continue to beg for the interaction principle of the Multi-Play box...
Finally, a MS-SQL Service monitor is provided, so that the local database can be used properly ~
[C #] WinForm SQL Server Service Monitor (to avoid starting the service boot): http://www.cnblogs.com/KC-Mei/p/4334179.html