Fusion.php 5.7 KB

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