supernova-ws /
SuperNova
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * Created by Gorlum 24.03.2018 21:54 |
||||
| 4 | */ |
||||
| 5 | |||||
| 6 | namespace Meta\Economic; |
||||
| 7 | |||||
| 8 | |||||
| 9 | use SN; |
||||
| 10 | |||||
| 11 | class BuildDataStatic { |
||||
| 12 | |||||
| 13 | /** |
||||
| 14 | * @param float $prevDivisor - because this is Hooker's client so prev result variable should be declared |
||||
| 15 | * @param array $user |
||||
| 16 | * @param array $planet |
||||
| 17 | * @param int $unit_id |
||||
| 18 | * @param array $unit_data |
||||
| 19 | * |
||||
| 20 | * @return float |
||||
| 21 | */ |
||||
| 22 | public static function getStructuresTimeDivisor($prevDivisor, $user, $planet, $unit_id, $unit_data) { |
||||
| 23 | $result = 1; |
||||
| 24 | if (in_array($unit_id, sn_get_groups('structures'))) { |
||||
| 25 | $result = pow(2, mrc_get_level($user, $planet, STRUC_FACTORY_NANO)) * (mrc_get_level($user, $planet, STRUC_FACTORY_ROBOT) + 1); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 26 | } elseif (in_array($unit_id, sn_get_groups('defense'))) { |
||||
| 27 | $result = pow(2, mrc_get_level($user, $planet, STRUC_FACTORY_NANO)) * (mrc_get_level($user, $planet, STRUC_FACTORY_HANGAR) + 1); |
||||
| 28 | } elseif (in_array($unit_id, sn_get_groups('fleet'))) { |
||||
| 29 | $result = pow(2, mrc_get_level($user, $planet, STRUC_FACTORY_NANO)) * (mrc_get_level($user, $planet, STRUC_FACTORY_HANGAR) + 1); |
||||
| 30 | } elseif (in_array($unit_id, sn_get_groups('tech'))) { |
||||
| 31 | $result = eco_get_lab_max_effective_level($user, intval($unit_data[P_REQUIRE][STRUC_LABORATORY])); |
||||
| 32 | } |
||||
| 33 | |||||
| 34 | return $result; |
||||
| 35 | } |
||||
| 36 | |||||
| 37 | /** |
||||
| 38 | * @param int $unit_id |
||||
| 39 | * |
||||
| 40 | * @return float |
||||
| 41 | */ |
||||
| 42 | public static function getMercenaryTimeDivisor($user, $planet, $unit_id) { |
||||
| 43 | if (in_array($unit_id, sn_get_groups('structures'))) { |
||||
| 44 | $mercenary = MRC_ENGINEER; |
||||
| 45 | } elseif (in_array($unit_id, sn_get_groups('tech'))) { |
||||
| 46 | $mercenary = MRC_ACADEMIC; |
||||
| 47 | } elseif (in_array($unit_id, sn_get_groups('defense'))) { |
||||
| 48 | $mercenary = MRC_FORTIFIER; |
||||
| 49 | } elseif (in_array($unit_id, sn_get_groups('fleet'))) { |
||||
| 50 | $mercenary = MRC_ENGINEER; |
||||
| 51 | } else { |
||||
| 52 | $mercenary = 0; |
||||
| 53 | } |
||||
| 54 | |||||
| 55 | return $mercenary ? mrc_modify_value($user, $planet, $mercenary, 1) : 1; |
||||
| 56 | } |
||||
| 57 | |||||
| 58 | /** |
||||
| 59 | * @param array $user |
||||
| 60 | * @param array $planet |
||||
| 61 | * @param int $unit_id |
||||
| 62 | * |
||||
| 63 | * @return float|int |
||||
| 64 | */ |
||||
| 65 | public static function getCapitalTimeDivisor($user, $planet, $unit_id) { |
||||
| 66 | static $capitalUnitGroups; |
||||
| 67 | empty($capitalUnitGroups) ? $capitalUnitGroups = sn_get_groups(sn_get_groups(GROUP_CAPITAL_BUILDING_BONUS_GROUPS)) : false; |
||||
|
0 ignored issues
–
show
It seems like
sn_get_groups(Meta\Econo..._BUILDING_BONUS_GROUPS) can also be of type array<mixed,array>; however, parameter $groups of sn_get_groups() does only seem to accept string|string[], maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 68 | |||||
| 69 | if ( |
||||
| 70 | // If planet is capital |
||||
| 71 | $user['id_planet'] == $planet['id'] |
||||
| 72 | && |
||||
| 73 | // There is capital building rate set |
||||
| 74 | SN::$gc->config->planet_capital_building_rate > 0 |
||||
| 75 | && |
||||
| 76 | // Unit is subject to Capital bonus |
||||
| 77 | in_array($unit_id, $capitalUnitGroups) |
||||
| 78 | ) { |
||||
| 79 | $capitalTimeDivisor = SN::$gc->config->planet_capital_building_rate; |
||||
| 80 | } else { |
||||
| 81 | $capitalTimeDivisor = 1; |
||||
| 82 | } |
||||
| 83 | |||||
| 84 | return $capitalTimeDivisor; |
||||
| 85 | } |
||||
| 86 | |||||
| 87 | /** |
||||
| 88 | * @param array $user |
||||
| 89 | * @param array $planet |
||||
| 90 | * @param int $unit_id |
||||
| 91 | * @param array $cost |
||||
| 92 | * |
||||
| 93 | * @return float - Time need to destroy current level of unit in seconds |
||||
| 94 | */ |
||||
| 95 | public static function getDestroyStatus($user, $planet, $unit_id, $cost) { |
||||
| 96 | static $groupStructures; |
||||
| 97 | empty($groupStructures) ? $groupStructures = sn_get_groups('structures') : false; |
||||
| 98 | |||||
| 99 | $result = !in_array($unit_id, $groupStructures) ? BUILD_INDESTRUCTABLE : |
||||
| 100 | (!mrc_get_level($user, $planet, $unit_id, false, true) ? BUILD_NO_UNITS : |
||||
| 101 | ( |
||||
| 102 | !($cost['CAN'][BUILD_DESTROY]) ? BUILD_NO_RESOURCES : |
||||
| 103 | ($cost['RESULT'][BUILD_CREATE] == BUILD_UNIT_BUSY ? BUILD_UNIT_BUSY : BUILD_ALLOWED) |
||||
| 104 | ) |
||||
| 105 | ); |
||||
| 106 | |||||
| 107 | return $result; |
||||
| 108 | } |
||||
| 109 | |||||
| 110 | /** |
||||
| 111 | * @param array $user |
||||
| 112 | * @param array $planet |
||||
| 113 | * @param array $cost |
||||
| 114 | * |
||||
| 115 | * @return float |
||||
| 116 | */ |
||||
| 117 | public static function getAutoconvertCount($user, $planet, $cost) { |
||||
| 118 | static $groupResourcesLoot; |
||||
| 119 | empty($groupResourcesLoot) ? $groupResourcesLoot = sn_get_groups('resources_loot') : false; |
||||
| 120 | |||||
| 121 | $cost_in_metal = 0; |
||||
| 122 | $planetResourcesInMetal = 0; |
||||
| 123 | foreach ($groupResourcesLoot as $resource_id) { |
||||
| 124 | $planetResourcesInMetal += floor(get_unit_cost_in([$resource_id => mrc_get_level($user, $planet, $resource_id)])); |
||||
| 125 | !empty($cost[BUILD_CREATE][$resource_id]) |
||||
| 126 | ? $cost_in_metal += get_unit_cost_in([$resource_id => $cost[BUILD_CREATE][$resource_id]]) |
||||
| 127 | : false; |
||||
| 128 | } |
||||
| 129 | |||||
| 130 | return floor($planetResourcesInMetal / $cost_in_metal); |
||||
| 131 | } |
||||
| 132 | |||||
| 133 | /** |
||||
| 134 | * @param $user |
||||
| 135 | * @param $planet |
||||
| 136 | * @param $unit_data |
||||
| 137 | * @param $unit_level |
||||
| 138 | * |
||||
| 139 | * @return array |
||||
| 140 | */ |
||||
| 141 | public static function getBasicData(&$user, $planet, $unit_data, $unit_level) { |
||||
| 142 | static $groupResourcesLoot; |
||||
| 143 | empty($groupResourcesLoot) ? $groupResourcesLoot = sn_get_groups('resources_loot') : false; |
||||
| 144 | |||||
| 145 | $cost = [ |
||||
| 146 | P_OPTIONS => [ |
||||
| 147 | P_TIME_RAW => 0, |
||||
| 148 | ], |
||||
| 149 | ]; |
||||
| 150 | |||||
| 151 | $unit_factor = !empty($unit_data[P_COST][P_FACTOR]) ? $unit_data[P_COST][P_FACTOR] : 1; |
||||
| 152 | $levelPriceFactor = pow($unit_factor, $unit_level); |
||||
| 153 | |||||
| 154 | $only_dark_matter = 0; |
||||
| 155 | $canDestroyAmount = 1000000000000; |
||||
| 156 | $canBuildAmount = !empty($unit_data[P_MAX_STACK]) ? $unit_data[P_MAX_STACK] : 1000000000000; |
||||
| 157 | foreach ($unit_data[P_COST] as $resourceId => $costResource) { |
||||
| 158 | if ($resourceId === P_FACTOR || !($levelPrice = ceil($costResource * $levelPriceFactor))) { |
||||
| 159 | continue; |
||||
| 160 | } |
||||
| 161 | |||||
| 162 | $only_dark_matter = $only_dark_matter ? $only_dark_matter : $resourceId; |
||||
| 163 | |||||
| 164 | $cost[BUILD_CREATE][$resourceId] = $levelPrice; |
||||
| 165 | $cost[BUILD_DESTROY][$resourceId] = ceil($levelPrice / 2); |
||||
| 166 | |||||
| 167 | if (in_array($resourceId, $groupResourcesLoot)) { |
||||
| 168 | $cost[P_OPTIONS][P_TIME_RAW] += get_unit_cost_in([$resourceId => $levelPrice], RES_DEUTERIUM); |
||||
| 169 | } |
||||
| 170 | |||||
| 171 | $resource_got = !empty($user) |
||||
| 172 | ? BuildDataStatic::eco_get_resource_on_location($user, $planet, $resourceId, $groupResourcesLoot) |
||||
| 173 | : 0; |
||||
| 174 | |||||
| 175 | $canBuildAmount = min($canBuildAmount, floor($resource_got / $cost[BUILD_CREATE][$resourceId])); |
||||
| 176 | $canDestroyAmount = min($canDestroyAmount, floor($resource_got / $cost[BUILD_DESTROY][$resourceId])); |
||||
| 177 | } |
||||
| 178 | $cost['CAN'][BUILD_CREATE] = $canBuildAmount > 0 ? floor($canBuildAmount) : 0; |
||||
| 179 | $cost['CAN'][BUILD_DESTROY] = $canDestroyAmount > 0 ? floor($canDestroyAmount) : 0; |
||||
| 180 | $cost[P_OPTIONS][P_ONLY_DARK_MATTER] = $only_dark_matter == RES_DARK_MATTER; |
||||
| 181 | |||||
| 182 | return $cost; |
||||
| 183 | } |
||||
| 184 | |||||
| 185 | /** |
||||
| 186 | * Special function to get resource on location |
||||
| 187 | * |
||||
| 188 | * @param array $user |
||||
| 189 | * @param array $planet |
||||
| 190 | * @param int $resource_id |
||||
| 191 | * @param array $groupResourcesLoot |
||||
| 192 | * |
||||
| 193 | * @return float |
||||
| 194 | */ |
||||
| 195 | public static function eco_get_resource_on_location($user, $planet, $resource_id, $groupResourcesLoot) { |
||||
| 196 | if (in_array($resource_id, $groupResourcesLoot)) { |
||||
| 197 | $resource_got = mrc_get_level($user, $planet, $resource_id); |
||||
| 198 | } elseif ($resource_id == RES_DARK_MATTER) { |
||||
| 199 | $resource_got = mrc_get_level($user, [], $resource_id); |
||||
| 200 | } elseif ($resource_id == RES_ENERGY) { |
||||
| 201 | $resource_got = max(0, $planet['energy_max'] - $planet['energy_used']); |
||||
| 202 | } else { |
||||
| 203 | $resource_got = 0; |
||||
| 204 | } |
||||
| 205 | |||||
| 206 | return $resource_got; |
||||
|
0 ignored issues
–
show
|
|||||
| 207 | } |
||||
| 208 | |||||
| 209 | /** |
||||
| 210 | * @param array $user |
||||
| 211 | * @param array $planet |
||||
| 212 | * @param int $unit_id |
||||
| 213 | * @param float $costCanBuildCreate |
||||
| 214 | * |
||||
| 215 | * @return int|mixed |
||||
| 216 | */ |
||||
| 217 | public static function getBuildStatus(&$user, $planet, $unit_id, $costCanBuildCreate) { |
||||
| 218 | $result = eco_can_build_unit($user, $planet, $unit_id); |
||||
| 219 | |||||
| 220 | // Additional check for resources. If no units can be built - it means that there are not enough resources |
||||
| 221 | return $result == BUILD_ALLOWED && $costCanBuildCreate < 1 ? BUILD_NO_RESOURCES : $result; |
||||
| 222 | } |
||||
| 223 | |||||
| 224 | |||||
| 225 | } |
||||
| 226 |