optimize_get_rune_list.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. /**
  3. * File for the optimization action.
  4. *
  5. * Implements an action function to be called from the {@see Controller}.
  6. *
  7. * @category Action
  8. */
  9. /**
  10. * Executes the optimization action.
  11. *
  12. * Reads the POST parameters looking for the following KEYS:
  13. * mail
  14. * pass + currentPass
  15. * api
  16. * Then it updates the selected info with the prameter vlue. Multiple
  17. * itemas can be updated at the same time.
  18. *
  19. * @return int|string 0 on success, negative values on error. If the API
  20. * key has been updated, the new key.
  21. * @category Action
  22. * @global resource Database connection.
  23. */
  24. function action(){
  25. global $db;
  26. // Increase max execution time.
  27. set_time_limit(60);
  28. $candidates = [
  29. 1 => [],
  30. 2 => [],
  31. 3 => [],
  32. 4 => [],
  33. 5 => [],
  34. 6 => []
  35. ];
  36. $response = null;
  37. $uid = filter_input(INPUT_POST, 'uid');
  38. if ($uid == null){
  39. return -1;
  40. }
  41. $unit_id = filter_input(INPUT_POST, 'unit');
  42. if ($unit_id == null){
  43. return -2;
  44. }
  45. // Sets
  46. $sets = [];
  47. foreach ($_POST["set"] as $x){
  48. array_push($sets, intval($x));
  49. }
  50. if (sizeof($sets) < 2 || sizeof($sets) > 3){
  51. return -3;
  52. }
  53. // Main stats in even slots.
  54. $stats = [];
  55. foreach ($_POST["stat"] as $x){
  56. array_push($stats, intval($x));
  57. }
  58. $source = filter_input(INPUT_POST, 'source');
  59. if ($source == null){
  60. return -4;
  61. }
  62. $limit = intval(filter_input(INPUT_POST, 'limit'));
  63. if ($limit == 0){
  64. return -5;
  65. }
  66. $tuning = filter_input(INPUT_POST, 'tuning');
  67. if ($tuning == null){
  68. return -6;
  69. }
  70. // Instantiate the unit
  71. $unit = new Unit($unit_id, true, $uid);
  72. // Get build query:
  73. $base_s = "
  74. SELECT
  75. id,
  76. type,
  77. slot,
  78. level,
  79. stars,
  80. main_stat,
  81. main_stat_value,
  82. innate_stat,
  83. innate_stat_value,
  84. substat_1,
  85. substat_1_value + substat_1_grind AS substat_1_value,
  86. substat_2,
  87. substat_2_value + substat_2_grind AS substat_2_value,
  88. substat_3,
  89. substat_3_value + substat_3_grind AS substat_3_value,
  90. substat_4,
  91. substat_4_value + substat_4_grind AS substat_4_value
  92. FROM rune
  93. WHERE
  94. uid = :uid AND
  95. type IN (
  96. ";
  97. foreach ($sets as $set){
  98. $base_s .= ($set . ",");
  99. }
  100. $base_s .= "-1) AND ";
  101. switch ($source){
  102. case 0: // Storage only (or itself)
  103. $base_s .= "
  104. (
  105. assigned_to = :unit_id OR
  106. assigned_to IS NULL
  107. )";
  108. break;
  109. case 1: // Units in no teams (or itself)
  110. $base_s .= "
  111. (
  112. assigned_to = :unit_id OR
  113. assigned_to IS NULL OR
  114. assigned_to NOT IN (SELECT DISTINCT unit FROM team_unit)
  115. )
  116. ";
  117. break;
  118. case 2: // Units in teams with 0 score (or itself)
  119. $base_s .= "
  120. (
  121. assigned_to = :unit_id OR
  122. assigned_to IS NULL OR
  123. assigned_to NOT IN (SELECT DISTINCT unit FROM team_unit) OR
  124. assigned_to NOT IN (
  125. SELECT DISTINCT unit
  126. FROM
  127. team,
  128. team_unit
  129. WHERE
  130. team.id = team_unit.team AND
  131. team.score > 0
  132. )
  133. )
  134. ";
  135. break;
  136. case 3: // Units with lower overall score (or itself)
  137. // TODO
  138. break;
  139. case 4: // All runes - dont filter
  140. $base_s .= " 1 = 1 ";
  141. break;
  142. default: // Invalid options - return nothing
  143. $base_s .= " 1 = 0 ";
  144. }
  145. $s_order = " ORDER BY stars DESC, original_quality DESC, max_efficiency DESC, efficiency DESC, level DESC";
  146. $s_in = " main_stat IN (:stat_0, :stat_1, :stat_2) ";
  147. $s = ["", "", "", "", "", "", ""];
  148. $s[1] = $base_s . " AND slot = 1 " . $s_order;
  149. $s[3] = $base_s . " AND slot = 3 " . $s_order;
  150. $s[5] = $base_s . " AND slot = 5 " . $s_order;
  151. $s[2] = $base_s . " AND slot = 2 AND " . $s_in . $s_order;
  152. $s[4] = $base_s . " AND slot = 4 AND " . $s_in . $s_order;
  153. $s[6] = $base_s . " AND slot = 6 AND " . $s_in . $s_order;
  154. $total_candidates = 0;
  155. for ($i = 1; $i <= 6; $i ++){
  156. $statement = $db->prepare($s[$i]);
  157. $statement->bindValue(':uid', $uid, SQLITE3_INTEGER);
  158. $statement->bindValue(':unit_id', $unit_id, SQLITE3_INTEGER);
  159. $statement->bindValue(':stat_0', $stats[0], SQLITE3_INTEGER);
  160. $statement->bindValue(':stat_1', $stats[1], SQLITE3_INTEGER);
  161. $statement->bindValue(':stat_2', $stats[2], SQLITE3_INTEGER);
  162. $q = $statement->execute();
  163. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  164. $rune = create_rune_array($unit, $r, $tuning);
  165. //array_push($candidates[$i], new Rune($r["id"]));
  166. array_push($candidates[$i], $rune);
  167. $total_candidates ++;
  168. }
  169. }
  170. //$max_combinations = sizeof($candidates[1]) * sizeof($candidates[2]) * sizeof($candidates[3]) * sizeof($candidates[4]) * sizeof($candidates[5]) * sizeof($candidates[6]);
  171. // TODO: Build response
  172. $response = json_encode($candidates);
  173. if ($response == null){
  174. return 0;
  175. }
  176. else{
  177. return $response;
  178. }
  179. }
  180. /**
  181. * Creates an array with the stats provided by a rune on a unit.
  182. *
  183. * @param Unit $unit Target unit.
  184. * @param int $r Rune main stat ID.
  185. * @param int $stars Rune stars.
  186. * @return int Value of the main stat at the selected level.
  187. */
  188. function create_rune_array($unit, $r, $tuning){
  189. $rune = [
  190. "ID" => $r["id"],
  191. "TYPE" => $r["type"],
  192. "ATK" => 0,
  193. "DEF" => 0,
  194. "HP" => 0,
  195. "SPD" => 0,
  196. "CRR" => 0,
  197. "CRD" => 0,
  198. "ACC" => 0,
  199. "RES" => 0
  200. ];
  201. // Calculate main stat value based on requested level.
  202. $level = $r["level"];
  203. $main_value = $r["main_stat_value"];
  204. switch ($tuning){
  205. case 1: // All +12
  206. if ($level < 12){
  207. $main_value = get_main_stat_at_level(12, $r["main_stat"], $r["stars"]);
  208. }
  209. break;
  210. case 2: // Even +15, Odd + 12
  211. if ($level < 15 && $r["slot"] % 2 == 0){
  212. $main_value = get_main_stat_at_level(15, $r["main_stat"], $r["stars"]);
  213. }
  214. elseif ($level < 12 && $r["slot"] % 2 != 0){
  215. $main_value = get_main_stat_at_level(12, $r["main_stat"], $r["stars"]);
  216. }
  217. break;
  218. case 3: // All +15
  219. if ($level < 15){
  220. $main_value = get_main_stat_at_level(15, $r["main_stat"], $r["stars"]);
  221. }
  222. break;
  223. }
  224. // Array to loop.
  225. $stats = [
  226. [
  227. "STAT" => $r["main_stat"],
  228. "VALUE" => $main_value
  229. ],
  230. [
  231. "STAT" => $r["innate_stat"],
  232. "VALUE" => $r["innate_stat_value"]
  233. ],
  234. [
  235. "STAT" => $r["substat_1"],
  236. "VALUE" => $r["substat_1_value"]
  237. ],
  238. [
  239. "STAT" => $r["substat_2"],
  240. "VALUE" => $r["substat_2_value"]
  241. ],
  242. [
  243. "STAT" => $r["substat_3"],
  244. "VALUE" => $r["substat_3_value"]
  245. ],
  246. [
  247. "STAT" => $r["substat_4"],
  248. "VALUE" => $r["substat_4_value"]
  249. ]
  250. ];
  251. foreach ($stats as $stat){
  252. switch ($stat["STAT"]){
  253. case RUNE_STAT_ID::ATK:
  254. $rune["ATK"] += $stat["VALUE"];
  255. break;
  256. case RUNE_STAT_ID::ATK_P:
  257. $rune["ATK"] += ($unit->attack * $stat["VALUE"] / 100);
  258. break;
  259. case RUNE_STAT_ID::DEF:
  260. $rune["DEF"] += $stat["VALUE"];
  261. break;
  262. case RUNE_STAT_ID::DEF_P:
  263. $rune["DEF"] += ($unit->defense * $stat["VALUE"] / 100);
  264. break;
  265. case RUNE_STAT_ID::HP:
  266. $rune["HP"] += $stat["VALUE"];
  267. break;
  268. case RUNE_STAT_ID::HP_P:
  269. $rune["HP"] += ($unit->hp * $stat["VALUE"] / 100);
  270. break;
  271. case RUNE_STAT_ID::SPD:
  272. $rune["SPD"] += $stat["VALUE"];
  273. break;
  274. case RUNE_STAT_ID::CRR:
  275. $rune["CRR"] += $stat["VALUE"];
  276. break;
  277. case RUNE_STAT_ID::CRD:
  278. $rune["CRD"] += $stat["VALUE"];
  279. break;
  280. case RUNE_STAT_ID::ACC:
  281. $rune["ACC"] += $stat["VALUE"];
  282. break;
  283. case RUNE_STAT_ID::RES:
  284. $rune["RES"] += $stat["VALUE"];
  285. break;
  286. }
  287. }
  288. return $rune;
  289. }
  290. /**
  291. * Gets the value of the main stat at an arbitrary level.
  292. *
  293. * @param int $level Desired level.
  294. * @param int $stat Rune main stat ID.
  295. * @param int $stars Rune stars.
  296. * @return int Value of the main stat at the selected level.
  297. */
  298. function get_main_stat_at_level($level, $stat, $stars){
  299. $level = intval($level);
  300. if ($level < 0 || $level > 15){
  301. return 0;
  302. }
  303. switch ($stat){
  304. case RUNE_STAT_ID::HP:
  305. return RUNE_STAT_VALUES::HP[$stars][$level];
  306. case RUNE_STAT_ID::HP_P:
  307. return RUNE_STAT_VALUES::HP_P[$stars][$level];
  308. case RUNE_STAT_ID::ATK:
  309. return RUNE_STAT_VALUES::ATK[$stars][$level];
  310. case RUNE_STAT_ID::ATK_P:
  311. return RUNE_STAT_VALUES::ATK_P[$stars][$level];
  312. case RUNE_STAT_ID::DEF:
  313. return RUNE_STAT_VALUES::DEF[$stars][$level];
  314. case RUNE_STAT_ID::DEF_P:
  315. return RUNE_STAT_VALUES::DEF_P[$stars][$level];
  316. case RUNE_STAT_ID::SPD:
  317. return RUNE_STAT_VALUES::SPD[$stars][$level];
  318. case RUNE_STAT_ID::CRR:
  319. return RUNE_STAT_VALUES::CRR[$stars][$level];
  320. case RUNE_STAT_ID::CRD:
  321. return RUNE_STAT_VALUES::CRD[$stars][$level];
  322. case RUNE_STAT_ID::RES:
  323. return RUNE_STAT_VALUES::RES[$stars][$level];
  324. case RUNE_STAT_ID::ACC:
  325. return RUNE_STAT_VALUES::ACC[$stars][$level];
  326. default:
  327. return 0;
  328. }
  329. }
  330. ?>