PageRenderTime 48ms CodeModel.GetById 8ms 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
  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" == Kill to Death Ratio
  1283. // "dmg" == average damage
  1284. // "exp" == average experience
  1285. // "efficiency" == efficiency rating
  1286. function printStatOverDays($days,$account_name,$stat)
  1287. {
  1288. global $db;
  1289. $account_id = getPlayerIdFromNick($_GET["name"]);
  1290. $i = 0;
  1291. $stats = array();
  1292. // $q = $db->prepare("select * from account_stats where account_id=:aid updateTime DESC limit 1");
  1293. //$q->execute(array(":aid"=>$account_id);
  1294. // $latest = $q->fetch();
  1295. if(0) // old logic
  1296. while ($i < $days)
  1297. {
  1298. if ($i == 0)
  1299. {
  1300. $q = $db->prepare("select * from account_stats where account_id=:aid order by updateTime DESC limit 1");
  1301. $q->execute(array(":aid"=>$account_id));
  1302. }else {
  1303. $q = $db->prepare("select * from account_stats where account_id=:aid and updateTime <= NOW() - INTERVAL :t DAY order by updateTime DESC limit 1");
  1304. $q->execute(array(":aid"=>$account_id,":t"=>$i));
  1305. }
  1306. if ($q->rowCount()) {
  1307. $result = $q->fetch();
  1308. array_push($stats,$result);
  1309. }
  1310. //return false;
  1311. $i++;
  1312. }
  1313. $stats = array();
  1314. // new logic, grab stats
  1315. $i = 1;
  1316. while($i <= $days)
  1317. {
  1318. array_push($stats,getStatPeriod($account_id, $i,$i-1,"d"));
  1319. $i++;
  1320. }
  1321. $last = array();
  1322. $i = 0;
  1323. $ar="";
  1324. while($i < $days)
  1325. {
  1326. if (isset($stats[$i]))
  1327. if ($stats[$i]["battles"]) {
  1328. // We have data for this period
  1329. // "wr" == win rate
  1330. // "battles" == Battles
  1331. // "kd" == Kill to Death Ratio
  1332. // "dmg" == average damage
  1333. // "exp" == average experience
  1334. // "efficiency" == efficiency rating
  1335. if ($stat == "wr")
  1336. $ar .= round(($stats[$i]["victories"] / $stats[$i]["battles"])*100,2) .",";
  1337. else if ($stat == "battles")
  1338. $ar.= $stats[$i]["battles"].",";
  1339. else if ($stat == "kd") {
  1340. if ($stats[$i]["battles"] - $stats[$i]["survived"])
  1341. $ar.= round($stats[$i]["destroyed"] / ($stats[$i]["battles"] - $stats[$i]["survived"]),2).",";
  1342. else
  1343. $ar .= $stats[$i]["destroyed"] .",";
  1344. } else if ($stat == "dmg")
  1345. // if ($stats[$i]["battles"])
  1346. if ($stats[$i]["battles"])
  1347. $ar .= round(($stats[$i]["damage"] / $stats[$i]["battles"] ),2) .",";
  1348. else
  1349. $ar .="0,";
  1350. else if ($stat == "exp")
  1351. $ar .= $stats[$i]["experience"].",";
  1352. else if ($stat == "efficiency")
  1353. $ar .= $stats[$i]["efficiency"].",";
  1354. } else
  1355. $ar .= "0,";
  1356. else
  1357. $ar .= "0,";
  1358. $i++;
  1359. }
  1360. $ar = trim($ar,",");
  1361. echo $ar;
  1362. }
  1363. //Valid Stats - based on totals
  1364. // "wr" == win rate
  1365. // "battles" == Battles
  1366. // "kd" == Kill to Death Ratio
  1367. // "dmg" == average damage
  1368. // "exp" == average experience
  1369. // "efficiency" == efficiency rating
  1370. function printStatOverPeriod($days,$account_name,$stat,$period="h")
  1371. {
  1372. global $db,$statCache;
  1373. $account_id = getPlayerIdFromNick($_GET["name"]);
  1374. $i = 0;
  1375. $stats = array();
  1376. if (!is_array($statCache[$period]))
  1377. $statCache[$period]=array();
  1378. // new logic, grab stats
  1379. $i = 1;
  1380. while($i <= $days)
  1381. {
  1382. if (!is_array($statCache[$period][$i])) {
  1383. $statz = getStatPeriod($account_id, $i-1,$i,"$period");
  1384. array_push($stats,$statz);
  1385. $statCache[$period][$i] = $statz;
  1386. } else
  1387. array_push($stats,$statCache[$period][$i]);
  1388. $i++;
  1389. }
  1390. $last = array();
  1391. $i = 0;
  1392. $ar="";
  1393. while($i < $days)
  1394. {
  1395. if (isset($stats[$i]))
  1396. if ($stats[$i]["battles"]) {
  1397. // We have data for this period
  1398. // "wr" == win rate
  1399. // "battles" == Battles
  1400. // "kd" == Kill to Death Ratio
  1401. // "dmg" == average damage
  1402. // "exp" == average experience
  1403. // "efficiency" == efficiency rating
  1404. if ($stat == "wr")
  1405. $ar .= round(($stats[$i]["victories"] / $stats[$i]["battles"])*100,2) .",";
  1406. else if ($stat == "survival")
  1407. $ar .= round(($stats[$i]["survived"] / $stats[$i]["battles"])*100,2) .",";
  1408. else if ($stat == "battles")
  1409. $ar.= $stats[$i]["battles"].",";
  1410. else if ($stat == "kd") {
  1411. if ($stats[$i]["battles"] - $stats[$i]["survived"])
  1412. $ar.= round($stats[$i]["destroyed"] / ($stats[$i]["battles"] - $stats[$i]["survived"]),2).",";
  1413. else
  1414. $ar .= $stats[$i]["destroyed"] .",";
  1415. } else if ($stat == "dmg")
  1416. if ($stats[$i]["battles"])
  1417. $ar .= round(($stats[$i]["damage"] / $stats[$i]["battles"] ),2).",";
  1418. else
  1419. $ar .="0,";
  1420. else if ($stat == "exp")
  1421. if ($stats[$i]["battles"])
  1422. $ar .= round(($stats[$i]["experience"] / $stats[$i]["battles"] ),2).",";
  1423. else
  1424. $ar .="0,";
  1425. else if ($stat == "efficiency")
  1426. $ar .= $stats[$i]["efficiency"].",";
  1427. } else
  1428. $ar .= "0,";
  1429. else
  1430. $ar .= "0,";
  1431. $i++;
  1432. }
  1433. $ar = trim($ar,",");
  1434. echo $ar;
  1435. }
  1436. function printPast24Hours()
  1437. {
  1438. $i=0;
  1439. $str = "";
  1440. while ($i < 24)
  1441. {
  1442. $str .="'$i',";
  1443. $i++;
  1444. }
  1445. echo $str;
  1446. }
  1447. function printPast7days(){
  1448. $m= date("m");
  1449. $de= date("d");
  1450. $y= date("Y");
  1451. $str="";
  1452. for($i=0; $i<=7; $i++){
  1453. $str .= date("'D',",mktime(0,0,0,$m,($de-$i),$y));
  1454. //echo "<br>";
  1455. }
  1456. echo trim($str,",");
  1457. }
  1458. function printPast30Days(){
  1459. $m= date("m");
  1460. $de= date("d");
  1461. $y= date("Y");
  1462. $str="";
  1463. for($i=0; $i<=30; $i++){
  1464. //$str .= date("'j',",mktime(0,0,0,$m,($de-$i),$y));
  1465. $str.="' ',";
  1466. //echo "<br>";
  1467. }
  1468. echo trim($str,",");
  1469. }
  1470. function getLastUpdates($number=25)
  1471. {
  1472. global $db;
  1473. if (!is_numeric($number))
  1474. return array();
  1475. $q = $db->prepare("select * from account_stats order by updateTime DESC limit $number");
  1476. $q->execute();
  1477. $results = array();
  1478. if ($q->rowCount()) {
  1479. while ($result = $q->fetch()) {
  1480. $result["name"]=getAccountName($result["account_id"]);
  1481. array_push($results,$result);
  1482. }
  1483. }
  1484. return $results;
  1485. }
  1486. function prettyStats($stats,$latest,$html=true)
  1487. {
  1488. foreach ($latest as $key=>$val)
  1489. {
  1490. $stats[$key."_color"] = "default";
  1491. if (isset($stats[$key])) {
  1492. if (strstr($key,"_p")){
  1493. if ($key !="draws_p")
  1494. if ($key !="defeats_p") {
  1495. if ($stats[$key] >= $latest[$key])
  1496. $class="green";
  1497. else
  1498. $class="red";
  1499. } else {
  1500. if ($stats[$key] >= $latest[$key])
  1501. $class="red";
  1502. else
  1503. $class="green";
  1504. }
  1505. if ($html)
  1506. $stats[$key] ="<span class=\"$class\">". $stats["$key"]."%</span>";
  1507. else {
  1508. $stats[$key] = $stats["$key"] ."%";
  1509. $stats[$key."_color"] =$class;
  1510. }
  1511. }
  1512. if (strstr($key,"_r")){
  1513. if ($stats[$key] >= $latest[$key])
  1514. $class="green";
  1515. else
  1516. $class="red";
  1517. if ($html)
  1518. $stats[$key] ="<span class=\"$class\">". $stats["$key"]."</span>";
  1519. else
  1520. $stats[$key."_color"] = $class;
  1521. }
  1522. } else {
  1523. if (strstr($key,"_p"))
  1524. $stats[$key] = "-";
  1525. else if (strstr($key,"_r"))
  1526. $stats[$key] = "-";
  1527. else
  1528. $stats[$key] = "0";
  1529. }
  1530. }
  1531. return $stats;
  1532. }
  1533. function loadOld()
  1534. {
  1535. global $db;
  1536. $q = $db->prepare("select * from accounts");
  1537. $q->execute();
  1538. if ($q->rowCount()) {
  1539. //Stats Found!
  1540. while ($result = $q->fetch()) {
  1541. echo "Loading data for " . $result["account_name"];
  1542. importOldData($result["account_id"]);
  1543. echo "\n";
  1544. }
  1545. }
  1546. }
  1547. function importOldData($account_id){
  1548. global $db;
  1549. //account_id = "1001775453";
  1550. $old = array();
  1551. $q = $db->prepare("select * from stats where account_id=:acid ");
  1552. $q->execute(array(":acid"=>$account_id));
  1553. $results = array();
  1554. if ($q->rowCount()) {
  1555. //Stats Found!
  1556. while ($result = $q->fetch()) {
  1557. array_push($old,$result);
  1558. }
  1559. }
  1560. if ($old) {
  1561. // we have all of the old results, lets merge them
  1562. //Convert object to array
  1563. foreach ($old as $val) {
  1564. $first = json_decode($val["data"]);
  1565. $wotStats = array();
  1566. foreach ($first as $k=>$v)
  1567. {
  1568. if ($k != "tanks")
  1569. $wotStats["$k"]= $v;
  1570. else {
  1571. //$tank = array();
  1572. $wotStats["tanks"] = array();
  1573. foreach ($v as $kt=>$vt)
  1574. {
  1575. $tank = array();
  1576. $tank["name"] = $vt->name;
  1577. $tank["url"] = $vt->url;
  1578. $tank["image"] = $vt->image;
  1579. $tank["level"] = romanToInt($vt->level);
  1580. $tank["battles"] = $vt->battles;
  1581. $tank["victories"] = $vt->victories;
  1582. $tank["name"] = $vt->name;
  1583. $tank["account_stats_update"] = $wotStats["updated"];
  1584. array_push($wotStats["tanks"],$tank);
  1585. }
  1586. }
  1587. }
  1588. //print_r($wotStats);
  1589. $q = $db->prepare("select * from account_stats where account_id=:acid and updated=:updated limit 1");
  1590. $q->execute(array(":acid"=>$account_id,":updated"=>$wotStats["updated"]));
  1591. $results = array();
  1592. if ($q->rowCount()) {
  1593. //Stats Found!
  1594. echo ".";
  1595. } else {
  1596. echo "+";
  1597. // }
  1598. // echo $["updateTime"];
  1599. //exit(0);
  1600. // if (0) {
  1601. //Store split data into new DB TAble
  1602. $q2 = $db->prepare("
  1603. insert into account_stats (updateTime,
  1604. defeats,
  1605. clan_tag,
  1606. registered,
  1607. updated,
  1608. account_id,
  1609. clan_url,
  1610. clan_img,
  1611. clan_name,
  1612. clan_motto,
  1613. clan_days,
  1614. clan_enrolled,
  1615. battles,
  1616. victories,
  1617. survived,
  1618. destroyed,
  1619. detected,
  1620. hitratio,
  1621. damage,
  1622. capture,
  1623. defense,
  1624. experience,
  1625. avg_exp,
  1626. max_exp,
  1627. global_rating_val,
  1628. global_rating_place,
  1629. vb_val,
  1630. vb_place,
  1631. avg_exp_val,
  1632. avg_exp_place,
  1633. victories_val,
  1634. victories_place,
  1635. battles_val,
  1636. battles_place,
  1637. capture_val,
  1638. capture_place,
  1639. defense_val,
  1640. defense_place,
  1641. frag_val,
  1642. frag_place,
  1643. detect_val,
  1644. detect_place,
  1645. experience_val,
  1646. experience_place,
  1647. efficiency) VALUES (
  1648. FROM_UNIXTIME(:updateTime),
  1649. :defeats,
  1650. :clan_tag,
  1651. :registered,
  1652. :updated,
  1653. :account_id,
  1654. :clan_url,
  1655. :clan_img,
  1656. :clan_name,
  1657. :clan_motto,
  1658. :clan_days,
  1659. :clan_enrolled,
  1660. :battles,
  1661. :victories,
  1662. :survived,
  1663. :destroyed,
  1664. :detected,
  1665. :hitratio,
  1666. :damage,
  1667. :capture,
  1668. :defense,
  1669. :experience,
  1670. :avg_exp,
  1671. :max_exp,
  1672. :global_rating_val,
  1673. :global_rating_place,
  1674. :vb_val,
  1675. :vb_place,
  1676. :avg_exp_val,
  1677. :avg_exp_place,
  1678. :victories_val,
  1679. :victories_place,
  1680. :battles_val,
  1681. :battles_place,
  1682. :capture_val,
  1683. :capture_place,
  1684. :defense_val,
  1685. :defense_place,
  1686. :frag_val,
  1687. :frag_place,
  1688. :detect_val,
  1689. :detect_place,
  1690. :experience_val,
  1691. :experience_place,
  1692. :efficiency
  1693. )
  1694. ");
  1695. $wotQuery = array();
  1696. $f = explode(",","defeats,clan_tag,registered,
  1697. updated,
  1698. account_id,
  1699. clan_url,
  1700. clan_img,
  1701. clan_name,
  1702. clan_motto,
  1703. clan_days,
  1704. clan_enrolled,
  1705. battles,
  1706. victories,
  1707. survived,
  1708. destroyed,
  1709. detected,
  1710. hitratio,
  1711. damage,
  1712. capture,
  1713. defense,
  1714. experience,
  1715. avg_exp,
  1716. max_exp,
  1717. global_rating_val,
  1718. global_rating_place,
  1719. vb_val,
  1720. vb_place,
  1721. avg_exp_val,
  1722. avg_exp_place,
  1723. victories_val,
  1724. victories_place,
  1725. battles_val,
  1726. battles_place,
  1727. capture_val,
  1728. capture_place,
  1729. defense_val,
  1730. defense_place,
  1731. frag_val,
  1732. frag_place,
  1733. detect_val,
  1734. detect_place,
  1735. experience_val,
  1736. experience_place,
  1737. efficiency");
  1738. foreach ($wotStats as $key=>$value)
  1739. {
  1740. foreach ($f as $b) {
  1741. $wotQuery[trim($b)] = "";
  1742. if ($key == trim($b))
  1743. $wotQuery[":".$key] = $value;
  1744. }
  1745. }
  1746. $wotQuery[":updateTime"]=$wotStats["updated"];
  1747. //Import the tank data too
  1748. $wotQuery["account_id"] = $account_id;
  1749. $q2->execute($wotQuery);
  1750. foreach($wotStats["tanks"] as $tank)
  1751. {
  1752. $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)");
  1753. $t->execute(array(
  1754. ":id"=>$account_id,
  1755. ":name"=>$tank["name"],
  1756. ":url"=>$tank["url"],
  1757. ":image"=>$tank["image"],
  1758. ":level"=>romanToInt($tank["level"]),
  1759. ":battles"=>$tank["battles"],
  1760. ":victories"=>$tank["victories"],
  1761. ":updated"=>$wotStats["updated"]
  1762. ));
  1763. }
  1764. } // if 0
  1765. }// if old
  1766. }
  1767. return $results;
  1768. }
  1769. function printClassPercentage($name,$class,$periodStat=false)
  1770. {
  1771. global $db,$latestStats,$tankList;
  1772. $name =ereg_replace("[^A-Za-z0-9_]", "", $name );
  1773. $account_id = getPlayerIdFromNick($name);
  1774. if($periodStat)
  1775. $stats = $periodStat;
  1776. else if ($latestStats)
  1777. $stats = $latestStats;
  1778. else
  1779. $stats = loadStatistics($account_id);
  1780. //$
  1781. $latestStats = $stats;
  1782. //figure out our averages per class / country
  1783. $countries = array("Soviet Vehicles",
  1784. "German Vehicles",
  1785. "USA Vehicles",
  1786. "French Vehicles",
  1787. "Chinese Vehicles",
  1788. "UK Vehicles"
  1789. );
  1790. $classes = array("Light Tanks",
  1791. "Heavy Tanks",
  1792. "Medium Tanks",
  1793. "Tank Destroyers",
  1794. "SPGs"
  1795. );
  1796. $tiers=array('1','2','3','4','5','6','7','8','9','10');
  1797. if (!$tankList)
  1798. {
  1799. //load the tank list from the db
  1800. $q = $db->prepare("select * from tank_list");
  1801. $q->execute();
  1802. $tanks = array();
  1803. if ($q->rowCount()) {
  1804. //Stats Found!
  1805. while ($result = $q->fetch()) {
  1806. $tankList[$result["name"]] = $result;
  1807. }
  1808. }
  1809. }
  1810. //$tankList = $tanks;
  1811. $tanks = $tankList;
  1812. if(!is_array($latestStats["class_tank_list"])) {
  1813. foreach ($stats["tanks"] as $tank)
  1814. {
  1815. $tanks[$tank["name"]]["battles"] = $tank["battles"];
  1816. }
  1817. $latestStats["class_tank_list"] = array();
  1818. $latestStats["tier_tank_list"] = array();
  1819. foreach ($classes as $c) {
  1820. foreach($countries as $s) {
  1821. $latestStats["class_tank_list"][$c][$s] = 0;
  1822. }
  1823. }
  1824. foreach ($classes as $c) {
  1825. foreach($tiers as $s) {
  1826. $latestStats["tier_tank_list"][$c][$s] = 0;
  1827. }
  1828. }
  1829. foreach ($tanks as $tank) {
  1830. $latestStats["class_tank_list"][$tank["class"]][$tank["country"]] += round( ( $tank["battles"] / $stats["battles"]) * 100,2);
  1831. $latestStats["tier_tank_list"][$tank["class"]][$tank["level"]] += round( ( $tank["battles"] / $stats["battles"]) * 100,2);
  1832. // echo "---" . $latestStats["class_tank_list"][$tank["class"]][$tank["country"]] ."----";
  1833. }
  1834. }
  1835. $output ="";
  1836. if ($class =="h_t") {
  1837. //print_r($latestStats["class_tank_list"]);
  1838. foreach ($latestStats["tier_tank_list"]["Heavy Tanks"] as $i)
  1839. $output .=$i.",";
  1840. }else if ($class =="m_t") {
  1841. foreach ($latestStats["tier_tank_list"]["Medium Tanks"] as $i)
  1842. $output .=$i.",";
  1843. }
  1844. else if ($class =="l_t") {
  1845. foreach ($latestStats["tier_tank_list"]["Light Tanks"] as $i)
  1846. $output .=$i.",";
  1847. }
  1848. else if ($class =="s_t") {
  1849. foreach ($latestStats["tier_tank_list"]["SPGs"] as $i)
  1850. $output .=$i.",";
  1851. }
  1852. else if ($class =="t_t") {
  1853. foreach ($latestStats["tier_tank_list"]["Tank Destroyers"] as $i)
  1854. $output .=$i.",";
  1855. }
  1856. else if ($class =="tt_t") {
  1857. foreach ($latestStats["tier_tank_list"]["Tank Destroyers"] as $i)
  1858. $output +=$i;
  1859. }
  1860. else if ($class =="st_t") {
  1861. foreach ($latestStats["tier_tank_list"]["SPGs"] as $i)
  1862. $output +=$i;
  1863. } else if ($class =="lt_t") {
  1864. foreach ($latestStats["tier_tank_list"]["Light Tanks"] as $i)
  1865. $output +=$i;
  1866. } else if ($class =="mt_t") {
  1867. foreach ($latestStats["tier_tank_list"]["Medium Tanks"] as $i)
  1868. $output +=$i;
  1869. } else if ($class =="ht_t") {
  1870. foreach ($latestStats["tier_tank_list"]["Heavy Tanks"] as $i)
  1871. $output +=$i;
  1872. }
  1873. if ($class =="h") {
  1874. //print_r($latestStats["class_tank_list"]);
  1875. foreach ($latestStats["class_tank_list"]["Heavy Tanks"] as $i)
  1876. $output .=$i.",";
  1877. }else if ($class =="m") {
  1878. foreach ($latestStats["class_tank_list"]["Medium Tanks"] as $i)
  1879. $output .=$i.",";
  1880. }
  1881. else if ($class =="l") {
  1882. foreach ($latestStats["class_tank_list"]["Light Tanks"] as $i)
  1883. $output .=$i.",";
  1884. }
  1885. else if ($class =="s") {
  1886. foreach ($latestStats["class_tank_list"]["SPGs"] as $i)
  1887. $output .=$i.",";
  1888. }
  1889. else if ($class =="t") {
  1890. foreach ($latestStats["class_tank_list"]["Tank Destroyers"] as $i)
  1891. $output .=$i.",";
  1892. }
  1893. else if ($class =="tt") {
  1894. foreach ($latestStats["class_tank_list"]["Tank Destroyers"] as $i)
  1895. $output +=$i;
  1896. }
  1897. else if ($class =="st") {
  1898. foreach ($latestStats["class_tank_list"]["SPGs"] as $i)
  1899. $output +=$i;
  1900. } else if ($class =="lt") {
  1901. foreach ($latestStats["class_tank_list"]["Light Tanks"] as $i)
  1902. $output +=$i;
  1903. } else if ($class =="mt") {
  1904. foreach ($latestStats["class_tank_list"]["Medium Tanks"] as $i)
  1905. $output +=$i;
  1906. } else if ($class =="ht") {
  1907. foreach ($latestStats["class_tank_list"]["Heavy Tanks"] as $i)
  1908. $output +=$i;
  1909. }
  1910. //print_r($latestStats["class_tank_list"]);
  1911. echo trim($output,",");
  1912. }
  1913. function createForumSignature($account_id,$name,$dark=0,$fg=false,$bg=false)
  1914. {
  1915. global $server;
  1916. $name =ereg_replace("[^A-Za-z0-9_]", "", $name );
  1917. header("Cache-Control: private, max-age=10800, pre-check=10800");
  1918. header("Pragma: private");
  1919. header("Expires: " . date(DATE_RFC822,strtotime(" 1 hour")));
  1920. $fg_color = html2rgb($fg);
  1921. $bg_color = html2rgb($bg);
  1922. if (!$dark)
  1923. $img = "forum_sig/$server-$account_id.png";
  1924. else if ($dark == 2)
  1925. $img = "forum_sig/dark-$server-$account_id.png";
  1926. else if ($dark == 3){
  1927. if ($fg_color and $bg_color)
  1928. {
  1929. $img = "forum_sig/color-".$fg_color[0].$fg_color[1].$fg_color[2]."-".$bg_color[0].$bg_color[1].$bg_color[2]."-$server-$account_id.png";
  1930. }
  1931. else
  1932. $img = "forum_sig/$server-$account_id.png";
  1933. }
  1934. if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
  1935. &&
  1936. (strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == filemtime($img))) {
  1937. // send the last mod time of the file back
  1938. header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($img)).' GMT',
  1939. true, 304);
  1940. exit;
  1941. }
  1942. header( "Content-type: image/png");
  1943. if (filemtime($img) >= strtotime(" -1 hour"))
  1944. {
  1945. if (is_file($img)) {
  1946. readfile($img);
  1947. exit;
  1948. }
  1949. }
  1950. // 468x100
  1951. $image = imagecreatetruecolor(936, 200);
  1952. $line = 0;
  1953. $white = imagecolorallocate($image, 255,255,255);
  1954. $yellow = imagecolorallocate($image, 240, 150, 50); // create color R=255, G=255, B=0
  1955. $cyan = imagecolorallocate($image, 0, 255, 255); // create color R=0, G=255, B=255
  1956. $red = imagecolorallocate($image, 255, 0, 0); // create color red
  1957. $blue = imagecolorallocate($image, 0, 0, 255); // create color blue
  1958. $green = imagecolorallocate($image, 24, 150, 13);
  1959. $black = imagecolorallocate($image, 0 , 0 ,0);
  1960. $purple = imagecolorallocate($image, 150 , 13 ,171);
  1961. //$white = imagecolorallocate($image, 255,255,255);
  1962. $navy = imagecolorallocate($image,0,0,102);
  1963. if ($dark == 2) {
  1964. imagefilledrectangle($image, 0, 0, 936, 200, $black);
  1965. $color["default"] = $white;
  1966. } else if ($dark == 3) {
  1967. if ($fg_color and $bg_color) {
  1968. $nfg = imagecolorallocate($image,$fg_color[0],$fg_color[1],$fg_color[2]);
  1969. $nbg = imagecolorallocate($image,$bg_color[0],$bg_color[1],$bg_color[2]);
  1970. imagefilledrectangle($image, 0, 0, 936, 200, $nbg);
  1971. $color["default"] = $nfg;
  1972. } else {
  1973. imagefilledrectangle($image, 0, 0, 936, 200, $white);
  1974. $color["default"] = $navy;
  1975. }
  1976. } else {
  1977. imagefilledrectangle($image, 0, 0, 936, 200, $white);
  1978. $color["default"] = $navy;
  1979. }
  1980. // $font = 'libs/VeraMono.ttf';
  1981. $font = 'libs/Arial.ttf';
  1982. $line=1;
  1983. $s0 = loadStatistics($account_id);
  1984. date_default_timezone_set('America/Los_Angeles');
  1985. $s0["registered"] = date("Y-m-d g:i A",$s0["registered"]) . " PST";
  1986. $s0["lastUpdate"] = date("Y-m-d g:i A",$s0["updated"]) . " PST";
  1987. $s0["name"] = $name;
  1988. $stats = $s0;
  1989. $id = $account_id;
  1990. $s60 = getStatPeriod($id, 0,1,"h");
  1991. $s24 = getStatPeriod($id, 0,24,"h");
  1992. // print_r($s24);
  1993. $days = 7;
  1994. $s7 = false;
  1995. while (!$s7 and $days > 0) {
  1996. $s7 = getStatPeriod($id, 0,$days * 24,"h");
  1997. $days--;
  1998. }
  1999. $days = 30;
  2000. $s30 = false;
  2001. while (!$s30 and $days > 0) {
  2002. $s30 = getStatPeriod($id, 0,$days * 24,"h");
  2003. $days--;
  2004. }
  2005. //$s7 = getStatPeriod($id, 0,168,"h");
  2006. //$s30 = getStatPeriod($id, 0,720,"h");
  2007. //$s0 = prettyStats($s0,$s0,false);
  2008. $s60 = prettyStats($s60,$s0,false);
  2009. $s60["efficiency"] = calcPeriodEfficiency($s0,$s60);
  2010. $s24 = prettyStats($s24,$s0,false);
  2011. $s24["efficiency"] = calcPeriodEfficiency($s0,$s24);
  2012. $s7 = prettyStats($s7,$s0,false);
  2013. $s7["efficiency"] = calcPeriodEfficiency($s0,$s7);
  2014. $s30 = prettyStats($s30,$s0,false);
  2015. $s30["efficiency"] = calcPeriodEfficiency($s0,$s30);
  2016. $c[0] = 5*2;
  2017. $c[1] = 100*2;
  2018. $c[2] = 175*2;
  2019. $c[3] = 250*2;
  2020. $c[4] = 325*2;
  2021. $c[5] = 400*2;
  2022. $r[0] = 45*2;
  2023. $r[1] = 58*2;
  2024. $r[2] = 71*2;
  2025. $r[3] = 84*2;
  2026. $r[4] = 97*2;
  2027. //$color["default"] = $navy;
  2028. $color["red"] = $red;
  2029. $color["green"] = $green;
  2030. imagettftextmultisampled($image, 15*2, 0, 15*2, 19*2, $color["default"], $font, $name);
  2031. $effColor = $red;
  2032. if ($stats["efficiency"] > 900)
  2033. $effColor = $yellow;
  2034. if ($stats["efficiency"] > 1200)
  2035. $effColor = $green;
  2036. if ($stats["efficiency"] > 1500)
  2037. $effColor = $purple;
  2038. $textSize = 7*2;
  2039. imagettftextmultisampled($image, $textSize, 0, $c[0], 34*2, $effColor, $font, "Efficiency: ".$stats["efficiency"]."");
  2040. if ($stats["clan_tag"])
  2041. imagettftextmultisampled($image, 10*2, 0, 275*2, 19*2, $color["default"], $font, $stats["clan_days"] . " days in clan " . trim($stats["clan_tag"],"[]"));
  2042. // Line 1 Headers (column lables)
  2043. imagettftextmultisampled($image, $textSize, 0, $c[1], 34*2, $color["default"], $font, "Total");
  2044. imagettftextmultisampled($image, $textSize, 0, $c[2], 34*2, $color["default"], $font, "Past 60 Mins");
  2045. imagettftextmultisampled($image, $textSize, 0, $c[3], 34*2, $color["default"], $font, "Past 24 Hours");
  2046. imagettftextmultisampled($image, $textSize, 0, $c[4], 34*2, $color["default"], $font, "Past 7 Days");
  2047. imagettftextmultisampled($image, $textSize, 0, $c[5], 34*2, $color["default"], $font, "Past 30 Days");
  2048. // row labels
  2049. imagettftextmultisampled($image, $textSize, 0, $c[0], $r[0], $color["default"], $font, "Battles:");
  2050. imagettftextmultisampled($image, $textSize, 0, $c[0], $r[1], $color["default"], $font, "Victory %:");
  2051. imagettftextmultisampled($image, $textSize, 0, $c[0], $r[2], $color["default"], $font, "K/D Ratio:");
  2052. imagettftextmultisampled($image, $textSize, 0, $c[0], $r[3], $color["default"], $font, "Damage Rate:");
  2053. imagettftextmultisampled($image, $textSize, 0, $c[0], $r[4], $color["default"], $font, "Exp. Rate:");
  2054. //Battles Data
  2055. imagettftextmultisampled($image, $textSize, 0, $c[1], $r[0], $color["default"], $font, $s0["battles"]);
  2056. imagettftextmultisampled($image, $textSize, 0, $c[2], $r[0], $color[$s60["battles_color"]], $font, $s60["battles"]);
  2057. imagettftextmultisampled($image, $textSize, 0, $c[3], $r[0], $color[$s24["battles_color"]], $font, $s24["battles"]);
  2058. imagettftextmultisampled($image, $textSize, 0, $c[4], $r[0], $color[$s7["battles_color"]], $font, $s7["battles"]);
  2059. imagettftextmultisampled($image, $textSize, 0, $c[5], $r[0], $color[$s30["battles_color"]], $font, $s30["battles"]);
  2060. imagettftextmultisampled($image, $textSize, 0, $c[1], $r[1], $color["default"], $font, $s0["victories_p"]."%");
  2061. imagettftextmultisampled($image, $textSize, 0, $c[2], $r[1], $color[$s60["victories_p_color"]], $font, $s60["victories_p"]);
  2062. imagettftextmultisampled($image, $textSize, 0, $c[3], $r[1], $color[$s24["victories_p_color"]], $font, $s24["victories_p"]);
  2063. imagettftextmultisampled($image, $textSize, 0, $c[4], $r[1], $color[$s7["victories_p_color"]], $font, $s7["victories_p"]);
  2064. imagettftextmultisampled($image, $textSize, 0, $c[5], $r[1], $color[$s30["victories_p_color"]], $font, $s30["victories_p"]);
  2065. imagettftextmultisampled($image, $textSize, 0, $c[1], $r[2], $color["default"], $font, $s0["kd_r"]);
  2066. imagettftextmultisampled($image, $textSize, 0, $c[2], $r[2], $color[$s60["kd_r_color"]], $font, $s60["kd_r"]);
  2067. imagettftextmultisampled($image, $textSize, 0, $c[3], $r[2], $color[$s24["kd_r_color"]], $font, $s24["kd_r"]);
  2068. imagettftextmultisampled($image, $textSize, 0, $c[4], $r[2], $color[$s7["kd_r_color"]], $font, $s7["kd_r"]);
  2069. imagettftextmultisampled($image, $textSize, 0, $c[5], $r[2], $color[$s30["kd_r_color"]], $font, $s30["kd_r"]);
  2070. imagettftextmultisampled($image, $textSize, 0, $c[1], $r[3], $color["default"], $font, $s0["damage_r"]);
  2071. imagettftextmultisampled($image, $textSize, 0, $c[2], $r[3], $color[$s60["damage_r_color"]], $font, $s60["damage_r"]);
  2072. imagettftextmultisampled($image, $textSize, 0, $c[3], $r[3], $color[$s24["damage_r_color"]], $font, $s24["damage_r"]);
  2073. imagettftextmultisampled($image, $textSize, 0, $c[4], $r[3], $color[$s7["damage_r_color"]], $font, $s7["damage_r"]);
  2074. imagettftextmultisampled($image, $textSize, 0, $c[5], $r[3], $color[$s30["damage_r_color"]], $font, $s30["damage_r"]);
  2075. imagettftextmultisampled($image, $textSize, 0, $c[1], $r[4], $color["default"], $font, $s0["experience_r"]);
  2076. imagettftextmultisampled($image, $textSize, 0, $c[2], $r[4], $color[$s60["experience_r_color"]], $font, $s60["experience_r"]);
  2077. imagettftextmultisampled($image, $textSize, 0, $c[3], $r[4], $color[$s24["experience_r_color"]], $font, $s24["experience_r"]);
  2078. imagettftextmultisampled($image, $textSize, 0, $c[4], $r[4], $color[$s7["experience_r_color"]], $font, $s7["experience_r"]);
  2079. imagettftextmultisampled($image, $textSize, 0, $c[5], $r[4], $color[$s30["experience_r_color"]], $font, $s30["experience_r"]);
  2080. //imagestring($image, 5, 15, $line++ * 12, 'This is a test.', $navy);
  2081. //imagestring($image, 2, 15, $line++ * 12, 'This is a test.', $navy);
  2082. //imagestring($image, 2, 15, $line++ * 12, 'This is a test.', $navy);
  2083. // imagestring($image, 2, 15, $line++ * 12, 'This is a test.', $navy);
  2084. // imagestring($image, 2, 15, $line++ * 12, 'This is a test.', $navy);
  2085. // imagestring($image, 2, 15, $line++ * 12, 'This is a test.', $navy);
  2086. $image_p = imagecreatetruecolor(936/2, 100);
  2087. imagecopyresampled($image_p,$image,0,0,0,0,936/2,100,936,200);
  2088. imagepng($image_p,$img);
  2089. imagepng($image_p);
  2090. imagedestroy($image);
  2091. imagedestroy($image_p);
  2092. }
  2093. function imagettftextmultisampled(&$hImg, $iSize, $sAngle, $iX, $iY, $cColor, $sFont, $sText, $iMultiSampling=2){
  2094. $iWidth = imagesx($hImg);
  2095. $iHeight = imagesy($hImg);
  2096. $hImgCpy = imagecreatetruecolor(ceil($iWidth*$iMultiSampling), ceil($iHeight*$iMultiSampling));
  2097. $cColor = imagecolorsforindex($hImg, $cColor);
  2098. $cColor = imagecolorallocatealpha($hImgCpy, $cColor['red'], $cColor['green'], $cColor['blue'], $cColor['alpha']);
  2099. imagesavealpha($hImgCpy, true);
  2100. imagealphablending($hImgCpy, false);
  2101. $cTransparent = imagecolortransparent($hImgCpy, imagecolorallocatealpha($hImgCpy, 0, 0, 0, 127));
  2102. imagefill($hImgCpy, 0, 0, $cTransparent);
  2103. $aBox = imagettftext($hImgCpy, $iSize*$iMultiSampling, $sAngle, ceil($iX*$iMultiSampling), ceil($iY*$iMultiSampling), $cColor, $sFont, $sText);
  2104. imagecopyresampled($hImg, $hImgCpy, 0, 0, 0, 0, $iWidth, $iHeight, ceil($iWidth*$iMultiSampling), ceil($iHeight*$iMultiSampling));
  2105. imagedestroy($hImgCpy);
  2106. foreach($aBox as $iKey => $iCoordinate)
  2107. $aBox[$iKey] = $iCoordinate/$iMultiSampling;
  2108. return($aBox);
  2109. }
  2110. function imagettfbboxmultisampled($iSize, $iAngle, $sFont, $sText, $iMultiSampling){
  2111. $aBox = imagettfbbox($iSize*$iMultiSampling, $iAngle, $sFont, $sText);
  2112. foreach($aBox as $iKey => $iCoordinate)
  2113. $aBox[$iKey] = $iCoordinate/$iMultiSampling;
  2114. return($aBox);
  2115. }
  2116. function html2rgb($color)
  2117. {
  2118. if ($color[0] == '#')
  2119. $color = substr($color, 1);
  2120. if (strlen($color) == 6)
  2121. list($r, $g, $b) = array($color[0].$color[1],
  2122. $color[2].$color[3],
  2123. $color[4].$color[5]);
  2124. elseif (strlen($color) == 3)
  2125. list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
  2126. else
  2127. return false;
  2128. $r = hexdec($r); $g = hexdec($g); $b = hexdec($b);
  2129. return array($r, $g, $b);
  2130. }
  2131. ?>