Artifact.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. /**
  3. * Artifact entity file.
  4. *
  5. * Creates the entity and makes it available.
  6. *
  7. * @category Entity
  8. */
  9. /**
  10. * Require dependent entities if not present.
  11. */
  12. require_once(PATH::ENTITY . "Entity.php");
  13. require_once(PATH::ENTITY . "Artifact_Stat.php");
  14. require_once(PATH::ENTITY . "Artifact_Effect.php");
  15. /**
  16. * Owned artifact.
  17. *
  18. * Represents an object from the table 'artifact'.
  19. *
  20. * @category Entity
  21. */
  22. class Artifact extends Entity{
  23. /**
  24. * @var int Owner's player identifier.
  25. */
  26. public $player;
  27. /**
  28. * @var int Artifact identifier.
  29. */
  30. public $id;
  31. /**
  32. * @var int Artifact type (null if element artifact).
  33. *
  34. * @see ARTIFACT_TYPE
  35. */
  36. public $type;
  37. /**
  38. * @var int Artifact element (null if type artifact).
  39. *
  40. * @see ELEMENT_ID
  41. */
  42. public $element;
  43. /**
  44. * @var int Artifact archetype (null if elemental artifact).
  45. *
  46. * @see ARCHETYPE_ID
  47. */
  48. public $archetype;
  49. /**
  50. * @var int Level of the rune.
  51. */
  52. public $level;
  53. /**
  54. * @var int Artifact quality.
  55. *
  56. * @see QUALITY_ID
  57. */
  58. public $quality;
  59. /**
  60. * @var int Artifact original quality.
  61. *
  62. * @see QUALITY_ID
  63. */
  64. public $original_quality;
  65. /**
  66. * @var string Artifact quality name (uppercase).
  67. */
  68. public $quality_name;
  69. /**
  70. * @var string Artifact original quality name (uppercase).
  71. */
  72. public $original_quality_name;
  73. /**
  74. * @var int Price of the artifact.
  75. */
  76. public $value;
  77. /**
  78. * @var float Current efficiency of the artifact.
  79. */
  80. public $efficiency;
  81. /**
  82. * @var float Maximum potential efficiency of the artifact.
  83. */
  84. public $max_efficiency;
  85. /**
  86. * @var Artifact_Stat Main stat of the artifact.
  87. *
  88. * @see STAT_ID
  89. */
  90. public $stat;
  91. /**
  92. * @var \Artifact_Effect Effects of the artifact.
  93. */
  94. public $effects = [];
  95. /**
  96. * @var Unit Unit the artifact is assigned to.
  97. */
  98. public $unit;
  99. /**
  100. * Constructor.
  101. *
  102. * Searches the database and retrieves the information about the
  103. * artifact, populating it and it's items.
  104. *
  105. * @param int $id Artifact id.
  106. */
  107. public function __construct($id){
  108. $statement = get_context()->get_db()->prepare("
  109. SELECT
  110. id,
  111. type,
  112. attribute,
  113. archetype,
  114. level,
  115. quality,
  116. original_quality,
  117. value,
  118. efficiency,
  119. max_efficiency,
  120. locked
  121. FROM artifact
  122. WHERE id = :id;
  123. ");
  124. $statement->bindValue(':id', $id, SQLITE3_TEXT);
  125. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  126. if ($r){
  127. $this->id = $r["id"];
  128. $this->type = $r["type"];
  129. $this->element = $r["attribute"];
  130. $this->archetype = $r["archetype"];
  131. $this->level = $r["level"];
  132. $this->quality = $r["quality"];
  133. $this->original_quality = $r["original_quality"];
  134. $this->value = $r["value"];
  135. $this->efficiency = $r["efficiency"];
  136. $this->max_efficiency = $r["max_efficiency"];
  137. $this->locked = $r["locked"];
  138. $this->quality_name = $this->get_quality_name($this->quality);
  139. $this->original_quality_name = $this->get_quality_name($this->original_quality);
  140. // Get stat
  141. $this->stat = new Artifact_Stat($this->id);
  142. // Get effects
  143. $statement = get_context()->get_db()->prepare("
  144. SELECT slot
  145. FROM artifact_effect
  146. WHERE artifact = :artifact
  147. ORDER BY slot;
  148. ");
  149. $statement->bindValue(':artifact', $this->id, SQLITE3_TEXT);
  150. $q = $statement->execute();
  151. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  152. array_push($this->effects, new Artifact_Effect($this->id, $r["slot"]));
  153. }
  154. // Get efficiencies, or calculate them if not yet set.
  155. if ($this->efficiency == null || $this->efficiency == "" || intval($this->efficiency) == 0){
  156. $this->efficiency = $this->calculate_efficiency();
  157. $this->max_efficiency = $this->calculate_maximum_efficiency();
  158. $statement = get_context()->get_db()->prepare("
  159. UPDATE artifact
  160. SET
  161. efficiency = :efficiency,
  162. max_efficiency = :max_efficiency
  163. WHERE id = :artifact
  164. ");
  165. $statement->bindValue(':efficiency', $this->efficiency, SQLITE3_FLOAT);
  166. $statement->bindValue(':max_efficiency', $this->max_efficiency, SQLITE3_FLOAT);
  167. $statement->bindValue(':artifact', $this->id, SQLITE3_INTEGER);
  168. $q = $statement->execute();
  169. }
  170. }
  171. }
  172. /**
  173. * Gets the quality name from the mappings.
  174. *
  175. * @param int $q Quality id.
  176. * @return string Quality (Uppercase)
  177. */
  178. private function get_quality_name($q){
  179. switch ($q){
  180. case QUALITY_ID::NORMAL:
  181. return "NORMAL";
  182. case QUALITY_ID::MAGIC:
  183. return "MAGIC";
  184. case QUALITY_ID::RARE:
  185. return "RARE";
  186. case QUALITY_ID::HERO:
  187. return "HERO";
  188. case QUALITY_ID::LEGEND:
  189. return "LEGEND";
  190. default:
  191. return "";
  192. }
  193. }
  194. /**
  195. * Calcuates the efficiency of the artifact.
  196. *
  197. * @return float Efficiency
  198. */
  199. private function calculate_efficiency(){
  200. $eff = [0.0, 0.0, 0.0, 0.0];
  201. $query = "
  202. SELECT
  203. min,
  204. max
  205. FROM
  206. artifact_roll_type,
  207. artifact_effect_roll
  208. WHERE
  209. artifact_roll_type.id = artifact_effect_roll.type AND
  210. artifact_roll_type.initial = :initial AND
  211. artifact_roll_type.upgrade = :upgrade AND
  212. artifact_effect_roll.effect = :effect
  213. ";
  214. for ($i = 0; $i <= 3; $i ++){
  215. if (sizeof($this->effects) > $i && $this->effects[$i] != null && $this->effects[$i]->value > 0){
  216. $value = $this->effects[$i]->value;
  217. $value = $this->effects[$i]->value;
  218. $statement = get_context()->get_db()->prepare($query);
  219. $statement->bindValue(':initial', 1, SQLITE3_TEXT);
  220. $statement->bindValue(':upgrade', 1, SQLITE3_TEXT);
  221. $statement->bindValue(':effect', $this->effects[$i]->effect, SQLITE3_TEXT);
  222. $r_roll = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  223. if ($r_roll ){
  224. $min_initial = 0;//$r_initial["min"];
  225. $max_initial = $r_roll["max"];
  226. $min_upgrade = 0;//$r_upgrade["min"];
  227. $max_upgrade = $r_roll["max"];
  228. $eff[$i] = ((5 * $value) - ( 4 * $min_upgrade + $min_initial)) / ( 8 * ($max_initial - $min_initial + (4 * ($max_upgrade + $min_upgrade))));
  229. }
  230. }
  231. }
  232. $efficiency = 0.0;
  233. for ($i = 0; $i <= 3; $i ++){
  234. $efficiency += $eff[$i];
  235. }
  236. $efficiency *= 100;
  237. return $efficiency;
  238. }
  239. /**
  240. * Calcuates the maximum efficiency of the artifact.
  241. *
  242. * @return float Efficiency
  243. */
  244. private function calculate_maximum_efficiency(){
  245. $max_efficiency = $this->efficiency;
  246. if ($this->level < 12){
  247. $max_efficiency += (1/8);
  248. }
  249. if ($this->level < 9){
  250. $max_efficiency += (1/8);
  251. }
  252. if ($this->level < 6){
  253. $max_efficiency += (1/8);
  254. }
  255. if ($this->level < 3){
  256. $max_efficiency += (1/8);
  257. }
  258. return $max_efficiency;
  259. }
  260. }
  261. ?>