PageRenderTime 59ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/tanks/tanks.ofscience.net/libs/WOTLib.php

https://github.com/nanak/tanklab
PHP | 2537 lines | 100 code | 32 blank | 2405 comment | 15 complexity | 865890dc70e35d0b8288a532f578d09b MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Website: https://github.com/pcarrigg
  4. *
  5. * Licensed under The MIT License
  6. * Redistributions of files must retain the above copyright notice.
  7. *
  8. * @author Paul Carrigg <pcarrigg@gmail.com>
  9. * @version 1.00
  10. */
  11. include_once('simple_html_dom.php');
  12. if ($server == "eu")
  13. include_once('/home/tanks/tanks_db.eu.inc.php');
  14. else if ($server == "sea")
  15. include_once('/home/tanks/tanks_db.sea.inc.php');
  16. else
  17. include_once('/home/tanks/tanks_db.inc.php');
  18. $WOTServer = "http://dava.worldoftanks.com";
  19. $userStats = "/userstats/2/stats/slice/?server=us&platform=ios&hours_ago=1&hours_ago=168&hours_ago=336&account_id=";
  20. $region["na"] = true;
  21. if ($server =="eu")
  22. $site = "worldoftanks.eu";
  23. else if ($server =="sea")
  24. $site = "worldoftanks-sea.com";
  25. else
  26. $site = "worldoftanks.com";
  27. $cch = curl_init("http://$site/community/accounts/");
  28. curl_setopt($cch, CURLOPT_HEADER, 0);
  29. curl_setopt($cch, CURLOPT_RETURNTRANSFER, 1);
  30. $db = new PDO("mysql:host=$DBHost;dbname=$DBName",$DBUser,$DBPass, array(
  31. PDO::ATTR_PERSISTENT => true
  32. ));
  33. $statCache = array();
  34. $latestStats=false;
  35. $idCache = array();
  36. $tankList= false;
  37. function updateClanStats($clanName)
  38. {
  39. global $db,$site; // http://worldoftanks.com/community/clans/#wot&ct_search=rddt5&ct_order_by=-name
  40. $ch = curl_init("http://$site/community/clans/?type=table&offset=0&limit=25&order_by=-name&search=$clanName&echo=1&id=clans_index");//"http://$site/community/clans/#wot&ct_search=$clanName&ct_order_by=-name");
  41. curl_setopt($ch, CURLOPT_HEADER, 0);
  42. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json, text/javascript, */*; q=0.01","Referer: http://$site/community/clans/","X-Requested-With:XMLHttpRequest"));
  43. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  44. $output = curl_exec($ch);
  45. curl_close($ch);
  46. $results = json_decode($output);
  47. //echo "<pre>";
  48. // print_r($results);
  49. // echo "</pre>";
  50. if($results->request_data->items[0]->id) {
  51. //Store the results in the DB
  52. $q2 = $db->prepare("insert into clan_stats (clan_id,abbreviation,created_at,name,member_count,owner,motto,clan_emblem_url,clan_color,owner_id) values (:cid,:abb,:cat,:name,:memb,:own,:mot,:emb,:color,:oid)");
  53. $q2->execute(array(
  54. ":cid"=>$results->request_data->items[0]->id,
  55. ":abb"=>$results->request_data->items[0]->abbreviation,
  56. ":cat"=>$results->request_data->items[0]->created_at,
  57. ":name"=>$results->request_data->items[0]->name,
  58. ":memb"=>$results->request_data->items[0]->member_count,
  59. ":own"=>$results->request_data->items[0]->owner,
  60. ":mot"=>$results->request_data->items[0]->motto,
  61. ":emb"=>$results->request_data->items[0]->clan_emblem_url,
  62. ":color"=>$results->request_data->items[0]->clan_color,
  63. ":oid"=>$results->request_data->items[0]->owner_id
  64. ));
  65. // return $results->request_data->items[0]->id;
  66. }
  67. }
  68. function getClan($clanName)
  69. {
  70. global $db;
  71. $clanName =ereg_replace("[^-A-Za-z0-9_]", "", $clanName );
  72. $q = $db->prepare("select * from clans where clan_name=?");
  73. $q->execute(array($clanName));
  74. if ($q->rowCount()) {
  75. // Found a user!
  76. // echo "Found Clan";
  77. $result = $q->fetch();
  78. $q2 = $db->prepare("select * from clan_stats where clan_id=? order by updateTime DESC LIMIT 1");
  79. $q2->execute(array($result["clan_id"]));
  80. $stat = $q2->fetch();
  81. $result["stats"] = $stat;
  82. return $result;
  83. }
  84. return false;
  85. }
  86. function getClanIdFromName($clanName)
  87. {
  88. global $db,$site,$debug;
  89. $clanName =ereg_replace("[^-A-Za-z0-9_]", "", $clanName );
  90. if ($debug) {
  91. echo "Searching for ".strtoupper($clanName)."<BR>";
  92. }
  93. $q = $db->prepare("select * from clans where clan_name=?");
  94. $q->execute(array($clanName));
  95. if ($q->rowCount()) {
  96. // Found a user!
  97. $result = $q->fetch();
  98. return $result["clan_id"];
  99. }
  100. $ch = curl_init("http://$site/community/clans/?type=table&offset=0&limit=25&order_by=-name&search=$clanName&echo=1&id=clans_index");
  101. curl_setopt($ch, CURLOPT_HEADER, 0);
  102. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json, text/javascript, */*; q=0.01","Referer: http://$site/community/clans/","X-Requested-With:XMLHttpRequest"));
  103. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  104. $output = curl_exec($ch);
  105. curl_close($ch);
  106. $results = json_decode($output);
  107. $i = 0;
  108. foreach ($results->request_data->items as $item) {
  109. if ($debug) {
  110. echo "Found: -" .$results->request_data->items[$i]->abbreviation."-<BR>";
  111. }
  112. if($results->request_data->items[$i]->abbreviation == strtoupper($clanName)) {
  113. //Store the results in the DB
  114. if ($debug) {
  115. echo "Adding: -" .$results->request_data->items[$i]->abbreviation."-<BR>";
  116. }
  117. $q = $db->prepare("insert into clans (clan_name,clan_id) values (:name,:id)");
  118. $q->execute(array(":name"=>$results->request_data->items[$i]->abbreviation,":id"=>$results->request_data->items[$i]->id));
  119. $q2 = $db->prepare("insert into clan_stats (clan_id,abbreviation,created_at,name,member_count,owner,motto,clan_emblem_url,clan_color,owner_id) values (:cid,:abb,:cat,:name,:memb,:own,:mot,:emb,:color,:oid)");
  120. $q2->execute(array(
  121. ":cid"=>$results->request_data->items[$i]->id,
  122. ":abb"=>$results->request_data->items[$i]->abbreviation,
  123. ":cat"=>$results->request_data->items[$i]->created_at,
  124. ":name"=>$results->request_data->items[$i]->name,
  125. ":memb"=>$results->request_data->items[$i]->member_count,
  126. ":own"=>$results->request_data->items[$i]->owner,
  127. ":mot"=>$results->request_data->items[$i]->motto,
  128. ":emb"=>$results->request_data->items[$i]->clan_emblem_url,
  129. ":color"=>$results->request_data->items[$i]->clan_color,
  130. ":oid"=>$results->request_data->items[$i]->owner_id
  131. ));
  132. return $results->request_data->items[$i]->id;
  133. }
  134. $i++;
  135. }
  136. return false;
  137. }
  138. function loadClanMembers($clanName)
  139. {
  140. global $db,$site,$forceUpdate,$debug;
  141. $clan_id = getClanIdFromName($clanName);
  142. //echo "<BR>".$clan_id."<BR>";
  143. //$clan_id = $clan_id."-".strtoupper($clanName);
  144. $clanUsers = array();
  145. //Only Refresh Clan User List every 24hours
  146. $q = $db->prepare("select * from clans where clan_name=? and refreshDate >= DATE_SUB(NOW() ,INTERVAL 24 HOUR)");
  147. $q->execute(array($clanName));
  148. $cusers=array();
  149. if ($q->rowCount()) {
  150. // Found a user!
  151. $row = $q->fetch();
  152. $q2 = $db->prepare("select * from accounts where clan_id=?");
  153. $q2->execute(array($row["clan_id"]));
  154. while($r = $q2->fetch()) {
  155. array_push($clanUsers,array("name"=>$r["account_name"],"battles"=>$r["battles"],"wr"=>$r["wr"],"eff"=>$r["eff"]));
  156. }
  157. if (!$forceUpdate)
  158. return $clanUsers;
  159. }
  160. //http://worldoftanks.com/community/clans/1000003624/members/?type=table&_=1352839328274&offset=0&limit=100&order_by=role&search=&echo=1&id=clan_members_index
  161. $ch = curl_init("http://$site/community/clans/$clan_id/members/?type=table&offset=0&limit=100&order_by=-date&search=&echo=1&id=clan_members_index");
  162. curl_setopt($ch, CURLOPT_HEADER, 0);
  163. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json, text/javascript, */*; q=0.01","Referer: http://$site/community/clans/$clan_id-". strtoupper($clanName) ."/","X-Requested-With:XMLHttpRequest"));
  164. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  165. $output = curl_exec($ch);
  166. curl_close($ch);
  167. if ($debug){
  168. echo "<pre>";
  169. echo $output;
  170. echo "</pre>";
  171. }
  172. $results = json_decode($output);
  173. $clanUsers = array();
  174. if ($results->request_data->items[0]->account_id) {
  175. foreach ($results->request_data->items as $member)
  176. {
  177. $q = $db->prepare("select * from accounts where account_id=?");
  178. $q->execute(array($member->account_id));
  179. if ($q->rowCount()) {
  180. // Found a user! we do nothing!
  181. //$result = $q->fetch();
  182. $q = $db->prepare("update accounts set clan_id=:cid where account_id=:acid");
  183. $q->execute(array(":cid"=>$clan_id,":acid"=>$member->account_id));
  184. //return $result["clan_id"];
  185. // echo "Already Tracking: ". $result["account_name"]."<BR>";
  186. } else {
  187. $q2 = $db->prepare("insert into accounts (account_name,account_id,clan_id) values (:name,:id,:cid)");
  188. $q2->execute(array(":name"=>$member->name,":id"=>$member->account_id,":cid"=>$clan_id));
  189. //return $results->request_data->items[0]->id;
  190. //print_r($member);
  191. echo "Now Tracking: " . $member->name."<BR>";
  192. }
  193. $stats = loadStatistics($member->account_id,24,false,false);
  194. if ($stats)
  195. if ($stats["battles"]) {
  196. $wr = round((($stats["victories"] / $stats["battles"]) * 100),2);
  197. $battles = $stats["battles"];
  198. $efficiency = $stats["efficiency"];
  199. $days = $stats["clan_days"];
  200. } else{
  201. $wr = 0;
  202. $battles =0;
  203. $efficiency = 0;
  204. $days = 0;
  205. }
  206. else {
  207. $wr = 0;
  208. $battles = 0;
  209. $efficiency = 0;
  210. $days =0;
  211. }
  212. array_push($clanUsers,array("name"=>$member->name,"battles"=>$battles,"wr"=>$wr,"eff"=>$efficiency,"days"=>$days));
  213. }
  214. //Remove users if they arent in the clan anymore
  215. $q = $db->prepare("select * from accounts where clan_id=:cid");
  216. $q->execute(array(":cid"=>$clan_id));
  217. //print_r($clanUsers);
  218. //echo count($clanUsers);
  219. while($row = $q->fetch()) {
  220. $found = false;
  221. foreach($clanUsers as $cmem)
  222. {
  223. if ($row["account_name"] == $cmem["name"])
  224. $found = true;
  225. }
  226. if (!$found)
  227. {
  228. $q2 = $db->prepare("update accounts set clan_id=:cid where account_id=:acid");
  229. $q2->execute(array(":cid"=>"0",":acid"=>$row["account_id"]));
  230. echo "Removing ".$row["account_name"]."<BR>";
  231. }
  232. }
  233. updateClanStats($clanName);
  234. $q = $db->prepare("update clans set refreshDate=NOW() WHERE clan_id=:cid");
  235. $q->execute(array(":cid"=>$clan_id));
  236. return $clanUsers;
  237. }
  238. return array();
  239. }
  240. function getPlayerIdFromNick($nickname)
  241. {
  242. global $db,$site;
  243. //curl -H "Accept:application/json, text/javascript, */*; q=0.01" -H "Referer:http://worldoftanks.com/community/accounts/" -H "X-
  244. //Requested-With:XMLHttpRequest" -G //"http://worldoftanks.com/community/accounts/?type=table&_=1339526733658&offset=0&limit=25&order_by=name&search=test&echo=2&id=
  245. // accounts_index"
  246. $nickname =ereg_replace("[^A-Za-z0-9_]", "", $nickname );
  247. //Check if the user is already in the database
  248. $q = $db->prepare("select * from accounts where account_name=?");
  249. $q->execute(array($nickname));
  250. if ($q->rowCount()) {
  251. // Found a user!
  252. $result = $q->fetch();
  253. return $result["account_id"];
  254. }
  255. //We didnt find our user in the database so lets query the WOT server for it
  256. $ch = curl_init("http://$site/community/accounts/?type=table&&offset=0&limit=25&order_by=name&search=$nickname&echo=1&id=accounts_index");
  257. curl_setopt($ch, CURLOPT_HEADER, 0);
  258. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json, text/javascript, */*; q=0.01","Referer: http://$site/community/accounts/","X-Requested-With:XMLHttpRequest"));
  259. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  260. $output = curl_exec($ch);
  261. curl_close($ch);
  262. $results = json_decode($output);
  263. if($results->request_data->items[0]->id) {
  264. // check again to see if the users' in the DB
  265. $q2 = $db->prepare("select * from accounts where account_name=?");
  266. $q2->execute(array($nickname));
  267. if ($q2->rowCount()) {
  268. // Found a user!
  269. $result = $q2->fetch();
  270. return $result["account_id"];
  271. }
  272. //Store the results in the DB
  273. $q = $db->prepare("insert into accounts (account_name,account_id) values (:name,:id)");
  274. $q->execute(array(":name"=>$results->request_data->items[0]->name,":id"=>$results->request_data->items[0]->id));
  275. return $results->request_data->items[0]->id;
  276. }
  277. return false;
  278. }
  279. function getTopTankers($order=1,$dir=1)
  280. {
  281. global $db;
  282. if ($dir == 1)
  283. $direct = "DESC";
  284. else
  285. $direct = "ASC";
  286. if ($order== 1)
  287. $sort = "eff";
  288. else if ($order == 2)
  289. $sort = "wr";
  290. else if ($order == 3)
  291. $sort = "battles";
  292. //else if ($order== 4)
  293. // "eff where update"
  294. $q = $db->prepare("select * from accounts where battles > 500 order by $sort $direct limit 10");
  295. $q->execute();
  296. $topTanks = array();
  297. if ($q->rowCount()) {
  298. // Found a user!
  299. while ($result = $q->fetch())
  300. {
  301. array_push($topTanks,$result);
  302. }
  303. //return $result["account_name"];
  304. }
  305. return $topTanks;
  306. }
  307. function colorEff($eff)
  308. {
  309. if ($eff <=600)
  310. return ("<span class='red'>$eff</span>");
  311. else if ($eff <=900)
  312. return ("<span class='red'>$eff</span>");
  313. else if ($eff <=1200)
  314. return ("<span class='yellow'>$eff</span>");
  315. else if ($eff <=1500)
  316. return ("<span class='green'>$eff</span>");
  317. else
  318. return ("<span class='purple'>$eff</span>");
  319. }
  320. function colorWR($eff)
  321. {
  322. if ($eff < 47)
  323. return ("<span class='red'>$eff</span>");
  324. else if ($eff <= 50)
  325. return ("<span class='yellow'>$eff</span>");
  326. else if ($eff <= 55)
  327. return ("<span class='green'>$eff</span>");
  328. else
  329. return ("<span class='purple'>$eff</span>");
  330. }
  331. function getRecentlyAdded()
  332. {
  333. global $db;
  334. $q = $db->prepare("select * from accounts order by createDate DESC limit 10");
  335. $q->execute();
  336. $topTanks = array();
  337. if ($q->rowCount()) {
  338. // Found a user!
  339. while ($result = $q->fetch())
  340. {
  341. array_push($topTanks,$result);
  342. }
  343. //return $result["account_name"];
  344. }
  345. return $topTanks;
  346. }
  347. function getAccountName($account_id)
  348. {
  349. global $db;
  350. $q = $db->prepare("select * from accounts where account_id=?");
  351. $q->execute(array($account_id));
  352. if ($q->rowCount()) {
  353. // Found a user!
  354. $result = $q->fetch();
  355. return $result["account_name"];
  356. }
  357. return false;
  358. }
  359. function getTrackingDate($account_id)
  360. {
  361. global $db;
  362. $q = $db->prepare("select * from accounts where account_id=?");
  363. $q->execute(array($account_id));
  364. if ($q->rowCount()) {
  365. // Found a user!
  366. $result = $q->fetch();
  367. return $result["createDate"];
  368. }
  369. return false;
  370. }
  371. function getLastUpdate($account_id)
  372. {
  373. global $db;
  374. $q = $db->prepare("select * from accounts where account_id=?");
  375. $q->execute(array($account_id));
  376. if ($q->rowCount()) {
  377. // Found a user!
  378. $result = $q->fetch();
  379. return $result["lastUpdate"];
  380. }
  381. return "Unknown";
  382. }
  383. function checkForceUpdate($account_id)
  384. {
  385. global $db;
  386. $q = $db->prepare("select * from accounts where account_id=? and lastUpdate >= (NOW() - INTERVAL 30 MINUTE)");
  387. $q->execute(array($account_id));
  388. if ($q->rowCount()) {
  389. // Found a user!
  390. $result = $q->fetch();
  391. return false;
  392. }
  393. return true;
  394. }
  395. function refreshStats()
  396. {
  397. global $db;
  398. $q = $db->prepare("select * from accounts where lastUpdate <= DATE_SUB(NOW() ,INTERVAL 45 MINUTE)");
  399. $q->execute();
  400. while($result = $q->fetch()) {
  401. // return $result["account_name"];
  402. //echo "Processing - " . $result["account_name"]."\n";
  403. loadStatistics($result["account_id"],0,true);
  404. //sleep(rand(0,2));
  405. }
  406. $q = $db->prepare("select * from clans");
  407. $q->execute();
  408. while($result = $q->fetch()) {
  409. // return $result["account_name"];
  410. updateClanStats($result["clan_name"]);
  411. // loadStatistics($result["account_id"]);
  412. //sleep(rand(0,2));
  413. }
  414. return true;
  415. }
  416. function tracking()
  417. {
  418. global $db;
  419. $q = $db->prepare("select * from accounts");
  420. $q->execute();
  421. $accounts = $q->rowCount();
  422. $q = $db->prepare("select * from clans");
  423. $q->execute();
  424. $clans = $q->rowCount();
  425. return "Currently tracking $accounts tankers and $clans clans";
  426. }
  427. //Loads the statistics from the database or WOT Servers
  428. // Age specifies the age in hours of the statistics to be loaded
  429. // age = 0, checks WOT servers for an update (if no update has been run in the past 15 minutes)
  430. // age = 1, past hour
  431. // age = 24, past day
  432. // age = 168, past week
  433. // age = 720, pasth 30 days
  434. //
  435. // returns WOTStats array on success, false on error
  436. function loadStatistics($account_id,$age=0,$loadData=false,$loadDataReally=true)
  437. {
  438. global $db,$WOTServer,$userStats,$latestStats,$site,$forceUpdate,$cch;
  439. if ($loadDataReally) {
  440. if ($forceUpdate)
  441. $loadData = true;
  442. if (checkForceUpdate($account_id))
  443. $loadData = true;
  444. }
  445. if ($age) {
  446. $q = $db->prepare("select * from account_stats where account_id=:id and updateTime >= NOW() - INTERVAL :age HOUR order by updateTime ASC LIMIT 1");
  447. // $q = $db->prepare("select * from stats where account_id=:id and dstamp >= NOW() - INTERVAL :age HOUR order by dstamp ASC LIMIT 1");
  448. $q->execute(array(":id"=>$account_id,":age"=>$age));
  449. if ($q->rowCount()) {
  450. $res = $q->fetch();
  451. //Load the Tanks stats
  452. $q2= $db->prepare("select * from tank_stats where account_stats_update=:update and account_id=:acid");
  453. $q2->execute(array(":update"=>$res["updated"],":acid"=>$account_id));
  454. $res["tanks"] = array();
  455. while($t=$q2->fetch()){
  456. array_push($res["tanks"],$t);
  457. }
  458. if ($res["battles"]) {
  459. $res["victories_p"] = round(($res["victories"] / $res["battles"]) * 100,2);
  460. $res["defeats_p"] = round(($res["defeats"] / $res["battles"]) * 100,2);
  461. $res["draws"] = $res["battles"] - $res["victories"] - $res["defeats"];
  462. $res["draws_p"] =round(($res["draws"] / $res["battles"]) * 100,2);
  463. $res["survived_p"] = round(($res["survived"] / $res["battles"]) * 100,2);
  464. $res["destroyed_r"] = round(($res["destroyed"] / $res["battles"]),2);
  465. $res["damage_r"] = round(($res["damage"] / $res["battles"]),2);
  466. if (($res["battles"] - $res["survived"]))
  467. $res["kd_r"] = round(($res["destroyed"] / ($res["battles"] - $res["survived"])),2);
  468. $res["detected_r"] = round(($res["detected"] / $res["battles"]) ,2);
  469. $res["capture_r"] = round(($res["capture"] / $res["battles"]) ,2);
  470. $res["defense_r"] = round(($res["defense"] / $res["battles"]) ,2);
  471. $res["experience_r"] = round(($res["experience"] / $res["battles"]),2);
  472. }
  473. // return json_decode($res["data"]);
  474. //$statCache[""]
  475. $latestStats = $res;
  476. return $res;
  477. }
  478. return false;
  479. } else {
  480. $q = $db->prepare("select * from account_stats where account_id=:id and updateTime >= (NOW() - INTERVAL 30 MINUTE) order by dstamp DESC");
  481. $q->execute(array(":id"=>$account_id));
  482. if ($q->rowCount()){
  483. // We've already got fairly current data - dont overload WOT servers
  484. $res = $q->fetch();
  485. $q2= $db->prepare("select * from tank_stats where account_stats_update=:update and account_id=:acid");
  486. $q2->execute(array(":update"=>$res["update"],":acid"=>$account_id));
  487. $res["tanks"] = array();
  488. while($t=$q2->fetch()){
  489. array_push($res["tanks"],$t);
  490. }
  491. //return json_decode($res["data"]);
  492. if ($res["battles"]) {
  493. $res["victories_p"] = round(($res["victories"] / $res["battles"]) * 100,2);
  494. $res["defeats_p"] = round(($res["defeats"] / $res["battles"]) * 100,2);
  495. $res["draws"] = $res["battles"] - $res["victories"] - $res["defeats"];
  496. $res["draws_p"] =round(($res["draws"] / $res["battles"]) * 100,2);
  497. $res["survived_p"] = round(($res["survived"] / $res["battles"]) * 100,2);
  498. $res["destroyed_r"] = round(($res["destroyed"] / $res["battles"]),2);
  499. $res["damage_r"] = round(($res["damage"] / $res["battles"]),2);
  500. if (($res["battles"] - $res["survived"]))
  501. $res["kd_r"] = round(($res["destroyed"] / ($res["battles"] - $res["survived"])),2);
  502. $res["detected_r"] = round(($res["detected"] / $res["battles"]) ,2);
  503. $res["capture_r"] = round(($res["capture"] / $res["battles"]) ,2);
  504. $res["defense_r"] = round(($res["defense"] / $res["battles"]) ,2);
  505. $res["experience_r"] = round(($res["experience"] / $res["battles"]) ,2);
  506. }
  507. $latestStats = $res;
  508. return $res;
  509. //return processStats(json_decode($res["data"]));
  510. }
  511. $q = $db->prepare("select * from account_stats where account_id=:id order by updateTime DESC limit 1");
  512. $q->execute(array(":id"=>$account_id));
  513. $lastUpdate =false;
  514. if ($q->rowCount()){
  515. // We've already got fairly current data - dont overload WOT servers
  516. $res = $q->fetch();
  517. $q2= $db->prepare("select * from tank_stats where account_stats_update=:update and account_id=:acid");
  518. $q2->execute(array(":update"=>$res["updated"],":acid"=>$account_id));
  519. $res["tanks"] = array();
  520. while($t=$q2->fetch()){
  521. array_push($res["tanks"],$t);
  522. }
  523. $latestStats = $res;
  524. $lastUpdate= $res;
  525. //$lastUpdate = json_decode($res["data"]);
  526. } else {
  527. $loadData = true;
  528. }
  529. if ($lastUpdate["battles"]) {
  530. $lastUpdate["victories_p"] = round(($lastUpdate["victories"] / $lastUpdate["battles"]) * 100,2);
  531. $lastUpdate["defeats_p"] = round(($lastUpdate["defeats"] / $lastUpdate["battles"]) * 100,2);
  532. $lastUpdate["draws"] = $lastUpdate["battles"] - $lastUpdate["victories"] - $lastUpdate["defeats"];
  533. $lastUpdate["draws_p"] =round(($lastUpdate["draws"] / $lastUpdate["battles"]) * 100,2);
  534. $lastUpdate["survived_p"] = round(($lastUpdate["survived"] / $lastUpdate["battles"]) * 100,2);
  535. $lastUpdate["destroyed_r"] = round(($lastUpdate["destroyed"] / $lastUpdate["battles"]),2);
  536. $lastUpdate["damage_r"] = round(($lastUpdate["damage"] / $lastUpdate["battles"]),2);
  537. if (($lastUpdate["battles"] - $lastUpdate["survived"]))
  538. $lastUpdate["kd_r"] = round(($lastUpdate["destroyed"] / ($lastUpdate["battles"] - $lastUpdate["survived"])),2);
  539. $lastUpdate["detected_r"] = round(($lastUpdate["detected"] / $lastUpdate["battles"]) ,2);
  540. $lastUpdate["capture_r"] = round(($lastUpdate["capture"] / $lastUpdate["battles"]),2);
  541. $lastUpdate["defense_r"] = round(($lastUpdate["defense"] / $lastUpdate["battles"]),2);
  542. $lastUpdate["experience_r"] = round(($lastUpdate["experience"] / $lastUpdate["battles"]) ,2);
  543. }
  544. if (!$loadData)
  545. return $lastUpdate;
  546. $account_name = getAccountName($account_id);
  547. $wotStats = array();
  548. $wotStats["web_check"] = true;
  549. // echo "Going to the web..";
  550. //$ch = curl_init("http://$site/community/accounts/$account_id-$account_name/");
  551. //curl_setopt($ch, CURLOPT_HEADER, 0);
  552. //curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  553. curl_setopt($cch,CURLOPT_URL,"http://$site/community/accounts/$account_id-$account_name/");
  554. $output = curl_exec($cch);
  555. $html = str_get_html($output);
  556. if ($html) {
  557. foreach( $html->find('div.b-data-create') as $b ) {
  558. $stamp = $b->find('span',0);
  559. $wotStats["registered"] = $stamp->getAttribute('data-timestamp');
  560. }
  561. foreach( $html->find('div.b-data-date') as $b) {
  562. $stamp = $b->find('span',0);
  563. $wotStats["updated"] = $stamp->getAttribute('data-timestamp');
  564. }
  565. date_default_timezone_set('America/Los_Angeles');
  566. //echo $lastUpdate->updated. " ~ " . $wotStats["updated"]."<BR>";
  567. //if($lastUpdate)
  568. $q = $db->prepare("update accounts set lastUpdate=NOW() where account_id=:acid");
  569. $q->execute(array(":acid"=>$account_id));
  570. if ($lastUpdate["updated"] != $wotStats["updated"])
  571. // if(1)
  572. {
  573. foreach ($html->find('div.b-clan-list') as $b)
  574. {
  575. $wotStats['clan_url'] = $b->find('a',0)->getAttribute('href');
  576. $wotStats['clan_img'] = $b->find('img',0)->getAttribute('src');
  577. $wotStats['clan_tag'] = $b->find('span.tag',0)->plaintext;
  578. $wotStats['clan_name'] = $b->find('span.name',0)->plaintext;
  579. $wotStats['clan_motto'] = $b->find('p.motto',0)->plaintext;
  580. $wotStats['clan_days'] = $b->find('td.first',0)->find('span',0)->plaintext;
  581. $wotStats['clan_enrolled'] = $b->find('td.first',0)->next_sibling()->find('span',0)->getAttribute('data-timestamp');
  582. }
  583. foreach( $html->find('table.t-table-dotted') as $t )
  584. {
  585. $table = $t->find('th',0)->plaintext;
  586. if ($table == "Overall Results")
  587. {
  588. $wotStats['battles'] = fixStat($t->find('td.td-number-nowidth',0)->plaintext);
  589. $wotStats['victories'] = fixStat($t->find('td.td-number-nowidth',1)->plaintext);
  590. $wotStats['defeats'] = fixStat($t->find('td.td-number-nowidth',2)->plaintext);
  591. $wotStats['survived'] = fixStat($t->find('td.td-number-nowidth',3)->plaintext);
  592. }
  593. if ($table == "Battle Performance")
  594. {
  595. $wotStats['destroyed'] = fixStat($t->find('td.td-number-nowidth',0)->plaintext);
  596. $wotStats['detected'] = fixStat($t->find('td.td-number-nowidth',1)->plaintext);
  597. $wotStats['hitratio'] = fixStat($t->find('td.td-number-nowidth',2)->plaintext);
  598. $wotStats['damage'] = fixStat($t->find('td.td-number-nowidth',3)->plaintext);
  599. $wotStats['capture'] = fixStat($t->find('td.td-number-nowidth',4)->plaintext);
  600. $wotStats['defense'] = fixStat($t->find('td.td-number-nowidth',5)->plaintext);
  601. }
  602. if ($table == "Experience")
  603. {
  604. $wotStats['experience'] = fixStat($t->find('td.td-number-nowidth',0)->plaintext);
  605. $wotStats['avg_exp'] = fixStat($t->find('td.td-number-nowidth',1)->plaintext);
  606. $wotStats['max_exp'] = fixStat($t->find('td.td-number-nowidth',2)->plaintext);
  607. }
  608. if ($table == "Rating")
  609. {
  610. $wotStats['global_rating_val'] = fixStat($t->find('td.value',0)->plaintext);
  611. $wotStats['global_rating_place'] =fixStat($t->find('td.value',1)->plaintext);
  612. $wotStats['vb_val'] = fixStat($t->find('td.value',2)->plaintext);
  613. $wotStats['vb_place'] =fixStat($t->find('td.value',3)->plaintext);
  614. $wotStats['avg_exp_val'] = fixStat($t->find('td.value',4)->plaintext);
  615. $wotStats['avg_exp_place'] =fixStat($t->find('td.value',5)->plaintext);
  616. $wotStats['victories_val'] = fixStat($t->find('td.value',6)->plaintext);
  617. $wotStats['victories_place'] =fixStat($t->find('td.value',7)->plaintext);
  618. $wotStats['battles_val'] = fixStat($t->find('td.value',8)->plaintext);
  619. $wotStats['battles_place'] =fixStat($t->find('td.value',9)->plaintext);
  620. $wotStats['capture_val'] = fixStat($t->find('td.value',10)->plaintext);
  621. $wotStats['capture_place'] =fixStat($t->find('td.value',11)->plaintext);
  622. $wotStats['defense_val'] = fixStat($t->find('td.value',12)->plaintext);
  623. $wotStats['defense_place'] =fixStat($t->find('td.value',13)->plaintext);
  624. $wotStats['frag_val'] = fixStat($t->find('td.value',14)->plaintext);
  625. $wotStats['frag_place'] =fixStat($t->find('td.value',15)->plaintext);
  626. $wotStats['detect_val'] = fixStat($t->find('td.value',16)->plaintext);
  627. $wotStats['detect_place'] =fixStat($t->find('td.value',17)->plaintext);
  628. $wotStats['experience_val'] = fixStat($t->find('td.value',18)->plaintext);
  629. $wotStats['experience_place'] =fixStat($t->find('td.value',19)->plaintext);
  630. }
  631. }
  632. foreach( $html->find('table.t-statistic') as $t )
  633. {
  634. $table = $t->find('th',0)->plaintext;
  635. if ($table == "Rating")
  636. {
  637. $wotStats['global_rating_val'] = fixStat($t->find('td.value',1)->plaintext);
  638. $wotStats['global_rating_place'] =fixStat($t->find('td.value',2)->plaintext);
  639. $wotStats['vb_val'] = fixStat($t->find('td.value',4)->plaintext);
  640. $wotStats['vb_place'] =fixStat($t->find('td.value',5)->plaintext);
  641. $wotStats['avg_exp_val'] = fixStat($t->find('td.value',7)->plaintext);
  642. $wotStats['avg_exp_place'] =fixStat($t->find('td.value',8)->plaintext);
  643. $wotStats['victories_val'] = fixStat($t->find('td.value',10)->plaintext);
  644. $wotStats['victories_place'] =fixStat($t->find('td.value',11)->plaintext);
  645. $wotStats['battles_val'] = fixStat($t->find('td.value',13)->plaintext);
  646. $wotStats['battles_place'] =fixStat($t->find('td.value',14)->plaintext);
  647. $wotStats['capture_val'] = fixStat($t->find('td.value',16)->plaintext);
  648. $wotStats['capture_place'] =fixStat($t->find('td.value',17)->plaintext);
  649. $wotStats['damage_val'] = fixStat($t->find('td.value',19)->plaintext);
  650. $wotStats['damage_place'] =fixStat($t->find('td.value',20)->plaintext);
  651. $wotStats['defense_val'] = fixStat($t->find('td.value',22)->plaintext);
  652. $wotStats['defense_place'] =fixStat($t->find('td.value',23)->plaintext);
  653. $wotStats['frag_val'] = fixStat($t->find('td.value',25)->plaintext);
  654. $wotStats['frag_place'] =fixStat($t->find('td.value',26)->plaintext);
  655. $wotStats['detect_val'] = fixStat($t->find('td.value',28)->plaintext);
  656. $wotStats['detect_place'] =fixStat($t->find('td.value',29)->plaintext);
  657. $wotStats['experience_val'] = fixStat($t->find('td.value',31)->plaintext);
  658. $wotStats['experience_place'] =fixStat($t->find('td.value',32)->plaintext);
  659. }
  660. if (trim($table) == "Vehicles")
  661. {
  662. $wotStats['tanks'] = array();
  663. foreach($t->find('tr') as $r)
  664. {
  665. $check = $r->find('td.td-armory-icon');
  666. if ($check) {
  667. $tank = array();
  668. $tank["name"] = $r->find('td.value a.b-gray-link',0)->plaintext;
  669. $tank["url"] = $r->find('a.b-gray-link',1)->getAttribute('href');
  670. $tank["image"] = $r->find('img.png',0)->getAttribute('src');
  671. $tank["level"] = $r->find('span.level',0)->find('a',0)->plaintext;
  672. $tank["battles"] = fixStat($r->find('td.right',0)->plaintext);
  673. $tank["victories"] = fixStat($r->find('td.right',1)->plaintext);
  674. $tank["updated"]=$wotStats["updated"];
  675. array_push($wotStats["tanks"],$tank);
  676. $t = $db->prepare("insert into tank_stats (account_id,name,url,image,level,battles,victories,account_stats_update) values (:id,:name,:url,:image,:level,:battles,:victories,:updated)");
  677. $t->execute(array(
  678. ":id"=>$account_id,
  679. ":name"=>$tank["name"],
  680. ":url"=>$tank["url"],
  681. ":image"=>$tank["image"],
  682. ":level"=>romanToInt($tank["level"]),
  683. ":battles"=>$tank["battles"],
  684. ":victories"=>$tank["victories"],
  685. ":updated"=>$wotStats["updated"]
  686. ));
  687. }
  688. }
  689. }
  690. }
  691. $html->clear();
  692. unset($html);
  693. $wotStats["efficiency"] = calcEfficiencyArray($wotStats);
  694. if ($wotStats["battles"]) {
  695. //Store Json encoded data
  696. //$q = $db->prepare("insert into stats (account_id,data,dstamp) values (:id,:data,NOW())");
  697. //$q->execute(array(":id"=>$account_id,":data"=>json_encode($wotStats)));
  698. //Store split data into new DB TAble
  699. $q2 = $db->prepare("
  700. insert into account_stats (
  701. defeats,
  702. clan_tag,
  703. registered,
  704. updated,
  705. account_id,
  706. clan_url,
  707. clan_img,
  708. clan_name,
  709. clan_motto,
  710. clan_days,
  711. clan_enrolled,
  712. battles,
  713. victories,
  714. survived,
  715. destroyed,
  716. detected,
  717. hitratio,
  718. damage,
  719. capture,
  720. defense,
  721. experience,
  722. avg_exp,
  723. max_exp,
  724. global_rating_val,
  725. global_rating_place,
  726. vb_val,
  727. vb_place,
  728. avg_exp_val,
  729. avg_exp_place,
  730. victories_val,
  731. victories_place,
  732. battles_val,
  733. battles_place,
  734. capture_val,
  735. capture_place,
  736. defense_val,
  737. defense_place,
  738. frag_val,
  739. frag_place,
  740. detect_val,
  741. detect_place,
  742. experience_val,
  743. experience_place,
  744. efficiency) VALUES (
  745. :defeats,
  746. :clan_tag,
  747. :registered,
  748. :updated,
  749. :account_id,
  750. :clan_url,
  751. :clan_img,
  752. :clan_name,
  753. :clan_motto,
  754. :clan_days,
  755. :clan_enrolled,
  756. :battles,
  757. :victories,
  758. :survived,
  759. :destroyed,
  760. :detected,
  761. :hitratio,
  762. :damage,
  763. :capture,
  764. :defense,
  765. :experience,
  766. :avg_exp,
  767. :max_exp,
  768. :global_rating_val,
  769. :global_rating_place,
  770. :vb_val,
  771. :vb_place,
  772. :avg_exp_val,
  773. :avg_exp_place,
  774. :victories_val,
  775. :victories_place,
  776. :battles_val,
  777. :battles_place,
  778. :capture_val,
  779. :capture_place,
  780. :defense_val,
  781. :defense_place,
  782. :frag_val,
  783. :frag_place,
  784. :detect_val,
  785. :detect_place,
  786. :experience_val,
  787. :experience_place,
  788. :efficiency
  789. )
  790. ");
  791. $wotQuery = array();
  792. $f = explode(",","defeats,clan_tag,registered,
  793. updated,
  794. account_id,
  795. clan_url,
  796. clan_img,
  797. clan_name,
  798. clan_motto,
  799. clan_days,
  800. clan_enrolled,
  801. battles,
  802. victories,
  803. survived,
  804. destroyed,
  805. detected,
  806. hitratio,
  807. damage,
  808. capture,
  809. defense,
  810. experience,
  811. avg_exp,
  812. max_exp,
  813. global_rating_val,
  814. global_rating_place,
  815. vb_val,
  816. vb_place,
  817. avg_exp_val,
  818. avg_exp_place,
  819. victories_val,
  820. victories_place,
  821. battles_val,
  822. battles_place,
  823. capture_val,
  824. capture_place,
  825. defense_val,
  826. defense_place,
  827. frag_val,
  828. frag_place,
  829. detect_val,
  830. detect_place,
  831. experience_val,
  832. experience_place,
  833. efficiency");
  834. foreach ($wotStats as $key=>$value)
  835. {
  836. foreach ($f as $b) {
  837. $wotQuery[trim($b)] = "";
  838. if ($key == trim($b))
  839. $wotQuery[":".$key] = $value;
  840. }
  841. }
  842. $wotQuery["account_id"] = $account_id;
  843. $q2->execute($wotQuery);
  844. $q3 =$db->prepare("update accounts set battles=:battles,wr=:wr,eff=:eff,lastUpdate=NOW() where account_id=:acid");
  845. if ($wotStats["battles"])
  846. $wr = round((($wotStats["victories"] / $wotStats["battles"]) * 100),2);
  847. else
  848. $wr = 0; $q3->execute(array(":acid"=>$account_id,":battles"=>$wotStats["battles"],":wr"=>$wr,":eff"=>$wotStats["efficiency"]));
  849. }
  850. if ($wotStats["battles"]) {
  851. $wotStats["victories_p"] = round(($wotStats["victories"] / $wotStats["battles"]) * 100,2);
  852. $wotStats["defeats_p"] = round(($wotStats["defeats"] / $wotStats["battles"]) * 100,2);
  853. $wotStats["draws"] = $wotStats["battles"] - $wotStats["victories"] - $wotStats["defeats"];
  854. $wotStats["draws_p"] =round(($wotStats["draws"] / $wotStats["battles"]) * 100,2);
  855. $wotStats["survived_p"] = round(($wotStats["survived"] / $wotStats["battles"]) * 100,2);
  856. $wotStats["destroyed_r"] = round(($wotStats["destroyed"] / $wotStats["battles"]),2);
  857. $wotStats["damage_r"] = round(($wotStats["damage"] / $wotStats["battles"]),2);
  858. if ($wotStats["battles"] - $wotStats["survived"])
  859. $wotStats["kd_r"] = round(($wotStats["destroyed"] / ($wotStats["battles"] - $wotStats["survived"])),2);
  860. $wotStats["detected_r"] = round(($wotStats["detected"] / $wotStats["battles"]) ,2);
  861. $wotStats["capture_r"] = round(($wotStats["capture"] / $wotStats["battles"]) ,2);
  862. $wotStats["defense_r"] = round(($wotStats["defense"] / $wotStats["battles"]) ,2);
  863. $wotStats["experience_"] = round(($wotStats["experience"] / $wotStats["battles"]),2);
  864. }
  865. $latestStats = $wotStats;
  866. return $wotStats;//json_decode(json_encode($wotStats));//processStats($wotStats);
  867. } else {
  868. $html->clear();
  869. unset($html);
  870. $latestStats = $lastUpdate;
  871. return $lastUpdate;
  872. }
  873. } else { //!html - some kinda read failure
  874. echo "Error Loading data for: " .getAccountName($account_id) ."\n";
  875. echo "CURL Output: $output\n";
  876. }
  877. }
  878. }
  879. function fixStat($string)
  880. {
  881. $p = explode(" ",trim($string));
  882. return ereg_replace("[^0-9]", "", $p[0]);
  883. }
  884. function calcPeriodEfficiencyArray($stats,$periodStats)
  885. {
  886. return calcPeriodEfficiency($stats,$periodStats);
  887. }
  888. // Calculates Effciencey over a period
  889. // TotalStats being the end, and PeriodStats being the beginning of that period
  890. function calcPeriodEfficiency($stats,$periodStats)
  891. {
  892. if ($stats["battles"]) {
  893. if ($periodStats["battles"]) {
  894. $i = 0;
  895. $tid = 0;
  896. $level = 0;
  897. foreach ($periodStats["tanks"] as $tank)
  898. {
  899. //echo $tank["level"] ."<BR>";
  900. if (!is_numeric($tank["level"]))
  901. $l = romanToInt($tank["level"]);
  902. else
  903. $l = $tank["level"];
  904. if ($l > 0) {
  905. if($stats["tanks"][$i]["battles"] - $tank["battles"]) {
  906. $level +=$l*($stats["tanks"][$tid]["battles"] - $tank["battles"]);
  907. $i++;
  908. }
  909. }
  910. $tid++;
  911. }
  912. // echo $level ." - $i<BR>";
  913. if ($i) {
  914. $pbattles = ($stats["battles"] - $periodStats["battles"]);
  915. //echo "battles - ". $pbattles."<BR>";
  916. if ($pbattles) {
  917. $avg_level = $level / $pbattles;
  918. $avg_dmg = ($stats["damage"] - $periodStats["damage"]) / $pbattles;
  919. $avg_frags = ($stats["destroyed"] - $periodStats["destroyed"]) / $pbattles;
  920. $avg_spots = ($stats["detected"] - $periodStats["detected"]) / $pbattles;
  921. $avg_cap = ($stats["capture"] - $periodStats["capture"]) / $pbattles;
  922. $avg_def = ($stats["defense"] - $periodStats["defense"]) / $pbattles;
  923. return (round(
  924. ($avg_dmg * (10 / $avg_level)*(.15 + $avg_level/50)) +
  925. ($avg_frags *(.35-$avg_level/50)*1000) +
  926. ($avg_spots * 200) +
  927. ($avg_cap *150) +
  928. ($avg_def *150)
  929. ));
  930. }
  931. }
  932. }
  933. }
  934. return 0;
  935. }
  936. function calcEfficiencyArray($stats)
  937. {
  938. //echo $stats["battles"];
  939. return calcEfficiency($stats);
  940. }
  941. function calcEfficiency($stats)
  942. {
  943. // echo $stats->battles ."\n";
  944. if ($stats)
  945. if ($stats["battles"]) {
  946. $i = 0;
  947. $level = 0;
  948. foreach ($stats["tanks"] as $tank)
  949. {
  950. $l = romanToInt($tank["level"]);
  951. if ($l > 0) {
  952. $level +=$l * $tank["battles"];// romanToInt($tank->level);// * ($tank->battles);
  953. $i++;
  954. }
  955. }
  956. if ($i) {
  957. $avg_level = $level / $stats["battles"];
  958. $avg_dmg = $stats["damage"] / $stats["battles"];
  959. $avg_frags = $stats["destroyed"] / $stats["battles"];
  960. $avg_spots = $stats["detected"] / $stats["battles"];
  961. $avg_cap = $stats["capture"] / $stats["battles"];
  962. $avg_def = $stats["defense"] / $stats["battles"];
  963. //echo "$avg_frags<BR>$avg_dmg<BR>$avg_spots<BR>$avg_def<BR>$avg_cap<BR>$avg_level<BR>";
  964. return (round(
  965. ($avg_dmg * (10 / $avg_level)*(.15 + $avg_level/50)) +
  966. ($avg_frags *(.35-$avg_level/50)*1000) +
  967. ($avg_spots * 200) +
  968. ($avg_cap *150) +
  969. ($avg_def *150)
  970. ));
  971. }
  972. }
  973. return 0;
  974. }
  975. function romanToInt($roman)
  976. {
  977. $romans = array(
  978. 'M' => 1000,
  979. 'CM' => 900,
  980. 'D' => 500,
  981. 'CD' => 400,
  982. 'C' => 100,
  983. 'XC' => 90,
  984. 'L' => 50,
  985. 'XL' => 40,
  986. 'X' => 10,
  987. 'IX' => 9,
  988. 'V' => 5,
  989. 'IV' => 4,
  990. 'I' => 1,
  991. );
  992. //$roman = 'MMMCMXCIX';
  993. $result = 0;
  994. foreach ($romans as $key => $value) {
  995. while (strpos($roman, $key) === 0) {
  996. $result += $value;
  997. $roman = substr($roman, strlen($key));
  998. }
  999. }
  1000. return $result;
  1001. }
  1002. function loadGeneralStats($account_id)
  1003. {
  1004. global $site;
  1005. $stats = loadStatistics($account_id);
  1006. if ($stats["battles"]) {
  1007. $stats60 = loadStatistics($account_id,1);
  1008. $stats24 = loadStatistics($account_id,24);
  1009. $stats7 = loadStatistics($account_id,168);
  1010. $stats30 = loadStatistics($account_id,768);
  1011. date_default_timezone_set('America/Los_Angeles');
  1012. $rs["currentStats"] = $stats;
  1013. $rs["registered"] = date("Y-m-d g:i A",$stats["registered"]) . " PST";
  1014. $rs["lastUpdate"] = date("Y-m-d g:i A",$stats["updated"]) . " PST";
  1015. $rs["name"] = getAccountName($account_id);
  1016. $rs["eff"] = calcEfficiency($stats);
  1017. $rs["eff_60"] = calcPeriodEfficiency($stats,$stats60);
  1018. $rs["eff_24"] = calcPeriodEfficiency($stats,$stats24);
  1019. $rs["eff_7"] = calcPeriodEfficiency($stats,$stats7);
  1020. $rs["eff_30"] = calcPeriodEfficiency($stats,$stats30);
  1021. if ($stats["clan_name"]) {
  1022. $rs["clan_tag"] = "Member of <a href=/?clanName=" .trim($stats["clan_tag"],"[]") .">".$stats["clan_tag"]."</a> for ". $stats["clan_days"] ." days.";
  1023. $rs["clan_img"] = "http://$site/" . $stats["clan_img"];
  1024. }
  1025. //Totals
  1026. $rs["battles"] = $stats["battles"];//->battles_count;
  1027. if ($rs["battles"]) {
  1028. $rs["victory"] = round( (( $stats["victories"]) / $rs["battles"]) *100,2) ;
  1029. $rs["kd"] = round($stats["destroyed"] / ($rs["battles"] - $stats["survived"]) ,2);
  1030. $rs["dmg"] = round($stats["damage"] / $rs["battles"],2);
  1031. $rs["exp"] = $stats["avg_exp"];
  1032. } else {
  1033. $rs["victory"] = 0;
  1034. $rs["kd"] = 0;
  1035. $rs["dmg"] = 0;
  1036. $rs["exp"] = 0;
  1037. }
  1038. // Past 30days
  1039. if ($stats30["battles"] && $stats30["battles"] < $rs["battles"]) {
  1040. $rs["battles_30"] = $rs["battles"] - $stats30["battles"];
  1041. $rs["victory_30"] = round( (($stats["victories"]- $stats30["victories"]) /$rs["battles_30"]) * 100,2) ;
  1042. if (($rs["battles_30"] - ($stats["survived"] - $stats30["survived"])))
  1043. $rs["kd_30"] = round( ($stats["destroyed"] - $stats30["destroyed"]) / ($rs["battles_30"] - ($stats["survived"] - $stats30["survived"])),2);
  1044. else
  1045. $rs["kd_30"] = round( ($stats["destroyed"] - $stats30["destroyed"]),2);
  1046. // v
  1047. // $rs["kd_30"] = round( ($stats->destroyed - $stats30->destroyed) / (($stats->survivied - $stats30->survived)),2);
  1048. $rs["dmg_30"] = round(($stats["damage"] - $stats30["damage"]) / $rs["battles_30"],2);
  1049. $rs["exp_30"] = round(( $stats["experience"] - $stats30["experience"] ) / $rs["battles_30"],2);
  1050. } else {
  1051. $rs["battles_30"] = 0;
  1052. $rs["victory_30"] = 0;
  1053. $rs["kd_30"] = 0;
  1054. $rs["dmg_30"] = 0;
  1055. $rs["exp_30"] = 0;
  1056. }
  1057. // Past 7days
  1058. if ($stats7["battles"] && $stats7["battles"] < $rs["battles"]) {
  1059. $rs["battles_7"] = $rs["battles"] - $stats7["battles"];
  1060. $rs["victory_7"] = round( (($stats["victories"]- $stats7["victories"]) /$rs["battles_7"]) * 100,2) ;
  1061. if (($rs["battles_7"] - ($stats["survived"] - $stats7["survived"])))
  1062. $rs["kd_7"] = round( ($stats["destroyed"] - $stats7["destroyed"]) / ($rs["battles_7"] - ($stats["survived"] - $stats7["survived"])),2);
  1063. else
  1064. $rs["kd_7"] = round( ($stats["destroyed"] - $stats7["destroyed"]),2);
  1065. //$rs["kd_7"] = round( ($stats->destroyed - $stats7->destroyed) / (($stats->survivied - $stats7->survived)),2);
  1066. $rs["dmg_7"] = round(($stats["damage"] - $stats7["damage"]) / $rs["battles_7"],2);
  1067. $rs["exp_7"] = round(( $stats["experience"] - $stats7["experience"] ) / $rs["battles_7"],2);
  1068. } else {
  1069. $rs["battles_7"] = 0;
  1070. $rs["victory_7"] = 0;
  1071. $rs["kd_7"] = 0;
  1072. $rs["dmg_7"] = 0;
  1073. $rs["exp_7"] = 0;
  1074. }
  1075. if ($stats24["battles"] && $stats24["battles"] < $rs["battles"]) {
  1076. $rs["battles_24"] = $rs["battles"] - $stats24["battles"];
  1077. $rs["victory_24"] = round( (($stats["victories"] - $stats24["victories"]) /$rs["battles_24"]) * 100,2) ;
  1078. if (($rs["battles_24"] - ($stats["survived"] - $stats24["survived"])))
  1079. $rs["kd_24"] = round( ($stats["destroyed"] - $stats24["destroyed"]) / ($rs["battles_24"] - ($stats["survived"] - $stats24["survived"])),2);
  1080. else
  1081. $rs["kd_24"] = round( ($stats["destroyed"] - $stats24["destroyed"]),2);
  1082. //$rs["kd_24"] = round( ($stats["destroyed - $stats24["destroyed) / (($stats["survivied - $stats24["survived)),2);
  1083. $rs["dmg_24"] = round(($stats["damage"] - $stats24["damage"]) / $rs["battles_24"],2);
  1084. $rs["exp_24"] = round( ($stats["experience"] - $stats24["experience"] ) / $rs["battles_24"],2);
  1085. } else {
  1086. $rs["battles_24"] = 0;
  1087. $rs["victory_24"] = 0;
  1088. $rs["kd_24"] = 0;
  1089. $rs["dmg_24"] = 0;
  1090. $rs["exp_24"] = 0;
  1091. }
  1092. if ($stats60["battles"] && $stats60["battles"] < $rs["battles"]) {
  1093. $rs["battles_60"] = $rs["battles"] - $stats60["battles"];
  1094. $rs["victory_60"] = round( (($stats["victories"] - $stats60["victories"]) / $rs["battles_60"]) * 100,2) ;
  1095. if (($rs["battles_60"] - ($stats["survived"] - $stats60["survived"])))
  1096. $rs["kd_60"] = round( ($stats["destroyed"] - $stats60["destroyed"]) / ($rs["battles_60"] - ($stats["survived"] - $stats60["survived"])),2);
  1097. else
  1098. $rs["kd_60"] = round( ($stats["destroyed"] - $stats60["destroyed"]),2);
  1099. $rs["dmg_60"] = round(($stats["damage"] - $stats60["damage"]) / $rs["battles_60"],2);
  1100. $rs["exp_60"] = round(( $stats["experience"] - $stats60["experience"] ) / $rs["battles_60"],2);
  1101. } else {
  1102. $rs["battles_60"] = 0;
  1103. $rs["victory_60"] = 0;
  1104. $rs["kd_60"] = 0;
  1105. $rs["dmg_60"] = 0;
  1106. $rs["exp_60"] = 0;
  1107. }
  1108. return $rs;
  1109. }
  1110. return false;
  1111. }
  1112. function topTanks($stat)
  1113. {
  1114. $topTanks = array();
  1115. foreach ($stat['tanks'] as $tank) {
  1116. $topTanks[$tank["battles"]][] = $tank;
  1117. }
  1118. asort($topTanks);
  1119. $num = count($topTanks);
  1120. if ($num > 20)
  1121. $num = 20;
  1122. $i = 0;
  1123. $tops = array();
  1124. foreach ($topTanks as $top) {
  1125. foreach ($top as $t){
  1126. if ($i < $num) {
  1127. //$i++;
  1128. $f = 0;
  1129. foreach ($tops as $tt)
  1130. if ($tt["name"] == $t["name"])
  1131. $f=1;
  1132. if (!$f) {
  1133. array_push($tops,$t);
  1134. $i++;
  1135. }
  1136. }
  1137. }
  1138. }
  1139. return($tops);
  1140. }
  1141. // Grabs stats and adjusts them for a specified period
  1142. // Interval can be
  1143. // d - days
  1144. // h - hours
  1145. // w - weeks
  1146. // m - months
  1147. //
  1148. // getStatPeriod(name, 0,1,d)
  1149. // loads stats for name between 0 days and 1 day ago
  1150. function getStatPeriod($account_id, $period_start,$period_end,$interval="d")
  1151. {
  1152. global $db;
  1153. if ($interval == "d")
  1154. $intval = " DAY ";
  1155. else if ($interval == "h")
  1156. $intval = " HOUR ";
  1157. else if ($interval == "w")
  1158. $intval = " WEEK ";
  1159. else if ($interval == "m")
  1160. $intval = " MONTH ";
  1161. else
  1162. $intval = " DAY ";
  1163. //$account_id = getPlayerIdFromNick($_GET["name"]);
  1164. $statsA = array();
  1165. $statsB = array();
  1166. $statsC = array();
  1167. // Try to load stats for given period start
  1168. if ($interval == "d") { // 1001775453
  1169. $q = $db->prepare("select * from account_stats where account_id=:aid and updateTime <= DATE_SUB(CONCAT(CURDATE(),' 23:59:59'), INTERVAL :t $intval) and updateTime >= DATE_SUB(CONCAT(CURDATE(),' 23:59:59'), INTERVAL :tb $intval) order by updateTime DESC limit 1");
  1170. $q->execute(array(":aid"=>$account_id,":t"=>$period_start,":tb"=>$period_start+1));
  1171. } else {
  1172. $q = $db->prepare("select * from account_stats where account_id=:aid and updateTime <= DATE_SUB(NOW(),INTERVAL :t $intval) order by updateTime DESC limit 1");
  1173. $q->execute(array(":aid"=>$account_id,":t"=>$period_start));
  1174. }
  1175. if ($q->rowCount()) {
  1176. $statsA = $q->fetch();
  1177. $q2= $db->prepare("select * from tank_stats where account_stats_update=:update and account_id=:acid");
  1178. $q2->execute(array(":update"=>$statsA["updated"],":acid"=>$account_id));
  1179. $statsA["tanks"] = array();
  1180. while($t=$q2->fetch()){
  1181. array_push($statsA["tanks"],$t);
  1182. }
  1183. } else {
  1184. //echo "StatsA<BR>";
  1185. $statsA = false;
  1186. }
  1187. // Try to load stats for period end
  1188. //
  1189. if ($period_start != $period_end) {
  1190. if ($interval =="d") {
  1191. $q = $db->prepare("select * from account_stats where account_id=:aid and updateTime <= DATE_SUB(CONCAT(CURDATE(),' 23:59:59'), INTERVAL :t $intval) and updateTime >= DATE_SUB(CONCAT(CURDATE(),' 23:59:59'), INTERVAL :tb $intval) order by updateTime DESC limit 1");
  1192. $q->execute(array(":aid"=>$account_id,":t"=>$period_end,":tb"=>$period_end+1));
  1193. } else {
  1194. $q = $db->prepare("select * from account_stats where account_id=:aid and updateTime <= DATE_SUB(NOW(), INTERVAL :t $intval) order by updateTime DESC limit 1");
  1195. $q->execute(array(":aid"=>$account_id,":t"=>$period_end));
  1196. }
  1197. if ($q->rowCount()) {
  1198. $statsB = $q->fetch();
  1199. $q2= $db->prepare("select * from tank_stats where account_stats_update=:update and account_id=:acid");
  1200. $q2->execute(array(":update"=>$statsB["updated"],":acid"=>$account_id));
  1201. $statsB["tanks"] = array();
  1202. while($t=$q2->fetch()){
  1203. array_push($statsB["tanks"],$t);
  1204. }
  1205. } else {
  1206. // echo "$period_start - $period_end - $intval = StatsB<BR>";
  1207. $statsB = false;
  1208. }
  1209. }
  1210. if (!$statsA or !$statsB)
  1211. return false;
  1212. // ok we've made it this far, we should have valid data in StatsA and statsB
  1213. $statsC = $statsA;
  1214. $tanks = array();
  1215. $statsC["tanks"] = array();
  1216. foreach($statsA as $key=>$val)
  1217. {
  1218. if (is_numeric($val))
  1219. $statsC["$key"] = ($statsA["$key"] - $statsB["$key"]);
  1220. if ($key == "tanks")
  1221. {
  1222. foreach($statsA["tanks"] as $keyb=>$valb) {
  1223. $tank = array();
  1224. if ($statsA["tanks"][$keyb]["name"] == $statsB["tanks"][$keyb]["name"]) {
  1225. //echo "Processing " .$statsA["tanks"][$keyb]["name"] ."<BR>";
  1226. foreach ($statsA["tanks"][$keyb] as $ky=>$vy)
  1227. {
  1228. if (!is_numeric($ky))
  1229. if(($ky == "battles") or ($ky == "victories")) {
  1230. // echo "$ky == battles or victories<BR>";
  1231. $tank[$ky] = $statsA["tanks"][$keyb][$ky] - $statsB["tanks"][$keyb][$ky];
  1232. // echo $tank[$ky] . "<BR>";
  1233. // if ($k != 0)
  1234. // echo "$ky => " . $tank["$ky"] ." = ". $statsA["tanks"][$keyb][$ky]." - ".$statsB["tanks"][$keyb][$ky]."<BR>";
  1235. }
  1236. else
  1237. $tank["$ky"] = $statsA["tanks"][$keyb][$ky];
  1238. }
  1239. //echo "<pre>";
  1240. // print_r($tank);
  1241. //echo "</pre>";
  1242. // print_r($tank);
  1243. array_push($tanks,$tank);
  1244. }
  1245. }
  1246. }
  1247. }
  1248. // echo "<pre>";
  1249. // print_r($statsC["tanks"]);
  1250. // echo "</pre>";
  1251. $statsC["damage"] = $statsA["damage"] - $statsB["damage"];
  1252. //$statsC["defeat"] = $statsA["damage"] - $statsB["damage"];
  1253. $statsC["efficiency"] = $statsA["efficiency"];
  1254. $statsC["avg_exp"] = $statsA["avg_exp"];
  1255. $statsC["max_exp"] = $statsA["max_exp"];
  1256. $statsC["hitratio"] = $statsA["hitratio"];
  1257. // Lets calculate some common ratio's just to make display easier
  1258. if ($statsC["battles"]) {
  1259. $statsC["victories_p"] = round(($statsC["victories"] / $statsC["battles"]) * 100,2);
  1260. $statsC["defeats_p"] = round(($statsC["defeats"] / $statsC["battles"]) * 100,2);
  1261. $statsC["draws"] = $statsC["battles"] - $statsC["victories"] - $statsC["defeats"];
  1262. $statsC["draws_p"] =round(($statsC["draws"] / $statsC["battles"]) * 100,2);
  1263. $statsC["survived_p"] = round(($statsC["survived"] / $statsC["battles"]) * 100,2);
  1264. $statsC["destroyed_r"] = round(($statsC["destroyed"] / $statsC["battles"]),2);
  1265. $statsC["damage_r"] = round(($statsC["damage"] / $statsC["battles"]),2);
  1266. if ($statsC["battles"] - $statsC["survived"])
  1267. $statsC["kd_r"] = round(($statsC["destroyed"] / ($statsC["battles"] - $statsC["survived"])),2);
  1268. $statsC["detected_r"] = round(($statsC["detected"] / $statsC["battles"]) ,2);
  1269. $statsC["capture_r"] = round(($statsC["capture"] / $statsC["battles"]) ,2);
  1270. $statsC["defense_r"] = round(($statsC["defense"] / $statsC["battles"]) ,2);
  1271. $statsC["experience_r"] = round(($statsC["experience"] / $statsC["battles"]),2);
  1272. $statC["tanks"] = array();
  1273. $statsC["tanks"] = $tanks;
  1274. }
  1275. // print_r($statsA);
  1276. // print_r($statsB);
  1277. return $statsC;
  1278. }
  1279. //Valid Stats - based on totals
  1280. // "wr" == win rate
  1281. // "battles" == Battles
  1282. // "kd" == Kil…

Large files files are truncated, but you can click here to view the full file