PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/Web Files/ASP/includes/admin.import.bf2web.php

https://code.google.com/p/bf2stats/
PHP | 389 lines | 375 code | 5 blank | 9 comment | 1 complexity | 8c0a921e29c8a40caf4bbf1588dfb491 MD5 | raw file
  1. <?php
  2. // Import EA BF2Web Gamespy Data
  3. // $cfg = new Config();
  4. // $connection = @mysql_connect($cfg->get('db_host'), $cfg->get('db_user'), $cfg->get('db_pass'));
  5. // @mysql_select_db($cfg->get('db_name'), $connection) or die("Database Error: " . mysql_error());
  6. // Temp PID
  7. $pid = 43890642;
  8. //$pid = 76591835;
  9. // BF2Web URL
  10. $gsURL = "http://bf2web.gamespy.com";
  11. //$gsURL = "http://www.shadowlans.com";
  12. // Get Player Info: Base (getplayerinfo.aspx query)
  13. $playerinfoURL1 = $gsURL."/ASP/getplayerinfo.aspx?pid={$pid}&info=per*,cmb*,twsc,cpcp,cacp,dfcp,kila,heal,rviv,rsup,rpar,tgte,dkas,dsab,cdsc,rank,cmsc,kick,kill,deth,suic,ospm,klpm,klpr,dtpr,bksk,wdsk,bbrs,tcdr,ban,dtpm,lbtl,osaa,vrk,tsql,tsqm,tlwf,mvks,vmks,mvn*,vmr*,fkit,fmap,fveh,fwea,wtm-,wkl-,wdt-,wac-,wkd-,vtm-,vkl-,vdt-,vkd-,vkr-,atm-,awn-,alo-,abr-,ktm-,kkl-,kdt-,kkd-";
  14. $playerinfoBase = getPageContents($playerinfoURL1);
  15. if(!stristr(implode("\n",$playerinfoBase),"O\nH\tasof\n")) {
  16. die("Player with PID {$pid} doesn't exist on the stat server !");
  17. }
  18. $playerinfoData1 = parsePlayerInfo($playerinfoBase);
  19. // Get Player Info: Extra (getplayerinfo.aspx query)
  20. $playerinfoURL2 = $gsURL."/ASP/getplayerinfo.aspx?pid={$pid}&info=mtm-,mwn-,mls-";
  21. $playerinfoExtra = getPageContents($playerinfoURL2);
  22. $playerinfoData2 = parsePlayerInfo($playerinfoExtra);
  23. // Get Player Info: Unlocks (getunlocksinfo.aspx query)
  24. $unlocksinfoURL = $gsURL."/ASP/getunlocksinfo.aspx?pid={$pid}";
  25. $playerinfoUnlocks = getPageContents($unlocksinfoURL);
  26. $playerinfoData3 = parseUnlocks($playerinfoUnlocks);
  27. // Get Player Info: Awards (getawardsinfo.aspx query)
  28. $awardsinfoURL = $gsURL."/ASP/getawardsinfo.aspx?pid={$pid}";
  29. $playerinfoAwards = getPageContents($awardsinfoURL);
  30. $playerinfoData4 = parseAwards($playerinfoAwards);
  31. print_r($playerinfoData1);
  32. print_r($playerinfoData2);
  33. print_r($playerinfoData3);
  34. print_r($playerinfoData4);
  35. // Check if Player Already Exists
  36. $query = "SELECT * FROM player WHERE id = {$pid}";
  37. $result = mysql_query($query);
  38. checkSQLResult ($result, $query);
  39. if (!mysql_num_rows($result))
  40. {
  41. $country = 'xx';
  42. // Insert information
  43. $query = "INSERT INTO player SET
  44. id = {$pid},
  45. name = '" . $playerinfoData1["nick"] . "',
  46. country = 'xx',
  47. time = " . $playerinfoData1["time"] . ",
  48. rounds = " . $playerinfoData1["mode0"] + $playerinfoData1["mode1"] + $playerinfoData1["mode2"] . ",
  49. ip = '0.0.0.0',
  50. score = " . $playerinfoData1["scor"] . ",
  51. cmdscore = " . $playerinfoData1["cdsc"] . ",
  52. skillscore = " . $playerinfoData1["cmsc"] . ",
  53. teamscore = " . $playerinfoData1["twsc"] . ",
  54. kills = " . $playerinfoData1["kill"] . ",
  55. deaths = " . $playerinfoData1["deth"] . ",
  56. captures = " . $playerinfoData1["cpcp"] . ",
  57. neutralizes = 0,
  58. captureassists = " . $playerinfoData1["cacp"] . ",
  59. neutralizeassists = 0,
  60. defends = " . $playerinfoData1["dfcp"] . ",
  61. damageassists = " . $playerinfoData1["kila"] . ",
  62. heals = " . $playerinfoData1["heal"] . ",
  63. revives = " . $playerinfoData1["rviv"] . ",
  64. ammos = " . $playerinfoData1["rsup"] . ",
  65. repairs = " . $playerinfoData1["rpar"] . ",
  66. targetassists = " . $playerinfoData1["tgte"] . ",
  67. driverspecials = " . $playerinfoData1["dsab"] . ",
  68. driverassists = " . $playerinfoData1["dkas"] . ",
  69. passengerassists = 0,
  70. teamkills = 0,
  71. teamdamage = 0,
  72. teamvehicledamage = 0,
  73. suicides = " . $playerinfoData1["suic"] . ",
  74. killstreak = " . $playerinfoData1["bksk"] . ",
  75. deathstreak = " . $playerinfoData1["wdsk"] . ",
  76. rank = " . $playerinfoData1["rank"] . ",
  77. banned = " . $playerinfoData1["ban"] . ",
  78. kicked = " . $playerinfoData1["kick"] . ",
  79. cmdtime = " . $playerinfoData1["tcdr"] . ",
  80. sqltime = " . $playerinfoData1["tsql"] . ",
  81. sqmtime = " . $playerinfoData1["tsqm"] . ",
  82. lwtime = " . $playerinfoData1["tlwf"] . ",
  83. wins = " . $playerinfoData1["wins"] . ",
  84. losses = " . $playerinfoData1["loss"] . ",
  85. availunlocks = 0,
  86. usedunlocks = 0,
  87. joined = " . $playerinfoData1["jond"] . ",
  88. rndscore = " . $playerinfoData1["bbrs"] . ",
  89. lastonline = " . $playerinfoData1["lbtl"] . ",
  90. mode0 = " . $playerinfoData1["mode0"] . ",
  91. mode1 = " . $playerinfoData1["mode1"] . ",
  92. mode2 = " . $playerinfoData1["mode2"] . "
  93. ";
  94. $result = mysql_query($query);
  95. checkSQLResult ($result, $query);
  96. // Insert unlocks
  97. for ($i = 11; $i < 100; $i += 11)
  98. {
  99. $query = "INSERT INTO unlocks SET
  100. id = " . $playerinfoData1["pID_$x"] . ",
  101. kit = {$i},
  102. state = 'n'
  103. ";
  104. $result = mysql_query($query);
  105. checkSQLResult ($result, $query);
  106. }
  107. for ($i = 111; $i < 556; $i += 111)
  108. {
  109. $query = "INSERT INTO unlocks SET
  110. id = " . $playerinfoData1["pID_$x"] . ",
  111. kit = {$i},
  112. state = 'n'
  113. ";
  114. $result = mysql_query($query);
  115. checkSQLResult ($result, $query);
  116. }
  117. }
  118. else
  119. {
  120. $row = mysql_fetch_array($result);
  121. // Check IP
  122. if ($row['ip'] != $playerinfoData1["ip_$x"])
  123. {
  124. $query2 = "SELECT country FROM ip2nation WHERE ip < INET_ATON('" . $playerinfoData1["ip_$x"] . "') ORDER BY ip DESC LIMIT 1";
  125. $result2 = mysql_query($query2);
  126. checkSQLResult ($result2, $query2);
  127. if (!mysql_num_rows($result2)) {
  128. $country = 'xx';
  129. } else {
  130. $row2 = mysql_fetch_array($result2);
  131. $country = $row2['country'];
  132. }
  133. }
  134. else {$country = $row['country'];}
  135. // Verify/Correct Rank
  136. if ($cfg->get('stats_rank_check')) {
  137. $score = $row['score'] + $playerinfoData1["rs_$x"];
  138. $rank = $playerinfoData1["rank_$x"];
  139. ErrorLog("Checking Rank for Player (".$playerinfoData1["pID_$x"].") : Score:{$score} : Rank:{$rank}",3);
  140. $expRank = array();
  141. // NOTE: Ranks 1SG/SGM/BG/MG/SMOC/GEN cannot be awarded here.
  142. if ($score >= 200000) {$expRank[0] = 20;$expRank[1] = 20;}
  143. elseif ($score >= 150000) {$expRank[0] = 17;$expRank[1] = 19;}
  144. elseif ($score >= 125000) {$expRank[0] = 16;$expRank[1] = 16;}
  145. elseif ($score >= 115000) {$expRank[0] = 15;$expRank[1] = 15;}
  146. elseif ($score >= 90000) {$expRank[0] = 14;$expRank[1] = 14;}
  147. elseif ($score >= 75000) {$expRank[0] = 13;$expRank[1] = 13;}
  148. elseif ($score >= 60000) {$expRank[0] = 12;$expRank[1] = 12;}
  149. elseif ($score >= 50000) {$expRank[0] = 9;$expRank[1] = 11;}
  150. elseif ($score >= 20000) {$expRank[0] = 7;$expRank[1] = 8;}
  151. elseif ($score >= 8000) {$expRank[0] = 6;$expRank[1] = 6;}
  152. elseif ($score >= 5000) {$expRank[0] = 5;$expRank[1] = 5;}
  153. elseif ($score >= 2500) {$expRank[0] = 4;$expRank[1] = 4;}
  154. elseif ($score >= 800) {$expRank[0] = 3;$expRank[1] = 3;}
  155. elseif ($score >= 500) {$expRank[0] = 2;$expRank[1] = 2;}
  156. elseif ($score >= 150) {$expRank[0] = 1;$expRank[1] = 1;}
  157. else {$expRank[0] = 0;$expRank[1] = 0;}
  158. // Only update if Rank is less than expected.
  159. if ($rank < $expRank[0] || $rank > $expRank[1]){
  160. // Rank seems to be messed up, will reset to minimum rank for this level
  161. $errmsg = "Rank Correction (".$playerinfoData1["pID_$x"]."): " .
  162. "Score:".$score."; " .
  163. "Expected:".$expRank[0]."-".$expRank[1]."; " .
  164. "Found:".$playerinfoData1["rank_$x"]."; " .
  165. "New Rank:".$expRank[0];
  166. ErrorLog($errmsg,2);
  167. $playerinfoData1["rank_$x"] = $expRank[0];
  168. }
  169. } else {
  170. // Fail-safe in-case rank data was not obtained and reset to '0' in-game.
  171. $rank = $playerinfoData1["rank_$x"];
  172. $rank_db = $row['rank'];
  173. if ($rank_db > $rank) {
  174. // SNAPSHOT rank data appears to be incorrect, will use current db rank
  175. $playerinfoData1["rank_$x"] = $rank_db;
  176. $errmsg = "Rank Correction (".$playerinfoData1["pID_$x"]."), using db rank ({$rank_db})";
  177. ErrorLog($errmsg,2);
  178. }
  179. }
  180. // Calculate kill/deathstreak
  181. $killstreak = ($row['killstreak'] > $playerinfoData1["ks_$x"]) ? $row['killstreak'] : $playerinfoData1["ks_$x"];
  182. $deathstreak = ($row['deathstreak'] > $playerinfoData1["ds_$x"]) ? $row['deathstreak'] : $playerinfoData1["ds_$x"];
  183. // Calculate best round score
  184. $rndscore = ($row['rndscore'] > $playerinfoData1["rs_$x"]) ? $row['rndscore'] : $playerinfoData1["rs_$x"];
  185. // Check if Minimal Central Update
  186. if ($centralupdate == 2) {
  187. // Ignore any Rank Data in SnapShot as this could mess up current data
  188. $playerinfoData1["rank_$x"] = $row['rank'];
  189. }
  190. // Calculate rank change
  191. $chng = $decr = 0;
  192. if ($playerinfoData1["rank_$x"] != $row['rank'])
  193. {
  194. if ($playerinfoData1["rank_$x"] > $row['rank']) {$chng = 1;}
  195. else {$decr = 1;}
  196. }
  197. // Update information
  198. $query = "UPDATE player SET
  199. name = '" . $playerinfoData1["name_$x"] . "',
  200. country = '{$country}',
  201. time = `time` + " . $playerinfoData1["ctime_$x"] . ",
  202. rounds = `rounds` + {$complete},
  203. ip = '" . $playerinfoData1["ip_$x"] . "',
  204. score = `score` + " . $playerinfoData1["rs_$x"] . ",
  205. cmdscore = `cmdscore` + " . $playerinfoData1["cs_$x"] . ",
  206. skillscore = `skillscore` + " . $playerinfoData1["ss_$x"] . ",
  207. teamscore = `teamscore` + " . $playerinfoData1["ts_$x"] . ",
  208. kills = `kills` + " . $playerinfoData1["kills_$x"] . ",
  209. deaths = `deaths` + " . $playerinfoData1["deaths_$x"] . ",
  210. captures = `captures` + " . $playerinfoData1["cpc_$x"] . ",
  211. neutralizes = `neutralizes` + " . $playerinfoData1["cpn_$x"] . ",
  212. captureassists = `captureassists` + " . $playerinfoData1["cpa_$x"] . ",
  213. neutralizeassists = `neutralizeassists` + " . $playerinfoData1["cpna_$x"] . ",
  214. defends = `defends` + " . $playerinfoData1["cpd_$x"] . ",
  215. damageassists = `damageassists` + " . $playerinfoData1["ka_$x"] . ",
  216. heals = `heals` + " . $playerinfoData1["he_$x"] . ",
  217. revives = `revives` + " . $playerinfoData1["rev_$x"] . ",
  218. ammos = `ammos` + " . $playerinfoData1["rsp_$x"] . ",
  219. repairs = `repairs` + " . $playerinfoData1["rep_$x"] . ",
  220. targetassists = `targetassists` + " . $playerinfoData1["tre_$x"] . ",
  221. driverspecials = `driverspecials` + " . $playerinfoData1["drs_$x"] . ",
  222. driverassists = `driverassists` + " . $playerinfoData1["dra_$x"] . ",
  223. passengerassists = `passengerassists` + " . $playerinfoData1["pa_$x"] . ",
  224. teamkills = `teamkills` + " . $playerinfoData1["tmkl_$x"] . ",
  225. teamdamage = `teamdamage` + " . $playerinfoData1["tmdg_$x"] . ",
  226. teamvehicledamage = `teamvehicledamage` + " . $playerinfoData1["tmvd_$x"] . ",
  227. suicides = `suicides` + " . $playerinfoData1["su_$x"] . ",
  228. killstreak = {$killstreak},
  229. deathstreak = {$deathstreak},
  230. rank = " . $playerinfoData1["rank_$x"] . ",
  231. banned = `banned` + " . $playerinfoData1["ban_$x"] . ",
  232. kicked = `kicked` + " . $playerinfoData1["kck_$x"] . ",
  233. cmdtime = `cmdtime` + " . $playerinfoData1["tco_$x"] . ",
  234. sqltime = `sqltime` + " . $playerinfoData1["tsl_$x"] . ",
  235. sqmtime = `sqmtime` + " . $playerinfoData1["tsm_$x"] . ",
  236. lwtime = `lwtime` + " . $playerinfoData1["tlw_$x"] . ",
  237. wins = `wins` + {$wins},
  238. losses = `losses` + {$losses},
  239. rndscore = {$rndscore},
  240. lastonline = " . time() . ",
  241. mode0 = `mode0` + " . $globals['mode0'] . ",
  242. mode1 = `mode1` + " . $globals['mode1'] . ",
  243. mode2 = `mode2` + " . $globals['mode2'] . ",
  244. chng = {$chng},
  245. decr = {$decr}
  246. WHERE id = " . $playerinfoData1["pID_$x"] . "
  247. ";
  248. $result = mysql_query($query);
  249. checkSQLResult ($result, $query);
  250. }
  251. function parsePlayerInfo($data) {
  252. $playerdata = array();
  253. if(count($data)>0) {
  254. // put header and data in arrays
  255. $i=0;
  256. foreach($data as $line) {
  257. if($i==3) {
  258. $H = explode(chr(9), trim($line));
  259. }
  260. if($i==4) {
  261. $D = explode(chr(9), trim($line));
  262. }
  263. $i++;
  264. }
  265. // merge header and data
  266. if(count($H)>0) {
  267. $i=0;
  268. foreach($H as $part) {
  269. if($part!="H")
  270. $playerdata[$part] = $D[$i];
  271. $i++;
  272. }
  273. }
  274. }
  275. return $playerdata;
  276. }
  277. // returns simple array of unlocks, e.g (11, 22, 55, 222)
  278. function parseUnlocks($data) {
  279. if(count($data)>0) {
  280. $i=0; $count=0;
  281. foreach($data as $line) {
  282. if($i>5 && stristr($line,"s")) {
  283. $parts = explode(chr(9), str_replace("\n","",$line));
  284. $unlocks[$count] = $parts[1];
  285. $count++;
  286. }
  287. $i++;
  288. }
  289. }
  290. return $unlocks;
  291. }
  292. function parseAwards($data) {
  293. // setup medal array
  294. $awards = array();
  295. $i=0;
  296. $first = true;
  297. foreach($data as $line) {
  298. if(substr($line,0,1)=="D") {
  299. if(!$first) {
  300. $parts = explode(chr(9),$line);
  301. $awards[$i]["id"] = $parts[1];
  302. $awards[$i]["level"] = $parts[2];
  303. $awards[$i]["when"] = $parts[3];
  304. $awards[$i]["first"] = $parts[4];
  305. $i++;
  306. } else {
  307. $first = false;
  308. }
  309. }
  310. }
  311. return $awards;
  312. }
  313. function getPageContents($url){
  314. // Try file() first
  315. if( function_exists('file') && function_exists('fopen') && ini_get('allow_url_fopen') ) {
  316. //ini_set("user_agent","Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)");
  317. ini_set("user_agent","GameSpyHTTP/1.0");
  318. $results = @file($url);
  319. }
  320. // either there was no function, or it failed -- try curl
  321. if( !($results) && (function_exists('curl_exec')) ) {
  322. $curl_handle = curl_init();
  323. curl_setopt($curl_handle, CURLOPT_URL, $url);
  324. curl_setopt($curl_handle, CURLOPT_USERAGENT, "GameSpyHTTP/1.0");
  325. curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
  326. curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 1);
  327. curl_setopt($curl_handle, CURLOPT_TIMEOUT, 10);
  328. $results = curl_exec($curl_handle);
  329. $err = curl_error($curl_handle);
  330. if( $err != '' ) {
  331. print "getData(): CURL failed: ";
  332. print "$err";
  333. return false;
  334. }
  335. $results = explode("\n",trim($results));
  336. curl_close($curl_handle);
  337. }
  338. if( !$results ) // still nothing, forgetd a'bout it
  339. return false;
  340. return $results;
  341. }
  342. ?>