Artifact.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. private $player_id;
  24. private $id;
  25. private $type;
  26. private $elemental = false;
  27. private $archetypal = false;
  28. private $element;
  29. private $archetype;
  30. private $level;
  31. private $quality;
  32. private $original_quality;
  33. private $quality_name;
  34. private $original_quality_name;
  35. private $value;
  36. private $efficiency;
  37. private $max_efficiency;
  38. private $flag_stats = false;
  39. private $stat;
  40. private $effects = [];
  41. private $flag_unit = false;
  42. private $unit_id;
  43. private $unit;
  44. /**
  45. * Constructor.
  46. *
  47. * Searches the database and retrieves the information about the
  48. * artifact, populating it and it's items.
  49. *
  50. * @param int $id Artifact id.
  51. */
  52. public function __construct($id){
  53. $statement = get_context()->get_db()->prepare("
  54. SELECT
  55. player,
  56. id,
  57. type,
  58. attribute,
  59. archetype,
  60. level,
  61. quality,
  62. original_quality,
  63. value,
  64. efficiency,
  65. max_efficiency,
  66. locked
  67. FROM artifact
  68. WHERE id = :id;
  69. ");
  70. $statement->bindValue(':id', $id, SQLITE3_TEXT);
  71. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  72. if ($r){
  73. $this->player_id = $r["player"];
  74. $this->id = $r["id"];
  75. $this->type = $r["type"];
  76. if ($r["type"] == ARTIFACT_TYPE::ARCHETYPE){
  77. $this->archetypal = true;
  78. }
  79. elseif ($r["type"] == ARTIFACT_TYPE::ELEMENT){
  80. $this->elemental = true;
  81. }
  82. $this->element = $r["attribute"];
  83. $this->archetype = $r["archetype"];
  84. $this->level = $r["level"];
  85. $this->quality = $r["quality"];
  86. $this->original_quality = $r["original_quality"];
  87. $this->value = $r["value"];
  88. $this->efficiency = $r["efficiency"];
  89. $this->max_efficiency = $r["max_efficiency"];
  90. $this->locked = $r["locked"];
  91. $this->quality_name = $this->set_quality_name($this->quality);
  92. $this->original_quality_name = $this->set_quality_name($this->original_quality);
  93. // TODO: Get unit ID
  94. // TODO: Move efficiencies to a separate method, but the profile API must read it so it's saved.
  95. // Get efficiencies, or calculate them if not yet set.
  96. if ($this->efficiency == null || $this->efficiency == "" || intval($this->efficiency) == 0){
  97. $this->efficiency = $this->calculate_efficiency();
  98. $this->max_efficiency = $this->calculate_maximum_efficiency();
  99. $statement = get_context()->get_db()->prepare("
  100. UPDATE artifact
  101. SET
  102. efficiency = :efficiency,
  103. max_efficiency = :max_efficiency
  104. WHERE id = :artifact
  105. ");
  106. $statement->bindValue(':efficiency', $this->efficiency, SQLITE3_FLOAT);
  107. $statement->bindValue(':max_efficiency', $this->max_efficiency, SQLITE3_FLOAT);
  108. $statement->bindValue(':artifact', $this->id, SQLITE3_INTEGER);
  109. $statement->execute();
  110. }
  111. }
  112. }
  113. /**
  114. * Gets the quality name from the mappings.
  115. *
  116. * @param int $q Quality id.
  117. * @return string Quality (Uppercase)
  118. */
  119. private function set_quality_name($q){
  120. switch ($q){
  121. case QUALITY_ID::NORMAL:
  122. return "NORMAL";
  123. case QUALITY_ID::MAGIC:
  124. return "MAGIC";
  125. case QUALITY_ID::RARE:
  126. return "RARE";
  127. case QUALITY_ID::HERO:
  128. return "HERO";
  129. case QUALITY_ID::LEGEND:
  130. return "LEGEND";
  131. default:
  132. return "";
  133. }
  134. }
  135. /**
  136. * Calcuates the efficiency of the artifact.
  137. *
  138. * @return float Efficiency
  139. */
  140. private function calculate_efficiency(){
  141. $eff = [0.0, 0.0, 0.0, 0.0];
  142. $query = "
  143. SELECT
  144. min,
  145. max
  146. FROM
  147. artifact_roll_type,
  148. artifact_effect_roll
  149. WHERE
  150. artifact_roll_type.id = artifact_effect_roll.type AND
  151. artifact_roll_type.initial = :initial AND
  152. artifact_roll_type.upgrade = :upgrade AND
  153. artifact_effect_roll.effect = :effect
  154. ";
  155. for ($i = 0; $i <= 3; $i ++){
  156. if (sizeof($this->effects) > $i && $this->effects[$i] != null && $this->effects[$i]->value > 0){
  157. $value = $this->effects[$i]->value;
  158. $value = $this->effects[$i]->value;
  159. $statement = get_context()->get_db()->prepare($query);
  160. $statement->bindValue(':initial', 1, SQLITE3_TEXT);
  161. $statement->bindValue(':upgrade', 1, SQLITE3_TEXT);
  162. $statement->bindValue(':effect', $this->effects[$i]->effect, SQLITE3_TEXT);
  163. $r_roll = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  164. if ($r_roll ){
  165. $min_initial = 0;//$r_initial["min"];
  166. $max_initial = $r_roll["max"];
  167. $min_upgrade = 0;//$r_upgrade["min"];
  168. $max_upgrade = $r_roll["max"];
  169. $eff[$i] = ((5 * $value) - ( 4 * $min_upgrade + $min_initial)) / ( 8 * ($max_initial - $min_initial + (4 * ($max_upgrade + $min_upgrade))));
  170. }
  171. }
  172. }
  173. $efficiency = 0.0;
  174. for ($i = 0; $i <= 3; $i ++){
  175. $efficiency += $eff[$i];
  176. }
  177. $efficiency *= 100;
  178. return $efficiency;
  179. }
  180. private function load_unit(){
  181. $statement = get_context()->get_db()->prepare("
  182. SELECT unit
  183. FROM unit_artifact
  184. WHERE
  185. artifact = :artifact AND
  186. rta = :rta;
  187. ");
  188. $statement->bindValue(':artifact', $this->id, SQLITE3_TEXT);
  189. $statement->bindValue(':rta', get_context()->get_game_mode(), SQLITE3_TEXT);
  190. $q = $statement->execute();
  191. $r = $q->fetchArray(SQLITE3_ASSOC);
  192. if ($r){
  193. $this->unit_id = $r["unit"];
  194. $this->unit = new Unit($r["unit"]);
  195. }
  196. $this->flag_unit = true;
  197. }
  198. private function load_stats(){
  199. $this->stat = new Artifact_Stat($this->id);
  200. // Get effects
  201. $statement = get_context()->get_db()->prepare("
  202. SELECT slot
  203. FROM artifact_effect
  204. WHERE artifact = :artifact
  205. ORDER BY slot;
  206. ");
  207. $statement->bindValue(':artifact', $this->id, SQLITE3_TEXT);
  208. $q = $statement->execute();
  209. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  210. array_push($this->effects, new Artifact_Effect($this->id, $r["slot"]));
  211. }
  212. $this->flag_stats = true;
  213. }
  214. /**
  215. * Calcuates the maximum efficiency of the artifact.
  216. *
  217. * @return float Efficiency
  218. */
  219. private function calculate_maximum_efficiency(){
  220. $max_efficiency = $this->efficiency;
  221. if ($this->level < 12){
  222. $max_efficiency += (1/8);
  223. }
  224. if ($this->level < 9){
  225. $max_efficiency += (1/8);
  226. }
  227. if ($this->level < 6){
  228. $max_efficiency += (1/8);
  229. }
  230. if ($this->level < 3){
  231. $max_efficiency += (1/8);
  232. }
  233. return $max_efficiency;
  234. }
  235. /**
  236. * Retrieves the owner's player identifier.
  237. *
  238. * @return int Player ID.
  239. */
  240. public function get_player_id(){
  241. return $this->player_id;
  242. }
  243. /**
  244. * Retrieves the artifact identifier.
  245. *
  246. * @return string Artifact ID.
  247. */
  248. public function get_id(){
  249. return $this->id;
  250. }
  251. /**
  252. * Checks if the artifact is elemental or archetiplal.
  253. *
  254. * @return int ARTIFACT_TYPE_ID::ELEMENT or ARTIFACT_TYPE:ARCHETYPE.
  255. */
  256. public function get_type(){
  257. return $this->type;
  258. }
  259. /**
  260. * Cheks if the artifact is an elemental one.
  261. *
  262. * @return boolean True for elemental artifacts, false for archetipal ones.
  263. */
  264. public function is_elemental(){
  265. return $this->elemental;
  266. }
  267. /**
  268. * Cheks if the artifact is an archetipal one.
  269. *
  270. * @return boolean True for archetipal artifacts, false for elemental ones.
  271. */
  272. public function is_archetypal(){
  273. return $this->archetypal;
  274. }
  275. /**
  276. * Retrieves the element of an elemental artifact.
  277. *
  278. * @see ELEMENT_ID
  279. *
  280. * @return int Element ID, null for archetipal artifacts.
  281. */
  282. public function get_element(){
  283. return $this->element;
  284. }
  285. /**
  286. * Retrieves the archetype of an archetipal artifact.
  287. *
  288. * @see ARCHETYPE_ID
  289. *
  290. * @return int Archetype ID, null for elemental artifacts.
  291. */
  292. public function get_archetype(){
  293. return $this->archetype;
  294. }
  295. /**
  296. * Retrieves the artifact level.
  297. *
  298. * @return int Artifact level (0-15).
  299. */
  300. public function get_level(){
  301. return $this->level;
  302. }
  303. /**
  304. * Retrieves the quality of the artifact.
  305. *
  306. * @see QUALITY_ID
  307. *
  308. * @return int Quality of the artifact.
  309. */
  310. public function get_quality(){
  311. return $this->quality;
  312. }
  313. /**
  314. * Retrieves the original quality of the artifact.
  315. *
  316. * @see QUALITY_ID
  317. *
  318. * @return int Original quality of the artifact.
  319. */
  320. public function get_original_quality(){
  321. return $this->original_quality;
  322. }
  323. /**
  324. * Retrieves the quality name.
  325. *
  326. * @return string Quality name, uppercase.
  327. */
  328. public function get_quality_name(){
  329. return $this->quality_name;
  330. }
  331. /**
  332. * Retrieves the original quality name.
  333. *
  334. * @return string Original quality name, uppercase.
  335. */
  336. public function get_original_quality_name(){
  337. return $this->original_quality_name;
  338. }
  339. /**
  340. * Retrieves the value of the artifact.
  341. *
  342. * @return int artifact value.
  343. */
  344. public function get_value(){
  345. return $this->value;
  346. }
  347. /**
  348. * Retrieves the efficiency of the artifact.
  349. *
  350. * @return float Artifact efficiency (0-100).
  351. */
  352. public function get_efficiency(){
  353. return $this->efficiency;
  354. }
  355. /**
  356. * Retrieves the maximum efficiency of the artifact.
  357. *
  358. * @return float Artifact maximum efficiency (0-100).
  359. */
  360. public function get_max_efficiency(){
  361. return $this->max_efficiency;
  362. }
  363. /**
  364. * Retrieves the artifact's main stat.
  365. *
  366. * @return Artifact_Stat Artifact main stat.
  367. */
  368. public function get_stat(){
  369. if (!$this->flag_stats){
  370. $this->load_stats();
  371. }
  372. return $this->stat;
  373. }
  374. /**
  375. * Retrieves a list of the artifact's effects.
  376. *
  377. * @return Artifact_Effect[] List of effects.
  378. */
  379. public function get_effects(){
  380. if (!$this->flag_stats){
  381. $this->load_stats();
  382. }
  383. return $this->effects;
  384. }
  385. /**
  386. * Retrieves the identifier of the unit who has eqquiped the artifact.
  387. *
  388. * @return string Unit ID.
  389. */
  390. public function get_unit_id(){
  391. if (!$this->flag_unit){
  392. $this->load_unit();
  393. }
  394. return $this->unit_id;
  395. }
  396. /**
  397. * Retrieves the unit who has eqquiped the artifact.
  398. *
  399. * @return Unit Unit who has equipped the artifact.
  400. */
  401. public function get_unit(){
  402. if (!$this->flag_unit){
  403. $this->load_unit();
  404. }
  405. return $this->unit;
  406. }
  407. }
  408. ?>