Player.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. <?php
  2. /**
  3. * Player 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 . "Unit.php");
  14. require_once(PATH::ENTITY . "Building.php");
  15. require_once(PATH::ENTITY . "Decoration.php");
  16. require_once(PATH::ENTITY . "Item.php");
  17. require_once(PATH::ENTITY . "Record.php");
  18. require_once(PATH::ENTITY . "Server.php");
  19. /**
  20. * A Player.
  21. *
  22. * Represents an object from the table 'player'.
  23. *
  24. * @category Entity
  25. */
  26. class Player extends Entity{
  27. private $id;
  28. private $server;
  29. private $country;
  30. private $language;
  31. private $level;
  32. private $name;
  33. private $experience;
  34. private $rep = null;
  35. private $rep_id = null;
  36. private $joined = 0;
  37. private $top_rank_arena = 0;
  38. private $top_rank_world_arena = 0;
  39. private $top_rank_special_league = 0;
  40. private $top_rank_gw = 0;
  41. private $top_rank_siege = 0;
  42. private $top_rank_wboss = 0;
  43. private $top_rank_toan = 0;
  44. private $top_rank_toah = 0;
  45. private $top_rank_toal = 0;
  46. private $flag_defense = false;
  47. private $arena_defense = [];
  48. private $gw_defense = [[], []];
  49. private $siege_defense = [];
  50. private $currency = [
  51. "mana" => 0,
  52. "crystal" => 0,
  53. "energy" => 0,
  54. "energy_max" => 0,
  55. "arena_energy" => 0,
  56. "darkportal_energy" => 0,
  57. "dimension_energy" => 0,
  58. "honor_point" => 0,
  59. "guild_point" => 0,
  60. "honor_medal" => 0,
  61. "honor_mark" => 0,
  62. "event_coin" => 0,
  63. "social_point" => 0,
  64. "ancient_stone" => 0,
  65. "costume_point" => 0,
  66. ];
  67. private $flag_building = false;
  68. private $building = [];
  69. private $decoration = [];
  70. private $flag_item = false;
  71. private $item_scroll = [];
  72. private $item_craft_rune = [];
  73. private $item_craft = [];
  74. private $item_essence = [];
  75. private $flag_record = false;
  76. private $record = [];
  77. private $public = false;
  78. /**
  79. * Constructor.
  80. *
  81. * Searches the database and retrieves the information about the
  82. * player, populating it and it's items.
  83. *
  84. * @param int $id Monster identifier.
  85. * @param bool $complete If false, query only monster and k_monster.
  86. */
  87. public function __construct($id, $complete = true){
  88. $statement = get_context()->get_db()->prepare("
  89. SELECT
  90. id,
  91. server,
  92. country,
  93. lang,
  94. name,
  95. level,
  96. experience,
  97. rep,
  98. mana,
  99. crystal,
  100. energy,
  101. energy_max,
  102. arena_energy,
  103. darkportal_energy,
  104. dimension_energy,
  105. honor_point,
  106. guild_point,
  107. honor_medal,
  108. honor_mark,
  109. event_coin,
  110. social_point,
  111. costume_point,
  112. joined,
  113. top_rank_arena,
  114. top_rank_world_arena,
  115. top_rank_special_league,
  116. top_rank_gw,
  117. top_rank_siege,
  118. top_rank_wboss,
  119. top_rank_toan,
  120. top_rank_toah,
  121. top_rank_toal,
  122. public
  123. FROM player
  124. WHERE id = :id;
  125. ");
  126. $statement->bindValue(':id', $id, SQLITE3_TEXT);
  127. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  128. if ($r){
  129. $this->id = $r["id"];
  130. $this->server = new Server($r["server"]);
  131. $this->country = $r["country"];
  132. $this->lang = $r["lang"];
  133. $this->name = $r["name"];
  134. $this->level = $r["level"];
  135. $this->experience = $r["experience"];
  136. if (strlen($r["rep"]) > 0){
  137. $this->rep_id = $r["rep"];
  138. }
  139. $this->currency["mana"] = $r["mana"];
  140. $this->currency["crystal"] = $r["crystal"];
  141. $this->currency["energy"] = $r["energy"];
  142. $this->currency["energy_max"] = $r["energy_max"];
  143. $this->currency["arena_energy"] = $r["arena_energy"];
  144. $this->currency["darkportal_energy"] = $r["darkportal_energy"];
  145. $this->currency["dimension_energy"] = $r["dimension_energy"];
  146. $this->currency["honor_point"] = $r["honor_point"];
  147. $this->currency["guild_point"] = $r["guild_point"];
  148. $this->currency["honor_medal"] = $r["honor_medal"];
  149. $this->currency["honor_mark"] = $r["honor_mark"];
  150. $this->currency["event_coin"] = $r["event_coin"];
  151. $this->currency["social_point"] = $r["social_point"];
  152. $this->currency["costume_point"] = $r["costume_point"];
  153. $this->joined = new Datetime();
  154. $this->joined->setTimestamp($r["joined"]);
  155. $this->top_rank_arena = $r["top_rank_arena"];
  156. $this->top_rank_world_arena = $r["top_rank_world_arena"];
  157. $this->top_rank_special_league = $r["top_rank_special_league"];
  158. $this->top_rank_gw = $r["top_rank_gw"];
  159. $this->top_rank_siege = $r["top_rank_siege"];
  160. $this->top_rank_wboss = $r["top_rank_wboss"];
  161. $this->top_rank_toan = $r["top_rank_toan"];
  162. $this->top_rank_toah = $r["top_rank_toah"];
  163. $this->top_rank_toal = $r["top_rank_toal"];
  164. if (strlen($r["public"]) == 1){
  165. $this->public = true;
  166. }
  167. }
  168. }
  169. private function load_defenses(){
  170. $statement = get_context()->get_db()->prepare("
  171. SELECT unit
  172. FROM defense
  173. WHERE
  174. player = :player AND
  175. area_type = :area_type
  176. ORDER BY position;
  177. ");
  178. $statement->bindValue(':player', $this->id, SQLITE3_TEXT);
  179. $statement->bindValue(':area_type', AREA_TYPE_ID::ARENA, SQLITE3_INTEGER);
  180. $q = $statement->execute();
  181. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  182. array_push($this->arena_defense, new Unit($r["unit"], false));
  183. }
  184. $statement = get_context()->get_db()->prepare("
  185. SELECT
  186. unit,
  187. position
  188. FROM defense
  189. WHERE
  190. player = :player AND
  191. area_type = :area_type
  192. ORDER BY position;
  193. ");
  194. $statement->bindValue(':id', $this->id, SQLITE3_TEXT);
  195. $statement->bindValue(':area_type', AREA_TYPE_ID::GUILD_WAR, SQLITE3_INTEGER);
  196. $q = $statement->execute();
  197. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  198. if ($r["position"] <= 3){
  199. array_push($this->gw_defense[0], new Unit($r["unit"], false));
  200. }
  201. else{
  202. array_push($this->gw_defense[1], new Unit($r["unit"], false));
  203. }
  204. }
  205. // TODO: Siege defenses
  206. $this->flag_defense = true;
  207. }
  208. private function load_buildings(){
  209. $statement = get_context()->get_db()->prepare("
  210. SELECT
  211. building.id AS id,
  212. min(building.buildable) AS min
  213. FROM
  214. building,
  215. buildable
  216. WHERE
  217. player = :player AND
  218. building.buildable = buildable.id
  219. GROUP BY building.buildable;
  220. ");
  221. $statement->bindValue(':player', $this->id, SQLITE3_TEXT);
  222. $q = $statement->execute();
  223. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  224. array_push($this->building, new Building($r["id"]));
  225. }
  226. $statement = get_context()->get_db()->prepare("
  227. SELECT id
  228. FROM decoration
  229. WHERE player = :player;
  230. ");
  231. $statement->bindValue(':player', $this->id, SQLITE3_TEXT);
  232. $q = $statement->execute();
  233. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  234. array_push($this->decoration, new Decoration($r["id"]));
  235. }
  236. $this->flag_building = true;
  237. }
  238. private function load_items(){
  239. $statement = get_context()->get_db()->prepare("
  240. SELECT
  241. id,
  242. type
  243. FROM item
  244. WHERE
  245. player = :player AND
  246. type = :type AND
  247. amount > 0
  248. ORDER BY item.type;
  249. ");
  250. $statement->bindValue(':player', $this->id, SQLITE3_TEXT);
  251. $statement->bindValue(':type', INVENTORY_TYPE_ID::SCROLL, SQLITE3_TEXT);
  252. $q = $statement->execute();
  253. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  254. array_push($this->item_scroll, new item($r["id"], $r["type"]));
  255. }
  256. // Rune crafting items are marked as generic crafting.
  257. $statement = get_context()->get_db()->prepare("
  258. SELECT
  259. id,
  260. type
  261. FROM item
  262. WHERE
  263. player = :player AND
  264. type = :type AND
  265. amount > 0
  266. ORDER BY id;
  267. ");
  268. $statement->bindValue(':player', $this->id, SQLITE3_TEXT);
  269. $statement->bindValue(':type', INVENTORY_TYPE_ID::ENCHANTMENT, SQLITE3_TEXT);
  270. $q = $statement->execute();
  271. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  272. array_push($this->item_craft_rune, new item($r["id"], $r["type"]));
  273. }
  274. $statement = get_context()->get_db()->prepare("
  275. SELECT
  276. id,
  277. type
  278. FROM item
  279. WHERE
  280. player = :player AND
  281. type = :type AND
  282. amount > 0
  283. ORDER BY id;
  284. ");
  285. $statement->bindValue(':player', $this->id, SQLITE3_TEXT);
  286. $statement->bindValue(':type', INVENTORY_TYPE_ID::CRAFT_STUFF, SQLITE3_TEXT);
  287. $q = $statement->execute();
  288. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  289. array_push($this->item_craft, new item($r["id"], $r["type"]));
  290. }
  291. $statement = get_context()->get_db()->prepare("
  292. SELECT
  293. item.id AS id,
  294. item.type AS type
  295. FROM
  296. item,
  297. inventory
  298. WHERE
  299. item.id = inventory.id AND
  300. item.type = inventory.type AND
  301. item.player = :player AND
  302. item.type = :type AND
  303. amount > 0
  304. ORDER BY
  305. upper(inventory.name) NOT LIKE '%MAGIC%',
  306. upper(inventory.name) NOT LIKE '%FIRE%',
  307. upper(inventory.name) NOT LIKE '%WATER%',
  308. upper(inventory.name) NOT LIKE '%WIND%',
  309. upper(inventory.name) NOT LIKE '%LIGHT%',
  310. upper(inventory.name) NOT LIKE '%DARK%',
  311. upper(inventory.name) NOT LIKE '%LOW%',
  312. upper(inventory.name) NOT LIKE '%MID%',
  313. upper(inventory.name) NOT LIKE '%HIGH%'
  314. ;
  315. ");
  316. $statement->bindValue(':player', $this->id, SQLITE3_TEXT);
  317. $statement->bindValue(':type', INVENTORY_TYPE_ID::ESSENCES, SQLITE3_TEXT);
  318. $q = $statement->execute();
  319. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  320. array_push($this->item_essence, new item($r["id"], $r["type"]));
  321. }
  322. $this->flag_item = true;
  323. }
  324. private function load_records(){
  325. $statement = get_context()->get_db()->prepare("
  326. SELECT
  327. player,
  328. area_type,
  329. area
  330. FROM record
  331. WHERE player = :player
  332. ORDER BY
  333. CASE
  334. WHEN (area_type = 2 AND area = 8001) THEN 1 -- Giant's Keep
  335. WHEN (area_type = 2 AND area = 9001) THEN 2 -- Dragon's Lair
  336. WHEN (area_type = 2 AND area = 6001) THEN 3 -- Necropolis
  337. WHEN (area_type = 2 AND area = 9501) THEN 4 -- Steel Fortress
  338. WHEN (area_type = 2 AND area = 9502) THEN 5 -- Punisher's Crypt
  339. WHEN (area_type = 4) THEN 6 -- Rift of Worlds
  340. WHEN (area_type = 3 AND area = 1001) THEN 7 -- Rift Dungeon - Ice Beast
  341. WHEN (area_type = 3 AND area = 2001) THEN 8 -- Rift Dungeon - Fire Beast
  342. WHEN (area_type = 3 AND area = 3001) THEN 9 -- Rift Dungeon - Wind Beast
  343. WHEN (area_type = 3 AND area = 4001) THEN 10 -- Rift Dungeon - Light Beast
  344. WHEN (area_type = 3 AND area = 5001) THEN 11 -- Rift Dungeon - Dark Beast
  345. WHEN (area_type = 2 AND area = 5001) THEN 12 -- Hall of Magic
  346. WHEN (area_type = 2 AND area = 3001) THEN 13 -- Hall of Water
  347. WHEN (area_type = 2 AND area = 2001) THEN 14 -- Hall of Fire
  348. WHEN (area_type = 2 AND area = 4001) THEN 15 -- Hall of Wind
  349. WHEN (area_type = 2 AND area = 7001) THEN 16 -- Hall of Light
  350. WHEN (area_type = 2 AND area = 1001) THEN 17 -- Hall of Dark
  351. ELSE area
  352. END ASC
  353. ");
  354. $statement->bindValue(':player', $this->id, SQLITE3_TEXT);
  355. $q = $statement->execute();
  356. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  357. array_push($this->record, new Record($r["player"], $r["area_type"], $r["area"]));
  358. }
  359. $this->flag_record = true;
  360. }
  361. /**
  362. * Retrieves the player identifier
  363. *
  364. * @return int Plyaer ID.
  365. */
  366. public function get_id(){
  367. return $this->id;
  368. }
  369. /**
  370. * Retrieves the player server.
  371. *
  372. * @return Server The player's account server.
  373. */
  374. public function get_server(){
  375. return $this->server;
  376. }
  377. /**
  378. * Retrieves the player's country.
  379. *
  380. * @return string Two digit country code.
  381. */
  382. public function get_country(){
  383. return $this->country;
  384. }
  385. /**
  386. * Retrieves the player's language.
  387. *
  388. * @return string Two digit language code.
  389. */
  390. public function get_language(){
  391. return $this->language;
  392. }
  393. /**
  394. * Retrieves the player's level,
  395. *
  396. * @return int Player's level
  397. */
  398. public function get_level(){
  399. return $this->level;
  400. }
  401. /**
  402. * Retrieves the player's name
  403. *
  404. * @return string The playe's name.
  405. */
  406. public function get_name(){
  407. return $this->name;
  408. }
  409. /**
  410. * Retrieves the total experience earned by the user.
  411. *
  412. * @return number Total experience.
  413. */
  414. public function get_experience(){
  415. return $this->experience;
  416. }
  417. /**
  418. * Retrieves the player's representative unit.
  419. *
  420. * @return Unit Player's representative
  421. */
  422. public function get_rep(){
  423. if ($this->rep_id != null && $this->rep == null){
  424. $this->rep = new Unit($this->rep_id);
  425. }
  426. return $this->rep;
  427. }
  428. /**
  429. * Gets the date the user joined the game.
  430. *
  431. * If $format is supplied, a string in the desired format will be returned. If not, the raw
  432. * DateTime object will e returned.
  433. *
  434. * @param string $format A date format (optional).
  435. * @return DateTime | string The raw date, or a formatted version.
  436. */
  437. public function get_joined($format = null){
  438. if (null == $format){
  439. return $this->joined;
  440. }
  441. else{
  442. return $this->joined->format($format);
  443. }
  444. }
  445. /**
  446. * Retrieves the user's top rank in the Arena.
  447. *
  448. * @return int Top rank.
  449. */
  450. public function get_top_rank_arena(){
  451. return $this->top_rank_arena;
  452. }
  453. /**
  454. * Retrieves the user's top rank in the WorldArena.
  455. *
  456. * @return int Top rank.
  457. */
  458. public function get_top_rank_world_arena(){
  459. return $this->top_rank_world_arena;
  460. }
  461. /**
  462. * Retrieves the user's top rank in the Arena.
  463. *
  464. * @return int Top rank.
  465. */
  466. public function get_top_rank_special_league(){
  467. return $this->top_rank_special_league;
  468. }
  469. /**
  470. * Retrieves the user's top rank in Guild War.
  471. *
  472. * @return int Top rank.
  473. */
  474. public function get_top_rank_gw(){
  475. return $this->top_rank_gw;
  476. }
  477. /**
  478. * Retrieves the user's top rank in Siege battle.
  479. *
  480. * @return int Top rank.
  481. */
  482. public function get_top_rank_siege(){
  483. return $this->top_rank_siege;
  484. }
  485. /**
  486. * Retrieves the user's top rank in the World Boss.
  487. *
  488. * @return int Top rank.
  489. */
  490. public function get_top_rank_wboss(){
  491. return $this->top_rank_wboss;
  492. }
  493. /**
  494. * Retrieves the user's top rank in the Trial of Ascension (normal difficulty).
  495. *
  496. * @return int Top rank.
  497. */
  498. public function get_top_rank_toan(){
  499. return $this->top_rank_toan;
  500. }
  501. /**
  502. * Retrieves the user's top rank in the Trial of Ascension (hard difficulty).
  503. *
  504. * @return int Top rank.
  505. */
  506. public function get_top_rank_toah(){
  507. return $this->top_rank_toah;
  508. }
  509. /**
  510. * Retrieves the user's top rank in the Trial of Ascension (hell difficulty).
  511. *
  512. * @return int Top rank.
  513. */
  514. public function get_top_rank_toal(){
  515. return $this->top_rank_toal;
  516. }
  517. /**
  518. * Retrieves the Arena defense units.
  519. *
  520. * @return \Unit[] Units in the arena defense.
  521. */
  522. public function get_arena_defense(){
  523. if (!$this->flag_defense){
  524. $this->load_defenses();
  525. }
  526. return $this->arena_defense;
  527. }
  528. /**
  529. * Retrieves the Guild War defenses.
  530. *
  531. * @return Unit[][]: Units in Guild War defense, grouped by team.
  532. */
  533. public function get_gw_defense(){
  534. if (!$this->flag_defense){
  535. $this->load_defenses();
  536. }
  537. return $this->gw_defense;
  538. }
  539. /**
  540. * Retrieves the Guild War defenses.
  541. *
  542. * @return Unit[][]: Units in Siege Battle defense, grouped by team.
  543. */
  544. public function get_siege_defense(){
  545. if (!$this->flag_defense){
  546. $this->load_defenses();
  547. }
  548. return $this->siege_defense;
  549. }
  550. /**
  551. * Retrieves various currencies.
  552. *
  553. * @return int[] Array with the keys: mana, crystal, energy, energy_max, arena_energy,
  554. * darkportal_energy, dimension_energy, honor_point, guild_point, honor_medal, honor_mark,
  555. * event_coin, social_point, ancient_stone, costume_point,
  556. */
  557. public function get_currencies(){
  558. return $this->currency;
  559. }
  560. /**
  561. * Retrieves the list of buildings owned by the user.
  562. *
  563. * @return \Building[] Buildings owned.
  564. */
  565. public function get_buildings(){
  566. if (!$this->flag_building){
  567. $this->load_buildings();
  568. }
  569. return $this->building;
  570. }
  571. /**
  572. * Retrieves the list of decorations owned by the user.
  573. *
  574. * @return \Decoration[] Owned decorations.
  575. */
  576. public function get_decorations(){
  577. if (!$this->flag_building){
  578. $this->load_buildings();
  579. }
  580. return $this->decoration;
  581. }
  582. /**
  583. * Retrieves the list of owned scrolls.
  584. *
  585. * @return \Item[] Scrolls.
  586. */
  587. public function get_scrolls(){
  588. if (!$this->flag_item){
  589. $this->load_items();
  590. }
  591. return $this->item_scroll;
  592. }
  593. /**
  594. * Retrieves the list of rune crafting mateials.
  595. *
  596. * @return \Item[] Rune craft materials.
  597. */
  598. public function get_rune_craft_materials(){
  599. if (!$this->flag_item){
  600. $this->load_items();
  601. }
  602. return $this->item_craft_rune;
  603. }
  604. /**
  605. * Retrieves the list of crafting materials.
  606. *
  607. * @return \Item[] Crafting materials
  608. */
  609. public function get_craft_materials(){
  610. if (!$this->flag_item){
  611. $this->load_items();
  612. }
  613. return $this->item_craft;
  614. }
  615. /**
  616. * Retrieves the list of essences owned by the player.
  617. *
  618. * @return \Item[] Essences.
  619. */
  620. public function get_essences(){
  621. if (!$this->flag_item){
  622. $this->load_items();
  623. }
  624. return $this->item_essence;
  625. }
  626. /**
  627. * Retrieves the list of the player's records in the logbook.
  628. *
  629. * @return \Record[] List of records.
  630. */
  631. public function get_records(){
  632. if (!$this->flag_record){
  633. $this->load_records();
  634. }
  635. return $this->record;
  636. }
  637. /**
  638. * Checks if the player profile is public.
  639. *
  640. * @return bool True for public profile, false otherwise.
  641. */
  642. public function is_public(){
  643. return $this->public;
  644. }
  645. }
  646. ?>