/**
 * PlanetResourceUpdate.php
 *
 * 2.1 - copyright (c) 2010 by Gorlum for http://supernova.ws
 *     [+] Bit more optimization
 * 2.0 - copyright (c) 2009-2010 by Gorlum for http://supernova.ws
 *     [+] Full rewrote and optimization
 * 1.1 - @copyright 2008 By Chlorel for XNova
 *     [*] Mise a jour automatique mines / silos / energie ...
 * 1.0 - @copyright 2008 By Chlorel for XNova
 *     [*] Mise en module initiale
 */
function sys_o_get_updated($user, $planet, $UpdateTime, $simulation = false)
{
    global $time_now, $sn_data, $lang;
    $no_data = array('user' => false, 'planet' => false, 'que' => false);
    if (!$planet) {
        return $no_data;
    }
    $suffix = $simulation ? '' : 'FOR UPDATE';
    if (is_array($planet)) {
        if (!(isset($planet['id']) && $planet['id']) || !$simulation) {
            $planet = doquery("SELECT * FROM `{{planets}}` WHERE `galaxy` = '{$planet['galaxy']}' AND `system` = '{$planet['system']}' AND `planet` = '{$planet['planet']}' and `planet_type` = '{$planet['planet_type']}' LIMIT 1 {$suffix};", '', true);
        }
    } else {
        $planet = doquery("SELECT * FROM `{{planets}}` WHERE `id` = '{$planet}' LIMIT 1 {$suffix};", '', true);
    }
    if (!($planet && isset($planet['id']) && $planet['id'])) {
        return $no_data;
    }
    if (!$user || !is_array($user) || !isset($user['id'])) {
        $user = doquery("SELECT * FROM `{{users}}` WHERE `id` = {$planet['id_owner']} LIMIT 1 {$suffix};", '', true);
        if (!$user) {
            return $no_data;
        }
    }
    $ProductionTime = max(0, $UpdateTime - $planet['last_update']);
    $planet['last_update'] += $ProductionTime;
    $Caps = ECO_getPlanetCaps($user, $planet);
    $incRes = array('metal' => 0, 'crystal' => 0, 'deuterium' => 0);
    switch ($planet['planet_type']) {
        case PT_PLANET:
            foreach ($incRes as $resName => &$incCount) {
                //        $Caps['planet'][$resName] = max(0, $Caps['planet'][$resName]);
                $incCount = ($Caps[$resName . '_perhour'][0] + $Caps['planet'][$resName . '_perhour'] * $Caps['production']) * $ProductionTime / 3600;
                $store_free = $Caps['planet'][$resName . '_max'] - $Caps['planet'][$resName];
                //        $incCount = max(0, min($incCount, max(0, $store_free)));
                $incCount = min($incCount, max(0, $store_free));
                if ($planet[$resName] + $incCount < 0) {
                    $GLOBALS['debug']->warning("Player ID {$user['id']} have negative resources on ID {$planet['id']}.{$planet['planet_type']} [{$planet['galaxy']}:{$planet['system']}:{$planet['planet']}]. Difference {$planet[$resName]} of {$resName}", 'Negative Resources', 501);
                }
                $Caps['planet'][$resName] += $incCount;
                $Caps['planet'][$resName . '_perhour'] = $Caps['real'][$resName . '_perhour'];
            }
            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;
    }
    $planet = array_merge($planet, $Caps['planet']);
    $que = eco_que_process($user, $planet, $ProductionTime);
    if ($simulation) {
        return array('user' => $user, 'planet' => $planet, 'que' => $que);
    }
    $QryUpdatePlanet = "UPDATE {{planets}} SET `last_update` = '{$planet['last_update']}', ";
    $QryUpdatePlanet .= "`metal`     = `metal`     + '{$incRes['metal']}', `crystal`   = `crystal`   + '{$incRes['crystal']}', `deuterium` = `deuterium` + '{$incRes['deuterium']}', ";
    $QryUpdatePlanet .= "`metal_perhour` = '{$planet['metal_perhour']}', `crystal_perhour` = '{$planet['crystal_perhour']}', `deuterium_perhour` = '{$planet['deuterium_perhour']}', ";
    $QryUpdatePlanet .= "`energy_used` = '{$planet['energy_used']}', `energy_max` = '{$planet['energy_max']}', ";
    $built = eco_bld_handle_que($user, $planet, $ProductionTime);
    if ($built['built']) {
        foreach ($built['built'] as $Element => $Count) {
            $Element = intval($Element);
            $Count = intval($Count);
            if ($Element) {
                $QryUpdatePlanet .= "`{$sn_data[$Element]['name']}` = `{$sn_data[$Element]['name']}` + '{$Count}', ";
            }
        }
        if (!$planet['b_hangar']) {
            msg_send_simple_message($user['id'], 0, $time_now, MSG_TYPE_QUE, $lang['msg_que_planet_from'], $lang['msg_que_hangar_subject'], sprintf($lang['msg_que_hangar_message'], uni_render_planet($planet)));
        }
    }
    $QryUpdatePlanet .= "`b_hangar_id` = '{$planet['b_hangar_id']}', ";
    $QryUpdatePlanet .= "`b_hangar` = '{$planet['b_hangar']}' ";
    $QryUpdatePlanet .= $que['query'] != $planet['que'] ? ",{$que['query']} " : '';
    if (!empty($que['built'])) {
        $message = array();
        foreach ($que['built'] as $unit_id => $built_count) {
            if ($built_count > 0) {
                $message[] = sprintf($lang['msg_que_built_message'], uni_render_planet($planet), $lang['tech'][$unit_id], $built_count);
            } else {
                $message[] = sprintf($lang['msg_que_destroy_message'], uni_render_planet($planet), $lang['tech'][$unit_id], -$built_count);
            }
        }
        msg_send_simple_message($user['id'], 0, $time_now, MSG_TYPE_QUE, $lang['msg_que_planet_from'], $lang['msg_que_built_subject'], implode('<br />', $message));
    }
    $QryUpdatePlanet .= "WHERE `id` = '{$planet['id']}' LIMIT 1;";
    doquery($QryUpdatePlanet);
    if (!empty($que['xp'])) {
        foreach ($que['xp'] as $xp_type => $xp_amount) {
            rpg_level_up($user, $xp_type, $xp_amount);
        }
    }
    // Can't use array_merge here - it will broke numeric array indexes those broke quest_id
    // TODO: Make own function for this
    foreach ($built['rewards'] as $quest_id => $quest_reward) {
        $que['rewards'][$quest_id] = $quest_reward;
    }
    qst_reward($user, $planet, $que['rewards'], $que['quests']);
    $planet['planet_caps'] = $Caps;
    return array('user' => $user, 'planet' => $planet, 'que' => $que);
}
Exemple #2
0
/**
* eco_build.php
*
* @version 1.5 - Using PTE (not everywhere) by Gorlum for http://supernova.ws
* @version 1.4 - Complying with PCG by Gorlum for http://supernova.ws
* @version 1.3 - Security checked for SQL-injection by Gorlum for http://supernova.ws
// 1.0 Mise en module initiale (creation)
// 1.1 FIX interception cheat +1
// 1.2 FIX interception cheat destruction a -1
* @version 1.1
* @copyright 2008 by Chlorel for XNova
*/
function eco_build($que_type, $user, &$planet, $que)
{
    global $sn_data, $lang, $config, $time_now;
    // start transaction here
    $template = gettemplate('buildings_builds', true);
    // Caching values that used more then one time into local variables
    $config_resource_multiplier = $config->resource_multiplier;
    $planet_type_structs = $sn_data['groups']['build_allow'][$planet['planet_type']];
    // Getting parameters
    $action = sys_get_param_escaped('action');
    $unit_id = sys_get_param_int('unit_id');
    $unit_level = sys_get_param_int('unit_level');
    $GET_listid = $_GET['listid'];
    $que_type = $que_type == SUBQUE_FLEET || $que_type == SUBQUE_DEFENSE ? QUE_HANGAR : $que_type;
    CheckPlanetUsedFields($planet);
    if ($action) {
        switch ($action) {
            case 'create':
                // Add unit to que for build
                $que = eco_que_add($user, $planet, $que, QUE_STRUCTURES, $unit_id);
                break;
            case 'destroy':
                // Add unit to que for remove
                $que = eco_que_add($user, $planet, $que, QUE_STRUCTURES, $unit_id, 1, BUILD_DESTROY);
                break;
            case 'trim':
                // Cancel unit from que
                $que = eco_que_clear($user, $planet, $que, QUE_STRUCTURES, true);
                break;
            case 'clear':
                // Clear que
                $que = eco_que_clear($user, $planet, $que, QUE_STRUCTURES);
                break;
        }
        header("Location: {$_SERVER['PHP_SELF']}?mode={$que_type}");
    }
    /*
      // Code for fully working new que system
      $hangar_busy = count($que['que'][QUE_HANGAR]);
      $lab_busy    = count($que['que'][QUE_RESEARCH]) && !$config->BuildLabWhileRun;
    */
    $que_length = count($que['que'][$que_type]);
    $can_que_element = $que_length < MAX_BUILDING_QUEUE_SIZE;
    $fleet_list = flt_get_fleets_to_planet($planet);
    $caps = ECO_getPlanetCaps($user, &$planet);
    $planet_fields_max = eco_planet_fields_max($planet);
    $planet_fields_current = $planet['field_current'];
    $planet_fields_que = -$que['amounts'][$que_type];
    $planet_fields_free = max(0, $planet_fields_max - $planet_fields_current + $planet_fields_que);
    $planet_fields_queable = $planet_fields_free > 0;
    $planet_temp_max = $planet['temp_max'];
    $GLOBALS['user_tech_energy'] = $user['energy_tech'];
    foreach ($planet_type_structs as $Element) {
        if (eco_can_build_unit($user, $planet, $Element)) {
            $element_name = $lang['tech'][$Element];
            $element_sn_data =& $sn_data[$Element];
            $element_level = $planet[$sn_data[$Element]['name']] + $que['in_que'][$Element];
            // show energy on BuildingPage
            //================================
            if ($element_sn_data['production']) {
                $element_production_energy = $element_sn_data['production'][RES_ENERGY];
                $energy_current = floor($element_production_energy($element_level, 10, $planet_temp_max));
                $energy_next = floor($element_production_energy($element_level + 1, 10, $planet_temp_max));
                if ($Element == STRUC_MINE_SOLAR || $Element == STRUC_MINE_FUSION) {
                    $energy_current = floor(mrc_modify_value($user, $planet, array(MRC_POWERMAN), $energy_current));
                    $energy_next = floor(mrc_modify_value($user, $planet, array(MRC_POWERMAN), $energy_next));
                }
                $energy_balance = floor($energy_next - $energy_current);
            } else {
                $energy_balance = 0;
            }
            //================================
            $build_data = eco_get_build_data($user, $planet, $Element, $element_level);
            $temp[RES_METAL] = floor($planet['metal'] + $fleet_list['own']['total'][RES_METAL] - $build_data[BUILD_CREATE][RES_METAL]);
            $temp[RES_CRYSTAL] = floor($planet['crystal'] + $fleet_list['own']['total'][RES_CRYSTAL] - $build_data[BUILD_CREATE][RES_CRYSTAL]);
            $temp[RES_DEUTERIUM] = floor($planet['deuterium'] + $fleet_list['own']['total'][RES_DEUTERIUM] - $build_data[BUILD_CREATE][RES_DEUTERIUM]);
            $template->assign_block_vars('production', array('ID' => $Element, 'NAME' => $element_name, 'DESCRIPTION' => $lang['info'][$Element]['description_short'], 'LEVEL' => $element_level, 'LEVEL_OLD' => $planet[$sn_data[$Element]['name']], 'LEVEL_CHANGE' => $que['in_que'][$Element], 'BUILD_CAN' => $build_data['CAN'][BUILD_CREATE], 'TIME' => pretty_time($build_data[BUILD_CREATE][RES_TIME]), 'METAL' => $build_data[BUILD_CREATE][RES_METAL], 'CRYSTAL' => $build_data[BUILD_CREATE][RES_CRYSTAL], 'DEUTERIUM' => $build_data[BUILD_CREATE][RES_DEUTERIUM], 'DESTROY_CAN' => $build_data['CAN'][BUILD_DESTROY], 'DESTROY_TIME' => pretty_time($build_data[BUILD_DESTROY][RES_TIME]), 'DESTROY_METAL' => $build_data[BUILD_DESTROY][RES_METAL], 'DESTROY_CRYSTAL' => $build_data[BUILD_DESTROY][RES_CRYSTAL], 'DESTROY_DEUTERIUM' => $build_data[BUILD_DESTROY][RES_DEUTERIUM], 'METAL_REST' => pretty_number($temp[RES_METAL], true, true), 'CRYSTAL_REST' => pretty_number($temp[RES_CRYSTAL], true, true), 'DEUTERIUM_REST' => pretty_number($temp[RES_DEUTERIUM], true, true), 'METAL_REST_NUM' => $temp[RES_METAL], 'CRYSTAL_REST_NUM' => $temp[RES_CRYSTAL], 'DEUTERIUM_REST_NUM' => $temp[RES_DEUTERIUM], 'METAL_BALANCE' => $caps['metal_perhour'][$Element], 'CRYSTAL_BALANCE' => $caps['crystal_perhour'][$Element], 'DEUTERIUM_BALANCE' => $caps['deuterium_perhour'][$Element], 'ENERGY_BALANCE' => $energy_balance, 'UNIT_BUSY' => eco_unit_busy($user, $planet, $que, $Element)));
        }
    }
    if (is_array($que['que'][$que_type])) {
        foreach ($que['que'][$que_type] as $que_element) {
            $template->assign_block_vars('que', $que_element);
        }
    }
    $template->assign_vars(array('TIME_NOW' => $time_now, 'QUE_ID' => $que_type, 'METAL' => $planet['metal'], 'CRYSTAL' => $planet['crystal'], 'DEUTERIUM' => $planet['deuterium'], 'METAL_INCOMING' => $fleet_list['own']['total'][RES_METAL], 'CRYSTAL_INCOMING' => $fleet_list['own']['total'][RES_CRYSTAL], 'DEUTERIUM_INCOMING' => $fleet_list['own']['total'][RES_DEUTERIUM], 'FIELDS_CURRENT' => $planet_fields_current, 'FIELDS_MAX' => $planet_fields_max, 'FIELDS_FREE' => $planet_fields_free, 'FIELDS_QUE' => $planet_fields_que == 0 ? '' : $planet_fields_que > 0 ? "+{$planet_fields_que}" : $planet_fields_que, 'QUE_HAS_PLACE' => $can_que_element, 'QUE_HAS_FIELDS' => $planet_fields_queable, 'FLEET_OWN' => $fleet_list['own']['count'], 'PAGE_HINT' => $lang['eco_bld_page_hint']));
    display(parsetemplate($template), $lang['Builds']);
}
Exemple #3
0
            $debug->warning('Supplying wrong ID in production array - attempt to change some field', 'Resource Page', 301);
            die;
        }
    }
    $SubQry = substr($SubQry, 0, -1);
    if ($SubQry) {
        doquery("UPDATE {{planets}} SET {$SubQry} WHERE `id` = '{$planetrow['id']}';");
    }
}
// -------------------------------------------------------------------------------------------------------
$BuildTemp = $planetrow['temp_max'];
$BuildEnergyTech = $user['energy_tech'];
$GLOBALS['user_tech_energy'] = $user['energy_tech'];
for ($Option = 10; $Option >= 0; $Option--) {
    $template->assign_block_vars('option', array('VALUE' => $Option * 10));
}
$caps = ECO_getPlanetCaps($user, $planetrow);
$ProdID = 0;
$template->assign_block_vars('production', array('TYPE' => $lang['res_basic_income'], 'METAL_TYPE' => pretty_number($caps['metal_perhour'][$ProdID], true, true), 'CRYSTAL_TYPE' => pretty_number($caps['crystal_perhour'][$ProdID], true, true), 'DEUTERIUM_TYPE' => pretty_number($caps['deuterium_perhour'][$ProdID], true, true), 'ENERGY_TYPE' => pretty_number($caps['energy'][$ProdID], true, true)));
foreach ($sn_data['groups']['prod'] as $ProdID) {
    $resource_db_name = $sn_data[$ProdID]['name'];
    if ($planetrow[$resource_db_name] > 0 && isset($sn_data[$ProdID])) {
        $template->assign_block_vars('production', array('ID' => $ProdID, 'NAME' => $resource_db_name, 'PERCENT' => $planetrow[$resource_db_name . '_porcent'] * 10, 'TYPE' => $lang['tech'][$ProdID], 'LEVEL' => $planetrow[$resource_db_name], 'LEVEL_TYPE' => $ProdID > 200 ? $lang['quantity'] : $lang['level'], 'METAL_TYPE' => pretty_number($caps['metal_perhour'][$ProdID] * $caps['production'], true, true), 'CRYSTAL_TYPE' => pretty_number($caps['crystal_perhour'][$ProdID] * $caps['production'], true, true), 'DEUTERIUM_TYPE' => pretty_number($caps['deuterium_perhour'][$ProdID] * $caps['production'], true, true), 'ENERGY_TYPE' => pretty_number($caps['energy'][$ProdID], true, true), 'SELECT' => $row_select));
    }
}
$template->assign_block_vars('production', array('TYPE' => $lang['res_total'], 'METAL_TYPE' => pretty_number($caps['planet']['metal_perhour'] * $caps['production'] + $caps['metal_perhour'][0], true, true), 'CRYSTAL_TYPE' => pretty_number($caps['planet']['crystal_perhour'] * $caps['production'] + $caps['crystal_perhour'][0], true, true), 'DEUTERIUM_TYPE' => pretty_number($caps['planet']['deuterium_perhour'] * $caps['production'] + $caps['deuterium_perhour'][0], true, true), 'ENERGY_TYPE' => pretty_number($caps['planet']['energy_max'] - $caps['planet']['energy_used'], true, true)));
int_calc_storage_bar('metal');
int_calc_storage_bar('crystal');
int_calc_storage_bar('deuterium');
$template->assign_vars(array('PLANET_NAME' => $planetrow['name'], 'PRODUCTION_LEVEL' => floor($caps['production'] * 100), 'PAGE_HINT' => $lang['res_hint']));
display(parsetemplate($template, $parse), $lang['res_planet_production']);