optimize.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. $candidates = [
  27. 1 => [],
  28. 2 => [],
  29. 3 => [],
  30. 4 => [],
  31. 5 => [],
  32. 6 => []
  33. ];
  34. $options = [];
  35. $response = null;
  36. $uid = filter_input(INPUT_POST, 'uid');
  37. if ($uid == null){
  38. return -1;
  39. }
  40. $unit_id = filter_input(INPUT_POST, 'unit');
  41. if ($unit_id == null){
  42. return -2;
  43. }
  44. // Sets
  45. // TODO: Check if valid.
  46. // TODO: Check if compatible sets, not just size.
  47. $sets = [];
  48. foreach ($_POST["set"] as $x){
  49. array_push($sets, intval($x));
  50. }
  51. if (sizeof($sets) < 2 || sizeof($sets) > 3){
  52. return -3;
  53. }
  54. // Main stats in even slots.
  55. // TODO: Check if valid.
  56. // TODO: Check if valid per slot
  57. $stats = [];
  58. foreach ($_POST["stat"] as $x){
  59. array_push($stats, intval($x));
  60. }
  61. $source = filter_input(INPUT_POST, 'source');
  62. if ($source == null){
  63. return -4;
  64. }
  65. $tuning = filter_input(INPUT_POST, 'tuning');
  66. if ($tuning == null){
  67. return -5;
  68. }
  69. $min = [
  70. "attack" => intval(filter_input(INPUT_POST, 'min_attack')),
  71. "defense" => intval(filter_input(INPUT_POST, 'min_defense')),
  72. "hp" => intval(filter_input(INPUT_POST, 'min_hp')),
  73. "speed" => intval(filter_input(INPUT_POST, 'min_speed')),
  74. "crit_rate" => intval(filter_input(INPUT_POST, 'min_crit_rate')),
  75. "crit_damage" => intval(filter_input(INPUT_POST, 'min_crit_damage')),
  76. "accuracy" => intval(filter_input(INPUT_POST, 'min_accuracy')),
  77. "resistance" => intval(filter_input(INPUT_POST, 'min_resistance')),
  78. "ehp" => intval(filter_input(INPUT_POST, 'min_ehp')),
  79. "dmg" => intval(filter_input(INPUT_POST, 'min_dmg'))
  80. ];
  81. // Get Buld query:
  82. $s = "
  83. SELECT
  84. id,
  85. slot
  86. FROM rune
  87. WHERE
  88. uid = $uid AND
  89. type IN (
  90. ";
  91. foreach ($sets as $set){
  92. $s .= ($set . ",");
  93. }
  94. $s .= "-1) AND ";
  95. switch ($source){
  96. case 0: // Storage only (or itself)
  97. $s .= "
  98. (
  99. assigned_to = $unit_id OR
  100. assigned_to IS NULL
  101. )";
  102. break;
  103. case 1: // Units in no teams (or itself)
  104. $s .= "
  105. (
  106. assigned_to = $unit_id OR
  107. assigned_to IS NULL OR
  108. assigned_to NOT IN (SELECT DISTINCT unit FROM team_unit)
  109. )
  110. ";
  111. break;
  112. case 2: // Units in teams with 0 score (or itself)
  113. $s .= "
  114. (
  115. assigned_to = $unit_id OR
  116. assigned_to IS NULL OR
  117. assigned_to NOT IN (SELECT DISTINCT unit FROM team_unit) OR
  118. assigned_to NOT IN (
  119. SELECT DISTINCT unit
  120. FROM
  121. team,
  122. team_unit
  123. WHERE
  124. team.id = team_unit.team AND
  125. team.score > 0
  126. )
  127. )
  128. ";
  129. break;
  130. case 3: // Units with lower overall score (or itself)
  131. // TODO
  132. break;
  133. case 4: // All runes - dont filter
  134. $s .= " 1 = 1 ";
  135. break;
  136. default: // Invalid options - return nothing
  137. $s .= " 1 = 0 ";
  138. }
  139. // Even slots stats
  140. $s .= "
  141. AND
  142. (
  143. slot <> 2 OR
  144. (
  145. slot = 2 AND
  146. main_stat = " . $stats[0] . "
  147. )
  148. ) AND
  149. (
  150. slot <> 4 OR
  151. (
  152. slot = 4 AND
  153. main_stat = " . $stats[1] . "
  154. )
  155. ) AND
  156. (
  157. slot <> 6 OR
  158. (
  159. slot = 6 AND
  160. main_stat = " . $stats[2] . "
  161. )
  162. )
  163. ";
  164. error_log($s);
  165. $q = $db->query($s);
  166. $total_candidates = 0;
  167. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  168. array_push($candidates[$r["slot"]], new Rune($r["id"]));
  169. $total_candidates ++;
  170. }
  171. $max_combinations = sizeof($candidates[1]) * sizeof($candidates[2]) * sizeof($candidates[3]) * sizeof($candidates[4]) * sizeof($candidates[5]) * sizeof($candidates[6]);
  172. if ($max_combinations == 0){
  173. $res = [
  174. "total" => 0,
  175. "options" => []
  176. ];
  177. $response = json_encode($res);
  178. return $response;
  179. }
  180. // Get the unit and its base stats
  181. $unit = new Unit($unit_id, true, $uid);
  182. $unit_attack = $unit->attack + $unit->artifact_attack + $unit->building_attack;
  183. $unit_base_attack = $unit->attack;
  184. $unit_defense = $unit->defense + $unit->artifact_defense + $unit->building_defense;
  185. $unit_base_defense = $unit->defense;
  186. $unit_hp = $unit->hp + $unit->artifact_hp + $unit->building_hp;
  187. $unit_base_hp = $unit->hp;
  188. $unit_speed = $unit->speed + $unit->artifact_speed + $unit->building_speed;
  189. $unit_base_speed = $unit->speed;
  190. $unit_crit_rate = $unit->crit_rate + $unit->artifact_crit_rate + $unit->building_crit_rate;
  191. $unit_base_crit_rate = $unit->crit_rate;
  192. $unit_crit_damage = $unit->crit_damage + $unit->artifact_crit_damage + $unit->building_crit_damage;
  193. $unit_base_crit_damage = $unit->crit_damage;
  194. $unit_accuracy = $unit->accuracy + $unit->artifact_accuracy + $unit->building_accuracy;
  195. $unit_base_accuracy = $unit->accuracy;
  196. $unit_resistance = $unit->resistance + $unit->artifact_resistance + $unit->building_resistance;
  197. $unit_base_resistance = $unit->resistance;
  198. $i = [0, 0, 0, 0, 0, 0, 0]; // 7, so I can start with 1
  199. while (true){
  200. // Check if set combination is valid
  201. if (valid_sets($candidates, $i)){
  202. // Do things
  203. $iteration = [
  204. "attack" => $unit_attack,
  205. "defense" => $unit_defense,
  206. "hp" => $unit_hp,
  207. "speed" => $unit_speed,
  208. "crit_rate" => $unit_crit_rate,
  209. "crit_damage" => $unit_crit_damage,
  210. "accuracy" => $unit_accuracy,
  211. "resistance" => $unit_resistance,
  212. "ehp" => 0, // TODO
  213. "dmg" => 0, // TODO
  214. ];
  215. for ($r = 1; $r <= 6; $r ++){
  216. // Loop selected runes and calculate stats
  217. sum_rune_stat(
  218. $candidates[$r][$i[$r]]->main_stat,
  219. $candidates[$r][$i[$r]]->main_stat_value,
  220. $iteration,
  221. $unit_base_attack,
  222. $unit_base_defense,
  223. $unit_base_hp,
  224. );
  225. sum_rune_stat(
  226. $candidates[$r][$i[$r]]->innate_stat,
  227. $candidates[$r][$i[$r]]->innate_stat_value,
  228. $iteration,
  229. $unit_base_attack,
  230. $unit_base_defense,
  231. $unit_base_hp,
  232. );
  233. sum_rune_stat(
  234. $candidates[$r][$i[$r]]->substat_1,
  235. $candidates[$r][$i[$r]]->substat_1_value,
  236. $iteration,
  237. $unit_base_attack,
  238. $unit_base_defense,
  239. $unit_base_hp,
  240. );
  241. sum_rune_stat(
  242. $candidates[$r][$i[$r]]->substat_2,
  243. $candidates[$r][$i[$r]]->substat_2_value,
  244. $iteration,
  245. $unit_base_attack,
  246. $unit_base_defense,
  247. $unit_base_hp,
  248. );
  249. sum_rune_stat(
  250. $candidates[$r][$i[$r]]->substat_3,
  251. $candidates[$r][$i[$r]]->substat_3_value,
  252. $iteration,
  253. $unit_base_attack,
  254. $unit_base_defense,
  255. $unit_base_hp,
  256. );
  257. sum_rune_stat(
  258. $candidates[$r][$i[$r]]->substat_4,
  259. $candidates[$r][$i[$r]]->substat_4_value,
  260. $iteration,
  261. $unit_base_attack,
  262. $unit_base_defense,
  263. $unit_base_hp,
  264. );
  265. }
  266. // Compare with filters:
  267. // TODO: Calculat EHP and DMG
  268. if (
  269. $iteration["attack"] >= $min["attack"] &&
  270. $iteration["defense"] >= $min["defense"] &&
  271. $iteration["hp"] >= $min["hp"] &&
  272. $iteration["speed"] >= $min["speed"] &&
  273. $iteration["crit_rate"] >= $min["crit_rate"] &&
  274. $iteration["crit_damage"] >= $min["crit_damage"] &&
  275. $iteration["accuracy"] >= $min["accuracy"] &&
  276. $iteration["resistance"] >= $min["resistance"]
  277. // TODO ehp dmg
  278. ){
  279. // Calculate the variation
  280. $variation = 0;
  281. $variation += (($iteration["attack"] - $unit->total_attack) * 1);
  282. $variation += (($iteration["defense"] - $unit->total_defense) * 1);
  283. $variation += (($iteration["hp"] - $unit->total_hp) * (1 / 15));
  284. $variation += (($iteration["speed"] - $unit->total_speed) * 1.5);
  285. $variation += (($iteration["crit_rate"] - $unit->total_crit_rate) * 0.85);
  286. $variation += (($iteration["crit_damage"] - $unit->total_crit_damage) * 0.9);
  287. $variation += (($iteration["accuracy"] - $unit->total_accuracy) * 0.85);
  288. $variation += (($iteration["resistance"] - $unit->total_resistance) * 0.85);
  289. $variation = ceil($variation);
  290. // Build the array
  291. $option = [
  292. "variation" => $variation,
  293. "runes" => [
  294. [
  295. "id" => $candidates[1][$i[1]]->id,
  296. "html" => (HTML::rune_table($candidates[1][$i[1]]))
  297. ],
  298. [
  299. "id" => $candidates[2][$i[2]]->id,
  300. "html" => (HTML::rune_table($candidates[2][$i[2]]))
  301. ],
  302. [
  303. "id" => $candidates[3][$i[3]]->id,
  304. "html" => (HTML::rune_table($candidates[3][$i[3]]))
  305. ],
  306. [
  307. "id" => $candidates[4][$i[4]]->id,
  308. "html" => (HTML::rune_table($candidates[4][$i[4]]))
  309. ],
  310. [
  311. "id" => $candidates[5][$i[5]]->id,
  312. "html" => (HTML::rune_table($candidates[5][$i[5]]))
  313. ],
  314. [
  315. "id" => $candidates[1][$i[1]]->id,
  316. "html" => HTML::rune_table($candidates[6][$i[6]])
  317. ]
  318. ],
  319. "stats" => [
  320. "attack" => ceil($iteration["attack"]),
  321. "defense" => ceil($iteration["defense"]),
  322. "hp" => ceil($iteration["hp"]),
  323. "speed" => ceil($iteration["speed"]),
  324. "crit_rate" => ceil($iteration["crit_rate"]),
  325. "crit_damage" => ceil($iteration["crit_damage"]),
  326. "accuracy" => ceil($iteration["accuracy"]),
  327. "attack" => ceil($iteration["attack"]),
  328. "resistance" => ceil($iteration["resistance"]),
  329. "ehp" => ceil($iteration["ehp"]),
  330. "dmg" => ceil($iteration["dmg"])
  331. ]
  332. ];
  333. array_push($options, $option);
  334. if (sizeof($options) > 100){
  335. http_response_code(413); // Payload too large;
  336. die();
  337. return;
  338. }
  339. }
  340. }
  341. // Increase counters
  342. $i[6] ++;
  343. if ($i[6] == sizeof($candidates[6])){
  344. $i[6] = 0;
  345. $i[5] ++;
  346. }
  347. if ($i[5] == sizeof($candidates[5])){
  348. $i[5] = 0;
  349. $i[4] ++;
  350. }
  351. if ($i[4] == sizeof($candidates[4])){
  352. $i[4] = 0;
  353. $i[3] ++;
  354. }
  355. if ($i[3] == sizeof($candidates[3])){
  356. $i[3] = 0;
  357. $i[2] ++;
  358. }
  359. if ($i[2] == sizeof($candidates[2])){
  360. $i[2] = 0;
  361. $i[1] ++;
  362. }
  363. // Calculate exit condition
  364. if (
  365. $i[1] >= sizeof($candidates[1]) - 1 &&
  366. $i[2] >= sizeof($candidates[2]) - 1 &&
  367. $i[3] >= sizeof($candidates[3]) - 1 &&
  368. $i[4] >= sizeof($candidates[4]) - 1 &&
  369. $i[5] >= sizeof($candidates[5]) - 1 &&
  370. $i[6] >= sizeof($candidates[6]) - 1
  371. ){
  372. break;
  373. }
  374. }
  375. // Sort options by variation
  376. usort($options, "sortOptions");
  377. $res = [
  378. "total" => sizeof($options),
  379. "options" => $options
  380. ];
  381. $response = json_encode($res);
  382. if ($response == null){
  383. return 0;
  384. }
  385. else{
  386. return $response;
  387. }
  388. }
  389. /**
  390. * Validates a rune combination.
  391. *
  392. * Checks that set numbers are OK.
  393. *
  394. * TODO: Verify that it complies with filters
  395. *
  396. * @param \Rune[][] $candidates Candidate runes.
  397. * @param int[] $indexes Currently selected indexes.
  398. */
  399. function valid_sets($candidates, $indexes){
  400. $sets = [
  401. RUNE_SET_ID::ENERGY => 0,
  402. RUNE_SET_ID::GUARD => 0,
  403. RUNE_SET_ID::SWIFT => 0,
  404. RUNE_SET_ID::BLADE => 0,
  405. RUNE_SET_ID::RAGE => 0,
  406. RUNE_SET_ID::FOCUS => 0,
  407. RUNE_SET_ID::ENDURE => 0,
  408. RUNE_SET_ID::FATAL => 0,
  409. RUNE_SET_ID::DESPAIR => 0,
  410. RUNE_SET_ID::VAMPIRE => 0,
  411. RUNE_SET_ID::VIOLENT => 0,
  412. RUNE_SET_ID::NEMESIS => 0,
  413. RUNE_SET_ID::WILL => 0,
  414. RUNE_SET_ID::SHIELD => 0,
  415. RUNE_SET_ID::REVENGE => 0,
  416. RUNE_SET_ID::DESTROY => 0,
  417. RUNE_SET_ID::FIGHT => 0,
  418. RUNE_SET_ID::DETERMINATION => 0,
  419. RUNE_SET_ID::ENHANCE => 0,
  420. RUNE_SET_ID::ACCURACY => 0,
  421. RUNE_SET_ID::TOLERANCE => 0
  422. ];
  423. for ($i = 1; $i <= 6; $i++){
  424. $sets[$candidates[$i][$indexes[$i]]->type->id] ++;
  425. }
  426. for ($i = 0; $i < sizeof($sets); $i ++){
  427. switch ($i){
  428. case RUNE_SET_ID::ENERGY:
  429. case RUNE_SET_ID::GUARD:
  430. case RUNE_SET_ID::NEMESIS:
  431. case RUNE_SET_ID::BLADE:
  432. case RUNE_SET_ID::FOCUS:
  433. case RUNE_SET_ID::ENDURE:
  434. case RUNE_SET_ID::WILL:
  435. case RUNE_SET_ID::SHIELD:
  436. case RUNE_SET_ID::REVENGE:
  437. case RUNE_SET_ID::DESTROY:
  438. case RUNE_SET_ID::FIGHT:
  439. case RUNE_SET_ID::DETERMINATION:
  440. case RUNE_SET_ID::ENHANCE:
  441. case RUNE_SET_ID::ACCURACY:
  442. case RUNE_SET_ID::TOLERANCE:
  443. if ($sets[$i] % 2 != 0){
  444. return false;
  445. }
  446. break;
  447. case RUNE_SET_ID::DESPAIR:
  448. case RUNE_SET_ID::SWIFT:
  449. case RUNE_SET_ID::RAGE:
  450. case RUNE_SET_ID::FATAL:
  451. case RUNE_SET_ID::VAMPIRE:
  452. case RUNE_SET_ID::VIOLENT:
  453. if ($sets[$i] != 4 && $sets[$i != 0]){
  454. return false;
  455. }
  456. break;
  457. }
  458. }
  459. return true;
  460. }
  461. /**
  462. * Calculates the stat increase by a rune property.
  463. *
  464. * @param string $stat Stat type.
  465. * @param int $value Stat increase value.
  466. * @param int[] Reference to iteration stats array.
  467. */
  468. function sum_rune_stat($stat, $value, &$iteration, $base_atk, $base_def, $base_hp){
  469. switch ($stat){
  470. case RUNE_STAT_ID::ATK:
  471. $iteration["attack"] += $value;
  472. break;
  473. case RUNE_STAT_ID::DEF:
  474. $iteration["defense"] += $value;
  475. break;
  476. case RUNE_STAT_ID::HP:
  477. $iteration["hp"] += $value;
  478. break;
  479. case RUNE_STAT_ID::SPD:
  480. $iteration["speed"] += $value;
  481. break;
  482. case RUNE_STAT_ID::CRR:
  483. $iteration["crit_rate"] += $value;
  484. break;
  485. case RUNE_STAT_ID::CRD:
  486. $iteration["crit_damage"] += $value;
  487. break;
  488. case RUNE_STAT_ID::ACC:
  489. $iteration["accuracy"] += $value;
  490. break;
  491. case RUNE_STAT_ID::RES:
  492. $iteration["resistance"] += $value;
  493. break;
  494. case RUNE_STAT_ID::ATK_P:
  495. $iteration["attack"] += ($base_atk * $value / 100);
  496. break;
  497. case RUNE_STAT_ID::DEF_P:
  498. $iteration["defense"] += ($base_def * $value / 100);
  499. break;
  500. case RUNE_STAT_ID::HP_P:
  501. $iteration["hp"] += ($base_hp * $value / 100);
  502. break;
  503. }
  504. }
  505. function sortOptions($a, $b) {
  506. return $b["variation"] - $a["variation"];
  507. }
  508. ?>