error_log(__FILE__ . '::' . __LINE__ . " Query selection failed: (" . $db->get_db()->errno . ") " . $db->get_db()->error); break; } while ($row = $rs->fetch_assoc()) { if (isset($port_place_types[$row['type']])) { $port_places[$row['record_id']] = $row; $port_place_count++; } } if ($port_place_count <= 0) { $return_codes[] = 1057; break; } // Now find out what goods we can add to a port include_once 'inc/start_goods.php'; if ($spacegame['start_good_count'] <= 0) { $return_codes[] = 1058; break; } $demand_start_amount = -PORT_LIMIT; $supply_start_amount = PORT_LIMIT; include_once 'inc/galaxy.php'; // Add each port and goods to the database. foreach ($port_places as $id => $row) { if (!insert_port($row['caption'], $row['system'], $row['x'], $row['y'], $port_place_types[$row['type']] + PLACE_TYPE_SUPPLY_OFFSET, $port_place_types[$row['type']] + PLACE_TYPE_DEMAND_OFFSET, $row['type'], false)) { $return_codes[] = 1074; break 2; } } update_distances(); } while (false);
private function add_upgrades_to_port($place_id, $upgrade_count) { global $db; $db = isset($db) ? $db : new DB(); global $spacegame; $time = time(); // Grab a list of supplies for this place. $supplies = array(); $existing_upgrades = array(); $rs = $db->get_db()->query("select good, upgrade, supply from port_goods where (supply > 0 or upgrade > 0) and place = '{$place_id}'"); if (!$rs || !$rs->data_seek(0)) { error_log(__FILE__ . '::' . __LINE__ . " Query failed. You may have a broken port with no supply or demand goods."); return false; } while ($row = $rs->fetch_assoc()) { if ($row['upgrade'] > 0 && $row['supply'] <= 0) { $existing_upgrades[$row['upgrade']] = $row; } else { if ($row['upgrade'] <= 0 && $row['supply'] > 0) { $supplies[$row['good']] = $row; } } } // Attempt to search for possible upgrades. This looks convoluted but // the idea is that to be eligible for an upgrade a port must have at // least one of the required goods for sale already but not all of // them. We must then make sure we don't go over the max and still have // some randomness by skipping. If we random over everything we need to // start over but also limit these restarts to prevent endless loops. $upgrades = array(); $upgrade_paths = $this->upgrade_paths; $reverse_upgrade_paths = $this->reverse_upgrade_paths; $loop_limit = 1000; while ($upgrade_count > 0 && $loop_limit > 0) { $loop_limit--; foreach ($supplies as $good => $row) { if (!isset($upgrade_paths[$good])) { continue; } if ($upgrade_paths[$good]['count'] <= 0) { continue; } $targets = array(); foreach ($upgrade_paths[$good]['targets'] as $target) { if (isset($existing_upgrades[$target])) { continue; } if (isset($upgrades[$target])) { continue; } if ($reverse_upgrade_paths[$target]['count'] <= 0) { continue; } if (mt_rand(0, 100) < UPGRADE_CHANCE) { continue; } // Reverse search targets to find all goods we may be able to // demand for an upgrade. $success = false; foreach ($reverse_upgrade_paths[$target]['goods'] as $other_good) { if ($good == $other_good) { continue; } if (isset($supplies[$other_good])) { continue; } $upgrades[$target][] = $other_good; $success = true; } if ($success) { $upgrade_count--; if ($upgrade_count <= 0) { break 3; } } } } } // We should have a list of upgrades we can add to the port. Systematically add // them and update distances for all goods involved. $good_list = array(); $good_count = 0; $demand_start_amount = UPGRADE_START_MULTIPLIER; if (!($st = $db->get_db()->prepare("insert into port_goods (place, good, amount, supply, upgrade, last_update) values (?,?,?,0,?,?)"))) { $return_codes[] = 1006; return; } $st->bind_param("iiiii", $place_id, $good, $upgrade_start_amount, $target, $time); foreach ($upgrades as $target => $list) { $good_list[] = $target; $good_count++; foreach ($list as $good) { $upgrade_start_amount = $demand_start_amount * $spacegame['goods'][$target]['level']; if (!$st->execute()) { error_log(__FILE__ . '::' . __LINE__ . " Query execution failed: (" . $st->errno . ") " . $st->error); return; } $good_list[] = $good; $good_count++; } } if ($good_count > 0) { update_distances($good_list); } else { if (mt_rand(0, 1000) < 100) { update_distances(); } } }
function insert_port($caption, $system, $x, $y, $supply_count, $demand_count, $planet_type, $update_distances = true) { global $db; $db = isset($db) ? $db : new DB(); global $spacegame; global $return_codes; $demand_start_amount = -PORT_LIMIT; $supply_start_amount = PORT_LIMIT; if (!($st = $db->get_db()->prepare("insert into places (caption, system, x, y, type) values (?,?,?,?,?)"))) { error_log(__FILE__ . '::' . __LINE__ . " Prepare failed: (" . $db->get_db()->errno . ") " . $db->get_db()->error); $return_codes[] = 1006; return false; } $st->bind_param("siiii", $caption, $system, $x, $y, $spacegame['place_types_index']['Port']); if (!$st->execute()) { $return_codes[] = 1006; error_log(__FILE__ . '::' . __LINE__ . " Query execution failed: (" . $st->errno . ") " . $st->error); return false; } $place_id = $db->last_insert_id('places'); // Grab a list of goods this place will supply or demand $supplies = array(); $demands = array(); $error_limiter = 1000; while ($supply_count > 0 && $error_limiter > 0) { $error_limiter--; foreach ($spacegame['start_goods'][$planet_type]['supply'] as $good => $percent) { if (isset($supplies[$good])) { continue; } if (mt_rand(0, 100) <= $percent) { // One chosen so it can be used later as part of the port_goods query $supplies[$good] = 1; $supply_count--; if ($supply_count <= 0) { break 2; } } } } $error_limiter = 1000; while ($demand_count > 0 && $error_limiter > 0) { $error_limiter--; foreach ($spacegame['start_goods'][$planet_type]['demand'] as $good => $percent) { // Note the check for both demand and supply here if (isset($demands[$good]) || isset($supplies[$good])) { continue; } if (mt_rand(0, 100) <= $percent) { // Zero chosen so it can be used later as part of the port_goods query $demands[$good] = 0; $demand_count--; if ($demand_count <= 0) { break 2; } } } } $time = PAGE_START_TIME; // Add our supplies and demands to the database $goods_array = null; foreach ($supplies as $good => $supply) { if (!($st = $db->get_db()->prepare("insert into port_goods (place, good, amount, supply, last_update) values (?,?,?,?,?)"))) { error_log(__FILE__ . '::' . __LINE__ . " Prepare failed: (" . $db->get_db()->errno . ") " . $db->get_db()->error); $return_codes[] = 1006; return false; } $st->bind_param("iiiii", $place_id, $good, $supply_start_amount, $supply, $time); if (!$st->execute()) { $return_codes[] = 1006; error_log(__FILE__ . '::' . __LINE__ . " Query execution failed: (" . $st->errno . ") " . $st->error); return false; } $goods_array[] = $good; } foreach ($demands as $good => $supply) { if (!($st = $db->get_db()->prepare("insert into port_goods (place, good, amount, supply, last_update) values (?,?,?,?,?)"))) { error_log(__FILE__ . '::' . __LINE__ . " Prepare failed: (" . $db->get_db()->errno . ") " . $db->get_db()->error); $return_codes[] = 1006; return false; } $st->bind_param("iiiii", $place_id, $good, $demand_start_amount, $supply, $time); if (!$st->execute()) { $return_codes[] = 1006; error_log(__FILE__ . '::' . __LINE__ . " Query execution failed: (" . $st->errno . ") " . $st->error); return false; } $goods_array[] = $good; } if ($update_distances) { update_distances($goods_array); } return true; }