Stats_Page.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /**
  3. * Statistics page file.
  4. *
  5. * Provides a class with all the properties and methods to display the page.
  6. *
  7. * @author Iñigo Valentin <i@inigovalentin.com>
  8. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
  9. * @package SWDB
  10. * @todo Not workig, redo.
  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. * @var mixed[] Page filters
  74. */
  75. public $filters = [
  76. "AREA_TYPE" => null,
  77. "AREA" => null,
  78. "STAGE" => null,
  79. "DIFFICULTY" => null,
  80. "INCLUDE_HELPER" => false
  81. ];
  82. /**
  83. * Constructor.
  84. *
  85. * Retrieves the data and initializes the variables.
  86. */
  87. public function __construct(){
  88. $this->view = PATH::VIEW . "stats.php";
  89. $this->title = "Stats - SWDB";
  90. $this->description = "Player statistics";
  91. $this->canonical = URL::BASE . "stats/";
  92. $this->parse_filters();
  93. // If no filters, stop
  94. if ($this->filters["AREA"] == null){
  95. $this->show_data = false;
  96. return;
  97. }
  98. // Build query modifier:
  99. $query_where = "
  100. run.player = '" . get_context()->get_player()->get_id() . "' AND
  101. run.area = " . $this->filters["AREA"] . "
  102. ";
  103. if ($this->filters["STAGE"] > 0){
  104. $query_where = $query_where . " AND run.stage = " . $this->filters["STAGE"] . " ";
  105. }
  106. if ($this->filters["DIFFICULTY"] > 0){
  107. $query_where = $query_where . " AND run.difficulty = " . $this->filters["DIFFICULTY"] . " ";
  108. }
  109. if (!$this->filters["INCLUDE_HELPER"]){
  110. $query_where = $query_where . " AND run.helper = 0 ";
  111. }
  112. // Best run:
  113. $s = "
  114. SELECT id
  115. FROM run
  116. WHERE
  117. $query_where
  118. ORDER BY time DESC
  119. LIMIT 1;
  120. ";
  121. $q = get_context()->get_db()->query($s);
  122. if ($r = $q->fetchArray(SQLITE3_ASSOC)){
  123. $this->best_run = new Run($r["id"]);
  124. }
  125. // Win/lose ratio
  126. $s = "
  127. SELECT
  128. (SELECT COUNT(win) FROM run WHERE $query_where AND win = 1 ) AS win,
  129. (SELECT COUNT(win) FROM run WHERE $query_where AND win = 0) AS lose;
  130. ";
  131. $q = get_context()->get_db()->query($s);
  132. $r = $q->fetchArray(SQLITE3_ASSOC);
  133. $this->win_lose = [$r["win"], $r["lose"]];
  134. // Drop type rate
  135. $s = "
  136. SELECT
  137. (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,
  138. (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,
  139. (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,
  140. (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,
  141. (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,
  142. (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;
  143. ";
  144. $q = get_context()->get_db()->query($s);
  145. $r = $q->fetchArray(SQLITE3_ASSOC);
  146. $this->drop_type["rune"] = $r["rune"];
  147. $this->drop_type["rune_craft"] = $r["rune_craft"];
  148. $this->drop_type["item"] = $r["item"];
  149. $this->drop_type["sd"] = $r["sd"];
  150. $this->drop_type["shapeshifting"] = $r["shapeshifting"];
  151. $this->drop_type["unit"] = $r["unit"];
  152. // Rune drop rate by slot
  153. $s = "
  154. SELECT
  155. slot,
  156. count(run.id) AS count
  157. FROM
  158. run run,
  159. run_drop_rune run_drop_rune
  160. WHERE
  161. $query_where AND
  162. run.id = run_drop_rune.run
  163. GROUP BY slot
  164. ORDER BY slot;
  165. ";
  166. $q = get_context()->get_db()->query($s);
  167. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  168. $this->drop_rune_slot[$r["slot"]] = $r["count"];
  169. }
  170. // Rune drop rate by slot
  171. $s = "
  172. SELECT
  173. stars,
  174. count(run.id) AS count
  175. FROM
  176. run run,
  177. run_drop_rune run_drop_rune
  178. WHERE
  179. $query_where AND
  180. run.id = run_drop_rune.run
  181. GROUP BY stars
  182. ORDER BY stars DESC;
  183. ";
  184. $q = get_context()->get_db()->query($s);
  185. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  186. $this->drop_rune_stars[strval($r["stars"])] = $r["count"];
  187. }
  188. // Rune drop rate by quality
  189. $s = "
  190. SELECT
  191. quality,
  192. count(run.id) AS count
  193. FROM
  194. run run,
  195. run_drop_rune run_drop_rune
  196. WHERE
  197. $query_where AND
  198. run.id = run_drop_rune.run
  199. GROUP BY quality
  200. ORDER BY quality DESC;
  201. ";
  202. $q = get_context()->get_db()->query($s);
  203. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  204. $this->drop_rune_quality[strval($r["quality"])] = $r["count"];
  205. }
  206. }
  207. /**
  208. * Parses the request looking for the selected filters, validates them
  209. * and adds them to the $filters array.
  210. */
  211. private function parse_filters(){
  212. if (isset($_GET["area_type"]) && intval($_GET["area_type"]) > 0){
  213. $this->filters["AREA_TYPE"] = $_GET["area_type"];
  214. }
  215. if (isset($_GET["area"]) && intval($_GET["area"]) > 0){
  216. $this->filters["AREA"] = $_GET["area"];
  217. }
  218. if (isset($_GET["stage"]) && intval($_GET["stage"]) > 0){
  219. $this->filters["STAGE"] = $_GET["stage"];
  220. }
  221. if (isset($_GET["difficulty"]) && intval($_GET["difficulty"]) >= DIFFICULTY_ID::NORMAL && intval($_GET["difficulty"]) <= DIFFICULTY_ID::HELL){
  222. $this->filters["DIFFICULTY"] = $_GET["difficulty"];
  223. }
  224. if (isset($_GET["include_helper"]) && $_GET["include_helper"] == "on"){
  225. $this->filters["INCLUDE_HELPER"] = true;
  226. }
  227. else{
  228. $this->filters["INCLUDE_HELPER"] = false;
  229. }
  230. return;
  231. }
  232. }