Stats_Page.php 8.2 KB

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