-
Notifications
You must be signed in to change notification settings - Fork 0
/
func.php
293 lines (264 loc) · 10.4 KB
/
func.php
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
if (preg_match ("/func.php/", $_SERVER['PHP_SELF'])) {
header ("Location: /index.php");
exit;
}
function now() {
return strtotime(date("Y-m-d H:i:s"));
}
function arraySpecialMerge($array1, $array2) {
foreach($array2 as $key2 => $val2) {
if(!array_key_exists($key2,$array1)) {
$array1[$key2] = $val2;
} else {
$array1[] = $val2;
}
}
return $array1;
}
function getHeadersCurl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$r = @curl_exec($ch);
$r = @explode("\n", $r);
return $r;
}
function isValidUrl($url) {
$url = @parse_url($url);
if(!$url) {
return false;
}
$url = array_map('trim', $url);
$url['port'] = (!isset($url['port'])) ? 80 : (int)$url['port'];
$path = (isset($url['path'])) ? $url['path'] : '';
if($path == '')
$path = '/';
$path .= (isset ($url['query'])) ? "?$url[query]" : '';
if(isset ($url['host']) AND $url['host'] != gethostbyname ($url['host'])) {
if(PHP_VERSION >= 5)
$headers = getHeadersCurl("$url[scheme]://$url[host]:$url[port]$path");
$headers = (is_array($headers)) ? implode ("\n", $headers) : $headers;
preg_match_all('#^HTTP/.*\s+[0-9]+\s#i', $headers,$match);
$tmp = explode(' ', $match[0][0]);
$code = array(200,301,302);
if(in_array($tmp[1],$code))
return true ;
}
return false;
}
function insertData($data) {
global $db;
$now = now();
foreach($data as $id => $ins) {
$name = $ins['name'];
$eff = $ins['eff'];
$win = $ins['win'];
$sql = "INSERT INTO players (id, name, time, eff, win)
VALUES ('$id','$name','$now','$eff','$win')
ON DUPLICATE KEY
UPDATE time='$now', eff='$eff', win='$win';";
echo $sql;
$db->executeQuery($sql);
}
}
function checkNames($names) {
global $db;
$sql = "SELECT * FROM players WHERE name IN ('".implode("','", $names)."')";
$db->executeQuery($sql);
$res = $db->loadResult();
// TODO check cache time
for($index = 0; $index < count($res); $index++) {
$name = $res[$index]['name'];
$win = $res[$index]['win'];
$eff = $res[$index]['eff'];
$cached[$name]['win'] = $win;
$cached[$name]['eff'] = $eff;
unset($names[$name]);
}
$names = array_values($names);
$result['names'] = &$names;
$result['str'] = &$cached;
return $result;
/*
Trash below
$str = array();
foreach($names as $name) {
$sql = "SELECT COUNT(*) FROM players WHERE time > ".(now() - CACHE * 3600)." AND name = '".$name."';";
$db->executeQuery($sql);
$str = $db->loadResult();
if($str[0]['COUNT(*)'] != 0) {
$sql = "SELECT * FROM players WHERE name = '".$name."';";
$db->executeQuery($sql);
$str = $db->loadResult();
$new_str[$name]['win'] = $str[0]['win'];
$new_str[$name]['eff'] = $str[0]['eff'];
} else {
$new_name[] = $name;
}
}
$new['names'] = &$new_name;
$new['str'] = &$new_str;
return $new;*/
}
function checkIds($ids) {
global $db;
// REVIEW redundant init?
$str = array();
foreach($ids as $id) {
// REVIEW Why no SELECT and check result length?
$sql = "SELECT COUNT(*) FROM players WHERE time > ".(now() - CACHE * 3600)." AND id = '".$id."';";
$db->executeQuery($sql);
$str = $db->loadResult();
if($str[0]['COUNT(*)'] != 0) {
$sql = "SELECT * FROM players WHERE id = '".$id."';";
$db->executeQuery($sql);
$str = $db->loadResult();
$new_str[$id]['name'] = $str[0]['name'];
$new_str[$id]['win'] = $str[0]['win'];
$new_str[$id]['eff'] = $str[0]['eff'];
} else {
$new_name[] = $id;
}
}
$new['ids'] = &$new_name;
$new['str'] = &$new_str;
return $new;
}
function batchGet($urls) {
$curl = new CURL();
$opts = array(CURLOPT_RETURNTRANSFER => true);
foreach($urls as $key => $link)
$curl->addSession($link, $key, $opts);
$result = $curl->exec();
$curl->clear();
return $result;
}
function getMemId($search) {
$data = array();
$error = 0;
$special = ' /uc/accounts/';
$request = "GET ".$special."?type=table&search=".(trim($search))." HTTP/1.0\r\n";
$request.= "Accept: text/html, */*\r\n";
$request.= "User-Agent: Mozilla/3.0 (compatible; easyhttp)\r\n";
$request.= "X-Requested-With: XMLHttpRequest\r\n";
$request.= "Host: worldoftanks.ru\r\n";
$request.= "Connection: Keep-Alive\r\n";
$request.= "\r\n";
$n = 0;
while(!isset($fp)) {
$fp = fsockopen('worldoftanks.ru', 80, $errno, $errstr, 20);
if($n == 5) {
break;
}
$n++;
}
if(!$fp) {
$error == 2; // Not seterd
} else {
stream_set_timeout($fp,20);
$info = stream_get_meta_data($fp);
fwrite($fp, $request);
$page = '';
while(!feof($fp) && (!$info['timed_out'])) {
$page .= fgets($fp, 4096);
$info = stream_get_meta_data($fp);
}
fclose($fp);
if($info['timed_out']) {
$error = 1; //Connection Timed Out
}
}
if($error == 0) {
preg_match_all("/{\"request_data\":(.*?),\"result\":\"success\"}/", $page, $matches);
$data = (json_decode($matches[0][0], true));
}
return $data;
}
function urlCreate($srch, $name) {
foreach($srch as $val) {
if(strtolower($val['name']) == strtolower($name)) {
$data = 'http://worldoftanks.ru/uc/accounts/'.$val['id'].'/api/1.2/?source_token=Intellect_Soft-WoT_Mobile-unofficial_stats';
}
}
return $data;
}
function linkCreater($names) {
// REVIEW what this?
// also: there are any differences with http://worldoftanks.ru/uc/accounts/1011592/api/1.1/?source_token=Intellect_Soft-WoT_Mobile-site ?
$chk_url = 'http://worldoftanks.ru/uc/accounts/1000/api/1.2/?source_token=Intellect_Soft-WoT_Mobile-unofficial_stats';
// REVIEW why 40? It seems we need max 30 players at once.
if(isValidUrl($chk_url) === true && count($names) < 40) {
foreach($names as $name) {
// REVIEW bad variable name
$srch = getMemId($name);
if(isset($srch['request_data']['items']['0'])) {
$data[$name] = urlCreate($srch['request_data']['items'], $name);
} else {
$srch = getMemId($name);
if(isset($srch['request_data']['items']['0'])) {
$data[$name] = urlCreate($srch['request_data']['items'], $name);
} else {
$data[$name] = 'http://worldoftanks.ru/uc/accounts/1/api/1.2/?source_token=Intellect_Soft-WoT_Mobile-unofficial_stats';
}
}
}
}
return $data;
}
function makeLinksFromIds($ids) {
foreach($ids as $id) {
$links[$id] = "http://worldoftanks.ru/uc/accounts/$id/api/1.2/?source_token=Intellect_Soft-WoT_Mobile-unofficial_stats";
}
return $links;
}
function processData($stats) {
foreach($stats as $id => $stat) {
$per_stat = (json_decode(trim($stat), true));
// REVIEW strong equal === is recommended anywhere when possible
if($per_stat['status'] == 'ok' && $per_stat['status_code'] == 'NO_ERROR') {
$data = $per_stat['data'];
$summary = $data['summary'];
$battlesCount = $summary['battles_count'];
$array[$id]['name'] = $data['name'];
if($battlesCount != 0) {
$array[$id]['win'] = round($summary['wins'] * 100 / $battlesCount, 0);
} else {
$array[$id]['win'] = '0';
}
$tank_lvl['battle_count'] = 0;
for($nl = 1; $nl <= 10; $nl++) {
$tank_lvl[$nl]['battle_count'] = 0;
}
foreach($data['vehicles'] as $tanks) {
$tank_lvl[$tanks['level']]['battle_count'] += $tanks['battle_count'];
$tank_lvl['battle_count'] += $tanks['battle_count'];
}
$mid = 0;
foreach($tank_lvl as $lvl => $tanks) {
$mid += $lvl * $tanks['battle_count'] / $tank_lvl['battle_count'];
}
$effect = array();
if($battlesCount != 0) {
$battles = $data['battles'];
$effect['dmg'] = $battles['damage_dealt'] / $battlesCount;
$effect['des'] = $battles['frags'] / $battlesCount;
$effect['det'] = $battles['spotted'] / $battlesCount;
$effect['cap'] = $battles['capture_points'] / $battlesCount;
$effect['def'] = $battles['dropped_capture_points'] / $battlesCount;
$array[$id]['eff'] = round(($effect['dmg'] * (10 / $mid) * (0.15 + $mid / 50) + $effect['des'] * (0.35 - $mid / 50)
* 1000 + $effect['det'] * 200 + $effect['cap'] * 150 + $effect['def'] * 150) / 10, 0) * 10;
} else {
$array[$id]['eff'] = 0;
}
} else {
$array[$id]['eff'] = 'X';
$array[$id]['win'] = 'X';
}
}
return $array;
}
?>