Exemple #1
0
unset($disable_reason);
if (defined('IN_ADMIN') && IN_ADMIN === true) {
    lng_include('admin');
} elseif ($sys_user_logged_in) {
    sys_user_vacation($user);
    $planet_id = SetSelectedPlanet($user);
    // TODO НЕ НУЖНО АЛЬЯНС КАЖДЫЙ РАЗ ОБНОВЛЯТЬ!!!
    if ($user['ally_id']) {
        sn_db_transaction_start();
        sn_ali_fill_user_ally($user);
        if (!$user['ally']['player']['id']) {
            sn_sys_logout(false, true);
            $debug->error("User ID {$user['id']} has ally ID {$user['ally_id']} but no ally info", 'User record error', 502);
        }
        // TODO UNCOMMENT
        que_process($user['ally']['player']);
        db_user_set_by_id($user['ally']['player']['id'], '`onlinetime` = ' . SN_TIME_NOW);
        sn_db_transaction_commit();
    }
    // TODO - в режиме эмуляции, на самом деле!
    sn_db_transaction_start();
    $global_data = sys_o_get_updated($user['id'], $planet_id, SN_TIME_NOW);
    sn_db_transaction_commit();
    $planetrow = $global_data['planet'];
    if (!($planetrow && isset($planetrow['id']) && $planetrow['id'])) {
        sn_sys_logout(false, true);
        $debug->error("User ID {$user['id']} has no current planet and no homeworld", 'User record error', 502);
    }
    $que = $global_data['que'];
}
require_once 'includes/vars_menu.php';
function sys_o_get_updated($user, $planet, $UpdateTime, $simulation = false, $no_user_update = false)
{
    sn_db_transaction_check(true);
    $no_data = array('user' => false, 'planet' => false, 'que' => false);
    if (!$planet) {
        return $no_data;
    }
    if (!$no_user_update) {
        $user = intval(is_array($user) && $user['id'] ? $user['id'] : $user);
        if (!$user) {
            // TODO - Убрать позже
            print '<h1>СООБЩИТЕ ЭТО АДМИНУ: sys_o_get_updated() - USER пустой!</h1>';
            $backtrace = debug_backtrace();
            array_shift($backtrace);
            pdump($backtrace);
            die;
        }
        $user = db_user_by_id($user, !$simulation, '*', true);
    }
    if (empty($user['id'])) {
        return $no_data;
    }
    if (is_array($planet) && isset($planet['galaxy']) && $planet['galaxy']) {
        $planet = db_planet_by_vector($planet, '', !$simulation);
    } else {
        $planet = intval(is_array($planet) && isset($planet['id']) ? $planet['id'] : $planet);
        $planet = db_planet_by_id($planet, !$simulation);
    }
    if (!is_array($planet) || !isset($planet['id'])) {
        return $no_data;
    }
    $que = que_process($user, $planet, $UpdateTime);
    $ProductionTime = max(0, $UpdateTime - $planet['last_update']);
    $planet['prev_update'] = $planet['last_update'];
    $planet['last_update'] += $ProductionTime;
    /*
    $que = eco_que_process($user, $planet, $ProductionTime);
    $hangar_built = $ProductionTime && !$simulation ? eco_bld_que_hangar($user, $planet, $ProductionTime) : array();
    */
    // TODO ЭТО НАДО ДЕЛАТЬ ТОЛЬКО ПРИ СПЕЦУСЛОВИЯХ
    $caps_real = eco_get_planet_caps($user, $planet, $ProductionTime);
    $resources_increase = array(RES_METAL => 0, RES_CRYSTAL => 0, RES_DEUTERIUM => 0);
    switch ($planet['planet_type']) {
        case PT_PLANET:
            foreach ($resources_increase as $resource_id => &$increment) {
                $resource_name = pname_resource_name($resource_id);
                $increment = $caps_real['total'][$resource_id] * $ProductionTime / 3600;
                $store_free = $caps_real['total_storage'][$resource_id] - $planet[$resource_name];
                $increment = min($increment, max(0, $store_free));
                if ($planet[$resource_name] + $increment < 0 && !$simulation) {
                    global $debug;
                    $debug->warning("Player ID {$user['id']} have negative resources on ID {$planet['id']}.{$planet['planet_type']} [{$planet['galaxy']}:{$planet['system']}:{$planet['planet']}]. Difference {$planet[$resource_name]} of {$resource_name}", 'Negative Resources', 501);
                }
                $planet[$resource_name] += $increment;
                $planet[$resource_name . '_perhour'] = $caps_real['total'][$resource_id];
            }
            break;
        case PT_MOON:
        default:
            $planet['metal_perhour'] = 0;
            $planet['crystal_perhour'] = 0;
            $planet['deuterium_perhour'] = 0;
            $planet['energy_used'] = 0;
            $planet['energy_max'] = 0;
            break;
    }
    // TODO пересчитывать размер планеты только при постройке чего-нибудь и при покупке сектора
    $planet['field_current'] = 0;
    $sn_group_build_allow = sn_get_groups('build_allow');
    if (is_array($sn_group_build_allow[$planet['planet_type']])) {
        foreach ($sn_group_build_allow[$planet['planet_type']] as $building_id) {
            $planet['field_current'] += mrc_get_level($user, $planet, $building_id, !$simulation, true);
        }
    }
    if ($simulation) {
        return array('user' => $user, 'planet' => $planet, 'que' => $que);
    }
    db_planet_set_by_id($planet['id'], "`last_update` = '{$planet['last_update']}', `field_current` = {$planet['field_current']},\n    `metal` = `metal` + '{$resources_increase[RES_METAL]}', `crystal` = `crystal` + '{$resources_increase[RES_CRYSTAL]}', `deuterium` = `deuterium` + '{$resources_increase[RES_DEUTERIUM]}',\n    `metal_perhour` = '{$planet['metal_perhour']}', `crystal_perhour` = '{$planet['crystal_perhour']}', `deuterium_perhour` = '{$planet['deuterium_perhour']}',\n    `energy_used` = '{$planet['energy_used']}', `energy_max` = '{$planet['energy_max']}'");
    return array('user' => $user, 'planet' => $planet, 'que' => $que);
}