[], 2 => [], 3 => [], 4 => [], 5 => [], 6 => [] ]; $response = null; $uid = filter_input(INPUT_POST, 'uid'); if ($uid == null){ return -1; } $unit_id = filter_input(INPUT_POST, 'unit'); if ($unit_id == null){ return -2; } // Sets $sets = []; foreach ($_POST["set"] as $x){ array_push($sets, intval($x)); } if (sizeof($sets) < 2 || sizeof($sets) > 3){ return -3; } // Main stats in even slots. $stats = []; foreach ($_POST["stat"] as $x){ array_push($stats, intval($x)); } $source = filter_input(INPUT_POST, 'source'); if ($source == null){ return -4; } $limit = intval(filter_input(INPUT_POST, 'limit')); if ($limit == 0){ return -5; } $tuning = filter_input(INPUT_POST, 'tuning'); if ($tuning == null){ return -6; } // Instantiate the unit $unit = new Unit($unit_id, true, $uid); // Get build query: $base_s = " SELECT id, type, slot, level, stars, main_stat, main_stat_value, innate_stat, innate_stat_value, substat_1, substat_1_value + substat_1_grind AS substat_1_value, substat_2, substat_2_value + substat_2_grind AS substat_2_value, substat_3, substat_3_value + substat_3_grind AS substat_3_value, substat_4, substat_4_value + substat_4_grind AS substat_4_value FROM rune WHERE uid = :uid AND type IN ( "; foreach ($sets as $set){ $base_s .= ($set . ","); } $base_s .= "-1) AND "; switch ($source){ case 0: // Storage only (or itself) $base_s .= " ( assigned_to = :unit_id OR assigned_to IS NULL )"; break; case 1: // Units in no teams (or itself) $base_s .= " ( assigned_to = :unit_id OR assigned_to IS NULL OR assigned_to NOT IN (SELECT DISTINCT unit FROM team_unit) ) "; break; case 2: // Units in teams with 0 score (or itself) $base_s .= " ( assigned_to = :unit_id OR assigned_to IS NULL OR assigned_to NOT IN (SELECT DISTINCT unit FROM team_unit) OR assigned_to NOT IN ( SELECT DISTINCT unit FROM team, team_unit WHERE team.id = team_unit.team AND team.score > 0 ) ) "; break; case 3: // Units with lower overall score (or itself) // TODO break; case 4: // All runes - dont filter $base_s .= " 1 = 1 "; break; default: // Invalid options - return nothing $base_s .= " 1 = 0 "; } $s_order = " ORDER BY stars DESC, original_quality DESC, max_efficiency DESC, efficiency DESC, level DESC"; $s_in = " main_stat IN (:stat_0, :stat_1, :stat_2) "; $s = ["", "", "", "", "", "", ""]; $s[1] = $base_s . " AND slot = 1 " . $s_order; $s[3] = $base_s . " AND slot = 3 " . $s_order; $s[5] = $base_s . " AND slot = 5 " . $s_order; $s[2] = $base_s . " AND slot = 2 AND " . $s_in . $s_order; $s[4] = $base_s . " AND slot = 4 AND " . $s_in . $s_order; $s[6] = $base_s . " AND slot = 6 AND " . $s_in . $s_order; $total_candidates = 0; for ($i = 1; $i <= 6; $i ++){ $statement = $db->prepare($s[$i]); $statement->bindValue(':uid', $uid, SQLITE3_INTEGER); $statement->bindValue(':unit_id', $unit_id, SQLITE3_INTEGER); $statement->bindValue(':stat_0', $stats[0], SQLITE3_INTEGER); $statement->bindValue(':stat_1', $stats[1], SQLITE3_INTEGER); $statement->bindValue(':stat_2', $stats[2], SQLITE3_INTEGER); $q = $statement->execute(); while ($r = $q->fetchArray(SQLITE3_ASSOC)){ $rune = create_rune_array($unit, $r, $tuning); //array_push($candidates[$i], new Rune($r["id"])); array_push($candidates[$i], $rune); $total_candidates ++; } } //$max_combinations = sizeof($candidates[1]) * sizeof($candidates[2]) * sizeof($candidates[3]) * sizeof($candidates[4]) * sizeof($candidates[5]) * sizeof($candidates[6]); // TODO: Build response $response = json_encode($candidates); if ($response == null){ return 0; } else{ return $response; } } /** * Creates an array with the stats provided by a rune on a unit. * * @param Unit $unit Target unit. * @param int $r Rune main stat ID. * @param int $stars Rune stars. * @return int Value of the main stat at the selected level. */ function create_rune_array($unit, $r, $tuning){ $rune = [ "ID" => $r["id"], "TYPE" => $r["type"], "ATK" => 0, "DEF" => 0, "HP" => 0, "SPD" => 0, "CRR" => 0, "CRD" => 0, "ACC" => 0, "RES" => 0 ]; // Calculate main stat value based on requested level. $level = $r["level"]; $main_value = $r["main_stat_value"]; switch ($tuning){ case 1: // All +12 if ($level < 12){ $main_value = get_main_stat_at_level(12, $r["main_stat"], $r["stars"]); } break; case 2: // Even +15, Odd + 12 if ($level < 15 && $r["slot"] % 2 == 0){ $main_value = get_main_stat_at_level(15, $r["main_stat"], $r["stars"]); } elseif ($level < 12 && $r["slot"] % 2 != 0){ $main_value = get_main_stat_at_level(12, $r["main_stat"], $r["stars"]); } break; case 3: // All +15 if ($level < 15){ $main_value = get_main_stat_at_level(15, $r["main_stat"], $r["stars"]); } break; } // Array to loop. $stats = [ [ "STAT" => $r["main_stat"], "VALUE" => $main_value ], [ "STAT" => $r["innate_stat"], "VALUE" => $r["innate_stat_value"] ], [ "STAT" => $r["substat_1"], "VALUE" => $r["substat_1_value"] ], [ "STAT" => $r["substat_2"], "VALUE" => $r["substat_2_value"] ], [ "STAT" => $r["substat_3"], "VALUE" => $r["substat_3_value"] ], [ "STAT" => $r["substat_4"], "VALUE" => $r["substat_4_value"] ] ]; foreach ($stats as $stat){ switch ($stat["STAT"]){ case RUNE_STAT_ID::ATK: $rune["ATK"] += $stat["VALUE"]; break; case RUNE_STAT_ID::ATK_P: $rune["ATK"] += ($unit->attack * $stat["VALUE"] / 100); break; case RUNE_STAT_ID::DEF: $rune["DEF"] += $stat["VALUE"]; break; case RUNE_STAT_ID::DEF_P: $rune["DEF"] += ($unit->defense * $stat["VALUE"] / 100); break; case RUNE_STAT_ID::HP: $rune["HP"] += $stat["VALUE"]; break; case RUNE_STAT_ID::HP_P: $rune["HP"] += ($unit->hp * $stat["VALUE"] / 100); break; case RUNE_STAT_ID::SPD: $rune["SPD"] += $stat["VALUE"]; break; case RUNE_STAT_ID::CRR: $rune["CRR"] += $stat["VALUE"]; break; case RUNE_STAT_ID::CRD: $rune["CRD"] += $stat["VALUE"]; break; case RUNE_STAT_ID::ACC: $rune["ACC"] += $stat["VALUE"]; break; case RUNE_STAT_ID::RES: $rune["RES"] += $stat["VALUE"]; break; } } return $rune; } /** * Gets the value of the main stat at an arbitrary level. * * @param int $level Desired level. * @param int $stat Rune main stat ID. * @param int $stars Rune stars. * @return int Value of the main stat at the selected level. */ function get_main_stat_at_level($level, $stat, $stars){ $level = intval($level); if ($level < 0 || $level > 15){ return 0; } switch ($stat){ case RUNE_STAT_ID::HP: return RUNE_STAT_VALUES::HP[$stars][$level]; case RUNE_STAT_ID::HP_P: return RUNE_STAT_VALUES::HP_P[$stars][$level]; case RUNE_STAT_ID::ATK: return RUNE_STAT_VALUES::ATK[$stars][$level]; case RUNE_STAT_ID::ATK_P: return RUNE_STAT_VALUES::ATK_P[$stars][$level]; case RUNE_STAT_ID::DEF: return RUNE_STAT_VALUES::DEF[$stars][$level]; case RUNE_STAT_ID::DEF_P: return RUNE_STAT_VALUES::DEF_P[$stars][$level]; case RUNE_STAT_ID::SPD: return RUNE_STAT_VALUES::SPD[$stars][$level]; case RUNE_STAT_ID::CRR: return RUNE_STAT_VALUES::CRR[$stars][$level]; case RUNE_STAT_ID::CRD: return RUNE_STAT_VALUES::CRD[$stars][$level]; case RUNE_STAT_ID::RES: return RUNE_STAT_VALUES::RES[$stars][$level]; case RUNE_STAT_ID::ACC: return RUNE_STAT_VALUES::ACC[$stars][$level]; default: return 0; } } ?>