Exemple #1
0
function db_change_units(&$user, &$planet, $unit_list = array(), $query = null)
{
    $query = is_array($query) ? $query : array(LOC_USER => array(), LOC_PLANET => array());
    $group = sn_get_groups('resources_loot');
    foreach ($unit_list as $unit_id => $unit_amount) {
        if (!in_array($unit_id, $group)) {
            // TODO - remove later
            print '<h1>СООБЩИТЕ ЭТО АДМИНУ: db_change_units() вызван для не-ресурсов!</h1>';
            pdump(debug_backtrace());
            die('db_change_units() вызван для не-ресурсов!');
        }
        if (!$unit_amount) {
            continue;
        }
        $unit_db_name = pname_resource_name($unit_id);
        $unit_location = sys_get_unit_location($user, $planet, $unit_id);
        // Changing value in object
        switch ($unit_location) {
            case LOC_USER:
                $user[$unit_db_name] += $unit_amount;
                break;
            case LOC_PLANET:
                $planet[$unit_db_name] += $unit_amount;
                break;
        }
        $unit_amount = $unit_amount < 0 ? $unit_amount : "+{$unit_amount}";
        // Converting positive unit_amount to string '+unit_amount'
        $query[$unit_location][$unit_id] = "`{$unit_db_name}`=`{$unit_db_name}`{$unit_amount}";
    }
    db_change_units_perform($query[LOC_USER], 'users', $user['id']);
    db_change_units_perform($query[LOC_PLANET], 'planets', $planet['id']);
}
Exemple #2
0
 public static function db_changeset_prepare_unit($unit_id, $unit_value, $user, $planet_id = null)
 {
     if (!is_array($user)) {
         // TODO - remove later
         print '<h1>СООБЩИТЕ ЭТО АДМИНУ: sn_db_unit_changeset_prepare() - USER is not ARRAY</h1>';
         pdump(debug_backtrace());
         die('USER is not ARRAY');
     }
     if (!isset($user['id']) || !$user['id']) {
         // TODO - remove later
         print '<h1>СООБЩИТЕ ЭТО АДМИНУ: sn_db_unit_changeset_prepare() - USER[id] пустой</h1>';
         pdump($user);
         pdump(debug_backtrace());
         die('USER[id] пустой');
     }
     $planet_id = is_array($planet_id) && isset($planet_id['id']) ? $planet_id['id'] : $planet_id;
     $unit_location = sys_get_unit_location($user, array(), $unit_id);
     $location_id = $unit_location == LOC_USER ? $user['id'] : $planet_id;
     $location_id = $location_id ? $location_id : 'NULL';
     $db_changeset = array();
     $temp = db_unit_by_location($user['id'], $unit_location, $location_id, $unit_id, true, 'unit_id');
     if ($temp['unit_id']) {
         $db_changeset = array('action' => SQL_OP_UPDATE, P_VERSION => 1, 'where' => array("unit_id" => $temp['unit_id']), 'fields' => array('unit_level' => array('delta' => $unit_value)));
     } else {
         $db_changeset = array('action' => SQL_OP_INSERT, 'fields' => array('unit_player_id' => array('set' => $user['id']), 'unit_location_type' => array('set' => $unit_location), 'unit_location_id' => array('set' => $unit_location == LOC_USER ? $user['id'] : $planet_id), 'unit_type' => array('set' => get_unit_param($unit_id, P_UNIT_TYPE)), 'unit_snid' => array('set' => $unit_id), 'unit_level' => array('set' => $unit_value)));
     }
     return $db_changeset;
 }