-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChampStatisticReader.java
More file actions
189 lines (186 loc) · 7.34 KB
/
Copy pathChampStatisticReader.java
File metadata and controls
189 lines (186 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import java.io.File;
import java.io.BufferedReader;
import java.util.Scanner;
import java.net.URL;
import java.io.InputStreamReader;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Collections;
import java.util.Map;
import java.util.ArrayList;
import java.util.TreeSet;
import java.util.Comparator;
import java.io.FileInputStream;
import java.util.Date;
/**
* A utility class which reads through the online user data from na.op.gg and region data from loldb.gameguyz.com
* WILL BE REPLACED upon this application receiving a Production API Key (enabling the collection and storage of
* data rather than relying on external sites for pre-processed data)
* Each function returns a mapping between Champion names and Winrates as doubles.
*/
public class ChampStatisticReader {
/**
* The regional data collector: reads the table of champion winrates for a given region
*/
public static HashMap<String, Double> collectGlobalStatistics(String region) {
HashMap<String, Double> result = new HashMap<String, Double>();
int regionVal = 0;
switch (region) {
case("NA"):
regionVal = 1;
break;
case("EUW"):
regionVal = 2;
break;
case("EUNE"):
regionVal = 3;
break;
case("BR"):
regionVal = 4;
break;
case("TR"):
regionVal = 5;
break;
case("RU"):
regionVal = 6;
break;
case("LAN"):
regionVal = 7;
break;
case("LAS"):
regionVal = 8;
break;
case("OCE"):
regionVal = 9;
break;
case("KR"):
regionVal = 10;
break;
default:
break;
}
try {
String URL = "http://loldb.gameguyz.com/statistics/winRate/" + regionVal + "/0/2/2/0/30";
URL website = new URL(URL);
Scanner input = new Scanner(website.openStream());
/**
* Because of personal lack of knowledge in PHP/HTML and web design in general, I "brute force" load the data with patterns I observed
* in the text of the page.
*/
boolean winRate = false;
String curr;
String currChamp = "";
while (input.hasNextLine()) {
curr = input.nextLine();
if (curr.contains("dname")) {
currChamp = curr.split("\"")[1];
if (currChamp.equals("LeBlanc")) currChamp = "Leblanc";
}
if (winRate) {
result.put(currChamp, Double.parseDouble(curr.split("\"")[1].split("%")[0]));
winRate = false;
}
if (curr.contains("ar ar3")) {
winRate = true;
}
}
Helpers.saveGlobalData(result, region);
Helpers.saveGlobalUpdateTime(region, new Date());
return result;
} catch (Exception e) {
System.out.println("There was an error compiling the data.");
System.out.println(e);
return result;
}
}
/**
* The user data collector: reads the user's ranked history for this season with champions that have been played
* at least minGames times
*/
public static HashMap<String, Double> collectUserStatistics(String summonerName, String region, int minGames) {
HashMap<String, String> DICTIONARY = new HashMap<String,String>();
try {
String encoder = "UTF-8";
InputStream in = ChampStatisticReader.class.getResourceAsStream(".coredata/championlists/Champ_dictionary.txt");
BufferedReader input = new BufferedReader(new InputStreamReader(in, encoder));
String curr;
while ((curr = input.readLine()) != null) {
DICTIONARY.put(curr.split(",")[0], curr.split(",")[1].split("\n")[0]);
}
} catch (Exception e) {
System.out.println(e);
}
HashMap<String, Double> result = new HashMap<String, Double>();
try {
String[] parts = summonerName.split(" ");
summonerName = "";
for (String s : parts) {
summonerName += s;
}
String urlRegion = region;
if (region.equals("kr")) {
urlRegion = "";
} else {
urlRegion += ".";
}
URL URL = new URL("http://" + urlRegion.toLowerCase() +"op.gg/summoner/champions/userName=" + summonerName);
String encoder = "UTF-8";
InputStream in = URL.openConnection().getInputStream();
BufferedReader input = new BufferedReader(new InputStreamReader(in, encoder));
/**
* Again, simply reads strings from the source of the site, causing it to be more prone to errors
* Will also be replaced in the near future with more robust methodology
*/
boolean reachedData = false;
String currChamp = "";
String curr;
int currWins = 0;
int currLosses = 0;
int line = 0;
while (((curr = input.readLine()) != null) && (!curr.contains("</table"))) {
line += 1;
if (!reachedData) {
if (curr.contains("ChampionStatsTable")) {
reachedData = true;
}
continue;
}
if (curr.contains("ChampionName Cell")) {
currChamp = curr.split(">")[1].split("<")[0];
if (DICTIONARY.containsKey(currChamp)){
currChamp = DICTIONARY.get(currChamp);
} else {
System.out.println("Oops, Dictionary does not contain the translation of: " + currChamp + " (line " + line + ")");
}
continue;
}
if (curr.contains("Text Left")) {
curr = curr.split(">")[1].split("<")[0];
curr = curr.substring(0, curr.length()-1);
currWins = Integer.parseInt(curr);
continue;
}
if (curr.contains("Text Right")) {
curr = curr.split(">")[1].split("<")[0];
curr = curr.substring(0, curr.length()-1);
currLosses = Integer.parseInt(curr);
}
if (curr.contains("span class")) {
if ((currWins + currLosses) > minGames) result.put(currChamp, Double.parseDouble(curr.split(">")[1].split("%")[0]));
continue;
}
}
} catch (Exception e) {
System.out.println("There was an error compiling the data.");
e.printStackTrace();
return result;
}
Helpers.saveUserData(result, summonerName, region);
Helpers.saveUserUpdateTime(summonerName, region, new Date());
return result;
}
}