Enchantments_Page.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * Enchantment list 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 . "Enchantment.php");
  14. /**
  15. * Enchantment list page model.
  16. *
  17. * @category Page
  18. */
  19. class Enchantments_Page extends Page{
  20. /**
  21. * @var Enchantment[] Owned enchantment gems.
  22. */
  23. public $gems = [];
  24. /**
  25. * @var Enchantment[] Owned enchantment grindstones.
  26. */
  27. public $grindstones = [];
  28. /**
  29. * Constructor.
  30. *
  31. * Retrieves the data and initializes the variables.
  32. */
  33. public function __construct(){
  34. $this->view = PATH::VIEW . "enchantments.php";
  35. $this->parse_filters();
  36. $s = $this->build_query();
  37. $q = get_context()->get_db()->query($s);
  38. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  39. $enchantment = new Enchantment($r["id"]);
  40. //if ($enchantment->gem == true){
  41. // array_push($this->gems, $enchantment);
  42. //}
  43. //else{
  44. array_push($this->grindstones, $enchantment);
  45. //}
  46. }
  47. $this->title = "Enchantments - SWDB";
  48. $this->description = "Enchantment inventory";
  49. $this->canonical = URL::BASE . "enchantments/";
  50. }
  51. /**
  52. * Parses the request looking for the selected filters, validates them
  53. * and adds them to the $filters array.
  54. */
  55. private function parse_filters(){
  56. $this->filters = FILTER::ENCHANTMENT;
  57. if (isset($_GET["min_quality"]) && intval($_GET["min_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["min_quality"]) <= QUALITY_ID::LEGEND){
  58. $this->filters["MIN_QUALITY"] = $_GET["min_quality"];
  59. }
  60. if (isset($_GET["max_quality"]) && intval($_GET["max_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["max_quality"]) <= QUALITY_ID::LEGEND){
  61. $this->filters["MAX_QUALITY"] = $_GET["max_quality"];
  62. }
  63. if (isset($_GET["stat"])){
  64. $stats = $_GET["stat"];
  65. foreach ($stats as $stat){
  66. array_push($this->filters["STAT"], intval($stat));
  67. }
  68. }
  69. if (isset($_GET["rune"])){
  70. $runes = $_GET["rune"];
  71. foreach ($runes as $rune){
  72. array_push($this->filters["RUNE"], intval($rune));
  73. }
  74. }
  75. if (isset($_GET["type"])){
  76. $types = $_GET["type"];
  77. foreach ($types as $type){
  78. array_push($this->filters["TYPE"], $type);
  79. }
  80. }
  81. // TODO
  82. /*
  83. if (isset($_GET["archetype"])){
  84. $archetypes = $_GET["archetype"];
  85. foreach ($archetypes as $archetype){
  86. array_push($this->filters["ARCHETYPE"], intval($archetype));
  87. }
  88. }
  89. if (isset($_GET["min_level"]) && intval($_GET["min_level"]) >= 0 && intval($_GET["min_level"]) <= 15){
  90. $this->filters["MIN_LEVEL"] = $_GET["min_level"];
  91. }
  92. if (isset($_GET["max_level"]) && intval($_GET["max_level"]) >= 0 && intval($_GET["max_level"]) <= 15){
  93. $this->filters["MAX_LEVEL"] = $_GET["max_level"];
  94. }
  95. if (isset($_GET["min_original_quality"]) && intval($_GET["min_original_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["min_original_quality"]) <= QUALITY_ID::LEGEND){
  96. $this->filters["MIN_ORIGINAL_QUALITY"] = $_GET["min_original_quality"];
  97. }
  98. if (isset($_GET["max_original_quality"]) && intval($_GET["max_original_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["max_original_quality"]) <= QUALITY_ID::LEGEND){
  99. $this->filters["MAX_ORIGINAL_QUALITY"] = $_GET["max_original_quality"];
  100. }
  101. if (isset($_GET["assigned"]) && intval($_GET["assigned"]) >= 0 && intval($_GET["assigned"]) <= 4){
  102. $this->filters["ASSIGNED"] = $_GET["assigned"];
  103. }*/
  104. }
  105. /**
  106. * Builds the query to the artifact table using the selected or default
  107. * filters.
  108. *
  109. * @return string The query to be executed.
  110. */
  111. private function build_query(){
  112. $s = "
  113. SELECT id
  114. FROM enchantment
  115. WHERE
  116. player = '" . get_context()->get_player()->get_id() . "' AND
  117. quality >= " . $this->filters["MIN_QUALITY"] . " AND
  118. quality <= " . $this->filters["MAX_QUALITY"] . "
  119. ";
  120. if (count($this->filters["STAT"]) > 0){
  121. $s = $s . " AND stat IN ( ";
  122. foreach($this->filters["STAT"] as $t){
  123. $s = $s . "$t, ";
  124. }
  125. $s = $s . "-1) ";
  126. }
  127. if (count($this->filters["RUNE"]) > 0){
  128. $s = $s . " AND (rune IN ( ";
  129. foreach($this->filters["RUNE"] as $t){
  130. $s = $s . "'$t', ";
  131. }
  132. $s = $s . "'DUMMY0') ";
  133. if (in_array('0', $this->filters["RUNE"])){
  134. // Include inmemorials
  135. $s .= " OR type in (3, 4)) ";
  136. }
  137. else{
  138. $s .= ") ";
  139. }
  140. }
  141. if (count($this->filters["TYPE"]) == 1){
  142. // Only one selectd, it's either gems or grindstones
  143. if (in_array("GEM", $this->filters["TYPE"])){
  144. $s .= " AND type IN (1, 3, 5) "; // Gems.
  145. }
  146. else{
  147. $s .= " AND type IN (2, 4, 6) "; // Grindstones.
  148. }
  149. }
  150. $s = $s . "
  151. ORDER BY
  152. type = 3 DESC, -- Inmemorial Grindstone
  153. type = 4 DESC, -- Inmemorial Gem
  154. rune,
  155. type,
  156. quality DESC,
  157. stat
  158. ";
  159. /*if (count($this->filters["MAIN"]) > 0){
  160. $s = $s . " AND main_stat IN ( ";
  161. foreach($this->filters["MAIN"] as $t){
  162. $s = $s . "$t, ";
  163. }
  164. $s = $s . "-1) ";
  165. }
  166. if (count($this->filters["ELEMENT"]) == 0 && count($this->filters["ARCHETYPE"]) == 0){
  167. // No element/archetype conditions
  168. }
  169. elseif (count($this->filters["ELEMENT"]) > 0 && count($this->filters["ARCHETYPE"]) == 0){
  170. // Only selected elements
  171. $s = $s . " AND attribute IN ( ";
  172. foreach($this->filters["ELEMENT"] as $t){
  173. $s = $s . "$t, ";
  174. }
  175. $s = $s . "-1) ";
  176. }
  177. elseif (count($this->filters["ELEMENT"]) == 0 && count($this->filters["ARCHETYPE"]) > 0){
  178. // Only selected types
  179. $s = $s . " AND archetype IN ( ";
  180. foreach($this->filters["ARCHETYPE"] as $t){
  181. $s = $s . "$t, ";
  182. }
  183. $s = $s . "-1) ";
  184. }
  185. elseif (count($this->filters["ELEMENT"]) > 0 && count($this->filters["ARCHETYPE"]) > 0){
  186. // Selected elements and selected types
  187. $s = $s . " AND (archetype IN ( ";
  188. foreach($this->filters["ARCHETYPE"] as $t){
  189. $s = $s . "$t, ";
  190. }
  191. $s = $s . "-1) OR attribute IN ( ";
  192. foreach($this->filters["ELEMENT"] as $t){
  193. $s = $s . "$t, ";
  194. }
  195. $s = $s . "-1)) ";
  196. }
  197. switch ($this->filters["ASSIGNED"]){
  198. case 1:
  199. $s = $s . " AND assigned_to IS NOT NULL ";
  200. break;
  201. case 2:
  202. $s = $s . " AND assigned_to IS NULL ";
  203. break;
  204. case 3:
  205. $s = $s . " AND (assigned_to IS NULL OR assigned_to IN (SELECT DISTINCT id FROM unit WHERE building <> " . BUILDING_ID::MONSTER_STORAGE . ")) ";
  206. break;
  207. case 4:
  208. $s = $s . " AND assigned_to IN (SELECT DISTINCT id FROM unit WHERE building <> " . BUILDING_ID::MONSTER_STORAGE . ") ";
  209. break;
  210. }
  211. $s = $s . " ORDER BY type, attribute, archetype, level DESC, original_quality DESC;";
  212. */
  213. return $s;
  214. }
  215. }
  216. ?>