* @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3 * @package SWDB */ require_once(PATH::ENTITY . "Entity.php"); require_once(PATH::ENTITY . "Artifact_Stat.php"); require_once(PATH::ENTITY . "Artifact_Effect.php"); /** * Artifact. * * Represents a game artifact. * * @category Entity */ class Artifact extends Entity{ /** * @var int The owner's player ID. */ private $player_id; /** * @var int The artifact ID. */ private $id; /** * @var int The artifact type ({@see ARTIFACT_TYPE}). */ private $type; /** * @var bool Indicates if is an elemental artifact. */ private $elemental = false; /** * @var bool Indicates it the artifact is archetypal. */ private $archetypal = false; /** * @var int Artifact element ID, null for archetypals ({@see ELEMENT_ID}). */ private $element; /** * @var int Artifact archetype ID, null for elementals ({@see ARCHETYPE_ID}). */ private $archetype; /** * @var int Artifact level. */ private $level; /** * @var int Artifact quality ({@see QUALITY_ID}). */ private $quality; /** * @var int Artifact original quality ({@see QUALITY_ID}). */ private $original_quality; /** * @var string Artifact quality name. */ private $quality_name; /** * @var string Artifact original quality name. */ private $original_quality_name; /** * @var string Artifact value in mana stones. */ private $value; /** * @var float Artifact efficiency. */ private $efficiency; /** * @var float Artifact maximum efficiency. */ private $max_efficiency; /** * @var bool Internal flag to check if the stat and effects have been loaded into the entity. */ private $flag_stats = false; /** * @var Artifact_Stat The artifact main stat. */ private $stat; /** * @var Artifact_Effect[] Every effect the artifact currently has. */ private $effects = []; /** * @var bool Internal flag to check if the unit that equips the artifact (if any) has been * loaded. */ private $flag_unit = false; /** * @var Unit Unit that equips the artifact in the curent game mode. */ private $unit; /** * Constructor. * * Searches the database and retrieves the information about the * artifact, populating it and it's items. * * @param int $id Artifact id. */ public function __construct($id){ $statement = get_context()->get_db()->prepare(" SELECT player, id, type, attribute, archetype, level, quality, original_quality, value, efficiency, max_efficiency, locked FROM artifact WHERE id = :id; "); $statement->bindValue(':id', $id, SQLITE3_TEXT); $r = $statement->execute()->fetchArray(SQLITE3_ASSOC); if ($r){ $this->player_id = $r["player"]; $this->id = $r["id"]; $this->type = $r["type"]; if ($r["type"] == ARTIFACT_TYPE::ARCHETYPE){ $this->archetypal = true; } elseif ($r["type"] == ARTIFACT_TYPE::ELEMENT){ $this->elemental = true; } $this->element = $r["attribute"]; $this->archetype = $r["archetype"]; $this->level = $r["level"]; $this->quality = $r["quality"]; $this->original_quality = $r["original_quality"]; $this->value = $r["value"]; $this->efficiency = $r["efficiency"]; $this->max_efficiency = $r["max_efficiency"]; $this->locked = $r["locked"]; $this->quality_name = $this->set_quality_name($this->quality); $this->original_quality_name = $this->set_quality_name($this->original_quality); // Get efficiencies, or calculate them if not yet set. if ($this->efficiency == null || $this->efficiency == "" || intval($this->efficiency) == 0){ $this->efficiency = $this->calculate_efficiency(); $this->max_efficiency = $this->calculate_maximum_efficiency(); $statement = get_context()->get_db()->prepare(" UPDATE artifact SET efficiency = :efficiency, max_efficiency = :max_efficiency WHERE id = :artifact "); $statement->bindValue(':efficiency', $this->efficiency, SQLITE3_FLOAT); $statement->bindValue(':max_efficiency', $this->max_efficiency, SQLITE3_FLOAT); $statement->bindValue(':artifact', $this->id, SQLITE3_INTEGER); $statement->execute(); $statement = get_context()->get_db()->prepare(" UPDATE run_drop_artifact SET efficiency = :efficiency, max_efficiency = :max_efficiency WHERE id = :artifact "); $statement->bindValue(':efficiency', $this->efficiency, SQLITE3_FLOAT); $statement->bindValue(':max_efficiency', $this->max_efficiency, SQLITE3_FLOAT); $statement->bindValue(':artifact', $this->id, SQLITE3_INTEGER); $statement->execute(); } } else{ $statement = get_context()->get_db()->prepare(" SELECT id, type, attribute, archetype, quality, value, efficiency, max_efficiency, effect_1, effect_2, effect_3, effect_4 FROM run_drop_artifact WHERE id = :id; "); $statement->bindValue(':id', $id, SQLITE3_TEXT); $r = $statement->execute()->fetchArray(SQLITE3_ASSOC); if ($r){ $this->id = $r["id"]; $this->type = $r["type"]; if ($r["type"] == ARTIFACT_TYPE::ARCHETYPE){ $this->archetypal = true; } elseif ($r["type"] == ARTIFACT_TYPE::ELEMENT){ $this->elemental = true; } $this->element = $r["attribute"]; $this->archetype = $r["archetype"]; $this->level = 0; $this->quality = $r["quality"]; $this->original_quality = $r["quality"]; $this->value = $r["value"]; $this->efficiency = $r["efficiency"]; $this->max_efficiency = $r["max_efficiency"]; $this->locked = false; $this->quality_name = $this->set_quality_name($this->quality); $this->original_quality_name = $this->set_quality_name($this->original_quality); $this->stat = new Artifact_Stat($this->id); if (is_int($r["effect_1"])){ array_push($this->effects, new Artifact_Effect($this->id, ARTIFACT_EFFECT_SLOT_ID::EFFECT_1)); } if (is_int($r["effect_2"])){ array_push($this->effects, new Artifact_Effect($this->id, ARTIFACT_EFFECT_SLOT_ID::EFFECT_2)); } if (is_int($r["effect_3"])){ array_push($this->effects, new Artifact_Effect($this->id, ARTIFACT_EFFECT_SLOT_ID::EFFECT_3)); } if (is_int($r["effect_4"])){ array_push($this->effects, new Artifact_Effect($this->id, ARTIFACT_EFFECT_SLOT_ID::EFFECT_4)); } $this->flag_stats = true; // Get efficiencies, or calculate them if not yet set. if ($this->efficiency == null || $this->efficiency == "" || intval($this->efficiency) == 0){ $this->efficiency = $this->calculate_efficiency(); $this->max_efficiency = $this->calculate_maximum_efficiency(); $statement = get_context()->get_db()->prepare(" UPDATE run_drop_artifact SET efficiency = :efficiency, max_efficiency = :max_efficiency WHERE id = :artifact "); $statement->bindValue(':efficiency', $this->efficiency, SQLITE3_FLOAT); $statement->bindValue(':max_efficiency', $this->max_efficiency, SQLITE3_FLOAT); $statement->bindValue(':artifact', $this->id, SQLITE3_INTEGER); $statement->execute(); } } } } /** * Gets the quality name from the mappings. * * @param int $q Quality id. * @return string Quality name (uppercase) */ private function set_quality_name($q){ switch ($q){ case QUALITY_ID::NORMAL: return "NORMAL"; case QUALITY_ID::MAGIC: return "MAGIC"; case QUALITY_ID::RARE: return "RARE"; case QUALITY_ID::HERO: return "HERO"; case QUALITY_ID::LEGEND: return "LEGEND"; default: return ""; } } /** * Calcuates the efficiency of the artifact. * * @return float Efficiency */ private function calculate_efficiency(){ if (! $this->flag_stats){ $this->load_stats(); } $eff = [0.0, 0.0, 0.0, 0.0]; $query = " SELECT min, max FROM artifact_roll_type, artifact_effect_roll WHERE artifact_roll_type.id = artifact_effect_roll.type AND artifact_roll_type.initial = :initial AND artifact_roll_type.upgrade = :upgrade AND artifact_effect_roll.effect = :effect "; for ($i = 0; $i <= 3; $i ++){ if (sizeof($this->effects) > $i && $this->effects[$i] != null && $this->effects[$i]->get_value() > 0){ $value = $this->effects[$i]->get_value(); $value = $this->effects[$i]->get_value(); $statement = get_context()->get_db()->prepare($query); $statement->bindValue(':initial', 1, SQLITE3_TEXT); $statement->bindValue(':upgrade', 1, SQLITE3_TEXT); $statement->bindValue(':effect', $this->effects[$i]->get_effect(), SQLITE3_TEXT); $r_roll = $statement->execute()->fetchArray(SQLITE3_ASSOC); if ($r_roll ){ $min_initial = 0;//$r_initial["min"]; $max_initial = $r_roll["max"]; $min_upgrade = 0;//$r_upgrade["min"]; $max_upgrade = $r_roll["max"]; $eff[$i] = ((5 * $value) - ( 4 * $min_upgrade + $min_initial)) / ( 8 * ($max_initial - $min_initial + (4 * ($max_upgrade + $min_upgrade)))); } } } $efficiency = 0.0; for ($i = 0; $i <= 3; $i ++){ $efficiency += $eff[$i]; } $efficiency *= 100; return $efficiency; } /** * Loads the unit that equips the artifact into the entity (if any). * * Must be called only once before trying to get the unit. Sets the flag * {@link Artifact:$flag_unit} to true. */ private function load_unit(){ $statement = get_context()->get_db()->prepare(" SELECT unit FROM unit_artifact WHERE artifact = :artifact AND rta = :rta; "); $statement->bindValue(':artifact', $this->id, SQLITE3_TEXT); $statement->bindValue(':rta', get_context()->get_game_mode(), SQLITE3_TEXT); $q = $statement->execute(); $r = $q->fetchArray(SQLITE3_ASSOC); if ($r){ $this->unit_id = $r["unit"]; $this->unit = new Unit($r["unit"]); } $this->flag_unit = true; } /** * Loads the artifact main stat and effects into the entity. * * Must be called only once before trying to get the stat or effects. Sets the flag * {@link Artifact:$flag_stats} to true. */ private function load_stats(){ $this->stat = new Artifact_Stat($this->id); $statement = get_context()->get_db()->prepare(" SELECT slot FROM artifact_effect WHERE artifact = :artifact ORDER BY slot; "); $statement->bindValue(':artifact', $this->id, SQLITE3_TEXT); $q = $statement->execute(); while ($r = $q->fetchArray(SQLITE3_ASSOC)){ array_push($this->effects, new Artifact_Effect($this->id, $r["slot"])); } $this->flag_stats = true; } /** * Calcuates the maximum efficiency of the artifact. * * @return float Efficiency. */ private function calculate_maximum_efficiency(){ $max_efficiency = $this->efficiency; if ($this->level < 12){ $max_efficiency += (100/8); } if ($this->level < 9){ $max_efficiency += (100/8); } if ($this->level < 6){ $max_efficiency += (100/8); } if ($this->level < 3){ $max_efficiency += (100/8); } return $max_efficiency; } /** * Retrieves the owner's player identifier. * * @return int Player ID. */ public function get_player_id(){ return $this->player_id; } /** * Retrieves the artifact identifier. * * @return string Artifact ID. */ public function get_id(){ return $this->id; } /** * Checks if the artifact is elemental or archetiplal. * * @return int ARTIFACT_TYPE_ID::ELEMENT or ARTIFACT_TYPE:ARCHETYPE. */ public function get_type(){ return $this->type; } /** * Cheks if the artifact is an elemental one. * * @return bool True for elemental artifacts, false for archetipal ones. */ public function is_elemental(){ return $this->elemental; } /** * Cheks if the artifact is an archetipal one. * * @return bool True for archetipal artifacts, false for elemental ones. */ public function is_archetypal(){ return $this->archetypal; } /** * Retrieves the element of an elemental artifact. * * @see ELEMENT_ID * * @return int Element ID, null for archetipal artifacts. */ public function get_element(){ return $this->element; } /** * Retrieves the archetype of an archetipal artifact. * * @see ARCHETYPE_ID * * @return int Archetype ID, null for elemental artifacts. */ public function get_archetype(){ return $this->archetype; } /** * Retrieves the artifact level. * * @return int Artifact level (0-15). */ public function get_level(){ return $this->level; } /** * Retrieves the quality of the artifact. * * @see QUALITY_ID * * @return int Quality of the artifact. */ public function get_quality(){ return $this->quality; } /** * Retrieves the original quality of the artifact. * * @see QUALITY_ID * * @return int Original quality of the artifact. */ public function get_original_quality(){ return $this->original_quality; } /** * Retrieves the quality name. * * @return string Quality name, uppercase. */ public function get_quality_name(){ return $this->quality_name; } /** * Retrieves the original quality name. * * @return string Original quality name, uppercase. */ public function get_original_quality_name(){ return $this->original_quality_name; } /** * Retrieves the value of the artifact. * * @return int artifact value. */ public function get_value(){ return $this->value; } /** * Retrieves the efficiency of the artifact. * * @return float Artifact efficiency (0-100). */ public function get_efficiency(){ return $this->efficiency; } /** * Retrieves the maximum efficiency of the artifact. * * @return float Artifact maximum efficiency (0-100). */ public function get_max_efficiency(){ return $this->max_efficiency; } /** * Retrieves the artifact's main stat. * * @return Artifact_Stat Artifact main stat. */ public function get_stat(){ if (!$this->flag_stats){ $this->load_stats(); } return $this->stat; } /** * Retrieves a list of the artifact's effects. * * @return Artifact_Effect[] List of effects. */ public function get_effects(){ if (!$this->flag_stats){ $this->load_stats(); } return $this->effects; } /** * Retrieves the identifier of the unit who has eqquiped the artifact. * * @return string Unit ID. */ public function get_unit_id(){ if (!$this->flag_unit){ $this->load_unit(); } return $this->unit_id; } /** * Retrieves the unit who has eqquiped the artifact. * * @return Unit Unit who has equipped the artifact. */ public function get_unit(){ if (!$this->flag_unit){ $this->load_unit(); } return $this->unit; } }