Artifact.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. <?php
  2. /**
  3. * Artifact entity file.
  4. *
  5. * Creates the entity and makes it available.
  6. *
  7. * @author Iñigo Valentin <i@inigovalentin.com>
  8. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
  9. * @package SWDB
  10. */
  11. require_once(PATH::ENTITY . "Entity.php");
  12. require_once(PATH::ENTITY . "Artifact_Stat.php");
  13. require_once(PATH::ENTITY . "Artifact_Effect.php");
  14. /**
  15. * Artifact.
  16. *
  17. * Represents a game artifact.
  18. *
  19. * @category Entity
  20. */
  21. class Artifact extends Entity{
  22. /**
  23. * @var int The owner's player ID.
  24. */
  25. private $player_id;
  26. /**
  27. * @var int The artifact ID.
  28. */
  29. private $id;
  30. /**
  31. * @var int The artifact type ({@see ARTIFACT_TYPE}).
  32. */
  33. private $type;
  34. /**
  35. * @var bool Indicates if is an elemental artifact.
  36. */
  37. private $elemental = false;
  38. /**
  39. * @var bool Indicates it the artifact is archetypal.
  40. */
  41. private $archetypal = false;
  42. /**
  43. * @var int Artifact element ID, null for archetypals ({@see ELEMENT_ID}).
  44. */
  45. private $element;
  46. /**
  47. * @var int Artifact archetype ID, null for elementals ({@see ARCHETYPE_ID}).
  48. */
  49. private $archetype;
  50. /**
  51. * @var int Artifact level.
  52. */
  53. private $level;
  54. /**
  55. * @var int Artifact quality ({@see QUALITY_ID}).
  56. */
  57. private $quality;
  58. /**
  59. * @var int Artifact original quality ({@see QUALITY_ID}).
  60. */
  61. private $original_quality;
  62. /**
  63. * @var string Artifact quality name.
  64. */
  65. private $quality_name;
  66. /**
  67. * @var string Artifact original quality name.
  68. */
  69. private $original_quality_name;
  70. /**
  71. * @var string Artifact value in mana stones.
  72. */
  73. private $value;
  74. /**
  75. * @var float Artifact efficiency.
  76. */
  77. private $efficiency;
  78. /**
  79. * @var float Artifact maximum efficiency.
  80. */
  81. private $max_efficiency;
  82. /**
  83. * @var bool Internal flag to check if the stat and effects have been loaded into the entity.
  84. */
  85. private $flag_stats = false;
  86. /**
  87. * @var Artifact_Stat The artifact main stat.
  88. */
  89. private $stat;
  90. /**
  91. * @var Artifact_Effect[] Every effect the artifact currently has.
  92. */
  93. private $effects = [];
  94. /**
  95. * @var bool Internal flag to check if the unit that equips the artifact (if any) has been
  96. * loaded.
  97. */
  98. private $flag_unit = false;
  99. /**
  100. * @var Unit Unit that equips the artifact in the curent game mode.
  101. */
  102. private $unit;
  103. /**
  104. * Constructor.
  105. *
  106. * Searches the database and retrieves the information about the
  107. * artifact, populating it and it's items.
  108. *
  109. * @param int $id Artifact id.
  110. */
  111. public function __construct($id){
  112. $statement = get_context()->get_db()->prepare("
  113. SELECT
  114. player,
  115. id,
  116. type,
  117. attribute,
  118. archetype,
  119. level,
  120. quality,
  121. original_quality,
  122. value,
  123. efficiency,
  124. max_efficiency,
  125. locked
  126. FROM artifact
  127. WHERE id = :id;
  128. ");
  129. $statement->bindValue(':id', $id, SQLITE3_TEXT);
  130. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  131. if ($r){
  132. $this->player_id = $r["player"];
  133. $this->id = $r["id"];
  134. $this->type = $r["type"];
  135. if ($r["type"] == ARTIFACT_TYPE::ARCHETYPE){
  136. $this->archetypal = true;
  137. }
  138. elseif ($r["type"] == ARTIFACT_TYPE::ELEMENT){
  139. $this->elemental = true;
  140. }
  141. $this->element = $r["attribute"];
  142. $this->archetype = $r["archetype"];
  143. $this->level = $r["level"];
  144. $this->quality = $r["quality"];
  145. $this->original_quality = $r["original_quality"];
  146. $this->value = $r["value"];
  147. $this->efficiency = $r["efficiency"];
  148. $this->max_efficiency = $r["max_efficiency"];
  149. $this->locked = $r["locked"];
  150. $this->quality_name = $this->set_quality_name($this->quality);
  151. $this->original_quality_name = $this->set_quality_name($this->original_quality);
  152. // Get efficiencies, or calculate them if not yet set.
  153. if ($this->efficiency == null || $this->efficiency == "" || intval($this->efficiency) == 0){
  154. $this->efficiency = $this->calculate_efficiency();
  155. $this->max_efficiency = $this->calculate_maximum_efficiency();
  156. $statement = get_context()->get_db()->prepare("
  157. UPDATE artifact
  158. SET
  159. efficiency = :efficiency,
  160. max_efficiency = :max_efficiency
  161. WHERE id = :artifact
  162. ");
  163. $statement->bindValue(':efficiency', $this->efficiency, SQLITE3_FLOAT);
  164. $statement->bindValue(':max_efficiency', $this->max_efficiency, SQLITE3_FLOAT);
  165. $statement->bindValue(':artifact', $this->id, SQLITE3_INTEGER);
  166. $statement->execute();
  167. $statement = get_context()->get_db()->prepare("
  168. UPDATE run_drop_artifact
  169. SET
  170. efficiency = :efficiency,
  171. max_efficiency = :max_efficiency
  172. WHERE id = :artifact
  173. ");
  174. $statement->bindValue(':efficiency', $this->efficiency, SQLITE3_FLOAT);
  175. $statement->bindValue(':max_efficiency', $this->max_efficiency, SQLITE3_FLOAT);
  176. $statement->bindValue(':artifact', $this->id, SQLITE3_INTEGER);
  177. $statement->execute();
  178. }
  179. }
  180. else{
  181. $statement = get_context()->get_db()->prepare("
  182. SELECT
  183. id,
  184. type,
  185. attribute,
  186. archetype,
  187. quality,
  188. value,
  189. efficiency,
  190. max_efficiency,
  191. effect_1,
  192. effect_2,
  193. effect_3,
  194. effect_4
  195. FROM run_drop_artifact
  196. WHERE id = :id;
  197. ");
  198. $statement->bindValue(':id', $id, SQLITE3_TEXT);
  199. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  200. if ($r){
  201. $this->id = $r["id"];
  202. $this->type = $r["type"];
  203. if ($r["type"] == ARTIFACT_TYPE::ARCHETYPE){
  204. $this->archetypal = true;
  205. }
  206. elseif ($r["type"] == ARTIFACT_TYPE::ELEMENT){
  207. $this->elemental = true;
  208. }
  209. $this->element = $r["attribute"];
  210. $this->archetype = $r["archetype"];
  211. $this->level = 0;
  212. $this->quality = $r["quality"];
  213. $this->original_quality = $r["quality"];
  214. $this->value = $r["value"];
  215. $this->efficiency = $r["efficiency"];
  216. $this->max_efficiency = $r["max_efficiency"];
  217. $this->locked = false;
  218. $this->quality_name = $this->set_quality_name($this->quality);
  219. $this->original_quality_name = $this->set_quality_name($this->original_quality);
  220. $this->stat = new Artifact_Stat($this->id);
  221. if (is_int($r["effect_1"])){
  222. array_push($this->effects, new Artifact_Effect($this->id, ARTIFACT_EFFECT_SLOT_ID::EFFECT_1));
  223. }
  224. if (is_int($r["effect_2"])){
  225. array_push($this->effects, new Artifact_Effect($this->id, ARTIFACT_EFFECT_SLOT_ID::EFFECT_2));
  226. }
  227. if (is_int($r["effect_3"])){
  228. array_push($this->effects, new Artifact_Effect($this->id, ARTIFACT_EFFECT_SLOT_ID::EFFECT_3));
  229. }
  230. if (is_int($r["effect_4"])){
  231. array_push($this->effects, new Artifact_Effect($this->id, ARTIFACT_EFFECT_SLOT_ID::EFFECT_4));
  232. }
  233. $this->flag_stats = true;
  234. // Get efficiencies, or calculate them if not yet set.
  235. if ($this->efficiency == null || $this->efficiency == "" || intval($this->efficiency) == 0){
  236. $this->efficiency = $this->calculate_efficiency();
  237. $this->max_efficiency = $this->calculate_maximum_efficiency();
  238. $statement = get_context()->get_db()->prepare("
  239. UPDATE run_drop_artifact
  240. SET
  241. efficiency = :efficiency,
  242. max_efficiency = :max_efficiency
  243. WHERE id = :artifact
  244. ");
  245. $statement->bindValue(':efficiency', $this->efficiency, SQLITE3_FLOAT);
  246. $statement->bindValue(':max_efficiency', $this->max_efficiency, SQLITE3_FLOAT);
  247. $statement->bindValue(':artifact', $this->id, SQLITE3_INTEGER);
  248. $statement->execute();
  249. }
  250. }
  251. }
  252. }
  253. /**
  254. * Gets the quality name from the mappings.
  255. *
  256. * @param int $q Quality id.
  257. * @return string Quality name (uppercase)
  258. */
  259. private function set_quality_name($q){
  260. switch ($q){
  261. case QUALITY_ID::NORMAL:
  262. return "NORMAL";
  263. case QUALITY_ID::MAGIC:
  264. return "MAGIC";
  265. case QUALITY_ID::RARE:
  266. return "RARE";
  267. case QUALITY_ID::HERO:
  268. return "HERO";
  269. case QUALITY_ID::LEGEND:
  270. return "LEGEND";
  271. default:
  272. return "";
  273. }
  274. }
  275. /**
  276. * Calcuates the efficiency of the artifact.
  277. *
  278. * @return float Efficiency
  279. */
  280. private function calculate_efficiency(){
  281. $eff = [0.0, 0.0, 0.0, 0.0];
  282. $query = "
  283. SELECT
  284. min,
  285. max
  286. FROM
  287. artifact_roll_type,
  288. artifact_effect_roll
  289. WHERE
  290. artifact_roll_type.id = artifact_effect_roll.type AND
  291. artifact_roll_type.initial = :initial AND
  292. artifact_roll_type.upgrade = :upgrade AND
  293. artifact_effect_roll.effect = :effect
  294. ";
  295. for ($i = 0; $i <= 3; $i ++){
  296. if (sizeof($this->effects) > $i && $this->effects[$i] != null && $this->effects[$i]->get_value() > 0){
  297. $value = $this->effects[$i]->get_value();
  298. $value = $this->effects[$i]->get_value();
  299. $statement = get_context()->get_db()->prepare($query);
  300. $statement->bindValue(':initial', 1, SQLITE3_TEXT);
  301. $statement->bindValue(':upgrade', 1, SQLITE3_TEXT);
  302. $statement->bindValue(':effect', $this->effects[$i]->get_effect(), SQLITE3_TEXT);
  303. $r_roll = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  304. if ($r_roll ){
  305. $min_initial = 0;//$r_initial["min"];
  306. $max_initial = $r_roll["max"];
  307. $min_upgrade = 0;//$r_upgrade["min"];
  308. $max_upgrade = $r_roll["max"];
  309. $eff[$i] = ((5 * $value) - ( 4 * $min_upgrade + $min_initial)) / ( 8 * ($max_initial - $min_initial + (4 * ($max_upgrade + $min_upgrade))));
  310. }
  311. }
  312. }
  313. $efficiency = 0.0;
  314. for ($i = 0; $i <= 3; $i ++){
  315. $efficiency += $eff[$i];
  316. }
  317. $efficiency *= 100;
  318. return $efficiency;
  319. }
  320. /**
  321. * Loads the unit that equips the artifact into the entity (if any).
  322. *
  323. * Must be called only once before trying to get the unit. Sets the flag
  324. * {@link Artifact:$flag_unit} to true.
  325. */
  326. private function load_unit(){
  327. $statement = get_context()->get_db()->prepare("
  328. SELECT unit
  329. FROM unit_artifact
  330. WHERE
  331. artifact = :artifact AND
  332. rta = :rta;
  333. ");
  334. $statement->bindValue(':artifact', $this->id, SQLITE3_TEXT);
  335. $statement->bindValue(':rta', get_context()->get_game_mode(), SQLITE3_TEXT);
  336. $q = $statement->execute();
  337. $r = $q->fetchArray(SQLITE3_ASSOC);
  338. if ($r){
  339. $this->unit_id = $r["unit"];
  340. $this->unit = new Unit($r["unit"]);
  341. }
  342. $this->flag_unit = true;
  343. }
  344. /**
  345. * Loads the artifact main stat and effects into the entity.
  346. *
  347. * Must be called only once before trying to get the stat or effects. Sets the flag
  348. * {@link Artifact:$flag_stats} to true.
  349. */
  350. private function load_stats(){
  351. $this->stat = new Artifact_Stat($this->id);
  352. $statement = get_context()->get_db()->prepare("
  353. SELECT slot
  354. FROM artifact_effect
  355. WHERE artifact = :artifact
  356. ORDER BY slot;
  357. ");
  358. $statement->bindValue(':artifact', $this->id, SQLITE3_TEXT);
  359. $q = $statement->execute();
  360. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  361. array_push($this->effects, new Artifact_Effect($this->id, $r["slot"]));
  362. }
  363. $this->flag_stats = true;
  364. }
  365. /**
  366. * Calcuates the maximum efficiency of the artifact.
  367. *
  368. * @return float Efficiency.
  369. */
  370. private function calculate_maximum_efficiency(){
  371. $max_efficiency = $this->efficiency;
  372. if ($this->level < 12){
  373. $max_efficiency += (1/8);
  374. }
  375. if ($this->level < 9){
  376. $max_efficiency += (1/8);
  377. }
  378. if ($this->level < 6){
  379. $max_efficiency += (1/8);
  380. }
  381. if ($this->level < 3){
  382. $max_efficiency += (1/8);
  383. }
  384. return $max_efficiency;
  385. }
  386. /**
  387. * Retrieves the owner's player identifier.
  388. *
  389. * @return int Player ID.
  390. */
  391. public function get_player_id(){
  392. return $this->player_id;
  393. }
  394. /**
  395. * Retrieves the artifact identifier.
  396. *
  397. * @return string Artifact ID.
  398. */
  399. public function get_id(){
  400. return $this->id;
  401. }
  402. /**
  403. * Checks if the artifact is elemental or archetiplal.
  404. *
  405. * @return int ARTIFACT_TYPE_ID::ELEMENT or ARTIFACT_TYPE:ARCHETYPE.
  406. */
  407. public function get_type(){
  408. return $this->type;
  409. }
  410. /**
  411. * Cheks if the artifact is an elemental one.
  412. *
  413. * @return bool True for elemental artifacts, false for archetipal ones.
  414. */
  415. public function is_elemental(){
  416. return $this->elemental;
  417. }
  418. /**
  419. * Cheks if the artifact is an archetipal one.
  420. *
  421. * @return bool True for archetipal artifacts, false for elemental ones.
  422. */
  423. public function is_archetypal(){
  424. return $this->archetypal;
  425. }
  426. /**
  427. * Retrieves the element of an elemental artifact.
  428. *
  429. * @see ELEMENT_ID
  430. *
  431. * @return int Element ID, null for archetipal artifacts.
  432. */
  433. public function get_element(){
  434. return $this->element;
  435. }
  436. /**
  437. * Retrieves the archetype of an archetipal artifact.
  438. *
  439. * @see ARCHETYPE_ID
  440. *
  441. * @return int Archetype ID, null for elemental artifacts.
  442. */
  443. public function get_archetype(){
  444. return $this->archetype;
  445. }
  446. /**
  447. * Retrieves the artifact level.
  448. *
  449. * @return int Artifact level (0-15).
  450. */
  451. public function get_level(){
  452. return $this->level;
  453. }
  454. /**
  455. * Retrieves the quality of the artifact.
  456. *
  457. * @see QUALITY_ID
  458. *
  459. * @return int Quality of the artifact.
  460. */
  461. public function get_quality(){
  462. return $this->quality;
  463. }
  464. /**
  465. * Retrieves the original quality of the artifact.
  466. *
  467. * @see QUALITY_ID
  468. *
  469. * @return int Original quality of the artifact.
  470. */
  471. public function get_original_quality(){
  472. return $this->original_quality;
  473. }
  474. /**
  475. * Retrieves the quality name.
  476. *
  477. * @return string Quality name, uppercase.
  478. */
  479. public function get_quality_name(){
  480. return $this->quality_name;
  481. }
  482. /**
  483. * Retrieves the original quality name.
  484. *
  485. * @return string Original quality name, uppercase.
  486. */
  487. public function get_original_quality_name(){
  488. return $this->original_quality_name;
  489. }
  490. /**
  491. * Retrieves the value of the artifact.
  492. *
  493. * @return int artifact value.
  494. */
  495. public function get_value(){
  496. return $this->value;
  497. }
  498. /**
  499. * Retrieves the efficiency of the artifact.
  500. *
  501. * @return float Artifact efficiency (0-100).
  502. */
  503. public function get_efficiency(){
  504. return $this->efficiency;
  505. }
  506. /**
  507. * Retrieves the maximum efficiency of the artifact.
  508. *
  509. * @return float Artifact maximum efficiency (0-100).
  510. */
  511. public function get_max_efficiency(){
  512. return $this->max_efficiency;
  513. }
  514. /**
  515. * Retrieves the artifact's main stat.
  516. *
  517. * @return Artifact_Stat Artifact main stat.
  518. */
  519. public function get_stat(){
  520. if (!$this->flag_stats){
  521. $this->load_stats();
  522. }
  523. return $this->stat;
  524. }
  525. /**
  526. * Retrieves a list of the artifact's effects.
  527. *
  528. * @return Artifact_Effect[] List of effects.
  529. */
  530. public function get_effects(){
  531. if (!$this->flag_stats){
  532. $this->load_stats();
  533. }
  534. return $this->effects;
  535. }
  536. /**
  537. * Retrieves the identifier of the unit who has eqquiped the artifact.
  538. *
  539. * @return string Unit ID.
  540. */
  541. public function get_unit_id(){
  542. if (!$this->flag_unit){
  543. $this->load_unit();
  544. }
  545. return $this->unit_id;
  546. }
  547. /**
  548. * Retrieves the unit who has eqquiped the artifact.
  549. *
  550. * @return Unit Unit who has equipped the artifact.
  551. */
  552. public function get_unit(){
  553. if (!$this->flag_unit){
  554. $this->load_unit();
  555. }
  556. return $this->unit;
  557. }
  558. }