Report_Runeupgrade_Page.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. require_once($path["page"] . "Page.php");
  3. require_once($path["entity"] . "Monster.php");
  4. require_once($path["entity"] . "Rune.php");
  5. /**
  6. * Upgradeable runes report page.
  7. */
  8. class Report_Runeupgrade_Page extends Page{
  9. /**
  10. * Filtrs the eport can handle.
  11. */
  12. public $filters = [
  13. "min_stars" => 5,
  14. "max_stars" => 6,
  15. "in_storage" => true,
  16. ];
  17. /**
  18. * List of available upgrades. Can be described as:
  19. * $upgrades = [
  20. * [
  21. * monster (Monster),
  22. * upgrade = [
  23. * [
  24. * rune (Rune),
  25. * alternative = [(Rune)]
  26. * ]
  27. * ]
  28. * ]
  29. * ]
  30. */
  31. public $upgrades = [];
  32. /**
  33. * Constructor.
  34. *
  35. * Retrieves the data and initializes the variables.
  36. *
  37. * @param SQLite3 $db Connection to the database.
  38. */
  39. public function __construct($db){
  40. global $path;
  41. global $root;
  42. parent::__construct($db);
  43. $this->view = $path["view"] . "report_runeupgrade.php";
  44. $this->parse_filters();
  45. $s = $this->build_query();
  46. $q = $this->db->query($s);
  47. $prev_monster = "";
  48. $prev_rune = "";
  49. $upgrade = null;
  50. $rupgrade = null;
  51. $rune_i = 0; // DEBUG
  52. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  53. if ($r["monster"] != $prev_monster){
  54. if ($prev_rune != ""){
  55. array_push($upgrade["upgrade"], $rupgrade);
  56. }
  57. if ($prev_monster != ""){
  58. array_push($this->upgrades, $upgrade);
  59. }
  60. $prev_monster = $r["monster"];
  61. $prev_rune = "";
  62. $upgrade = [
  63. "monster" => new Monster($this->db, $r["monster"]),
  64. "upgrade" => [],
  65. ];
  66. $rupgrade = [
  67. "rune" => new Rune($this->db, $r["id"]),
  68. "alternative" => [],
  69. ];
  70. }
  71. if ($r["id"] != $prev_rune){
  72. if ($prev_rune != ""){
  73. array_push($upgrade["upgrade"], $rupgrade);
  74. }
  75. $prev_rune = $r["id"];
  76. $rupgrade = [
  77. "rune" => new Rune($this->db, $r["id"]),
  78. "alternative" => [],
  79. ];
  80. }
  81. array_push($rupgrade["alternative"], new Rune($this->db, $r["alternative"]));
  82. $rune_i ++;
  83. }
  84. // Last entries
  85. array_push($upgrade["upgrade"], $rupgrade);
  86. array_push($this->upgrades, $upgrade);
  87. $this->title = "Replaceable runes - SWDB";
  88. $this->description = "Find equiped runes that can be substituted for better ones.";
  89. $this->canonical = $root . "report/runeupgrade/";
  90. }
  91. /**
  92. * Parses the filters passed in the request and sets $filters.
  93. * Filters must be in $_GET, nd the available ones are max_stars (1-6),
  94. * min_stars (1-6) and in_storage (on/off).
  95. */
  96. private function parse_filters(){
  97. if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) < 7){
  98. $this->filters["min_stars"] = $_GET["min_stars"];
  99. }
  100. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) < 7){
  101. $this->filters["max_stars"] = $_GET["max_stars"];
  102. }
  103. if (isset($_GET["in_storage"]) && $_GET["in_storage"] != "on"){
  104. $this->filters["in_storage"] = false;
  105. }
  106. return;
  107. }
  108. /**
  109. * Builds the query for the page, using the providded or default filters.
  110. *
  111. * @return The query to be executed.
  112. */
  113. private function build_query(){
  114. $s =
  115. " SELECT " .
  116. " monster.id AS monster, " .
  117. " rune.id AS id, " .
  118. " rune_alt.id AS alternative " .
  119. "FROM " .
  120. " rune, " .
  121. " rune rune_alt, " .
  122. " monster " .
  123. "WHERE " .
  124. " monster.id = rune.assigned_to AND " .
  125. " monster.stars >= " . $this->filters["min_stars"] . " AND " .
  126. " monster.stars <= " . $this->filters["max_stars"] . " AND " .
  127. " rune.assigned_to IS NOT NULL AND " .
  128. " rune_alt.assigned_to IS NULL AND " .
  129. " rune.type = rune_alt.type AND " .
  130. " rune.slot = rune_alt.slot AND " .
  131. " rune.main_stat = rune_alt.main_stat AND " .
  132. " ( " .
  133. " rune_alt.stars > rune.stars OR " .
  134. " ( " .
  135. " rune_alt.stars = rune.stars AND " .
  136. " rune_alt.original_quality > rune.original_quality" .
  137. " ) " .
  138. " ) ";
  139. if (!$this->filters["in_storage"]){
  140. $s = $s . " AND monster.in_storage = 0 ";
  141. }
  142. $s = $s .
  143. "ORDER BY " .
  144. " monster.stars DESC, " .
  145. " monster.level DESC, " .
  146. " monster.id, " .
  147. " rune.slot, " .
  148. " id, " .
  149. " rune_alt.stars, " .
  150. " rune_alt.original_quality, " .
  151. " rune_alt.main_stat_value;";
  152. return $s;
  153. }
  154. }
  155. ?>