Stats_Page.php 8.4 KB

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