supernova-ws /
SuperNova
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Created by Gorlum 01.10.2017 17:53 |
||
| 4 | */ |
||
| 5 | |||
| 6 | namespace Deprecated; |
||
| 7 | |||
| 8 | use \template; |
||
| 9 | use \classLocale; |
||
| 10 | use Planet\DBStaticPlanet; |
||
| 11 | use \HelperString; |
||
| 12 | |||
| 13 | class PageImperium { |
||
| 14 | |||
| 15 | const GROUPS_TO_NAMES = [ |
||
| 16 | UNIT_STRUCTURES => 'structures', |
||
| 17 | UNIT_STRUCTURES_SPECIAL => 'structures', |
||
| 18 | UNIT_SHIPS => 'fleet', |
||
| 19 | UNIT_DEFENCE => 'defense', |
||
| 20 | ]; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var classLocale $lang ; |
||
| 24 | */ |
||
| 25 | protected $lang; |
||
| 26 | |||
| 27 | |||
| 28 | /** |
||
| 29 | * @param template|null $template |
||
| 30 | */ |
||
| 31 | public static function viewStatic(template $template = null) { |
||
| 32 | $that = new static(); |
||
| 33 | |||
| 34 | $template = $that->view($template); |
||
| 35 | unset($that); |
||
| 36 | |||
| 37 | return $template; |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param template|null $template |
||
| 42 | */ |
||
| 43 | public static function modelStatic(template $template = null) { |
||
| 44 | $that = new static(); |
||
| 45 | |||
| 46 | $that->modelAdjustMinePercent(); |
||
| 47 | unset($that); |
||
| 48 | |||
| 49 | return $template; |
||
| 50 | } |
||
| 51 | |||
| 52 | public function __construct() { |
||
| 53 | global $lang; |
||
| 54 | $this->lang = $lang; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param template|null $template |
||
| 59 | * |
||
| 60 | * @return template |
||
| 61 | */ |
||
| 62 | public function view(template $template = null) { |
||
| 63 | global $user; |
||
| 64 | |||
| 65 | list($planets, $ques) = $this->getUpdatedUserPlanetsAndQues($user); |
||
| 66 | $fleets = $this->fleetGetFlyingToPlanets($planets); |
||
| 67 | |||
| 68 | $template = gettemplate('imperium', $template); |
||
| 69 | |||
| 70 | $template->assign_recursive(templateFillPercent()); |
||
| 71 | |||
| 72 | $template->assign_recursive($this->tplRenderPlanets($user, $planets, $fleets)); |
||
| 73 | $template->assign_recursive($this->tplRenderFleets($planets, $fleets)); |
||
| 74 | $this->tplTotalPlanetInfo($template, $planets); |
||
| 75 | |||
| 76 | foreach (self::GROUPS_TO_NAMES as $unit_group_id => $internalGroupName) { |
||
| 77 | $template->assign_block_vars('prods', array( |
||
| 78 | 'NAME' => $this->lang['tech'][$unit_group_id], |
||
| 79 | )); |
||
| 80 | $this->imperiumTemplatizeUnitGroup($user, $template, $unit_group_id, $planets, $ques, $fleets); |
||
| 81 | } |
||
| 82 | |||
| 83 | $template->assign_var('amount', count($planets) + 2); |
||
| 84 | $this->tplAddGlobals($user, $template); |
||
| 85 | |||
| 86 | return $template; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Store current mines load in DB |
||
| 91 | */ |
||
| 92 | protected function modelAdjustMinePercent() { |
||
| 93 | global $user; |
||
| 94 | |||
| 95 | if (!sys_get_param('save_production') || !is_array($production = sys_get_param('percent')) || empty($production)) { |
||
| 96 | return; |
||
| 97 | } |
||
| 98 | |||
| 99 | $sn_group_factories = sn_get_groups('factories'); |
||
| 100 | |||
| 101 | foreach (DBStaticPlanet::db_planet_list_sorted($user, false, '*') as $planetId => $planet) { |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 102 | $query = []; |
||
| 103 | foreach ($sn_group_factories as $factory_unit_id) { |
||
| 104 | $unit_db_name_porcent = pname_factory_production_field_name($factory_unit_id); |
||
| 105 | if ( |
||
| 106 | // Changes required to mine production |
||
| 107 | isset($production[$factory_unit_id][$planet['id']]) |
||
| 108 | // If mine is managed |
||
| 109 | && get_unit_param($factory_unit_id, P_MINING_IS_MANAGED) |
||
| 110 | // Input value is valid |
||
| 111 | && ($actual_porcent = intval($production[$factory_unit_id][$planet['id']] / 10)) >= 0 |
||
| 112 | && $actual_porcent <= 10 |
||
| 113 | // And changes really should be stored to DB |
||
| 114 | && $actual_porcent != $planet[$unit_db_name_porcent] |
||
| 115 | ) { |
||
| 116 | $actual_porcent = intval($actual_porcent); |
||
| 117 | $query[] = "`{$unit_db_name_porcent}` = {$actual_porcent}"; |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | if (!empty($query)) { |
||
| 122 | DBStaticPlanet::db_planet_set_by_id($planet['id'], implode(',', $query)); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @param array $user |
||
| 129 | * |
||
| 130 | * @return array[] |
||
| 131 | */ |
||
| 132 | protected function getUpdatedUserPlanetsAndQues($user) { |
||
| 133 | $planets = array(); |
||
| 134 | $ques = array(); |
||
| 135 | $planet_row_list = DBStaticPlanet::db_planet_list_sorted($user); |
||
| 136 | foreach ($planet_row_list as $planet) { |
||
| 137 | sn_db_transaction_start(); |
||
| 138 | $global_data = sys_o_get_updated($user, $planet['id'], SN_TIME_NOW, false, true); |
||
| 139 | $planets[$planet['id']] = $global_data['planet']; |
||
| 140 | $ques[$planet['id']] = $global_data['que']; |
||
| 141 | sn_db_transaction_commit(); |
||
| 142 | } |
||
| 143 | |||
| 144 | return array($planets, $ques); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param array $user |
||
| 149 | * @param template $template |
||
| 150 | * @param int $unit_group_id |
||
| 151 | * @param array[] $planets |
||
| 152 | * @param array[] $ques |
||
| 153 | * @param array $fleets |
||
| 154 | */ |
||
| 155 | protected function imperiumTemplatizeUnitGroup(&$user, $template, $unit_group_id, $planets, $ques, $fleets) { |
||
| 156 | $sn_group_factories = sn_get_groups('factories'); |
||
| 157 | |||
| 158 | foreach (get_unit_param('techtree', $unit_group_id) as $unit_id) { |
||
| 159 | $unit_count = $unit_count_abs = 0; |
||
| 160 | $block_vars = array(); |
||
| 161 | $unit_is_factory = in_array($unit_id, $sn_group_factories) && get_unit_param($unit_id, P_MINING_IS_MANAGED); |
||
| 162 | foreach ($planets as $planet) { |
||
| 163 | $unit_level_plain = mrc_get_level($user, $planet, $unit_id, false, true); |
||
| 164 | |||
| 165 | $levelGreen = 0; |
||
| 166 | $levelYellow = 0; |
||
| 167 | |||
| 168 | switch ($unit_group_id) { |
||
| 169 | /** @noinspection PhpMissingBreakStatementInspection */ |
||
| 170 | case UNIT_SHIPS: |
||
| 171 | $levelYellow = !empty($fleets[$planet['id']]['own']['total'][$unit_id]) ? floatval($fleets[$planet['id']]['own']['total'][$unit_id]) : 0; |
||
| 172 | |||
| 173 | case UNIT_STRUCTURES: |
||
| 174 | case UNIT_STRUCTURES_SPECIAL: |
||
| 175 | case UNIT_DEFENCE: |
||
| 176 | $levelGreen = floatval($ques[$planet['id']]['in_que'][que_get_unit_que($unit_id)][$user['id']][$planet['id']][$unit_id]); |
||
| 177 | break; |
||
| 178 | |||
| 179 | default: |
||
| 180 | break; |
||
| 181 | } |
||
| 182 | $unitsPresentOrChanged = $unit_level_plain + abs($levelYellow) + abs($levelGreen); |
||
| 183 | |||
| 184 | $unit_count += $unit_level_plain; |
||
| 185 | $unit_count_abs += $unitsPresentOrChanged; |
||
| 186 | |||
| 187 | $block_vars[] = [ |
||
| 188 | 'ID' => $planet['id'], |
||
| 189 | 'TYPE' => $planet['planet_type'], |
||
| 190 | 'LEVEL' => $unitsPresentOrChanged ? $unit_level_plain : '-', |
||
| 191 | 'LEVEL_PLUS_YELLOW' => $levelYellow, |
||
| 192 | 'LEVEL_PLUS_GREEN' => $levelGreen, |
||
| 193 | 'LEVEL_TEXT' => $unitsPresentOrChanged ? HelperString::numberFloorAndFormat($unit_level_plain) : '-', |
||
| 194 | 'LEVEL_PLUS_YELLOW_TEXT' => tplPrettyPlus($levelYellow), |
||
| 195 | 'LEVEL_PLUS_GREEN_TEXT' => tplPrettyPlus($levelGreen), |
||
| 196 | 'PERCENT' => $unit_is_factory ? ($unit_level_plain ? $planet[pname_factory_production_field_name($unit_id)] * 10 : -1) : -1, |
||
| 197 | 'FACTORY' => $unit_is_factory, |
||
| 198 | ]; |
||
| 199 | } |
||
| 200 | |||
| 201 | if ($unit_count_abs) { |
||
| 202 | $this->tplRenderUnitLine($template, $unit_id, $block_vars, $unit_count, $unit_is_factory); |
||
| 203 | } |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Renders line of unit for each planet |
||
| 209 | * |
||
| 210 | * @param template $template |
||
| 211 | * @param int $unit_id |
||
| 212 | * @param array $block_vars |
||
| 213 | * @param int $unit_count |
||
| 214 | * @param bool $unit_is_factory |
||
| 215 | * |
||
| 216 | */ |
||
| 217 | protected function tplRenderUnitLine($template, $unit_id, $block_vars, $unit_count, $unit_is_factory) { |
||
| 218 | // Adding unit cell name |
||
| 219 | $template->assign_block_vars('prods', [ |
||
| 220 | 'ID' => $unit_id, |
||
| 221 | 'FIELD' => 'unit_' . $unit_id, // TODO Делать это прямо в темплейте |
||
| 222 | 'NAME' => $this->lang['tech'][$unit_id], |
||
| 223 | 'MODE' => static::GROUPS_TO_NAMES[get_unit_param($unit_id, P_UNIT_TYPE)], |
||
| 224 | ]); |
||
| 225 | |||
| 226 | $imperiumYellows = []; |
||
| 227 | $imperiumGreens = []; |
||
| 228 | // Adding data for each planet for this unit |
||
| 229 | foreach ($block_vars as $block_var) { |
||
| 230 | $imperiumYellows[$unit_id] += $block_var['LEVEL_PLUS_YELLOW']; |
||
| 231 | $imperiumGreens[$unit_id] += $block_var['LEVEL_PLUS_GREEN']; |
||
| 232 | $template->assign_block_vars('prods.planet', $block_var); |
||
| 233 | } |
||
| 234 | |||
| 235 | // Adding final cell with Imperium total stat about this unit |
||
| 236 | $template->assign_block_vars('prods.planet', [ |
||
| 237 | 'ID' => 0, |
||
| 238 | 'LEVEL' => $unit_count, |
||
| 239 | 'LEVEL_TEXT' => HelperString::numberFloorAndFormat($unit_count), |
||
| 240 | 'LEVEL_PLUS_YELLOW' => $imperiumYellows[$unit_id], |
||
| 241 | 'LEVEL_PLUS_GREEN' => $imperiumGreens[$unit_id], |
||
| 242 | 'LEVEL_PLUS_YELLOW_TEXT' => $imperiumYellows[$unit_id] == 0 ? '' : tplPrettyPlus($imperiumYellows[$unit_id]), |
||
| 243 | 'LEVEL_PLUS_GREEN_TEXT' => $imperiumGreens[$unit_id] == 0 ? '' : tplPrettyPlus($imperiumGreens[$unit_id]), |
||
| 244 | 'PERCENT' => $unit_is_factory ? '' : -1, |
||
| 245 | 'FACTORY' => $unit_is_factory, |
||
| 246 | ]); |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param array[] $planets |
||
| 251 | * @param array $fleets |
||
| 252 | * |
||
| 253 | * @return array[][] |
||
| 254 | */ |
||
| 255 | protected function tplRenderPlanets($user, &$planets, $fleets) { |
||
| 256 | $result = []; |
||
| 257 | |||
| 258 | $planet_density = sn_get_groups('planet_density'); |
||
| 259 | |||
| 260 | foreach ($planets as $planetId => $planet) { |
||
| 261 | $templatizedPlanet = tpl_parse_planet($user, $planet); |
||
| 262 | |||
| 263 | $fleet_list = $fleets[$planetId]; |
||
| 264 | foreach ([RES_METAL, RES_CRYSTAL, RES_DEUTERIUM] as $resourceId) { |
||
| 265 | if (empty($fleet_list['own']['total'][$resourceId])) { |
||
| 266 | $templatizedPlanet['RES_' . $resourceId] = 0; |
||
| 267 | } else { |
||
| 268 | $templatizedPlanet['RES_' . $resourceId] = $fleet_list['own']['total'][$resourceId]; |
||
| 269 | $templatizedPlanet['RES_' . $resourceId . '_TEXT'] = HelperString::numberFloorAndFormat($fleet_list['own']['total'][$resourceId]); |
||
| 270 | } |
||
| 271 | } |
||
| 272 | $templatizedPlanet += tpl_parse_planet_result_fleet($planet, $fleet_list); |
||
| 273 | |||
| 274 | $templatizedPlanet += [ |
||
| 275 | 'METAL_CUR' => prettyNumberStyledCompare($planet['metal'], $planet['metal_max']), |
||
| 276 | 'METAL_PROD' => $planet['metal_perhour'], |
||
| 277 | |||
| 278 | 'CRYSTAL_CUR' => prettyNumberStyledCompare($planet['crystal'], $planet['crystal_max']), |
||
| 279 | 'CRYSTAL_PROD' => $planet['crystal_perhour'], |
||
| 280 | |||
| 281 | 'DEUTERIUM_CUR' => prettyNumberStyledCompare($planet['deuterium'], $planet['deuterium_max']), |
||
| 282 | 'DEUTERIUM_PROD' => $planet['deuterium_perhour'], |
||
| 283 | |||
| 284 | 'ENERGY_CUR' => $planet['energy_max'] - $planet['energy_used'], |
||
| 285 | 'ENERGY_MAX' => $planet['energy_max'], |
||
| 286 | |||
| 287 | 'TEMP_MIN' => $planet['temp_min'], |
||
| 288 | 'TEMP_MAX' => $planet['temp_max'], |
||
| 289 | |||
| 290 | 'DENSITY_CLASS' => $planet['density_index'], |
||
| 291 | 'DENSITY_RICHNESS' => $planet_density[$planet['density_index']][UNIT_PLANET_DENSITY_RICHNESS], |
||
| 292 | 'DENSITY_CLASS_TEXT' => $this->lang['uni_planet_density_types'][$planet['density_index']], |
||
| 293 | ]; |
||
| 294 | |||
| 295 | $result[] = $templatizedPlanet; |
||
| 296 | } |
||
| 297 | |||
| 298 | return [ |
||
| 299 | '.' => [ |
||
| 300 | 'planet' => $result |
||
| 301 | ] |
||
| 302 | ]; |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @param array[] $planets |
||
| 307 | * @param array[] $fleets |
||
| 308 | * |
||
| 309 | * @return array |
||
| 310 | */ |
||
| 311 | protected function tplRenderFleets($planets, $fleets) { |
||
| 312 | $fleetsRendered = []; |
||
| 313 | foreach ($fleets as $planetId => $fleet_list) { |
||
| 314 | if (!empty($fleet_list['own']['count'])) { |
||
| 315 | $fleetsRendered[$planets[$planetId]['id']] = tpl_parse_fleet_sn($fleet_list['own']['total'], getUniqueFleetId(['id' => $planetId])); |
||
| 316 | } |
||
| 317 | } |
||
| 318 | |||
| 319 | return tpl_assign_fleet_generate($fleetsRendered); |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param array[] $planets |
||
| 324 | * |
||
| 325 | * @return array |
||
| 326 | */ |
||
| 327 | protected function fleetGetFlyingToPlanets(&$planets) { |
||
| 328 | $fleets = []; |
||
| 329 | foreach ($planets as $planetId => &$planet) { |
||
| 330 | $fleets[$planet['id']] = flt_get_fleets_to_planet($planet); |
||
| 331 | } |
||
| 332 | |||
| 333 | return $fleets; |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param template $template |
||
| 338 | * @param array $planets |
||
| 339 | */ |
||
| 340 | function tplTotalPlanetInfo($template, $planets) { |
||
| 341 | $imperiumStats = [ |
||
| 342 | 'temp_min' => 1000, |
||
| 343 | 'temp_max' => -999, |
||
| 344 | ]; |
||
| 345 | |||
| 346 | foreach ($planets as $planetId => &$planet) { |
||
| 347 | $imperiumStats['fields'] += $planet['field_current']; |
||
| 348 | $imperiumStats['metal'] += $planet['metal']; |
||
| 349 | $imperiumStats['crystal'] += $planet['crystal']; |
||
| 350 | $imperiumStats['deuterium'] += $planet['deuterium']; |
||
| 351 | $imperiumStats['energy'] += $planet['energy_max'] - $planet['energy_used']; |
||
| 352 | |||
| 353 | $imperiumStats['fields_max'] += eco_planet_fields_max($planet); |
||
| 354 | $imperiumStats['metal_perhour'] += $planet['metal_perhour']; |
||
| 355 | $imperiumStats['crystal_perhour'] += $planet['crystal_perhour']; |
||
| 356 | $imperiumStats['deuterium_perhour'] += $planet['deuterium_perhour']; |
||
| 357 | $imperiumStats['energy_max'] += $planet['energy_max']; |
||
| 358 | |||
| 359 | $imperiumStats['temp_min'] = min($planet['temp_min'], $imperiumStats['temp_min']); |
||
| 360 | $imperiumStats['temp_max'] = max($planet['temp_max'], $imperiumStats['temp_max']); |
||
| 361 | } |
||
| 362 | |||
| 363 | $template->assign_block_vars('planet', array_merge([ |
||
| 364 | 'ID' => 0, |
||
| 365 | 'NAME' => $this->lang['sys_total'], |
||
| 366 | |||
| 367 | 'FIELDS_CUR' => $imperiumStats['fields'], |
||
| 368 | 'FIELDS_MAX' => $imperiumStats['fields_max'], |
||
| 369 | |||
| 370 | 'METAL_CUR' => HelperString::numberFloorAndFormat($imperiumStats['metal']), |
||
| 371 | 'METAL_PROD' => $imperiumStats['metal_perhour'], |
||
| 372 | |||
| 373 | 'CRYSTAL_CUR' => HelperString::numberFloorAndFormat($imperiumStats['crystal']), |
||
| 374 | 'CRYSTAL_PROD' => $imperiumStats['crystal_perhour'], |
||
| 375 | |||
| 376 | 'DEUTERIUM_CUR' => HelperString::numberFloorAndFormat($imperiumStats['deuterium']), |
||
| 377 | 'DEUTERIUM_PROD' => $imperiumStats['deuterium_perhour'], |
||
| 378 | |||
| 379 | 'ENERGY_CUR' => $imperiumStats['energy'], |
||
| 380 | 'ENERGY_MAX' => $imperiumStats['energy_max'], |
||
| 381 | |||
| 382 | 'TEMP_MIN' => $imperiumStats['temp_min'], |
||
| 383 | 'TEMP_MAX' => $imperiumStats['temp_max'], |
||
| 384 | ])); |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @param array $user |
||
| 389 | * @param template $template |
||
| 390 | */ |
||
| 391 | protected function tplAddGlobals(&$user, $template) { |
||
| 392 | $template->assign_vars([ |
||
| 393 | 'COLONIES_CURRENT' => get_player_current_colonies($user), |
||
| 394 | 'COLONIES_MAX' => get_player_max_colonies($user), |
||
| 395 | |||
| 396 | 'EXPEDITIONS_CURRENT' => fleet_count_flying($user['id'], MT_EXPLORE), |
||
| 397 | 'EXPEDITIONS_MAX' => get_player_max_expeditons($user), |
||
| 398 | |||
| 399 | 'PLANET_DENSITY_RICHNESS_NORMAL' => PLANET_DENSITY_RICHNESS_NORMAL, |
||
| 400 | 'PLANET_DENSITY_RICHNESS_AVERAGE' => PLANET_DENSITY_RICHNESS_AVERAGE, |
||
| 401 | 'PLANET_DENSITY_RICHNESS_GOOD' => PLANET_DENSITY_RICHNESS_GOOD, |
||
| 402 | 'PLANET_DENSITY_RICHNESS_PERFECT' => PLANET_DENSITY_RICHNESS_PERFECT, |
||
| 403 | |||
| 404 | 'PAGE_HEADER' => $this->lang['imp_overview'], |
||
| 405 | ]); |
||
| 406 | } |
||
| 407 | |||
| 408 | } |
||
| 409 |