| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- <?php
- require_once($path["page"] . "Page.php");
- require_once($path["entity"] . "Unit.php");
- /**
- * Monster list page.
- */
- class Monsters_Page extends Page{
- /**
- * Array of @{see Monsters}s.
- */
- public $monsters = [];
- /**
- * The filters the page can handle.
- */
- public $filters = [
- "element" => "",
- "name" => "",
- "min_stars" => 1,
- "max_stars" => 6,
- "in_storage" => false,
- "without_runes" => false
- ];
- /**
- * Stats gained by building.
- */
- public $building_stats = [
- "atk" => 0,
- "def" => 0,
- "hp" => 0,
- "spd" => 0,
- "crd" => 0,
- "elm" => [
- "Fire" => 0,
- "Water" => 0,
- "Wind" => 0,
- "Light" => 0,
- "Dark" => 0
- ]
- ];
- /**
- * Constructor.
- *
- * Retrieves the data and initializes the variables.
- *
- * @param SQLite3 $db Connection to the database.
- */
- public function __construct($db){
- global $path;
- global $root;
- parent::__construct($db);
- $this->view = $path["view"] . "monsters.php";
- $this->parse_filters();
- $this->calculate_buildings();
- $s_mon = $this->build_query();
- $q_mon = $this->db->query($s_mon);
- while ($r_mon = $q_mon->fetchArray(SQLITE3_ASSOC)){
- array_push($this->monsters, new Unit($db, $r_mon["id"]));
- }
- $this->title = "Monsters - SWDB";
- $this->description = "Owned monsters";
- $this->canonical = $root . "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"])){
- $f_element = strtoupper($_GET["element"]);
- if ($f_element == "FIRE" || $f_element == "WATER" || $f_element == "WIND" || $f_element == "LIGHT" || $f_element == "DARK"){
- $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"]) < 7){
- $this->filters["min_stars"] = $_GET["min_stars"];
- }
- if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) < 7){
- $this->filters["max_stars"] = $_GET["max_stars"];
- }
- if (isset($_GET["in_storage"]) && $_GET["in_storage"] == "on"){
- $this->filters["in_storage"] = true;
- }
- if (isset($_GET["without_runes"]) && $_GET["without_runes"] == "on"){
- $this->filters["without_runes"] = true;
- }
- return;
- }
- /**
- * Builds the query to the rune table using the selected or default
- * filters.
- *
- * @return The query to be executed.
- */
- private function build_query(){
- $s = "SELECT unit.id AS id FROM unit, k_unit WHERE unit.unit = k_unit.id AND stars >= " . $this->filters["min_stars"] . " AND stars <= " . $this->filters["max_stars"];
- if (strlen($this->filters["element"]) > 0){
- $s = $s . " AND unit IN (SELECT id FROM k_unit WHERE upper(element) = '" . strtoupper($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 in_storage = 0 ";
- }
- if (!$this->filters["without_runes"]){
- $s = $s . " AND unit.id IN (SELECT DISTINCT assigned_to FROM rune) ";
- }
- $s = $s .
- " ORDER BY " .
- " stars DESC, " .
- " level DESC, " .
- " element = 'Fire' DESC, " .
- " element = 'Water' DESC, " .
- " element = 'Wind' DESC, " .
- " element = 'Light' DESC, " .
- " element = 'Dark' DESC; ";
- return $s;
- }
- /**
- * Calculates the stats gained from bulding upgrades and populates the
- * $building_stats array.
- */
- private function calculate_buildings(){
- $s =
- "SELECT " .
- " affected_stat, " .
- " bonus, " .
- " element " .
- "FROM " .
- " k_building, " .
- " k_building_level, " .
- " building " .
- "WHERE " .
- " k_building.id = building.building AND " .
- " building.building = k_building_level.building AND " .
- " building.level = k_building_level.level AND " .
- " area = 'Everywhere' AND " .
- " affected_stat IN ('ATK', 'DEF', 'HP', 'SPD', 'CRI Dmg');";
- $q = $this->db->query($s);
- while ($r = $q->fetchArray(SQLITE3_ASSOC)){
- switch($r["affected_stat"]){
- case "DEF":
- $this->building_stats["def"] += $r["bonus"];
- break;
- case "HP":
- $this->building_stats["hp"] += $r["bonus"];
- break;
- case "SPD":
- $this->building_stats["spd"] += $r["bonus"];
- break;
- case "CRI Dmg":
- $this->building_stats["crd"] += $r["bonus"];
- break;
- case "ATK":
- if(strlen($r["element"]) > 0){
- $this->building_stats["elm"][$r["element"]] += $r["bonus"];
- }
- else{
- $this->building_stats["atk"] += $r["bonus"];
- }
- break;
- }
- }
- }
- }
- ?>
|