optimize.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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. // Get Buld query:
  85. $base_s = "
  86. SELECT
  87. id,
  88. slot
  89. FROM rune
  90. WHERE
  91. uid = $uid AND
  92. type IN (
  93. ";
  94. foreach ($sets as $set){
  95. $base_s .= ($set . ",");
  96. }
  97. $base_s .= "-1) AND ";
  98. switch ($source){
  99. case 0: // Storage only (or itself)
  100. $base_s .= "
  101. (
  102. assigned_to = $unit_id OR
  103. assigned_to IS NULL
  104. )";
  105. break;
  106. case 1: // Units in no teams (or itself)
  107. $base_s .= "
  108. (
  109. assigned_to = $unit_id OR
  110. assigned_to IS NULL OR
  111. assigned_to NOT IN (SELECT DISTINCT unit FROM team_unit)
  112. )
  113. ";
  114. break;
  115. case 2: // Units in teams with 0 score (or itself)
  116. $base_s .= "
  117. (
  118. assigned_to = $unit_id OR
  119. assigned_to IS NULL OR
  120. assigned_to NOT IN (SELECT DISTINCT unit FROM team_unit) OR
  121. assigned_to NOT IN (
  122. SELECT DISTINCT unit
  123. FROM
  124. team,
  125. team_unit
  126. WHERE
  127. team.id = team_unit.team AND
  128. team.score > 0
  129. )
  130. )
  131. ";
  132. break;
  133. case 3: // Units with lower overall score (or itself)
  134. // TODO
  135. break;
  136. case 4: // All runes - dont filter
  137. $base_s .= " 1 = 1 ";
  138. break;
  139. default: // Invalid options - return nothing
  140. $base_s .= " 1 = 0 ";
  141. }
  142. $s_order = " ORDER BY stars DESC, original_quality DESC, max_efficiency DESC, efficiency DESC, level DESC";
  143. $s = ["", "", "", "", "", "", ""];
  144. $s[1] = $base_s . " AND slot = 1 " . $s_order . " LIMIT 25";
  145. $s[3] = $base_s . " AND slot = 3 " . $s_order . " LIMIT 25";
  146. $s[5] = $base_s . " AND slot = 5 " . $s_order . " LIMIT 25";
  147. $s[2] = $base_s . " AND slot = 2 AND main_stat = " . $stats[0] . $s_order . " LIMIT 20";
  148. $s[4] = $base_s . " AND slot = 4 AND main_stat = " . $stats[1] . $s_order . " LIMIT 20";
  149. $s[6] = $base_s . " AND slot = 6 AND main_stat = " . $stats[2] . $s_order . " LIMIT 20";
  150. $total_candidates = 0;
  151. for ($i = 1; $i <= 6; $i ++){
  152. $q = $db->query($s[$i]);
  153. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  154. array_push($candidates[$i], new Rune($r["id"]));
  155. $total_candidates ++;
  156. }
  157. }
  158. $max_combinations = sizeof($candidates[1]) * sizeof($candidates[2]) * sizeof($candidates[3]) * sizeof($candidates[4]) * sizeof($candidates[5]) * sizeof($candidates[6]);
  159. if ($max_combinations == 0){
  160. $res = [
  161. "total" => 0,
  162. "options" => []
  163. ];
  164. $response = json_encode($res);
  165. return $response;
  166. }
  167. elseif ($max_combinations > 125000000){
  168. // This should never happen unles query limits are changed
  169. http_response_code(413); // Payload too large;
  170. die();
  171. return;
  172. }
  173. // Get the unit and its base stats
  174. $unit = new Unit($unit_id, true, $uid);
  175. $unit_attack = $unit->attack + $unit->artifact_attack + $unit->building_attack;
  176. $unit_base_attack = $unit->attack;
  177. $unit_defense = $unit->defense + $unit->artifact_defense + $unit->building_defense;
  178. $unit_base_defense = $unit->defense;
  179. $unit_hp = $unit->hp + $unit->artifact_hp + $unit->building_hp;
  180. $unit_base_hp = $unit->hp;
  181. $unit_speed = $unit->speed + $unit->artifact_speed + $unit->building_speed;
  182. $unit_base_speed = $unit->speed;
  183. $unit_crit_rate = $unit->crit_rate + $unit->artifact_crit_rate + $unit->building_crit_rate;
  184. $unit_base_crit_rate = $unit->crit_rate;
  185. $unit_crit_damage = $unit->crit_damage + $unit->artifact_crit_damage + $unit->building_crit_damage;
  186. $unit_base_crit_damage = $unit->crit_damage;
  187. $unit_accuracy = $unit->accuracy + $unit->artifact_accuracy + $unit->building_accuracy;
  188. $unit_base_accuracy = $unit->accuracy;
  189. $unit_resistance = $unit->resistance + $unit->artifact_resistance + $unit->building_resistance;
  190. $unit_base_resistance = $unit->resistance;
  191. $i = [0, 0, 0, 0, 0, 0, 0]; // 7, so I can start with 1
  192. while (true){
  193. // Check if set combination is valid
  194. if (valid_sets($candidates, $i, $sets) == true){
  195. // Do things
  196. $iteration = [
  197. "attack" => $unit_attack,
  198. "defense" => $unit_defense,
  199. "hp" => $unit_hp,
  200. "speed" => $unit_speed,
  201. "crit_rate" => $unit_crit_rate,
  202. "crit_damage" => $unit_crit_damage,
  203. "accuracy" => $unit_accuracy,
  204. "resistance" => $unit_resistance,
  205. "ehp" => 0,
  206. "dmg" => 0,
  207. ];
  208. for ($r = 1; $r <= 6; $r ++){
  209. // Loop selected runes and calculate stats
  210. $main_value = $candidates[$r][$i[$r]]->main_stat_value;
  211. if ($tuning == 1){ // All +12
  212. if ($candidates[$r][$i[$r]]->level < 12){
  213. $main_value = $candidates[$r][$i[$r]]->get_main_stat_at_level(12);
  214. }
  215. }
  216. elseif ($tuning == 2){ // Even +15, Odd + 12
  217. if ($candidates[$r][$i[$r]]->level < 15 && $candidates[$r][$i[$r]]->slot % 2 == 0){
  218. $main_value = $candidates[$r][$i[$r]]->get_main_stat_at_level(15);
  219. }
  220. elseif ($candidates[$r][$i[$r]]->level < 12 && $candidates[$r][$i[$r]]->slot % 2 != 0){
  221. $main_value = $candidates[$r][$i[$r]]->get_main_stat_at_level(12);
  222. }
  223. }
  224. if ($tuning == 3){ // All +15
  225. if ($candidates[$r][$i[$r]]->level < 15){
  226. $main_value = $candidates[$r][$i[$r]]->get_main_stat_at_level(15);
  227. }
  228. }
  229. sum_rune_stat(
  230. $candidates[$r][$i[$r]]->main_stat,
  231. $main_value,
  232. $iteration,
  233. $unit_base_attack,
  234. $unit_base_defense,
  235. $unit_base_hp,
  236. );
  237. sum_rune_stat(
  238. $candidates[$r][$i[$r]]->innate_stat,
  239. $candidates[$r][$i[$r]]->innate_stat_value,
  240. $iteration,
  241. $unit_base_attack,
  242. $unit_base_defense,
  243. $unit_base_hp,
  244. );
  245. sum_rune_stat(
  246. $candidates[$r][$i[$r]]->substat_1,
  247. $candidates[$r][$i[$r]]->substat_1_value,
  248. $iteration,
  249. $unit_base_attack,
  250. $unit_base_defense,
  251. $unit_base_hp,
  252. );
  253. sum_rune_stat(
  254. $candidates[$r][$i[$r]]->substat_2,
  255. $candidates[$r][$i[$r]]->substat_2_value,
  256. $iteration,
  257. $unit_base_attack,
  258. $unit_base_defense,
  259. $unit_base_hp,
  260. );
  261. sum_rune_stat(
  262. $candidates[$r][$i[$r]]->substat_3,
  263. $candidates[$r][$i[$r]]->substat_3_value,
  264. $iteration,
  265. $unit_base_attack,
  266. $unit_base_defense,
  267. $unit_base_hp,
  268. );
  269. sum_rune_stat(
  270. $candidates[$r][$i[$r]]->substat_4,
  271. $candidates[$r][$i[$r]]->substat_4_value,
  272. $iteration,
  273. $unit_base_attack,
  274. $unit_base_defense,
  275. $unit_base_hp,
  276. );
  277. }
  278. // Calculate effective_hp
  279. $hp = $iteration["hp"];
  280. $def = $iteration["defense"];
  281. $ehp = ceil(((($def * 3.5) + 1140) * $hp) / 1000);
  282. $iteration["ehp"] = $ehp;
  283. // Calculate effective_dmg
  284. $atk = $iteration["attack"];
  285. $crr = $iteration["crit_rate"];
  286. if ($crr > 100){
  287. $crr = 100;
  288. }
  289. $crd = $iteration["crit_damage"];
  290. $edmg = ceil(($atk * (100 - $crr) / 100) + (($atk + ($atk * $crd / 100)) * $crr / 100));
  291. $iteration["dmg"] = $edmg;
  292. // Compare with filters:
  293. if (
  294. $iteration["attack"] >= $min["attack"] &&
  295. $iteration["defense"] >= $min["defense"] &&
  296. $iteration["hp"] >= $min["hp"] &&
  297. $iteration["speed"] >= $min["speed"] &&
  298. $iteration["crit_rate"] >= $min["crit_rate"] &&
  299. $iteration["crit_damage"] >= $min["crit_damage"] &&
  300. $iteration["accuracy"] >= $min["accuracy"] &&
  301. $iteration["resistance"] >= $min["resistance"] &&
  302. $iteration["ehp"] >= $min["ehp"] &&
  303. $iteration["dmg"] >= $min["dmg"]
  304. ){
  305. // Calculate the variation
  306. $variation = 0;
  307. $variation += (($iteration["attack"] - $unit->total_attack) * 1);
  308. $variation += (($iteration["defense"] - $unit->total_defense) * 1);
  309. $variation += (($iteration["hp"] - $unit->total_hp) * (1 / 15));
  310. $variation += (($iteration["speed"] - $unit->total_speed) * 1.5);
  311. $variation += (($iteration["crit_rate"] - $unit->total_crit_rate) * 0.85);
  312. $variation += (($iteration["crit_damage"] - $unit->total_crit_damage) * 0.9);
  313. $variation += (($iteration["accuracy"] - $unit->total_accuracy) * 0.85);
  314. $variation += (($iteration["resistance"] - $unit->total_resistance) * 0.85);
  315. $variation = ceil($variation);
  316. // Calculate efficiency
  317. $efficiency = 0;
  318. // Build the array
  319. $option = [
  320. "variation" => $variation,
  321. "runes" => [
  322. [
  323. "id" => $candidates[1][$i[1]]->id,
  324. "html" => (HTML::rune_table($candidates[1][$i[1]]))
  325. ],
  326. [
  327. "id" => $candidates[2][$i[2]]->id,
  328. "html" => (HTML::rune_table($candidates[2][$i[2]]))
  329. ],
  330. [
  331. "id" => $candidates[3][$i[3]]->id,
  332. "html" => (HTML::rune_table($candidates[3][$i[3]]))
  333. ],
  334. [
  335. "id" => $candidates[4][$i[4]]->id,
  336. "html" => (HTML::rune_table($candidates[4][$i[4]]))
  337. ],
  338. [
  339. "id" => $candidates[5][$i[5]]->id,
  340. "html" => (HTML::rune_table($candidates[5][$i[5]]))
  341. ],
  342. [
  343. "id" => $candidates[1][$i[1]]->id,
  344. "html" => HTML::rune_table($candidates[6][$i[6]])
  345. ]
  346. ],
  347. "stats" => [
  348. "attack" => ceil($iteration["attack"]),
  349. "defense" => ceil($iteration["defense"]),
  350. "hp" => ceil($iteration["hp"]),
  351. "speed" => ceil($iteration["speed"]),
  352. "crit_rate" => ceil($iteration["crit_rate"]),
  353. "crit_damage" => ceil($iteration["crit_damage"]),
  354. "accuracy" => ceil($iteration["accuracy"]),
  355. "attack" => ceil($iteration["attack"]),
  356. "resistance" => ceil($iteration["resistance"]),
  357. "ehp" => ceil($iteration["ehp"]),
  358. "dmg" => ceil($iteration["dmg"])
  359. ]
  360. ];
  361. array_push($options, $option);
  362. if (sizeof($options) > 5000){
  363. http_response_code(413); // Payload too large;
  364. die();
  365. return;
  366. }
  367. }
  368. }
  369. // Increase counters
  370. $i[6] ++;
  371. if ($i[6] == sizeof($candidates[6])){
  372. $i[6] = 0;
  373. $i[5] ++;
  374. }
  375. if ($i[5] == sizeof($candidates[5])){
  376. $i[5] = 0;
  377. $i[4] ++;
  378. }
  379. if ($i[4] == sizeof($candidates[4])){
  380. $i[4] = 0;
  381. $i[3] ++;
  382. }
  383. if ($i[3] == sizeof($candidates[3])){
  384. $i[3] = 0;
  385. $i[2] ++;
  386. }
  387. if ($i[2] == sizeof($candidates[2])){
  388. $i[2] = 0;
  389. $i[1] ++;
  390. }
  391. // Calculate exit condition
  392. if (
  393. $i[1] >= sizeof($candidates[1]) - 1 &&
  394. $i[2] >= sizeof($candidates[2]) - 1 &&
  395. $i[3] >= sizeof($candidates[3]) - 1 &&
  396. $i[4] >= sizeof($candidates[4]) - 1 &&
  397. $i[5] >= sizeof($candidates[5]) - 1 &&
  398. $i[6] >= sizeof($candidates[6]) - 1
  399. ){
  400. break;
  401. }
  402. }
  403. // Sort options by variation
  404. usort($options, "sortOptions");
  405. // Limit the array to the top options
  406. $original_options_size = sizeof($options);
  407. while (sizeof($options) > $limit || sizeof($options) > $HARD_LIMIT){
  408. array_pop($options);
  409. }
  410. $res = [
  411. "total" => sizeof($options),
  412. "skipped" => ($original_options_size - sizeof($options)),
  413. "options" => $options
  414. ];
  415. $response = json_encode($res);
  416. if ($response == null){
  417. return 0;
  418. }
  419. else{
  420. return $response;
  421. }
  422. }
  423. /**
  424. * Validates a rune combination.
  425. *
  426. * Checks that set numbers are OK.
  427. *
  428. * @param \Rune[][] $candidates Candidate runes.
  429. * @param int[] $indexes Currently selected indexes.
  430. */
  431. function valid_sets($candidates, $indexes, $requested){
  432. $sets = [
  433. RUNE_SET_ID::ENERGY => 0,
  434. RUNE_SET_ID::GUARD => 0,
  435. RUNE_SET_ID::SWIFT => 0,
  436. RUNE_SET_ID::BLADE => 0,
  437. RUNE_SET_ID::RAGE => 0,
  438. RUNE_SET_ID::FOCUS => 0,
  439. RUNE_SET_ID::ENDURE => 0,
  440. RUNE_SET_ID::FATAL => 0,
  441. RUNE_SET_ID::DESPAIR => 0,
  442. RUNE_SET_ID::VAMPIRE => 0,
  443. RUNE_SET_ID::VIOLENT => 0,
  444. RUNE_SET_ID::NEMESIS => 0,
  445. RUNE_SET_ID::WILL => 0,
  446. RUNE_SET_ID::SHIELD => 0,
  447. RUNE_SET_ID::REVENGE => 0,
  448. RUNE_SET_ID::DESTROY => 0,
  449. RUNE_SET_ID::FIGHT => 0,
  450. RUNE_SET_ID::DETERMINATION => 0,
  451. RUNE_SET_ID::ENHANCE => 0,
  452. RUNE_SET_ID::ACCURACY => 0,
  453. RUNE_SET_ID::TOLERANCE => 0
  454. ];
  455. for ($i = 1; $i <= 6; $i++){
  456. $sets[$candidates[$i][$indexes[$i]]->type->id] ++;
  457. }
  458. for ($i = 0; $i < sizeof($sets); $i ++){
  459. switch ($i){
  460. case RUNE_SET_ID::ENERGY:
  461. case RUNE_SET_ID::GUARD:
  462. case RUNE_SET_ID::NEMESIS:
  463. case RUNE_SET_ID::BLADE:
  464. case RUNE_SET_ID::FOCUS:
  465. case RUNE_SET_ID::ENDURE:
  466. case RUNE_SET_ID::WILL:
  467. case RUNE_SET_ID::SHIELD:
  468. case RUNE_SET_ID::REVENGE:
  469. case RUNE_SET_ID::DESTROY:
  470. case RUNE_SET_ID::FIGHT:
  471. case RUNE_SET_ID::DETERMINATION:
  472. case RUNE_SET_ID::ENHANCE:
  473. case RUNE_SET_ID::ACCURACY:
  474. case RUNE_SET_ID::TOLERANCE:
  475. if ($sets[$i] % 2 != 0){
  476. return false;
  477. }
  478. break;
  479. case RUNE_SET_ID::DESPAIR:
  480. case RUNE_SET_ID::SWIFT:
  481. case RUNE_SET_ID::RAGE:
  482. case RUNE_SET_ID::FATAL:
  483. case RUNE_SET_ID::VAMPIRE:
  484. case RUNE_SET_ID::VIOLENT:
  485. if ($sets[$i] != 4 && $sets[$i != 0]){
  486. return false;
  487. }
  488. break;
  489. }
  490. }
  491. // Sets are valid, now check if they comply with filters
  492. $requested_sets = [
  493. RUNE_SET_ID::ENERGY => 0,
  494. RUNE_SET_ID::GUARD => 0,
  495. RUNE_SET_ID::SWIFT => 0,
  496. RUNE_SET_ID::BLADE => 0,
  497. RUNE_SET_ID::RAGE => 0,
  498. RUNE_SET_ID::FOCUS => 0,
  499. RUNE_SET_ID::ENDURE => 0,
  500. RUNE_SET_ID::FATAL => 0,
  501. RUNE_SET_ID::DESPAIR => 0,
  502. RUNE_SET_ID::VAMPIRE => 0,
  503. RUNE_SET_ID::VIOLENT => 0,
  504. RUNE_SET_ID::NEMESIS => 0,
  505. RUNE_SET_ID::WILL => 0,
  506. RUNE_SET_ID::SHIELD => 0,
  507. RUNE_SET_ID::REVENGE => 0,
  508. RUNE_SET_ID::DESTROY => 0,
  509. RUNE_SET_ID::FIGHT => 0,
  510. RUNE_SET_ID::DETERMINATION => 0,
  511. RUNE_SET_ID::ENHANCE => 0,
  512. RUNE_SET_ID::ACCURACY => 0,
  513. RUNE_SET_ID::TOLERANCE => 0
  514. ];
  515. for ($i = 0; $i < sizeof($requested); $i ++){
  516. switch ($requested[$i]){
  517. case RUNE_SET_ID::ENERGY:
  518. case RUNE_SET_ID::GUARD:
  519. case RUNE_SET_ID::NEMESIS:
  520. case RUNE_SET_ID::BLADE:
  521. case RUNE_SET_ID::FOCUS:
  522. case RUNE_SET_ID::ENDURE:
  523. case RUNE_SET_ID::WILL:
  524. case RUNE_SET_ID::SHIELD:
  525. case RUNE_SET_ID::REVENGE:
  526. case RUNE_SET_ID::DESTROY:
  527. case RUNE_SET_ID::FIGHT:
  528. case RUNE_SET_ID::DETERMINATION:
  529. case RUNE_SET_ID::ENHANCE:
  530. case RUNE_SET_ID::ACCURACY:
  531. case RUNE_SET_ID::TOLERANCE:
  532. $requested_sets[$requested[$i]] += 2;
  533. break;
  534. case RUNE_SET_ID::DESPAIR:
  535. case RUNE_SET_ID::SWIFT:
  536. case RUNE_SET_ID::RAGE:
  537. case RUNE_SET_ID::FATAL:
  538. case RUNE_SET_ID::VAMPIRE:
  539. case RUNE_SET_ID::VIOLENT:
  540. $requested_sets[$requested[$i]] += 4;
  541. break;
  542. }
  543. }
  544. if ($sets === $requested_sets){
  545. return true;
  546. }
  547. else{
  548. return false;
  549. }
  550. }
  551. /**
  552. * Calculates the stat increase by a rune property.
  553. *
  554. * @param string $stat Stat type.
  555. * @param int $value Stat increase value.
  556. * @param int[] Reference to iteration stats array.
  557. */
  558. function sum_rune_stat($stat, $value, &$iteration, $base_atk, $base_def, $base_hp){
  559. switch ($stat){
  560. case RUNE_STAT_ID::ATK:
  561. $iteration["attack"] += $value;
  562. break;
  563. case RUNE_STAT_ID::DEF:
  564. $iteration["defense"] += $value;
  565. break;
  566. case RUNE_STAT_ID::HP:
  567. $iteration["hp"] += $value;
  568. break;
  569. case RUNE_STAT_ID::SPD:
  570. $iteration["speed"] += $value;
  571. break;
  572. case RUNE_STAT_ID::CRR:
  573. $iteration["crit_rate"] += $value;
  574. break;
  575. case RUNE_STAT_ID::CRD:
  576. $iteration["crit_damage"] += $value;
  577. break;
  578. case RUNE_STAT_ID::ACC:
  579. $iteration["accuracy"] += $value;
  580. break;
  581. case RUNE_STAT_ID::RES:
  582. $iteration["resistance"] += $value;
  583. break;
  584. case RUNE_STAT_ID::ATK_P:
  585. $iteration["attack"] += ($base_atk * $value / 100);
  586. break;
  587. case RUNE_STAT_ID::DEF_P:
  588. $iteration["defense"] += ($base_def * $value / 100);
  589. break;
  590. case RUNE_STAT_ID::HP_P:
  591. $iteration["hp"] += ($base_hp * $value / 100);
  592. break;
  593. }
  594. }
  595. /**
  596. * Function to sort arrays based on the "increment" key.
  597. *
  598. * @param $a An array
  599. * @param $b Other array
  600. * @return int Positive if $b before $a, negative otherwise.
  601. */
  602. function sortOptions($a, $b) {
  603. return $b["variation"] - $a["variation"];
  604. }
  605. ?>