<commit_before><?php declare(strict_types=1);

// Use this exception to help override container forwarding for NPC's
class ForwardException extends Exception {}

function overrideForward($container) {
	global $forwardedContainer;
	$forwardedContainer = $container;
	if ($container['body'] == 'error.php') {
		// We hit a create_error - this shouldn't happen for an NPC often,
		// for now we want to throw an exception for it for testing.
		debug('Hit an error');
		throw new Exception($container['message']);
	}
	// We have to throw the exception to get back up the stack,
	// otherwise we quickly hit problems of overflowing the stack.
	throw new ForwardException;
}
const OVERRIDE_FORWARD = true;

// Must be defined before anything that might throw an exception
const NPC_SCRIPT = true;

// global config
require_once(realpath(dirname(__FILE__)) . '/../../bootstrap.php');
// bot config
require_once(CONFIG . 'npc/config.specific.php');
// needed libs
require_once(get_file_loc('smr.inc.php'));
require_once(get_file_loc('shop_goods.inc.php'));

// Raise exceptions for all types of errors for improved error reporting
// and to attempt to shut down the NPCs cleanly on errors.
set_error_handler("exception_error_handler");

const SHIP_UPGRADE_PATH = array(
	RACE_ALSKANT => array(
		SHIP_TYPE_TRADE_MASTER,
		SHIP_TYPE_TRIP_MAKER,
		SHIP_TYPE_SMALL_TIMER
	),
	RACE_CREONTI => array(
		SHIP_TYPE_LEVIATHAN,
		SHIP_TYPE_MEDIUM_CARGO_HULK
	),
	RACE_HUMAN => array(
		SHIP_TYPE_AMBASSADOR,
		SHIP_TYPE_RENAISSANCE,
		SHIP_TYPE_LIGHT_FREIGHTER
	),
	RACE_IKTHORNE => array(
		SHIP_TYPE_FAVOURED_OFFSPRING,
		SHIP_TYPE_PROTO_CARRIER,
		SHIP_TYPE_TINY_DELIGHT
	),
	RACE_SALVENE => array(
		SHIP_TYPE_DRUDGE,
		SHIP_TYPE_HATCHLINGS_DUE
	),
	RACE_THEVIAN => array(
		SHIP_TYPE_EXPEDITER,
		SHIP_TYPE_SWIFT_VENTURE
	),
	RACE_WQHUMAN => array(
		SHIP_TYPE_BLOCKADE_RUNNER,
		SHIP_TYPE_NEGOTIATOR,
		SHIP_TYPE_SLIP_FREIGHTER
	),
	RACE_NIJARIN => array(
		SHIP_TYPE_VENGEANCE,
		SHIP_TYPE_REDEEMER
	)
);


try {
	$db = Smr\Database::getInstance();
	debug('Script started');

	// Make sure NPC's have been set up in the database
	$db->query('SELECT 1 FROM npc_logins LIMIT 1');
	if (!$db->nextRecord()) {
		debug('No NPCs have been created yet!');
		exit;
	}

	try {
		changeNPCLogin();
	} catch (ForwardException $e) {}

	NPCStuff();
} catch (Throwable $e) {
	logException($e);
	// Try to shut down cleanly
	exitNPC();
}


