Fusion.php 5.6 KB

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