| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442 |
- <?php
- /**
- * Artifact entity file.
- *
- * Creates the entity and makes it available.
- *
- * @category Entity
- */
- /**
- * Require dependent entities if not present.
- */
- require_once(PATH::ENTITY . "Entity.php");
- require_once(PATH::ENTITY . "Artifact_Stat.php");
- require_once(PATH::ENTITY . "Artifact_Effect.php");
- /**
- * Owned artifact.
- *
- * Represents an object from the table 'artifact'.
- *
- * @category Entity
- */
- class Artifact extends Entity{
- private $player_id;
- private $id;
- private $type;
- private $elemental = false;
- private $archetypal = false;
- private $element;
- private $archetype;
- private $level;
- private $quality;
- private $original_quality;
- private $quality_name;
- private $original_quality_name;
- private $value;
- private $efficiency;
- private $max_efficiency;
- private $flag_stats = false;
- private $stat;
- private $effects = [];
- private $flag_unit = false;
- private $unit_id;
- 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);
- // TODO: Get unit ID
- // TODO: Move efficiencies to a separate method, but the profile API must read it so it's saved.
- // 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();
- }
- }
- }
- /**
- * Gets the quality name from the mappings.
- *
- * @param int $q Quality id.
- * @return string Quality (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(){
- $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]->value > 0){
- $value = $this->effects[$i]->value;
- $value = $this->effects[$i]->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]->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;
- }
-
- 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;
- }
-
- private function load_stats(){
- $this->stat = new Artifact_Stat($this->id);
- // Get effects
- $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 += (1/8);
- }
- if ($this->level < 9){
- $max_efficiency += (1/8);
- }
- if ($this->level < 6){
- $max_efficiency += (1/8);
- }
- if ($this->level < 3){
- $max_efficiency += (1/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 boolean True for elemental artifacts, false for archetipal ones.
- */
- public function is_elemental(){
- return $this->elemental;
- }
-
- /**
- * Cheks if the artifact is an archetipal one.
- *
- * @return boolean 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;
- }
- }
- ?>
|