Stats_Page.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 = true;
  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. $this->view = PATH::VIEW . "stats.php";
  84. $this->title = "Stats - SWDB";
  85. $this->description = "Player statistics";
  86. $this->canonical = URL::BASE . "stats/";
  87. $this->parse_filters();
  88. // If no filters, stop
  89. if ($this->filters["AREA"] == null){
  90. $this->show_data = false;
  91. return;
  92. }
  93. // Build query modifier:
  94. $query_where = "
  95. run.uid = '$UID' AND
  96. run.area = " . $this->filters["AREA"] . "
  97. ";
  98. if ($this->filters["STAGE"] > 0){
  99. $query_where = $query_where . " AND run.stage = " . $this->filters["STAGE"] . " ";
  100. }
  101. if ($this->filters["DIFFICULTY"] > 0){
  102. $query_where = $query_where . " AND run.difficulty = " . $this->filters["DIFFICULTY"] . " ";
  103. }
  104. if (!$this->filters["INCLUDE_HELPER"]){
  105. $query_where = $query_where . " AND run.helper = 0 ";
  106. }
  107. // Best run:
  108. $s = "
  109. SELECT id
  110. FROM run
  111. WHERE
  112. $query_where
  113. ORDER BY time DESC
  114. LIMIT 1;
  115. ";
  116. $q = $db->query($s);
  117. if ($r = $q->fetchArray(SQLITE3_ASSOC)){
  118. $this->best_run = new Run($r["id"]);
  119. }
  120. // Win/lose ratio
  121. $s = "
  122. SELECT
  123. (SELECT COUNT(win) FROM run WHERE $query_where AND win = 1 ) AS win,
  124. (SELECT COUNT(win) FROM run WHERE $query_where AND win = 0) AS lose;
  125. ";
  126. $q = $db->query($s);
  127. $r = $q->fetchArray(SQLITE3_ASSOC);
  128. $this->win_lose = [$r["win"], $r["lose"]];
  129. // Drop type rate
  130. $s = "
  131. SELECT
  132. (SELECT COUNT(run) FROM run, run_drop_rune WHERE $query_where AND run.id = run_drop_rune.run AND uid = '$UID') AS rune,
  133. (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,
  134. (SELECT COUNT(run) FROM run, run_drop_item WHERE $query_where AND run.id = run_drop_item.run AND uid = '$UID') AS item,
  135. (SELECT COUNT(run) FROM run, run_drop_sd WHERE $query_where AND run.id = run_drop_sd.run AND uid = '$UID') AS sd,
  136. (SELECT COUNT(run) FROM run, run_drop_shapeshifting WHERE $query_where AND run.id = run_drop_shapeshifting.run AND uid = '$UID') AS shapeshifting,
  137. (SELECT COUNT(run) FROM run, run_drop_unit WHERE $query_where AND run.id = run_drop_unit.run AND uid = '$UID') AS unit;
  138. ";
  139. $q = $db->query($s);
  140. $r = $q->fetchArray(SQLITE3_ASSOC);
  141. $this->drop_type["rune"] = $r["rune"];
  142. $this->drop_type["rune_craft"] = $r["rune_craft"];
  143. $this->drop_type["item"] = $r["item"];
  144. $this->drop_type["sd"] = $r["sd"];
  145. $this->drop_type["shapeshifting"] = $r["shapeshifting"];
  146. $this->drop_type["unit"] = $r["unit"];
  147. // Rune drop rate by slot
  148. $s = "
  149. SELECT
  150. slot,
  151. count(run.id) AS count
  152. FROM
  153. run,
  154. run_drop_rune
  155. WHERE
  156. $query_where AND
  157. run.id = run_drop_rune.run
  158. GROUP BY slot
  159. ORDER BY slot;
  160. ";
  161. $q = $db->query($s);
  162. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  163. $this->drop_rune_slot[$r["slot"]] = $r["count"];
  164. }
  165. // Rune drop rate by slot
  166. $s = "
  167. SELECT
  168. stars,
  169. count(run.id) AS count
  170. FROM
  171. run,
  172. run_drop_rune
  173. WHERE
  174. $query_where AND
  175. run.id = run_drop_rune.run
  176. GROUP BY stars
  177. ORDER BY stars DESC;
  178. ";
  179. $q = $db->query($s);
  180. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  181. $this->drop_rune_stars[strval($r["stars"])] = $r["count"];
  182. }
  183. // Rune drop rate by quality
  184. $s = "
  185. SELECT
  186. quality,
  187. count(run.id) AS count
  188. FROM
  189. run,
  190. run_drop_rune
  191. WHERE
  192. $query_where AND
  193. run.id = run_drop_rune.run
  194. GROUP BY quality
  195. ORDER BY quality DESC;
  196. ";
  197. $q = $db->query($s);
  198. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  199. $this->drop_rune_quality[strval($r["quality"])] = $r["count"];
  200. }
  201. }
  202. /**
  203. * Parses the request looking for the selected filters, validates them
  204. * and adds them to the $filters array.
  205. */
  206. private function parse_filters(){
  207. $this->filters = FILTER::STATS;
  208. if (isset($_GET["area_type"]) && intval($_GET["area_type"]) > 0){
  209. $this->filters["AREA_TYPE"] = $_GET["area_type"];
  210. }
  211. if (isset($_GET["area"]) && intval($_GET["area"]) > 0){
  212. $this->filters["AREA"] = $_GET["area"];
  213. }
  214. if (isset($_GET["stage"]) && intval($_GET["stage"]) > 0){
  215. $this->filters["STAGE"] = $_GET["stage"];
  216. }
  217. if (isset($_GET["difficulty"]) && intval($_GET["difficulty"]) >= DIFFICULTY_ID::NORMAL && intval($_GET["difficulty"]) <= DIFFICULTY_ID::HELL){
  218. $this->filters["DIFFICULTY"] = $_GET["difficulty"];
  219. }
  220. if (isset($_GET["include_helper"]) && $_GET["include_helper"] == "on"){
  221. $this->filters["INCLUDE_HELPER"] = true;
  222. }
  223. else{
  224. $this->filters["INCLUDE_HELPER"] = false;
  225. }
  226. return;
  227. }
  228. }
  229. ?>