Artifact.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. }
  168. }
  169. }
  170. /**
  171. * Gets the quality name from the mappings.
  172. *
  173. * @param int $q Quality id.
  174. * @return string Quality name (uppercase)
  175. */
  176. private function set_quality_name($q){
  177. switch ($q){
  178. case QUALITY_ID::NORMAL:
  179. return "NORMAL";
  180. case QUALITY_ID::MAGIC:
  181. return "MAGIC";
  182. case QUALITY_ID::RARE:
  183. return "RARE";
  184. case QUALITY_ID::HERO:
  185. return "HERO";
  186. case QUALITY_ID::LEGEND:
  187. return "LEGEND";
  188. default:
  189. return "";
  190. }
  191. }
  192. /**
  193. * Calcuates the efficiency of the artifact.
  194. *
  195. * @return float Efficiency
  196. */
  197. private function calculate_efficiency(){
  198. $eff = [0.0, 0.0, 0.0, 0.0];
  199. $query = "
  200. SELECT
  201. min,
  202. max
  203. FROM
  204. artifact_roll_type,
  205. artifact_effect_roll
  206. WHERE
  207. artifact_roll_type.id = artifact_effect_roll.type AND
  208. artifact_roll_type.initial = :initial AND
  209. artifact_roll_type.upgrade = :upgrade AND
  210. artifact_effect_roll.effect = :effect
  211. ";
  212. for ($i = 0; $i <= 3; $i ++){
  213. if (sizeof($this->effects) > $i && $this->effects[$i] != null && $this->effects[$i]->value > 0){
  214. $value = $this->effects[$i]->value;
  215. $value = $this->effects[$i]->value;
  216. $statement = get_context()->get_db()->prepare($query);
  217. $statement->bindValue(':initial', 1, SQLITE3_TEXT);
  218. $statement->bindValue(':upgrade', 1, SQLITE3_TEXT);
  219. $statement->bindValue(':effect', $this->effects[$i]->effect, SQLITE3_TEXT);
  220. $r_roll = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  221. if ($r_roll ){
  222. $min_initial = 0;//$r_initial["min"];
  223. $max_initial = $r_roll["max"];
  224. $min_upgrade = 0;//$r_upgrade["min"];
  225. $max_upgrade = $r_roll["max"];
  226. $eff[$i] = ((5 * $value) - ( 4 * $min_upgrade + $min_initial)) / ( 8 * ($max_initial - $min_initial + (4 * ($max_upgrade + $min_upgrade))));
  227. }
  228. }
  229. }
  230. $efficiency = 0.0;
  231. for ($i = 0; $i <= 3; $i ++){
  232. $efficiency += $eff[$i];
  233. }
  234. $efficiency *= 100;
  235. return $efficiency;
  236. }
  237. /**
  238. * Loads the unit that equips the artifact into the entity (if any).
  239. *
  240. * Must be called only once before trying to get the unit. Sets the flag
  241. * {@link Artifact:$flag_unit} to true.
  242. */
  243. private function load_unit(){
  244. $statement = get_context()->get_db()->prepare("
  245. SELECT unit
  246. FROM unit_artifact
  247. WHERE
  248. artifact = :artifact AND
  249. rta = :rta;
  250. ");
  251. $statement->bindValue(':artifact', $this->id, SQLITE3_TEXT);
  252. $statement->bindValue(':rta', get_context()->get_game_mode(), SQLITE3_TEXT);
  253. $q = $statement->execute();
  254. $r = $q->fetchArray(SQLITE3_ASSOC);
  255. if ($r){
  256. $this->unit_id = $r["unit"];
  257. $this->unit = new Unit($r["unit"]);
  258. }
  259. $this->flag_unit = true;
  260. }
  261. /**
  262. * Loads the artifact main stat and effects into the entity.
  263. *
  264. * Must be called only once before trying to get the stat or effects. Sets the flag
  265. * {@link Artifact:$flag_stats} to true.
  266. */
  267. private function load_stats(){
  268. $this->stat = new Artifact_Stat($this->id);
  269. $statement = get_context()->get_db()->prepare("
  270. SELECT slot
  271. FROM artifact_effect
  272. WHERE artifact = :artifact
  273. ORDER BY slot;
  274. ");
  275. $statement->bindValue(':artifact', $this->id, SQLITE3_TEXT);
  276. $q = $statement->execute();
  277. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  278. array_push($this->effects, new Artifact_Effect($this->id, $r["slot"]));
  279. }
  280. $this->flag_stats = true;
  281. }
  282. /**
  283. * Calcuates the maximum efficiency of the artifact.
  284. *
  285. * @return float Efficiency.
  286. */
  287. private function calculate_maximum_efficiency(){
  288. $max_efficiency = $this->efficiency;
  289. if ($this->level < 12){
  290. $max_efficiency += (1/8);
  291. }
  292. if ($this->level < 9){
  293. $max_efficiency += (1/8);
  294. }
  295. if ($this->level < 6){
  296. $max_efficiency += (1/8);
  297. }
  298. if ($this->level < 3){
  299. $max_efficiency += (1/8);
  300. }
  301. return $max_efficiency;
  302. }
  303. /**
  304. * Retrieves the owner's player identifier.
  305. *
  306. * @return int Player ID.
  307. */
  308. public function get_player_id(){
  309. return $this->player_id;
  310. }
  311. /**
  312. * Retrieves the artifact identifier.
  313. *
  314. * @return string Artifact ID.
  315. */
  316. public function get_id(){
  317. return $this->id;
  318. }
  319. /**
  320. * Checks if the artifact is elemental or archetiplal.
  321. *
  322. * @return int ARTIFACT_TYPE_ID::ELEMENT or ARTIFACT_TYPE:ARCHETYPE.
  323. */
  324. public function get_type(){
  325. return $this->type;
  326. }
  327. /**
  328. * Cheks if the artifact is an elemental one.
  329. *
  330. * @return bool True for elemental artifacts, false for archetipal ones.
  331. */
  332. public function is_elemental(){
  333. return $this->elemental;
  334. }
  335. /**
  336. * Cheks if the artifact is an archetipal one.
  337. *
  338. * @return bool True for archetipal artifacts, false for elemental ones.
  339. */
  340. public function is_archetypal(){
  341. return $this->archetypal;
  342. }
  343. /**
  344. * Retrieves the element of an elemental artifact.
  345. *
  346. * @see ELEMENT_ID
  347. *
  348. * @return int Element ID, null for archetipal artifacts.
  349. */
  350. public function get_element(){
  351. return $this->element;
  352. }
  353. /**
  354. * Retrieves the archetype of an archetipal artifact.
  355. *
  356. * @see ARCHETYPE_ID
  357. *
  358. * @return int Archetype ID, null for elemental artifacts.
  359. */
  360. public function get_archetype(){
  361. return $this->archetype;
  362. }
  363. /**
  364. * Retrieves the artifact level.
  365. *
  366. * @return int Artifact level (0-15).
  367. */
  368. public function get_level(){
  369. return $this->level;
  370. }
  371. /**
  372. * Retrieves the quality of the artifact.
  373. *
  374. * @see QUALITY_ID
  375. *
  376. * @return int Quality of the artifact.
  377. */
  378. public function get_quality(){
  379. return $this->quality;
  380. }
  381. /**
  382. * Retrieves the original quality of the artifact.
  383. *
  384. * @see QUALITY_ID
  385. *
  386. * @return int Original quality of the artifact.
  387. */
  388. public function get_original_quality(){
  389. return $this->original_quality;
  390. }
  391. /**
  392. * Retrieves the quality name.
  393. *
  394. * @return string Quality name, uppercase.
  395. */
  396. public function get_quality_name(){
  397. return $this->quality_name;
  398. }
  399. /**
  400. * Retrieves the original quality name.
  401. *
  402. * @return string Original quality name, uppercase.
  403. */
  404. public function get_original_quality_name(){
  405. return $this->original_quality_name;
  406. }
  407. /**
  408. * Retrieves the value of the artifact.
  409. *
  410. * @return int artifact value.
  411. */
  412. public function get_value(){
  413. return $this->value;
  414. }
  415. /**
  416. * Retrieves the efficiency of the artifact.
  417. *
  418. * @return float Artifact efficiency (0-100).
  419. */
  420. public function get_efficiency(){
  421. return $this->efficiency;
  422. }
  423. /**
  424. * Retrieves the maximum efficiency of the artifact.
  425. *
  426. * @return float Artifact maximum efficiency (0-100).
  427. */
  428. public function get_max_efficiency(){
  429. return $this->max_efficiency;
  430. }
  431. /**
  432. * Retrieves the artifact's main stat.
  433. *
  434. * @return Artifact_Stat Artifact main stat.
  435. */
  436. public function get_stat(){
  437. if (!$this->flag_stats){
  438. $this->load_stats();
  439. }
  440. return $this->stat;
  441. }
  442. /**
  443. * Retrieves a list of the artifact's effects.
  444. *
  445. * @return Artifact_Effect[] List of effects.
  446. */
  447. public function get_effects(){
  448. if (!$this->flag_stats){
  449. $this->load_stats();
  450. }
  451. return $this->effects;
  452. }
  453. /**
  454. * Retrieves the identifier of the unit who has eqquiped the artifact.
  455. *
  456. * @return string Unit ID.
  457. */
  458. public function get_unit_id(){
  459. if (!$this->flag_unit){
  460. $this->load_unit();
  461. }
  462. return $this->unit_id;
  463. }
  464. /**
  465. * Retrieves the unit who has eqquiped the artifact.
  466. *
  467. * @return Unit Unit who has equipped the artifact.
  468. */
  469. public function get_unit(){
  470. if (!$this->flag_unit){
  471. $this->load_unit();
  472. }
  473. return $this->unit;
  474. }
  475. }