Stats_Page.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. * Statistics page file.
  4. *
  5. * Provides a class with all the properties and methods to display the page.
  6. *
  7. * @category Page
  8. */
  9. /**
  10. * Require dependent files if not present.
  11. */
  12. require_once(PATH::PAGE . "Page.php");
  13. require_once(PATH::ENTITY . "Run.php");
  14. /**
  15. * Statistics page model.
  16. *
  17. * @category Page
  18. */
  19. class Stats_Page extends Page{
  20. /**
  21. * @var bool Indicates if there is data to show.
  22. */
  23. public $show_data = false;
  24. /**
  25. * @var Run The best run for the selected area
  26. */
  27. public $best_run = null;
  28. /**
  29. * @var int[] 0 index: total victories. 1 index: total losses.
  30. */
  31. public $win_lose = [0, 0];
  32. /**
  33. * @var int[] Item type drops.
  34. */
  35. public $drop_type = [
  36. "rune" => 0,
  37. "rune_craft" => 0,
  38. "item" => 0,
  39. "sd" => 0,
  40. "shapeshifting" => 0,
  41. "unit" => 0
  42. ];
  43. /**
  44. * @var int[] Rune drop rate by slot.
  45. */
  46. public $drop_rune_slot = [
  47. "1" => 0,
  48. "2" => 0,
  49. "3" => 0,
  50. "4" => 0,
  51. "5" => 0,
  52. "6" => 0
  53. ];
  54. /**
  55. * @var int[] Rune drop rate by stars.
  56. */
  57. public $drop_rune_stars = [
  58. "1" => 0,
  59. "2" => 0,
  60. "3" => 0,
  61. "4" => 0,
  62. "5" => 0,
  63. "6" => 0
  64. ];
  65. public $drop_rune_quality = [
  66. QUALITY_ID::NORMAL => 0,
  67. QUALITY_ID::MAGIC => 0,
  68. QUALITY_ID::RARE => 0,
  69. QUALITY_ID::HERO => 0,
  70. QUALITY_ID::LEGEND => 0
  71. ];
  72. /**
  73. * Constructor.
  74. *
  75. * Retrieves the data and initializes the variables.
  76. *
  77. * @global int Player ID.
  78. * @global resource Database connection.
  79. */
  80. public function __construct(){
  81. global $UID;
  82. global $db;
  83. $best_run = null;
  84. $this->view = PATH::VIEW . "stats.php";
  85. $this->parse_filters();
  86. // TODO DEBUG Force NB10
  87. $this->filters["AREA"] = 6001;
  88. $this->filters["AREA_TYPE"] = 2;
  89. $this->filters["STAGE"] = 10;
  90. // TODO END DEBUG
  91. // Build query modifier:
  92. $query_where = "
  93. run.uid = '$UID' AND
  94. run.area = " . $this->filters["AREA"] . "
  95. ";
  96. if ($this->filters["STAGE"] > 0){
  97. $query_where = $query_where . " AND run.stage = " . $this->filters["STAGE"] . " ";
  98. }
  99. if ($this->filters["DIFFICULTY"] > 0){
  100. $query_where = $query_where . " AND run.difficulty = " . $this->filters["DIFFICULTY"] . " ";
  101. }
  102. if (!$this->filters["INCLUDE_HELPER"]){
  103. $query_where = $query_where . " AND run.helper = 0 ";
  104. }
  105. // Best run:
  106. $s = "
  107. SELECT id
  108. FROM run
  109. WHERE
  110. $query_where
  111. ORDER BY time DESC
  112. LIMIT 1;
  113. ";
  114. $q = $db->query($s);
  115. if ($r = $q->fetchArray(SQLITE3_ASSOC)){
  116. $this->best_run = new Run($r["id"]);
  117. }
  118. // Win/lose ratio
  119. $s = "
  120. SELECT
  121. (SELECT COUNT(win) FROM run WHERE $query_where AND win = 1 ) AS win,
  122. (SELECT COUNT(win) FROM run WHERE $query_where AND win = 0) AS lose;
  123. ";
  124. $q = $db->query($s);
  125. $r = $q->fetchArray(SQLITE3_ASSOC);
  126. $this->win_lose = [$r["win"], $r["lose"]];
  127. // Drop type rate
  128. $s = "
  129. SELECT
  130. (SELECT COUNT(run) FROM run, run_drop_rune WHERE $query_where AND run.id = run_drop_rune.run AND uid = '$UID') AS rune,
  131. (SELECT COUNT(run) FROM run, run_drop_rune_craft WHERE $query_where AND run.id = run_drop_rune_craft.run AND uid = '$UID') AS rune_craft,
  132. (SELECT COUNT(run) FROM run, run_drop_item WHERE $query_where AND run.id = run_drop_item.run AND uid = '$UID') AS item,
  133. (SELECT COUNT(run) FROM run, run_drop_sd WHERE $query_where AND run.id = run_drop_sd.run AND uid = '$UID') AS sd,
  134. (SELECT COUNT(run) FROM run, run_drop_shapeshifting WHERE $query_where AND run.id = run_drop_shapeshifting.run AND uid = '$UID') AS shapeshifting,
  135. (SELECT COUNT(run) FROM run, run_drop_unit WHERE $query_where AND run.id = run_drop_unit.run AND uid = '$UID') AS unit;
  136. ";
  137. $q = $db->query($s);
  138. $r = $q->fetchArray(SQLITE3_ASSOC);
  139. $this->drop_type["rune"] = $r["rune"];
  140. $this->drop_type["rune_craft"] = $r["rune_craft"];
  141. $this->drop_type["item"] = $r["item"];
  142. $this->drop_type["sd"] = $r["sd"];
  143. $this->drop_type["shapeshifting"] = $r["shapeshifting"];
  144. $this->drop_type["unit"] = $r["unit"];
  145. // Rune drop rate by slot
  146. $s = "
  147. SELECT
  148. slot,
  149. count(run.id) AS count
  150. FROM
  151. run,
  152. run_drop_rune
  153. WHERE
  154. $query_where AND
  155. run.id = run_drop_rune.run
  156. GROUP BY slot
  157. ORDER BY slot;
  158. ";
  159. $q = $db->query($s);
  160. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  161. $this->drop_rune_slot[$r["slot"]] = $r["count"];
  162. }
  163. // Rune drop rate by slot
  164. $s = "
  165. SELECT
  166. stars,
  167. count(run.id) AS count
  168. FROM
  169. run,
  170. run_drop_rune
  171. WHERE
  172. $query_where AND
  173. run.id = run_drop_rune.run
  174. GROUP BY stars
  175. ORDER BY stars DESC;
  176. ";
  177. $q = $db->query($s);
  178. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  179. $this->drop_rune_stars[strval($r["stars"])] = $r["count"];
  180. }
  181. // Rune drop rate by quality
  182. $s = "
  183. SELECT
  184. quality,
  185. count(run.id) AS count
  186. FROM
  187. run,
  188. run_drop_rune
  189. WHERE
  190. $query_where AND
  191. run.id = run_drop_rune.run
  192. GROUP BY quality
  193. ORDER BY quality DESC;
  194. ";
  195. $q = $db->query($s);
  196. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  197. $this->drop_rune_quality[strval($r["quality"])] = $r["count"];
  198. }
  199. $this->title = "Stats - SWDB";
  200. $this->description = "Player statistics";
  201. $this->canonical = URL::BASE . "stats/";
  202. }
  203. /**
  204. * Parses the request looking for the selected filters, validates them
  205. * and adds them to the $filters array.
  206. */
  207. private function parse_filters(){
  208. $this->filters = FILTER::STATS;
  209. if (isset($_GET["area_type"]) && intval($_GET["area_type"]) > 0){
  210. $this->filters["AREA_TYPE"] = $_GET["area_type"];
  211. }
  212. if (isset($_GET["area"]) && intval($_GET["area"]) > 0){
  213. $this->filters["AREA"] = $_GET["area"];
  214. }
  215. if (isset($_GET["stage"]) && intval($_GET["stage"]) > 0){
  216. $this->filters["STAGE"] = $_GET["stage"];
  217. }
  218. if (isset($_GET["difficulty"]) && intval($_GET["difficulty"]) >= DIFFICULTY_ID::NORMAL && intval($_GET["difficulty"]) <= DIFFICULTY_ID::HELL){
  219. $this->filters["DIFFICULTY"] = $_GET["difficulty"];
  220. }
  221. if (isset($_GET["include_helper"]) && $_GET["include_helper"] == "on"){
  222. $this->filters["INCLUDE_HELPER"] = true;
  223. }
  224. else{
  225. $this->filters["INCLUDE_HELPER"] = false;
  226. }
  227. return;
  228. }
  229. }
  230. ?>