Optimizer_Unit_Page.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * Optimizer unit page file.
  4. *
  5. * Provides a class with all the properties and methods to display the page.
  6. *
  7. * @category Page
  8. */
  9. /**
  10. * Require dependent files if not present.
  11. */
  12. require_once(PATH::PAGE . "Page.php");
  13. require_once(PATH::ENTITY . "Unit.php");
  14. /**
  15. * Optimizer team list page model.
  16. *
  17. * @category Page
  18. */
  19. class Optimizer_Unit_Page extends Page{
  20. /**
  21. * @var Unit The unit to optimize.
  22. */
  23. public $unit;
  24. /**
  25. * @var mixed[] Stats gained by building.
  26. */
  27. public $building_stats = [
  28. "atk" => 0,
  29. "def" => 0,
  30. "hp" => 0,
  31. "spd" => 0,
  32. "crd" => 0,
  33. "elm" => [
  34. 1 => 0,
  35. 2 => 0,
  36. 3 => 0,
  37. 4 => 0,
  38. 5 => 0
  39. ]
  40. ];
  41. /**
  42. * @var int[] Stats gained by artifacts.
  43. */
  44. public $artifact_stats = [
  45. "atk" => 0,
  46. "def" => 0,
  47. "hp" => 0
  48. ];
  49. /**
  50. * Constructor.
  51. *
  52. * Retrieves the data and initializes the variables.
  53. *
  54. * @param string $id Selected unit id
  55. */
  56. public function __construct($id){
  57. $this->view = PATH::VIEW . "optimizer_unit.php";
  58. $this->unit = new Unit($id);
  59. $this->calculate_buildings();
  60. $this->calculate_artifacts();
  61. $this->title = "Optimize " . $this->unit->unit->name . " - SWDB";
  62. $this->description = "Optimize " . $this->unit->unit->name;
  63. $this->canonical = URL::BASE . "optmizer/$id";
  64. }
  65. /**
  66. * Calculates the stats gained from bulding upgrades and populates the
  67. * $building_stats array.
  68. *
  69. * @gobal int User ID.
  70. * @global resource Database connection.
  71. */
  72. private function calculate_buildings(){
  73. global $UID;
  74. global $db;
  75. $s = "
  76. SELECT
  77. affected_stat,
  78. bonus,
  79. element
  80. FROM
  81. k_decoration,
  82. k_decoration_level,
  83. decoration
  84. WHERE
  85. decoration.uid = '$UID' AND
  86. k_decoration.id = decoration.decoration AND
  87. decoration.decoration = k_decoration_level.decoration AND
  88. decoration.level = k_decoration_level.level AND
  89. area = " . EFFECT_AREA_ID::GENERAL . " AND
  90. affected_stat IN (" . STAT_ID::ATK . ", " . STAT_ID::DEF . ", " . STAT_ID::HP . ", " . STAT_ID::SPD . ", " . STAT_ID::CRIT_DMG . ");
  91. ";
  92. $q = $db->query($s);
  93. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  94. switch($r["affected_stat"]){
  95. case STAT_ID::DEF:
  96. $this->building_stats["def"] += $r["bonus"];
  97. break;
  98. case STAT_ID::HP:
  99. $this->building_stats["hp"] += $r["bonus"];
  100. break;
  101. case STAT_ID::SPD:
  102. $this->building_stats["spd"] += $r["bonus"];
  103. break;
  104. case STAT_ID::CRIT_DMG:
  105. $this->building_stats["crd"] += $r["bonus"];
  106. break;
  107. case STAT_ID::ATK:
  108. if(strlen($r["element"]) > 0){
  109. $this->building_stats["elm"][$r["element"]] += $r["bonus"];
  110. }
  111. else{
  112. $this->building_stats["atk"] += $r["bonus"];
  113. }
  114. break;
  115. }
  116. }
  117. }
  118. private function calculate_artifacts(){
  119. if ($this->unit->artifact_type != null){
  120. switch ($this->unit->artifact_type->main_stat){
  121. case ARTIFACT_STAT_ID::HP:
  122. $this->artifact_stats["hp"] += $this->unit->artifact_type->main_stat_value;
  123. break;
  124. case ARTIFACT_STAT_ID::DEF:
  125. $this->artifact_stats["def"] += $this->unit->artifact_type->main_stat_value;
  126. break;
  127. case ARTIFACT_STAT_ID::ATK:
  128. $this->artifact_stats["atk"] += $this->unit->artifact_type->main_stat_value;
  129. break;
  130. }
  131. }
  132. if ($this->unit->artifact_element != null){
  133. switch ($this->unit->artifact_element->main_stat){
  134. case ARTIFACT_STAT_ID::HP:
  135. $this->artifact_stats["hp"] += $this->unit->artifact_element->main_stat_value;
  136. break;
  137. case ARTIFACT_STAT_ID::DEF:
  138. $this->artifact_stats["def"] += $this->unit->artifact_element->main_stat_value;
  139. break;
  140. case ARTIFACT_STAT_ID::ATK:
  141. $this->artifact_stats["atk"] += $this->unit->artifact_element->main_stat_value;
  142. break;
  143. }
  144. }
  145. }
  146. }
  147. ?>