PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/html/import/import_weapons.php

https://github.com/graywh/utstats
PHP | 249 lines | 207 code | 26 blank | 16 comment | 21 complexity | dadbf1145ca8bf14321d77094d41d2d4 MD5 | raw file
  1. <?php
  2. // Read all available weapons, we'll need them later
  3. if (!isset($weaponnames))
  4. {
  5. $sql_weaponnames = "SELECT id, name FROM uts_weapons";
  6. $q_weaponnames = mysql_query($sql_weaponnames);
  7. while ($r_weaponnames = mysql_fetch_array($q_weaponnames))
  8. {
  9. $weaponnames[$r_weaponnames['name']] = $r_weaponnames['id'];
  10. }
  11. }
  12. // Get all kills by weapon and player
  13. $sql_weapons = " SELECT col2 AS player,
  14. col3 AS weaponname,
  15. COUNT(*) AS kills
  16. FROM uts_temp_$uid
  17. WHERE col1 = 'kill'
  18. OR col1 = 'teamkill'
  19. GROUP BY col3, col2";
  20. $q_weapons = mysql_query($sql_weapons) or die(mysql_error());
  21. $weapons = array();
  22. while ($r_weapons = mysql_fetch_array($q_weapons))
  23. {
  24. // Get the wepon's id or assign a new one
  25. if (empty($r_weapons['weaponname']))
  26. continue;
  27. if (isset($weaponnames[$r_weapons['weaponname']]))
  28. {
  29. $weaponid = $weaponnames[$r_weapons['weaponname']];
  30. }
  31. else
  32. {
  33. mysql_query("INSERT INTO uts_weapons SET name = '". addslashes($r_weapons['weaponname']) ."'") or die(mysql_error());
  34. $weaponid = mysql_insert_id();
  35. $weaponnames[$r_weapons['weaponname']] = $weaponid;
  36. }
  37. // Get the unique pid of this player
  38. if (!isset($playerid2pid[$r_weapons['player']]))
  39. {
  40. continue;
  41. }
  42. else
  43. {
  44. $pid = $playerid2pid[$r_weapons['player']];
  45. }
  46. $weapons[$pid][$weaponid] = array(
  47. 'weap_kills' => $r_weapons['kills'],
  48. 'weap_shotcount' => 0,
  49. 'weap_hitcount' => 0,
  50. 'weap_damagegiven' => 0,
  51. 'weap_accuracy' => 0
  52. );
  53. }
  54. // Get the weapon statistics
  55. $sql_weapons = " SELECT col1 AS type,
  56. col2 AS weaponname,
  57. col3 AS player,
  58. col4 AS value
  59. FROM uts_temp_$uid
  60. WHERE col1 LIKE 'weap_%'";
  61. $q_weapons = mysql_query($sql_weapons) or die(mysql_error());
  62. while ($r_weapons = mysql_fetch_array($q_weapons))
  63. {
  64. // Get the wepon's id or assign a new one
  65. if (empty($r_weapons['weaponname'])) continue;
  66. if (isset($weaponnames[$r_weapons['weaponname']]))
  67. {
  68. $weaponid = $weaponnames[$r_weapons['weaponname']];
  69. }
  70. else
  71. {
  72. mysql_query("INSERT INTO uts_weapons SET name = '". addslashes($r_weapons['weaponname']) ."'") or die(mysql_error());
  73. $weaponid = mysql_insert_id();
  74. $weaponnames[$r_weapons['weaponname']] = $weaponid;
  75. }
  76. // Get the unique pid of this player
  77. if (!isset($playerid2pid[$r_weapons['player']]))
  78. {
  79. // Happens if we're ignoring bots or banned players
  80. continue;
  81. }
  82. else
  83. {
  84. $pid = $playerid2pid[$r_weapons['player']];
  85. }
  86. if (!isset($weapons[$pid][$weaponid]['weap_kills']))
  87. {
  88. $weapons[$pid][$weaponid] = array(
  89. 'weap_kills' => 0,
  90. 'weap_shotcount' => 0,
  91. 'weap_hitcount' => 0,
  92. 'weap_damagegiven' => 0,
  93. 'weap_accuracy' => 0
  94. );
  95. }
  96. $weapons[$pid][$weaponid][$r_weapons['type']] = $r_weapons['value'];
  97. }
  98. // Write the weapon statistics for this match
  99. $s_weapons = array();
  100. foreach($weapons as $playerid => $weapon) // For each player
  101. {
  102. foreach($weapon as $weaponid => $infos) // For each weapon
  103. {
  104. if ($infos['weap_kills'] == 0 and $infos['weap_shotcount'] == 0 and $infos['damagegiven'] == 0 and $infos['hitcount'] == 0)
  105. continue;
  106. if ($infos['weap_shotcount'] > 0 and $infos['weap_hitcount'] > 0)
  107. {
  108. $infos['weap_accuracy'] = round(100 * $infos['weap_hitcount'] / $infos['weap_shotcount'], 2);
  109. if ($infos['weap_accuracy'] > 100.0)
  110. $infos['weap_accuracy'] = 100;
  111. }
  112. else
  113. {
  114. $infos['weap_accuracy'] = 0;
  115. }
  116. mysql_query(" INSERT uts_weaponstats
  117. SET matchid = '$matchid',
  118. pid = '$playerid',
  119. weapon = '$weaponid',
  120. kills = '${infos['weap_kills']}',
  121. shots = '${infos['weap_shotcount']}',
  122. hits = '${infos['weap_hitcount']}',
  123. damage = '${infos['weap_damagegiven']}',
  124. acc = '${infos['weap_accuracy']}';"
  125. ) or die(mysql_error());
  126. // Update the player's weapon statistics (matchid 0)
  127. mysql_query(" REPLACE uts_weaponstats
  128. SELECT 0 AS matchid,
  129. '$playerid' AS pid,
  130. '$weaponid' AS weapon,
  131. SUM(kills) AS kills,
  132. SUM(shots) AS shots,
  133. SUM(hits) AS hits,
  134. SUM(damage) AS damage,
  135. LEAST(ROUND(10000*SUM(hits)/SUM(shots))/100, 100) AS acc
  136. FROM uts_weaponstats
  137. WHERE pid = '$playerid'
  138. AND weapon = '$weaponid'
  139. AND matchid > 0
  140. GROUP BY pid, weapon;"
  141. ) or die(mysql_error());
  142. }
  143. // Create the player's match statistics (weapon 0)
  144. mysql_query(" REPLACE uts_weaponstats
  145. SELECT '$matchid' AS matchid,
  146. '$playerid' AS pid,
  147. 0 AS weapon,
  148. SUM(kills) AS kills,
  149. SUM(shots) AS shots,
  150. SUM(hits) AS hits,
  151. SUM(damage) AS damage,
  152. LEAST(ROUND(10000*SUM(hits)/SUM(shots))/100, 100) AS acc
  153. FROM uts_weaponstats
  154. WHERE matchid = '$matchid'
  155. AND pid = '$playerid'
  156. AND weapon > 0
  157. GROUP BY matchid, pid;"
  158. ) or die(mysql_error());
  159. // Update the player's match entry in uts_player
  160. mysql_query(" UPDATE uts_player AS p,
  161. uts_weaponstats AS w
  162. SET p.accuracy = w.acc
  163. WHERE p.matchid = '$matchid'
  164. AND w.matchid = p.matchid
  165. AND p.pid = '$playerid'
  166. AND w.pid = p.pid
  167. AND w.weapon = 0;"
  168. ) or die(mysql_error());
  169. // Update the player's career statistics (weapon 0, match 0)
  170. mysql_query(" REPLACE uts_weaponstats
  171. SELECT 0 AS matchid,
  172. '$playerid' AS pid,
  173. 0 AS weapon,
  174. SUM(kills) AS kills,
  175. SUM(shots) AS shots,
  176. SUM(hits) AS hits,
  177. SUM(damage) AS damage,
  178. LEAST(ROUND(10000*SUM(hits)/SUM(shots))/100, 100) AS acc
  179. FROM uts_weaponstats
  180. WHERE matchid > 0
  181. AND pid = '$playerid'
  182. AND weapon > 0
  183. GROUP BY pid;"
  184. ) or die(mysql_error());
  185. }
  186. // Update the global weapon statistics (matchid 0, playerid 0)
  187. foreach($s_weapons as $weaponid => $infos) // For each weapon
  188. {
  189. if ($infos['weap_kills'] == 0 and $infos['weap_shotcount'] == 0)
  190. continue;
  191. // Update the weapon's global record (pid 0, matchid 0)
  192. mysql_query(" REPLACE uts_weaponstats
  193. SELECT 0 AS matchid,
  194. 0 AS pid,
  195. '$weaponid' AS weapon,
  196. SUM(kills) AS kills,
  197. SUM(shots) AS shots,
  198. SUM(hits) AS hits,
  199. SUM(damage) AS damage,
  200. LEAST(ROUND(10000*SUM(hits)/SUM(shots))/100, 100) AS acc
  201. FROM uts_weaponstats
  202. WHERE matchid > 0
  203. AND pid > 0
  204. AND weapon = '$weaponid'
  205. GROUP BY weapon;"
  206. ) or die(mysql_error());
  207. }
  208. // Update the global statistics (matchid 0, playerid 0, weaponid 0)
  209. mysql_query(" REPLACE uts_weaponstats
  210. SELECT 0 as matchid,
  211. 0 as pid,
  212. 0 as weapon,
  213. SUM(kills) as kills,
  214. SUM(shots) as shots,
  215. SUM(hits) as hits,
  216. SUM(damage) as damage,
  217. LEAST(ROUND(10000*SUM(hits)/SUM(shots))/100, 100) as acc
  218. FROM uts_weaponstats
  219. WHERE matchid > 0
  220. AND pid > 0
  221. AND weapon > 0;"
  222. ) or die(mysql_error());
  223. ?>