optimize_get_rune_list.php 12 KB

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