* @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3 * @package SWDB */ require_once(PATH::PAGE . "Page.php"); require_once(PATH::ENTITY . "Monster.php"); /** * Monster list page model. * * @category Page */ class Monsters_Page extends Page{ /** * @var Monster[] List of monsters to show. */ private $monsters = []; /** * @var int[] IDs of the monsters marked in the catalog. */ private $monsters_open = []; /** * @var mixed[] Default filter values. */ private $filters = [ "ELEMENT" => -1, "NAME" => "", "MIN_STARS" => 1, "MAX_STARS" => 6, ]; /** * Constructor. * * Retrieves the data and initializes the variables. */ public function __construct(){ parent::__construct(); $this->set_public(true); $this->set_view("monsters.php"); $this->set_title("Monsters"); $this->set_description("Catalog of all monsters"); $this->set_canonical("monsters/"); $this->add_css("monsters.css"); $this->parse_filters(); $result_set = $this->prepare_statement()->execute(); while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){ array_push($this->monsters, new Monster($result["id"], false)); } // Get open monsters $statement = get_context()->get_db()->prepare(" SELECT monster FROM collection WHERE player = :player AND open = :open"); $statement->bindValue(":player", get_context()->get_player()->get_id(), SQLITE3_TEXT); $statement->bindValue(":open", 1, SQLITE3_INTEGER); $result_set = $statement->execute(); while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){ array_push($this->monsters_open, $result["monster"]); } $this->set_code(200); $this->set_message("OK"); } /** * 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"]) <= 5){ $this->filters["MIN_STARS"] = $_GET["min_stars"]; } if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) <= 5){ $this->filters["MAX_STARS"] = $_GET["max_stars"]; } return; } /** * Prepares the statement to execute against the database using the filter values. * * @return SQLite3Stmt prepared statement. */ private function prepare_statement(){ // Common items $query = " SELECT id FROM monster WHERE obtainable = 1 AND awakens_from IS NULL AND natural_stars BETWEEN :min_stars AND :max_stars "; if(APPLICATION::valid_id("ELEMENT_ID", $this->filters["ELEMENT"])){ $query .= " AND element = :element "; } if ($this->filters["NAME"] != ""){ $query .= " AND upper(name) LIKE upper(:name) " ; } $query .= " ORDER BY element = :fire DESC, element = :water DESC, element = :wind DESC, element = :light DESC, element = :dark DESC, natural_stars; "; $statement = get_context()->get_db()->prepare($query); $statement->bindValue(":min_stars", $this->filters["MIN_STARS"], SQLITE3_INTEGER); $statement->bindValue(":max_stars", $this->filters["MAX_STARS"], SQLITE3_INTEGER); $statement->bindValue(":element", $this->filters["ELEMENT"], SQLITE3_INTEGER); $statement->bindValue(":name", "%" . $this->filters["NAME"] . "%", SQLITE3_TEXT); $statement->bindValue(":fire", ELEMENT_ID::FIRE, SQLITE3_INTEGER); $statement->bindValue(":water", ELEMENT_ID::WATER, SQLITE3_INTEGER); $statement->bindValue(":wind", ELEMENT_ID::WIND, SQLITE3_INTEGER); $statement->bindValue(":light", ELEMENT_ID::LIGHT, SQLITE3_INTEGER); $statement->bindValue(":dark", ELEMENT_ID::DARK, SQLITE3_INTEGER); return $statement; } /** * Retrieves the monsters to display * * @return Monster[] Selected monsters. */ public function get_monsters(){ return $this->monsters; } /** * Retrieves the page filters. * * @return mixed[] Page filtes */ public function get_filters(){ return $this->filters; } /** * Retrieves one filter. * * @param string $key * @return mixed Filter value, null if if doesn't exist. */ public function get_filter($key){ if (array_key_exists($key, $this->filters)){ return $this->filters[$key]; } else{ return null; } } public function is_open($id){ if (in_array($id, $this->monsters_open)){ return true; } else{ return false; } } } ?>