Report_Rune_Enchantment_Page.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <?php
  2. /**
  3. * Upgradeable runes report 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 . "Unit.php");
  14. require_once(PATH::ENTITY . "Rune.php");
  15. require_once(PATH::ENTITY . "Enchantment.php");
  16. /**
  17. * Upgradeable runes report page model.
  18. *
  19. * @category Page
  20. */
  21. class Report_Rune_Enchantment_Page extends Page{
  22. /**
  23. * @var mixed[] List of available upgrades.
  24. *
  25. * Can be described as:
  26. * $upgrades = [
  27. * [
  28. * unit (Unit),
  29. * upgrade = [
  30. * [
  31. * rune (Rune),
  32. * enchant = [(Enchantment)]
  33. * ]
  34. * ]
  35. * ]
  36. * ]
  37. */
  38. public $upgrades = [];
  39. /**
  40. * @var mixed[] Default filter values.
  41. */
  42. public $filters = [
  43. "MONSTER_NAME" => "",
  44. "MONSTER_MIN_STARS" => 5,
  45. "MONSTER_MAX_STARS" => 6,
  46. "MONSTER_IN_STORAGE" => false,
  47. "TEAM" => false,
  48. "RUNE_MIN_STARS" => 5,
  49. "RUNE_MAX_STARS" => 6,
  50. "RUNE_MIN_LEVEL" => 0,
  51. "RUNE_MAX_LEVEL" => 15,
  52. "RUNE_MIN_QUALITY" => QUALITY_ID::HERO,
  53. "RUNE_MAX_QUALITY" => QUALITY_ID::LEGEND,
  54. "RUNE_MIN_ORIGINAL_QUALITY" => QUALITY_ID::HERO,
  55. "RUNE_MAX_ORIGINAL_QUALITY" => QUALITY_ID::LEGEND,
  56. "RUNE_MIN_EFFICIENCY" => 0,
  57. "RUNE_MAX_EFFICIENCY" => 100,
  58. "RUNE_MIN_MAX_EFFICIENCY" => 0,
  59. "RUNE_MAX_MAX_EFFICIENCY" => 100,
  60. "RUNE_SLOT" => [],
  61. "GRIND" => 1,
  62. "GRIND_MIN_QUALITY" => QUALITY_ID::RARE,
  63. "GRIND_STAT" => [
  64. RUNE_STAT_ID::HP_P,
  65. RUNE_STAT_ID::ATK_P,
  66. RUNE_STAT_ID::DEF_P,
  67. RUNE_STAT_ID::SPD,
  68. RUNE_STAT_ID::CRD,
  69. RUNE_STAT_ID::CRR,
  70. RUNE_STAT_ID::ACC
  71. ],
  72. "GEM" => 1,
  73. "GEM_MIN_QUALITY" => QUALITY_ID::RARE,
  74. "GEM_STAT_FROM" => [
  75. RUNE_STAT_ID::HP,
  76. RUNE_STAT_ID::ATK,
  77. RUNE_STAT_ID::DEF,
  78. RUNE_STAT_ID::RES
  79. ],
  80. "GEM_STAT_TO" => [
  81. RUNE_STAT_ID::HP_P,
  82. RUNE_STAT_ID::ATK_P,
  83. RUNE_STAT_ID::DEF_P,
  84. RUNE_STAT_ID::SPD,
  85. RUNE_STAT_ID::CRD,
  86. RUNE_STAT_ID::CRR,
  87. RUNE_STAT_ID::ACC
  88. ]
  89. ];
  90. /**
  91. * Constructor.
  92. *
  93. * Retrieves the data and initializes the variables.
  94. *
  95. * @global resource Database connection.
  96. */
  97. public function __construct(){
  98. global $db;
  99. $this->view = PATH::VIEW . "report_rune_enchantment.php";
  100. $this->parse_filters();
  101. $s = $this->build_query();
  102. $q = $db->query($s);
  103. $prev_monster = "";
  104. $prev_rune = "";
  105. $upgrade = null;
  106. $rupgrade = null;
  107. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  108. if ($r["unit"] != $prev_monster){
  109. if ($prev_rune != ""){
  110. array_push($upgrade["upgrade"], $rupgrade);
  111. }
  112. if ($prev_monster != ""){
  113. array_push($this->upgrades, $upgrade);
  114. }
  115. $prev_monster = $r["unit"];
  116. $prev_rune = "";
  117. $upgrade = [
  118. "unit" => new Unit($r["unit"]),
  119. "upgrade" => [],
  120. ];
  121. $rupgrade = [
  122. "rune" => new Rune($r["rune"]),
  123. "enchantment" => [],
  124. ];
  125. }
  126. if ($r["rune"] != $prev_rune){
  127. if ($prev_rune != ""){
  128. array_push($upgrade["upgrade"], $rupgrade);
  129. }
  130. $prev_rune = $r["rune"];
  131. $rupgrade = [
  132. "rune" => new Rune($r["rune"]),
  133. "enchantment" => [],
  134. ];
  135. }
  136. $enchantment= new Enchantment($r["enchantment"]);
  137. array_push($rupgrade["enchantment"], $enchantment);
  138. }
  139. // Last entries
  140. array_push($upgrade["upgrade"], $rupgrade);
  141. array_push($this->upgrades, $upgrade);
  142. $this->title = "Rune enchantment - SWDB";
  143. $this->description = "Find runes that can be upgraded via enchantments.";
  144. $this->canonical = URL::BASE . "report/rune_enchantment/";
  145. }
  146. /**
  147. * Parses the filters passed in the request and sets $filters.
  148. * Filters must be in $_GET, nd the available ones are max_stars (1-6),
  149. * min_stars (1-6) and in_storage (on/off).
  150. */
  151. private function parse_filters(){
  152. // Monster
  153. if (isset($_GET["monster_min_stars"]) && intval($_GET["monster_min_stars"]) > 0 && intval($_GET["monster_min_stars"]) < 7){
  154. $this->filters["MONSTER_MIN_STARS"] = $_GET["monster_min_stars"];
  155. }
  156. if (isset($_GET["monster_max_stars"]) && intval($_GET["monster_max_stars"]) > 0 && intval($_GET["monster_max_stars"]) < 7){
  157. $this->filters["MONSTER_MAX_STARS"] = $_GET["monster_max_stars"];
  158. }
  159. if (isset($_GET["monster_in_storage"]) && $_GET["monster_in_storage"] == "on"){
  160. $this->filters["MONSTER_IN_STORAGE"] = true;
  161. }
  162. else{
  163. $this->filters["MONSTER_IN_STORAGE"] = false;
  164. }
  165. if (isset($_GET["monster_name"]) && strlen($_GET["monster_name"]) > 0){
  166. $this->filters["MONSTER_NAME"] = SQLite3::escapeString($_GET["monster_name"]);
  167. }
  168. // Runes
  169. if (isset($_GET["rune_min_stars"]) && intval($_GET["rune_min_stars"]) > 0 && intval($_GET["rune_min_stars"]) < 7){
  170. $this->filters["RUNE_MIN_STARS"] = $_GET["rune_min_stars"];
  171. }
  172. if (isset($_GET["rune_max_stars"]) && intval($_GET["rune_max_stars"]) > 0 && intval($_GET["rune_max_stars"]) < 7){
  173. $this->filters["RUNE_MAX_STARS"] = $_GET["rune_max_stars"];
  174. }
  175. if (isset($_GET["rune_min_level"]) && intval($_GET["rune_min_level"]) >= 0 && intval($_GET["rune_min_level"]) <= 15){
  176. $this->filters["RUNE_MIN_LEVEL"] = $_GET["rune_min_level"];
  177. }
  178. if (isset($_GET["rune_max_level"]) && intval($_GET["rune_max_level"]) >= 0 && intval($_GET["rune_max_level"]) <= 15){
  179. $this->filters["RUNE_MAX_LEVEL"] = $_GET["rune_max_level"];
  180. }
  181. if (isset($_GET["rune_min_quality"]) && intval($_GET["rune_min_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["rune_min_quality"]) <= QUALITY_ID::LEGEND){
  182. $this->filters["RUNE_MIN_QUALITY"] = $_GET["rune_min_quality"];
  183. }
  184. if (isset($_GET["rune_max_quality"]) && intval($_GET["rune_max_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["rune_max_quality"]) <= QUALITY_ID::LEGEND){
  185. $this->filters["RUNE_MAX_QUALITY"] = $_GET["rune_max_quality"];
  186. }
  187. if (isset($_GET["rune_min_original_quality"]) && intval($_GET["rune_min_original_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["rune_min_original_quality"]) <= QUALITY_ID::LEGEND){
  188. $this->filters["RUNE_MIN_ORIGINAL_QUALITY"] = $_GET["rune_min_original_quality"];
  189. }
  190. if (isset($_GET["rune_max_original_quality"]) && intval($_GET["rune_max_original_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["rune_max_original_quality"]) <= QUALITY_ID::LEGEND){
  191. $this->filters["RUNE_MAX_ORIGINAL_QUALITY"] = $_GET["rune_max_original_quality"];
  192. }
  193. if (isset($_GET["rune_min_efficiency"]) && intval($_GET["rune_min_efficiency"]) >= 0 && intval($_GET["rune_min_efficiency"]) <= 100){
  194. $this->filters["RUNE_MIN_EFFICIENCY"] = $_GET["rune_min_efficiency"];
  195. }
  196. if (isset($_GET["rune_max_efficiency"]) && intval($_GET["rune_max_efficiency"]) >= 0 && intval($_GET["rune_max_efficiency"]) <= 100){
  197. $this->filters["RUNE_MAX_EFFICIENCY"] = $_GET["rune_max_efficiency"];
  198. }
  199. if (isset($_GET["rune_min_max_efficiency"]) && intval($_GET["rune_min_max_efficiency"]) >= 0 && intval($_GET["rune_min_max_efficiency"]) <= 100){
  200. $this->filters["RUNE_MIN_MAX_EFFICIENCY"] = $_GET["rune_min_max_efficiency"];
  201. }
  202. if (isset($_GET["rune_max_max_efficiency"]) && intval($_GET["rune_max_max_efficiency"]) >= 0 && intval($_GET["rune_max_max_efficiency"]) <= 100){
  203. $this->filters["RUNE_MAX_MAX_EFFICIENCY"] = $_GET["rune_max_max_efficiency"];
  204. }
  205. if (isset($_GET["rune_slot"])){
  206. $slots = $_GET["rune_slot"];
  207. foreach ($slots as $slot){
  208. if (intval($slot) >= 1 && intval($slot) < 7){
  209. array_push($this->filters["RUNE_SLOT"], intval($slot));
  210. }
  211. }
  212. }
  213. // Grindstones
  214. if (isset($_GET["grind_min_quality"]) && intval($_GET["grind_min_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["grind_min_quality"]) < QUALITY_ID::LEGEND){
  215. $this->filters["GRIND_MIN_QUALITY"] = $_GET["grind_min_quality"];
  216. }
  217. if (isset($_GET["grind_stat"])){
  218. $stats = $_GET["grind_stat"];
  219. $this->filters["GRIND_STAT"] = [];
  220. foreach ($stats as $stat){
  221. array_push($this->filters["GRIND_STAT"], intval($stat));
  222. }
  223. }
  224. if (isset($_GET["gem_stat_from"])){
  225. $stats = $_GET["gem_stat_from"];
  226. $this->filters["GEM_STAT_FROM"] = [];
  227. foreach ($stats as $stat){
  228. array_push($this->filters["GEM_STAT_FROM"], intval($stat));
  229. }
  230. }
  231. if (isset($_GET["gem_stat_to"])){
  232. $stats = $_GET["gem_stat_to"];
  233. $this->filters["GEM_STAT_TO"] = [];
  234. foreach ($stats as $stat){
  235. array_push($this->filters["GEM_STAT_TO"], intval($stat));
  236. }
  237. }
  238. return;
  239. }
  240. /**
  241. * Builds the query for the page, using the providded or default filters.
  242. *
  243. * @return string The query to be executed.
  244. * @global Player Currently selected player.
  245. * @global int Game mode.
  246. */
  247. private function build_query(){
  248. global $PLAYER;
  249. global $MODE;
  250. $s = "
  251. SELECT DISTINCT
  252. unit.id AS unit,
  253. rune.id AS rune,
  254. enchantment.id AS enchantment
  255. FROM
  256. unit,
  257. monster,
  258. rune,
  259. unit_rune,
  260. enchantment,
  261. enchantment_type
  262. WHERE
  263. unit.player = '" . $PLAYER->id . "'
  264. AND rune.player = '" . $PLAYER->id . "'
  265. AND enchantment.player = '" . $PLAYER->id . "'
  266. AND unit.id = unit_rune.unit
  267. AND rune.id = unit_rune.rune
  268. AND unit_rune.rta = $MODE
  269. AND unit.monster = monster.id
  270. AND enchantment.type = enchantment_type.id
  271. -- RUNE FILTERS
  272. AND unit.stars >= " . $this->filters["RUNE_MIN_STARS"] . "
  273. AND unit.stars <= " . $this->filters["RUNE_MAX_STARS"] . "
  274. AND rune.quality >= " . $this->filters["RUNE_MIN_QUALITY"] . "
  275. AND rune.quality <= " . $this->filters["RUNE_MAX_QUALITY"] . "
  276. AND rune.level >= " . $this->filters["RUNE_MIN_LEVEL"] . "
  277. AND rune.level <= " . $this->filters["RUNE_MAX_LEVEL"] . "
  278. AND rune.stars >= " . $this->filters["RUNE_MIN_STARS"] . "
  279. AND rune.stars <= " . $this->filters["RUNE_MAX_STARS"] . "
  280. AND rune.original_quality >= " . $this->filters["RUNE_MIN_ORIGINAL_QUALITY"] . "
  281. AND rune.original_quality <= " . $this->filters["RUNE_MAX_ORIGINAL_QUALITY"] . "
  282. AND rune.efficiency >= " . $this->filters["RUNE_MIN_EFFICIENCY"] . "
  283. AND rune.efficiency <= " . $this->filters["RUNE_MAX_EFFICIENCY"] . "
  284. AND rune.max_efficiency >= " . $this->filters["RUNE_MIN_MAX_EFFICIENCY"] . "
  285. AND rune.max_efficiency <= " . $this->filters["RUNE_MAX_MAX_EFFICIENCY"] . "
  286. AND (
  287. rune.type = enchantment.rune
  288. OR enchantment_type.inmemorial = 1
  289. )
  290. AND enchantment_type.ancient = rune.ancient
  291. ";
  292. // Monster name
  293. $cond = "";
  294. if (strlen($this->filters["MONSTER_NAME"]) > 0){
  295. $cond = "AND upper(monster.name) LIKE upper('%" . $this->filters["MONSTER_NAME"] . "%') ";
  296. }
  297. $s .= $cond . " -- MONSTER NAME \n";
  298. // Monsters in storage
  299. $cond = "";
  300. if (!$this->filters["MONSTER_IN_STORAGE"]){
  301. $cond = "AND unit.building <> " . BUILDING_ID::MONSTER_STORAGE . " ";
  302. }
  303. $s .= $cond . " -- STORAGE \n";
  304. // Rune slot
  305. $cond = "";
  306. if (count($this->filters["RUNE_SLOT"]) > 0){
  307. $in = "(";
  308. foreach($this->filters["RUNE_SLOT"] as $t){
  309. $in .= "$t, ";
  310. }
  311. $in = rtrim($in, ", ") . ")";
  312. $cond = "AND rune.slot IN $in";
  313. }
  314. $s .= $cond . " -- RUNE SLOT \n";
  315. // Nevermind the type, discard enchantments with the same stat as
  316. // the main or inate substat
  317. $s .= "
  318. AND enchantment.stat NOT IN (
  319. SELECT stat
  320. FROM rune_stat
  321. WHERE
  322. rune_stat.rune = rune.id
  323. AND (
  324. rune_stat.slot = " . RUNE_STAT_SLOT_ID::MAIN . "
  325. OR rune_stat.slot = " . RUNE_STAT_SLOT_ID::INNATE . "
  326. )
  327. )
  328. ";
  329. // Begin type switch block
  330. $s .= " AND ( -- TYPE SWITCH \n";
  331. // Begin grindstone block
  332. $s .= "
  333. (
  334. enchantment_type.gem = 0
  335. AND enchantment.stat IN (SELECT stat FROM rune_stat rune_stat WHERE rune_stat.rune = rune.id)
  336. AND enchantment.quality >= " . $this->filters["GRIND_MIN_QUALITY"] . " -- GRINDSTONE QUALITY
  337. ";
  338. // Grindstone stat
  339. $cond = "";
  340. if (count($this->filters["GRIND_STAT"]) > 0){
  341. $in = "(";
  342. foreach($this->filters["GRIND_STAT"] as $t){
  343. $in .= "$t, ";
  344. }
  345. $in = rtrim($in, ", ") . ")";
  346. $cond = "AND enchantment.stat IN $in";
  347. }
  348. $s .= $cond . " -- GRINDSTONE STAT \n";
  349. $s .= "
  350. )\n
  351. ";
  352. // Begin gem block
  353. $s .= " OR (
  354. enchantment_type.gem = 1
  355. AND enchantment.stat NOT IN (SELECT stat FROM rune_stat rune_stat WHERE rune_stat.rune = rune.id)
  356. AND enchantment.quality >= " . $this->filters["GEM_MIN_QUALITY"] . " -- GEM QUALITY
  357. ";
  358. // Gem stat to
  359. $cond = "";
  360. if (count($this->filters["GEM_STAT_TO"]) > 0){
  361. $in = "(";
  362. foreach($this->filters["GEM_STAT_TO"] as $t){
  363. $in .= "$t, ";
  364. }
  365. $in = rtrim($in, ", ") . ")";
  366. $cond = "AND enchantment.stat IN $in";
  367. }
  368. $s .= $cond . " -- GEM STAT TO \n";
  369. // Gem stat from
  370. $cond = "";
  371. if (count($this->filters["GEM_STAT_FROM"]) > 0){
  372. $in = "(";
  373. foreach($this->filters["GEM_STAT_FROM"] as $t){
  374. $in .= "$t, ";
  375. }
  376. $in = rtrim($in, ", ") . ")";
  377. $cond = "
  378. AND rune.id IN(
  379. SELECT DISTINCT rune
  380. FROM rune_stat rune_stat
  381. WHERE
  382. rune_stat.rune = rune.id
  383. AND stat IN $in
  384. )
  385. ";
  386. }
  387. $s .= $cond . " -- GEM STAT FROM \n";
  388. $s .= "
  389. )\n
  390. ";
  391. // End type switch block
  392. $s .= " ) -- END TYPE SWITCH \n";
  393. return $s;
  394. }
  395. }
  396. ?>