optimize_get_rune_list.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. $q = $db->query($s[$i]);
  171. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  172. $rune = create_rune_array($unit, $r, $tuning);
  173. //array_push($candidates[$i], new Rune($r["id"]));
  174. array_push($candidates[$i], $rune);
  175. $total_candidates ++;
  176. }
  177. }
  178. $max_combinations = sizeof($candidates[1]) * sizeof($candidates[2]) * sizeof($candidates[3]) * sizeof($candidates[4]) * sizeof($candidates[5]) * sizeof($candidates[6]);
  179. // TODO: Build response
  180. $response = json_encode($candidates);
  181. if ($response == null){
  182. return 0;
  183. }
  184. else{
  185. return $response;
  186. }
  187. }
  188. /**
  189. * Creates an array with the stats provided by a rune on a unit.
  190. *
  191. * @param Unit $unit Target unit.
  192. * @param SQLResult $r Rune main stat ID.
  193. * @param int $stars Rune stars.
  194. * @return int Value of the main stat at the selected level.
  195. */
  196. function create_rune_array($unit, $r, $tuning){
  197. $rune = [
  198. "ID" => $r["id"],
  199. "TYPE" => $r["type"],
  200. "ATK" => 0,
  201. "DEF" => 0,
  202. "HP" => 0,
  203. "SPD" => 0,
  204. "CRR" => 0,
  205. "CRD" => 0,
  206. "ACC" => 0,
  207. "RES" => 0
  208. ];
  209. // Calculate main stat value based on requested level.
  210. $level = $r["level"];
  211. $main_value = $r["main_stat_value"];
  212. switch ($tuning){
  213. case 1: // All +12
  214. if ($level < 12){
  215. $main_value = get_main_stat_at_level(12, $r["main_stat"], $r["stars"]);
  216. }
  217. break;
  218. case 2: // Even +15, Odd + 12
  219. if ($level < 15 && $r["slot"] % 2 == 0){
  220. $main_value = get_main_stat_at_level(15, $r["main_stat"], $r["stars"]);
  221. }
  222. elseif ($level < 12 && $r["slot"] % 2 != 0){
  223. $main_value = get_main_stat_at_level(12, $r["main_stat"], $r["stars"]);
  224. }
  225. break;
  226. case 3: // All +15
  227. if ($level < 15){
  228. $main_value = get_main_stat_at_level(15, $r["main_stat"], $r["stars"]);
  229. }
  230. break;
  231. }
  232. // Array to loop.
  233. $stats = [
  234. [
  235. "STAT" => $r["main_stat"],
  236. "VALUE" => $main_value
  237. ],
  238. [
  239. "STAT" => $r["innate_stat"],
  240. "VALUE" => $r["innate_stat_value"]
  241. ],
  242. [
  243. "STAT" => $r["substat_1"],
  244. "VALUE" => $r["substat_1_value"]
  245. ],
  246. [
  247. "STAT" => $r["substat_2"],
  248. "VALUE" => $r["substat_2_value"]
  249. ],
  250. [
  251. "STAT" => $r["substat_3"],
  252. "VALUE" => $r["substat_3_value"]
  253. ],
  254. [
  255. "STAT" => $r["substat_4"],
  256. "VALUE" => $r["substat_4_value"]
  257. ]
  258. ];
  259. foreach ($stats as $stat){
  260. switch ($stat["STAT"]){
  261. case RUNE_STAT_ID::ATK:
  262. $rune["ATK"] += $stat["VALUE"];
  263. break;
  264. case RUNE_STAT_ID::ATK_P:
  265. $rune["ATK"] += ($unit->attack * $stat["VALUE"] / 100);
  266. break;
  267. case RUNE_STAT_ID::DEF:
  268. $rune["DEF"] += $stat["VALUE"];
  269. break;
  270. case RUNE_STAT_ID::DEF_P:
  271. $rune["DEF"] += ($unit->defense * $stat["VALUE"] / 100);
  272. break;
  273. case RUNE_STAT_ID::HP:
  274. $rune["HP"] += $stat["VALUE"];
  275. break;
  276. case RUNE_STAT_ID::HP_P:
  277. $rune["HP"] += ($unit->hp * $stat["VALUE"] / 100);
  278. break;
  279. case RUNE_STAT_ID::SPD:
  280. $rune["SPD"] += $stat["VALUE"];
  281. break;
  282. case RUNE_STAT_ID::CRR:
  283. $rune["CRR"] += $stat["VALUE"];
  284. break;
  285. case RUNE_STAT_ID::CRD:
  286. $rune["CRD"] += $stat["VALUE"];
  287. break;
  288. case RUNE_STAT_ID::ACC:
  289. $rune["ACC"] += $stat["VALUE"];
  290. break;
  291. case RUNE_STAT_ID::RES:
  292. $rune["RES"] += $stat["VALUE"];
  293. break;
  294. }
  295. }
  296. return $rune;
  297. }
  298. /**
  299. * Gets the value of the main stat at an arbitrary level.
  300. *
  301. * @param int $level Desired level.
  302. * @param int $stat Rune main stat ID.
  303. * @param int $stars Rune stars.
  304. * @return int Value of the main stat at the selected level.
  305. */
  306. function get_main_stat_at_level($level, $stat, $stars){
  307. $level = intval($level);
  308. if ($level < 0 || $level > 15){
  309. return 0;
  310. }
  311. switch ($stat){
  312. case RUNE_STAT_ID::HP:
  313. return RUNE_STAT_VALUES::HP[$stars][$level];
  314. case RUNE_STAT_ID::HP_P:
  315. return RUNE_STAT_VALUES::HP_P[$stars][$level];
  316. case RUNE_STAT_ID::ATK:
  317. return RUNE_STAT_VALUES::ATK[$stars][$level];
  318. case RUNE_STAT_ID::ATK_P:
  319. return RUNE_STAT_VALUES::ATK_P[$stars][$level];
  320. case RUNE_STAT_ID::DEF:
  321. return RUNE_STAT_VALUES::DEF[$stars][$level];
  322. case RUNE_STAT_ID::DEF_P:
  323. return RUNE_STAT_VALUES::DEF_P[$stars][$level];
  324. case RUNE_STAT_ID::SPD:
  325. return RUNE_STAT_VALUES::SPD[$stars][$level];
  326. case RUNE_STAT_ID::CRR:
  327. return RUNE_STAT_VALUES::CRR[$stars][$level];
  328. case RUNE_STAT_ID::CRD:
  329. return RUNE_STAT_VALUES::CRD[$stars][$level];
  330. case RUNE_STAT_ID::RES:
  331. return RUNE_STAT_VALUES::RES[$stars][$level];
  332. case RUNE_STAT_ID::ACC:
  333. return RUNE_STAT_VALUES::ACC[$stars][$level];
  334. default:
  335. return 0;
  336. }
  337. }
  338. ?>