Fusion.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * Fusion 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 . "K_Unit.php");
  14. require_once(PATH::ENTITY . "Unit.php");
  15. /**
  16. * Extension of K_Unit that also stores information about owned
  17. * monsters.
  18. *
  19. * @category Entity
  20. */
  21. class Monster_Fusion extends K_Unit{
  22. /**
  23. * @var \Unit[] All owned units of this type.
  24. */
  25. public $owned = [];
  26. /**
  27. * Constructor.
  28. *
  29. * Searches the database and retrieves the information about the
  30. * fusion, populating it and it's items.
  31. *
  32. * @param int $id Unit id.
  33. * @param int $uid Player id.
  34. * @global resource Database connection.
  35. */
  36. public function __construct($id, $uid){
  37. global $db;
  38. parent::__construct($id, true);
  39. $statement = $db->prepare("
  40. SELECT unit.id AS id
  41. FROM
  42. unit,
  43. k_unit
  44. WHERE
  45. k_unit.id = unit.unit AND
  46. unit.uid = :uid AND
  47. (
  48. k_unit.id = :id OR
  49. k_unit.awakens_from = :id OR
  50. k_unit.awakens_to = :id
  51. );
  52. ");
  53. $statement->bindValue(':uid', $uid, SQLITE3_INTEGER);
  54. $statement->bindValue(':id', $id, SQLITE3_INTEGER);
  55. $q = $statement->execute();
  56. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  57. array_push($this->owned, new Unit($r["id"], false));
  58. }
  59. }
  60. }
  61. /**
  62. * Fusion.
  63. *
  64. * Represents an object from the table 'fusion'.
  65. *
  66. * @category Entity
  67. */
  68. class Fusion extends Entity{
  69. /**
  70. * @var K_Unit K_Unit to fuse.
  71. */
  72. public $product;
  73. /**
  74. * @var K_Unit K_Unit to fuse (awakened).
  75. */
  76. public $product_awaken;
  77. /**
  78. * @var K_Unit K_Unit to fuse (unawakened).
  79. */
  80. public $product_unawaken;
  81. /**
  82. * @var Monster_Fusion Fusions required to create ingredients.
  83. */
  84. public $fusion = [];
  85. /**
  86. * @var \K_Unit[] List of unfuseable K_Unit material.
  87. */
  88. public $ingredient = [];
  89. /**
  90. * @var \K_Unit[] List of unfuseable K_Unit material. (unawakened).
  91. */
  92. public $ingredient_unawaken = [];
  93. /**
  94. * @var int Stars of the monster to fuse.
  95. */
  96. public $stars;
  97. /**
  98. * @var int Fusion cost.
  99. */
  100. public $cost;
  101. /**
  102. * Constructor.
  103. *
  104. * Searches the database and retrieves the information about the
  105. * fusion, populating it and it's items.
  106. *
  107. * @param int $id Product monster id.
  108. * @param int $uid User id.
  109. * @global resource Database connection.
  110. */
  111. public function __construct($id, $uid = null){
  112. global $db;
  113. $statement = $db->prepare("
  114. SELECT DISTINCT
  115. product,
  116. stars,
  117. cost
  118. FROM k_fusion
  119. WHERE
  120. product = $id OR
  121. product = (SELECT awakens_to FROM k_unit WHERE id = $id) OR
  122. product = (SELECT awakens_from FROM k_unit WHERE id = $id);
  123. ");
  124. $statement->bindValue(':id', $id, SQLITE3_INTEGER);
  125. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  126. $this->product = new Monster_Fusion($r["product"], $uid);
  127. $this->product_awaken = new Monster_Fusion($this->product->awakens_to, $uid);
  128. $this->stars = $r["stars"];
  129. $this->cost = $r["cost"];
  130. $statement = $db->prepare("
  131. SELECT DISTINCT ingredient
  132. FROM k_fusion
  133. WHERE
  134. product = :product AND
  135. ingredient IN (
  136. SELECT DISTINCT k_unit.id
  137. FROM
  138. k_fusion,
  139. k_unit
  140. WHERE
  141. k_fusion.product = k_unit.awakens_from OR
  142. k_fusion.product = k_unit.awakens_to OR
  143. k_fusion.product = k_unit.id
  144. );
  145. ");
  146. $statement->bindValue(':product', $this->product->id, SQLITE3_INTEGER);
  147. $q_f = $statement->execute();
  148. while ($r_f = $q_f->fetchArray(SQLITE3_ASSOC)){
  149. array_push($this->fusion, new Fusion($r_f["ingredient"], $uid));
  150. }
  151. $statement = $db->prepare("
  152. SELECT
  153. id,
  154. awakens_from
  155. FROM k_unit
  156. WHERE id IN (
  157. SELECT DISTINCT
  158. ingredient
  159. FROM k_fusion
  160. WHERE
  161. product = :product AND
  162. ingredient NOT IN (
  163. SELECT DISTINCT k_unit.id
  164. FROM
  165. k_fusion,
  166. k_unit
  167. WHERE
  168. k_fusion.product = k_unit.awakens_from OR
  169. k_fusion.product = k_unit.awakens_to OR
  170. k_fusion.product = k_unit.id
  171. )
  172. );
  173. ");
  174. $statement->bindValue(':product', $this->product->id, SQLITE3_INTEGER);
  175. $q_m = $statement->execute();
  176. while ($r_m = $q_m->fetchArray(SQLITE3_ASSOC)){
  177. $m = new Monster_Fusion($r_m["id"], $uid);
  178. array_push($this->ingredient, $m);
  179. $m = new Monster_Fusion($r_m["awakens_from"], $uid);
  180. $m->load_essences();
  181. array_push($this->ingredient_unawaken, $m);
  182. }
  183. }
  184. }
  185. ?>