| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php
- /**
- * Monster list page file.
- *
- * Provides a class with all the properties and methods to display the page.
- *
- * @category Page
- */
- /**
- * Require dependent files if not present.
- */
- require_once(PATH::PAGE . "Page.php");
- require_once(PATH::ENTITY . "Unit.php");
- require_once(PATH::ENTITY . "K_Effect.php");
- /**
- * Monster list page model.
- *
- * @category Page
- */
- class Monsters_Page extends Page{
- /**
- * @var \Unit[] Units to show.
- */
- public $monsters = [];
- /**
- * @var mixed[] Default filter values.
- */
- public $filters = [
- "ELEMENT" => -1,
- "NAME" => "",
- "MIN_STARS" => 1,
- "MAX_STARS" => 6,
- "IN_STORAGE" => false,
- "WITHOUT_RUNES" => false,
- "EFFECT" => [],
- "EFFECT_OPTION" => "any"
- ];
- /**
- * @var K_Effect[] All availabe skill effects, for the tfilter selector.
- */
- public $effects = [];
- /**
- * Constructor.
- *
- * Retrieves the data and initializes the variables.
- *
- * @global resource Database connection.
- */
- public function __construct(){
- global $db;
- $this->view = PATH::VIEW . "monsters.php";
- $this->parse_filters();
- $s_mon = $this->build_query();
- $q_mon = $db->query($s_mon);
- while ($r_mon = $q_mon->fetchArray(SQLITE3_ASSOC)){
- array_push($this->monsters, new Unit($r_mon["id"]));
- }
- // Get skill effects
- $s_eff = "SELECT id FROM k_effect ORDER BY is_buff DESC";
- $q_eff = $db->query($s_eff);
- while ($r_eff = $q_eff->fetchArray(SQLITE3_ASSOC)){
- array_push($this->effects, new K_Effect($r_eff["id"]));
- }
- $this->title = "Monsters - SWDB";
- $this->description = "Owned monsters";
- $this->canonical = URL::BASE . "monsters/";
- }
- /**
- * Parses the request looking for the selected filters, validates them
- * and adds them to the $filters array.
- */
- private function parse_filters(){
- if (isset($_GET["element"]) && APPLICATION::valid_id("ELEMENT_ID", strtoupper($_GET["element"]))){
- $this->filters["ELEMENT"] = $_GET["element"];
- }
- if (isset($_GET["name"]) && strlen($_GET["name"]) > 0){
- $this->filters["NAME"] = SQLite3::escapeString($_GET["name"]);
- }
- if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) <= 6){
- $this->filters["MIN_STARS"] = $_GET["min_stars"];
- }
- if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) <= 6){
- $this->filters["MAX_STARS"] = $_GET["max_stars"];
- }
- if (isset($_GET["in_storage"]) && $_GET["in_storage"] == "on"){
- $this->filters["IN_STORAGE"] = true;
- }
- else{
- $this->filters["IN_STORAGE"] = false;
- }
- if (isset($_GET["without_runes"]) && $_GET["without_runes"] == "on"){
- $this->filters["WITHOUT_RUNES"] = true;
- }
- if (isset($_GET["effect"])){
- $effects = $_GET["effect"];
- foreach ($effects as $effect){
- if (intval($effect) > 0){
- array_push($this->filters["EFFECT"], intval($effect));
- }
- }
- }
- if (isset($_GET["effect_option"]) && ($_GET["effect_option"] == "and" || $_GET["effect_option"] == "or")){
- $this->filters["EFFECT_OPTION"] = $_GET["effect_option"];
- }
- return;
- }
- /**
- * Builds the query to the rune table using the selected or default
- * filters.
- *
- * @return string The query to be executed.
- * @global int Player ID.
- */
- private function build_query(){
- global $UID;
- $s = "
- SELECT unit.id AS id
- FROM
- unit,
- k_unit
- WHERE
- unit.uid = '$UID' AND
- unit.unit = k_unit.id AND
- stars >= " . $this->filters["MIN_STARS"] . " AND
- stars <= " . $this->filters["MAX_STARS"];
- if (APPLICATION::valid_id("ELEMENT_ID", $this->filters["ELEMENT"])){
- $s = $s . " AND unit IN (SELECT id FROM k_unit WHERE element = " . $this->filters["ELEMENT"] . ") ";
- }
- if ($this->filters["NAME"] != ""){
- $s = $s . " AND unit IN (SELECT id FROM k_unit WHERE upper(name) LIKE '%" . strtoupper($this->filters["NAME"]) . "%') ";
- }
- if (!$this->filters["IN_STORAGE"]){
- $s = $s . " AND building NOT IN (SELECT id FROM building WHERE building = " . BUILDING_ID::MONSTER_STORAGE . ") ";
- }
- if (!$this->filters["WITHOUT_RUNES"]){
- $s = $s . " AND unit.id IN (SELECT DISTINCT assigned_to FROM rune) ";
- }
- if (sizeof($this->filters["EFFECT"]) > 0){
- if ($this->filters["EFFECT_OPTION"] == "and"){
- foreach ($this->filters["EFFECT"] as $effect){
- $s .= "
- AND k_unit.id IN (
- SELECT DISTINCT unit
- FROM
- k_unit_skill,
- k_skill_effect
- WHERE
- k_unit_skill.skill = k_skill_effect.skill AND
- k_skill_effect.effect = $effect
- )
- ";
- }
- }
- elseif ($this->filters["EFFECT_OPTION"] == "or"){
- $s .= " AND ( ";
- foreach ($this->filters["EFFECT"] as $effect){
- $s .= "
- k_unit.id IN (
- SELECT DISTINCT unit
- FROM
- k_unit_skill,
- k_skill_effect
- WHERE
- k_unit_skill.skill = k_skill_effect.skill AND
- k_skill_effect.effect = $effect
- ) OR
- ";
- }
- $s .= " 1 = 2 ) ";
- }
- }
- $s = $s . "
- ORDER BY
- stars DESC,
- level DESC,
- element = " . ELEMENT_ID::FIRE ." DESC,
- element = " . ELEMENT_ID::WATER ." DESC,
- element = " . ELEMENT_ID::WIND ." DESC,
- element = " . ELEMENT_ID::LIGHT ." DESC,
- element = " . ELEMENT_ID::DARK ." DESC
- ";
- return $s;
- }
- }
- ?>
|