function NPCStuff() {
	global $actions, $previousContainer;

	$underAttack = false;
	$actions = -1;

	$session = Smr\Session::getInstance();
	$session->setCurrentVar(new Page()); // initialize empty var

	while (true) {
		// Clear the $_REQUEST global, in case we had set it, to avoid
		// contaminating subsequent page processing.
		$_REQUEST = [];

		$actions++;

		// Avoid infinite loops by restricting the number of actions
		if ($actions > NPC_MAX_ACTIONS) {
			debug('Reached maximum number of actions: ' . NPC_MAX_ACTIONS);
			changeNPCLogin();
		}

		try {
			$TRADE_ROUTE =& $GLOBALS['TRADE_ROUTE'];
			debug('Action #' . $actions);

			//We have to reload player on each loop
			$player = $session->getPlayer(true);
			// Sanity check to be certain we actually have an NPC
			if (!$player->isNPC()) {
				throw new Exception('Player is not an NPC!');
			}<commit_msg>npc.php: fix session init in db logging mode<commit_after><?php declare(strict_types=1);

// Use this exception to help override container forwarding for NPC's
class ForwardException extends Exception {}

function overrideForward($container) {
	global $forwardedContainer;
	$forwardedContainer = $container;
	if ($container['body'] == 'error.php') {
		// We hit a create_error - this shouldn't happen for an NPC often,
		// for now we want to throw an exception for it for testing.
		debug('Hit an error');
		throw new Exception($container['message']);
	}
	// We have to throw the exception to get back up the stack,
	// otherwise we quickly hit problems of overflowing the stack.
	throw new ForwardException;
}
const OVERRIDE_FORWARD = true;

// Must be defined before anything that might throw an exception
const NPC_SCRIPT = true;

// global config
require_once(realpath(dirname(__FILE__)) . '/../../bootstrap.php');
// bot config
require_once(CONFIG . 'npc/config.specific.php');
// needed libs
require_once(get_file_loc('smr.inc.php'));
require_once(get_file_loc('shop_goods.inc.php'));

// Raise exceptions for all types of errors for improved error reporting
// and to attempt to shut down the NPCs cleanly on errors.
set_error_handler("exception_error_handler");

const SHIP_UPGRADE_PATH = array(
	RACE_ALSKANT => array(
		SHIP_TYPE_TRADE_MASTER,
		SHIP_TYPE_TRIP_MAKER,
		SHIP_TYPE_SMALL_TIMER
	),
	RACE_CREONTI => array(
		SHIP_TYPE_LEVIATHAN,
		SHIP_TYPE_MEDIUM_CARGO_HULK
	),
	RACE_HUMAN => array(
		SHIP_TYPE_AMBASSADOR,
		SHIP_TYPE_RENAISSANCE,
		SHIP_TYPE_LIGHT_FREIGHTER
	),
	RACE_IKTHORNE => array(
		SHIP_TYPE_FAVOURED_OFFSPRING,
		SHIP_TYPE_PROTO_CARRIER,
		SHIP_TYPE_TINY_DELIGHT
	),
	RACE_SALVENE => array(
		SHIP_TYPE_DRUDGE,
		SHIP_TYPE_HATCHLINGS_DUE
	),
	RACE_THEVIAN => array(
		SHIP_TYPE_EXPEDITER,
		SHIP_TYPE_SWIFT_VENTURE
	),
	RACE_WQHUMAN => array(
		SHIP_TYPE_BLOCKADE_RUNNER,
		SHIP_TYPE_NEGOTIATOR,
		SHIP_TYPE_SLIP_FREIGHTER
	),
	RACE_NIJARIN => array(
		SHIP_TYPE_VENGEANCE,
		SHIP_TYPE_REDEEMER
	)
);


try {
	NPCStuff();
} catch (Throwable $e) {
	logException($e);
	// Try to shut down cleanly
	exitNPC();
}


function NPCStuff() {
	global $actions, $previousContainer;

	$session = Smr\Session::getInstance();
	$session->setCurrentVar(new Page()); // initialize empty var

	debug('Script started');

	// Make sure NPC's have been set up in the database
	$db = Smr\Database::getInstance();
	$db->query('SELECT 1 FROM npc_logins LIMIT 1');
	if (!$db->nextRecord()) {
		debug('No NPCs have been created yet!');
		return;
	}

	// Load the first available NPC
	try {
		changeNPCLogin();
	} catch (ForwardException $e) {}

	$underAttack = false;
	$actions = -1;

	while (true) {
		// Clear the $_REQUEST global, in case we had set it, to avoid
		// contaminating subsequent page processing.
		$_REQUEST = [];

		$actions++;

		// Avoid infinite loops by restricting the number of actions
		if ($actions > NPC_MAX_ACTIONS) {
			debug('Reached maximum number of actions: ' . NPC_MAX_ACTIONS);
			changeNPCLogin();
		}

		try {
			$TRADE_ROUTE =& $GLOBALS['TRADE_ROUTE'];
			debug('Action #' . $actions);

			//We have to reload player on each loop
			$player = $session->getPlayer(true);
			// Sanity check to be certain we actually have an NPC
			if (!$player->isNPC()) {
				throw new Exception('Player is not an NPC!');
			}
{'fraction_non_alphanumeric': 0.11338345864661654, 'fraction_numerical': 0.0012030075187969924, 'mean_word_length': 4.355072463768116, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4113987', 'n_tokens_mistral': 2519, 'n_tokens_neox': 2324, 'n_words': 618}