Enchantments_Page.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 . "Custom_Filter.php");
  14. require_once(PATH::ENTITY . "Rune_Craft.php");
  15. /**
  16. * Enchantment list page model.
  17. *
  18. * @category Page
  19. */
  20. class Enchantments_Page extends Page{
  21. /**
  22. * @var \Rune_Craft[] Owned enchantment gems.
  23. */
  24. public $gems = [];
  25. /**
  26. * @var \Rune_Craft[] Owned enchantment grindstones.
  27. */
  28. public $grindstones = [];
  29. /**
  30. * @var \Filter[] Custom filters for this page.
  31. */
  32. public $custom_filters = [];
  33. /**
  34. * Constructor.
  35. *
  36. * Retrieves the data and initializes the variables.
  37. *
  38. * @global int Player ID.
  39. * @global resource Database connection.
  40. */
  41. public function __construct(){
  42. global $UID;
  43. global $db;
  44. $this->view = PATH::VIEW . "enchantments.php";
  45. $this->parse_filters();
  46. if (!isset($_GET["custom_filter"])){
  47. $s = $this->build_query();
  48. }
  49. else{
  50. $s = $this->build_custom_query($_GET["custom_filter"]);
  51. }
  52. $q = $db->query($s);
  53. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  54. $enchantment = new Rune_Craft($r["id"]);
  55. //if ($enchantment->gem == true){
  56. // array_push($this->gems, $enchantment);
  57. //}
  58. //else{
  59. array_push($this->grindstones, $enchantment);
  60. //}
  61. }
  62. $s = "
  63. SELECT id
  64. FROM filter
  65. WHERE
  66. uid = '$UID' AND
  67. page = 'ENCHANTMENTS';
  68. ";
  69. $q = $db->query($s);
  70. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  71. array_push($this->custom_filters, new Custom_Filter($r["id"]));
  72. }
  73. $this->title = "Enchantments - SWDB";
  74. $this->description = "Enchantment inventory";
  75. $this->canonical = URL::BASE . "enchantments/";
  76. }
  77. /**
  78. * Parses the request looking for the selected filters, validates them
  79. * and adds them to the $filters array.
  80. */
  81. private function parse_filters(){
  82. $this->filters = FILTER::ENCHANTMENT;
  83. if (isset($_GET["min_quality"]) && intval($_GET["min_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["min_quality"]) <= QUALITY_ID::LEGEND){
  84. $this->filters["MIN_QUALITY"] = $_GET["min_quality"];
  85. }
  86. if (isset($_GET["max_quality"]) && intval($_GET["max_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["max_quality"]) <= QUALITY_ID::LEGEND){
  87. $this->filters["MAX_QUALITY"] = $_GET["max_quality"];
  88. }
  89. if (isset($_GET["stat"])){
  90. $stats = $_GET["stat"];
  91. foreach ($stats as $stat){
  92. array_push($this->filters["STAT"], intval($stat));
  93. }
  94. }
  95. if (isset($_GET["rune"])){
  96. $runes = $_GET["rune"];
  97. foreach ($runes as $rune){
  98. array_push($this->filters["RUNE"], intval($rune));
  99. }
  100. }
  101. if (isset($_GET["type"])){
  102. $types = $_GET["type"];
  103. foreach ($types as $type){
  104. array_push($this->filters["TYPE"], $type);
  105. }
  106. }
  107. // TODO
  108. /*
  109. if (isset($_GET["archetype"])){
  110. $archetypes = $_GET["archetype"];
  111. foreach ($archetypes as $archetype){
  112. array_push($this->filters["ARCHETYPE"], intval($archetype));
  113. }
  114. }
  115. if (isset($_GET["min_level"]) && intval($_GET["min_level"]) >= 0 && intval($_GET["min_level"]) <= 15){
  116. $this->filters["MIN_LEVEL"] = $_GET["min_level"];
  117. }
  118. if (isset($_GET["max_level"]) && intval($_GET["max_level"]) >= 0 && intval($_GET["max_level"]) <= 15){
  119. $this->filters["MAX_LEVEL"] = $_GET["max_level"];
  120. }
  121. if (isset($_GET["min_original_quality"]) && intval($_GET["min_original_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["min_original_quality"]) <= QUALITY_ID::LEGEND){
  122. $this->filters["MIN_ORIGINAL_QUALITY"] = $_GET["min_original_quality"];
  123. }
  124. if (isset($_GET["max_original_quality"]) && intval($_GET["max_original_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["max_original_quality"]) <= QUALITY_ID::LEGEND){
  125. $this->filters["MAX_ORIGINAL_QUALITY"] = $_GET["max_original_quality"];
  126. }
  127. if (isset($_GET["assigned"]) && intval($_GET["assigned"]) >= 0 && intval($_GET["assigned"]) <= 4){
  128. $this->filters["ASSIGNED"] = $_GET["assigned"];
  129. }*/
  130. }
  131. /**
  132. * Builds the query to the artifact table using the a custom filter.
  133. *
  134. * @param int $filter Custom filter ID.
  135. * @return string The query to be executed.
  136. * @global int Player ID.
  137. * @global resource Database connection.
  138. */
  139. private function build_custom_query($filter){
  140. global $UID;
  141. global $db;
  142. $s = "
  143. SELECT condition
  144. FROM filter
  145. WHERE
  146. uid = '$UID' AND
  147. id = $filter;
  148. ";
  149. $q = $db->query($s);
  150. $r = $q->fetchArray(SQLITE3_ASSOC);
  151. $cond = $r["condition"];
  152. $s = "
  153. SELECT
  154. id
  155. FROM rune_craft
  156. WHERE uid = '$UID' AND (
  157. $cond
  158. )
  159. ORDER BY type, rune DESC, quality DESC;
  160. ";
  161. return $s;
  162. }
  163. /**
  164. * Builds the query to the artifact table using the selected or default
  165. * filters.
  166. *
  167. * @return string The query to be executed.
  168. * @global int Player ID.
  169. */
  170. private function build_query(){
  171. global $UID;
  172. $s = "
  173. SELECT id
  174. FROM rune_craft
  175. WHERE
  176. uid = '$UID' AND
  177. quality >= " . $this->filters["MIN_QUALITY"] . " AND
  178. quality <= " . $this->filters["MAX_QUALITY"] . "
  179. ";
  180. if (count($this->filters["STAT"]) > 0){
  181. $s = $s . " AND stat IN ( ";
  182. foreach($this->filters["STAT"] as $t){
  183. $s = $s . "$t, ";
  184. }
  185. $s = $s . "-1) ";
  186. }
  187. if (count($this->filters["RUNE"]) > 0){
  188. $s = $s . " AND (rune IN ( ";
  189. foreach($this->filters["RUNE"] as $t){
  190. $s = $s . "'$t', ";
  191. }
  192. $s = $s . "'DUMMY0') ";
  193. if (in_array('0', $this->filters["RUNE"])){
  194. // Include inmemorials
  195. $s .= " OR type in (3, 4)) ";
  196. }
  197. else{
  198. $s .= ") ";
  199. }
  200. }
  201. error_log( print_r($this->filters["TYPE"], TRUE) );
  202. if (count($this->filters["TYPE"]) == 1){
  203. // Only one selectd, it's either gems or grindstones
  204. if (in_array("GEM", $this->filters["TYPE"])){
  205. $s .= " AND type IN (1, 3, 5) "; // Gems.
  206. }
  207. else{
  208. $s .= " AND type IN (2, 4, 6) "; // Grindstones.
  209. }
  210. }
  211. $s = $s . "
  212. ORDER BY
  213. type = 3 DESC, -- Inmemorial Grindstone
  214. type = 4 DESC, -- Inmemorial Gem
  215. rune,
  216. type,
  217. quality DESC,
  218. stat
  219. ";
  220. /*if (count($this->filters["MAIN"]) > 0){
  221. $s = $s . " AND main_stat IN ( ";
  222. foreach($this->filters["MAIN"] as $t){
  223. $s = $s . "$t, ";
  224. }
  225. $s = $s . "-1) ";
  226. }
  227. if (count($this->filters["ELEMENT"]) == 0 && count($this->filters["ARCHETYPE"]) == 0){
  228. // No element/archetype conditions
  229. }
  230. elseif (count($this->filters["ELEMENT"]) > 0 && count($this->filters["ARCHETYPE"]) == 0){
  231. // Only selected elements
  232. $s = $s . " AND attribute IN ( ";
  233. foreach($this->filters["ELEMENT"] as $t){
  234. $s = $s . "$t, ";
  235. }
  236. $s = $s . "-1) ";
  237. }
  238. elseif (count($this->filters["ELEMENT"]) == 0 && count($this->filters["ARCHETYPE"]) > 0){
  239. // Only selected types
  240. $s = $s . " AND archetype IN ( ";
  241. foreach($this->filters["ARCHETYPE"] as $t){
  242. $s = $s . "$t, ";
  243. }
  244. $s = $s . "-1) ";
  245. }
  246. elseif (count($this->filters["ELEMENT"]) > 0 && count($this->filters["ARCHETYPE"]) > 0){
  247. // Selected elements and selected types
  248. $s = $s . " AND (archetype IN ( ";
  249. foreach($this->filters["ARCHETYPE"] as $t){
  250. $s = $s . "$t, ";
  251. }
  252. $s = $s . "-1) OR attribute IN ( ";
  253. foreach($this->filters["ELEMENT"] as $t){
  254. $s = $s . "$t, ";
  255. }
  256. $s = $s . "-1)) ";
  257. }
  258. switch ($this->filters["ASSIGNED"]){
  259. case 1:
  260. $s = $s . " AND assigned_to IS NOT NULL ";
  261. break;
  262. case 2:
  263. $s = $s . " AND assigned_to IS NULL ";
  264. break;
  265. case 3:
  266. $s = $s . " AND (assigned_to IS NULL OR assigned_to IN (SELECT DISTINCT id FROM unit WHERE building <> " . BUILDING_ID::MONSTER_STORAGE . ")) ";
  267. break;
  268. case 4:
  269. $s = $s . " AND assigned_to IN (SELECT DISTINCT id FROM unit WHERE building <> " . BUILDING_ID::MONSTER_STORAGE . ") ";
  270. break;
  271. }
  272. $s = $s . " ORDER BY type, attribute, archetype, level DESC, original_quality DESC;";
  273. */
  274. error_log($s);
  275. return $s;
  276. }
  277. }
  278. ?>