<commit_before><?php

$submit = isset($var['submit']) ? $var['submit'] : (isset($_REQUEST['submit'])?$_REQUEST['submit']:'');
unset($var['submit']);

if ($submit=='Create Galaxies') {
	for ($i=1;$i<=$var['num_gals'];$i++) {
		$galaxy = SmrGalaxy::createGalaxy($var['game_id'],$i);
		$galaxy->setName($_POST['gal' . $i]);
		$galaxy->setWidth($_POST['width' . $i]);
		$galaxy->setHeight($_POST['height' . $i]);
		$galaxy->setGalaxyType($_POST['type' . $i]);
		$galaxy->setMaxForceTime($_POST['forces' . $i] * 3600);
	}
	SmrGalaxy::saveGalaxies();
	$galaxies = SmrGalaxy::getGameGalaxies($var['game_id'],true);
	foreach ($galaxies as $galaxy) {
		$galaxy->generateSectors();
	}
	SmrSector::saveSectors();
	$var['message'] = '<span class="green">Success</span> : Succesfully created galaxies.';
}
else if ($submit=='Redo Connections') {
	$galaxy = SmrGalaxy::getGalaxy($var['game_id'],$var['gal_on']);
	if(!$galaxy->setConnectivity($_REQUEST['connect']))
		$var['message'] = '<span class="red">Error</span> : Regenerating connections failed.';
	else
		$var['message'] = '<span class="green">Success</span> : Regenerated connections.';
	SmrSector::saveSectors();
}
elseif ($submit == 'Jump To Galaxy') {
	if(!is_numeric($_REQUEST['jumpgal'])) {
		create_error('New galaxy must be a number.');
	}
	$var['gal_on'] = (int)$_REQUEST['jumpgal'];
}
elseif ($submit == 'Toggle Link') {
	$linkSector = SmrSector::getSector($var['game_id'],$var['sector_id']);
	$linkSector->toggleLink($var['dir']);
	SmrSector::saveSectors();
}
elseif ($submit == 'Modify Sector') {
	if(!empty($_POST['sector_edit'])) {
		$galaxy = SmrGalaxy::getGalaxy($var['game_id'],$var['gal_on']);
		if($galaxy->contains($_POST['sector_edit'])) {
			$var['sector_id'] = $_POST['sector_edit'];
			$var['body'] = '1.6/universe_create_sector_details.php';
		}
		else
			$var['message'] = '<span class="red">Error</span> : That sector does not exist in this galaxy.';
	}
}
elseif ($submit == 'Create Locations') {
	$galSectors = SmrSector::getGalaxySectors($var['game_id'],$var['gal_on']);
	foreach ($galSectors as $galSector) {
		$galSector->removeAllLocations();
	}
	foreach (SmrLocation::getAllLocations() as $location) {
		if (isset($_POST['loc' . $location->getTypeID()])) {
			for ($i=0;$i<$_POST['loc' . $location->getTypeID()];$i++) {
				$randSector = $galSectors[array_rand($galSectors)]; //get random sector from start of gal to end of gal
				//4 per sector max locs and no locations inside fed
				
				while (!checkSectorAllowedForLoc($randSector, $location)) {
					$randSector = $galSectors[array_rand($galSectors)]; //get valid sector
				}
				
				addLocationToSector($location, $randSector);
			}
		}
	}
	$var['message'] = '<span class="green">Success</span> : Succesfully added locations.';
}
elseif ($submit == 'Create Warps') {
	//get all warp info from all gals, some need to be removed, some need to be added
	$galaxy = SmrGalaxy::getGalaxy($var['game_id'],$var['gal_on']);
	$galSectors = $galaxy->getSectors();
	//get totals
	foreach ($galSectors as $galSector) {
		if($galSector->hasWarp()) {
			$galSector->removeWarp();
		}
	}
	//iterate over all the galaxies
	$galaxies = SmrGalaxy::getGameGalaxies($var['game_id']);
	foreach ($galaxies as $eachGalaxy) {
		//do we have a warp to this gal?
		if (isset($_POST['warp' . $eachGalaxy->getGalaxyID()])) {
			// Sanity check the number
			$numWarps = $_POST['warp' . $eachGalaxy->getGalaxyID()];
			if ($numWarps > 10) {
				create_error('Specify no more than 10 warps between two galaxies!');
			}
			//iterate for each warp to this gal
			for ($i=1; $i<=$numWarps; $i++) {
				$galSector = $galSectors[array_rand($galSectors)];
				//only 1 warp per sector
				while ($galSector->hasWarp() || $galSector->offersFederalProtection()) {
					$galSector = $galSectors[array_rand($galSectors)];
				}
				//get other side
				$otherSectors = $eachGalaxy->getSectors();
				$otherSector = $otherSectors[array_rand($otherSectors)];
				//make sure it does not go to itself
				while ($otherSector->hasWarp() || $otherSector->offersFederalProtection() || $otherSector->equals($galSector)) {
					$otherSector = $otherSectors[array_rand($otherSectors)];
				}
				$galSector->setWarp($otherSector);
			}
		}
	}
	SmrSector::saveSectors();
	$var['message'] = '<span class="green">Success</span> : Succesfully added warps.';
}
elseif ($submit == 'Create Planets') {
	$galaxy = SmrGalaxy::getGalaxy($var['game_id'],$var['gal_on']);
	$galSectors = $galaxy->getSectors();
	foreach ($galSectors as $galSector) {
		if($galSector->hasPlanet()) {
			$galSector->removePlanet();
		}
	}
//	$numberOfNpcPlanets = $_POST['NPC'];

	foreach (array_keys(SmrPlanetType::PLANET_TYPES) as $planetTypeID) {
		$numberOfPlanets = $_POST['type' . $planetTypeID];
		for ($i=1;$i<=$numberOfPlanets;$i++) {
			$galSector = $galSectors[array_rand($galSectors)];
			while ($galSector->hasPlanet()) $galSector = $galSectors[array_rand($galSectors)]; //1 per sector
			$galSector->createPlanet($planetTypeID);
		}
	}
	$var['message'] = '<span class="green">Success</span> : Succesfully added planets.';
}
elseif ($submit == 'Create Ports') {
	$totalPorts=0;
	for ($i=1; $i<=SmrPort::MAX_LEVEL; $i++) {
		$totalPorts+=$_REQUEST['port' . $i];
	}
	$totalRaceDist=0;
	$assignedPorts=0;
	$numRacePorts = array();
	foreach (Globals::getRaces() as $race) {
		$totalRaceDist+=$_REQUEST['race' . $race['Race ID']];
		$numRacePorts[$race['Race ID']] = ceil($_REQUEST['race' . $race['Race ID']] / 100 * $totalPorts);
		$assignedPorts+=$numRacePorts[$race['Race ID']];
		if($numRacePorts[$race['Race ID']]==0)
			unset($numRacePorts[$race['Race ID']]);
	}
	if ($totalRaceDist == 100 || $totalPorts == 0) {
		$galaxy = SmrGalaxy::getGalaxy($var['game_id'],$var['gal_on']);
		$galSectors = $galaxy->getSectors();
		foreach ($galSectors as $galSector) {
			if($galSector->hasPort()) {
				$galSector->removePort();
			}
		}
		//get race for all ports
		while ($totalPorts > $assignedPorts) {
			//this adds extra ports until we reach the requested #<commit_msg>Uni Gen: handle empty race % in Modify Ports<commit_after><?php

$submit = isset($var['submit']) ? $var['submit'] : (isset($_REQUEST['submit'])?$_REQUEST['submit']:'');
unset($var['submit']);

if ($submit=='Create Galaxies') {
	for ($i=1;$i<=$var['num_gals'];$i++) {
		$galaxy = SmrGalaxy::createGalaxy($var['game_id'],$i);
		$galaxy->setName($_POST['gal' . $i]);
		$galaxy->setWidth($_POST['width' . $i]);
		$galaxy->setHeight($_POST['height' . $i]);
		$galaxy->setGalaxyType($_POST['type' . $i]);
		$galaxy->setMaxForceTime($_POST['forces' . $i] * 3600);
	}
	SmrGalaxy::saveGalaxies();
	$galaxies = SmrGalaxy::getGameGalaxies($var['game_id'],true);
	foreach ($galaxies as $galaxy) {
		$galaxy->generateSectors();
	}
	SmrSector::saveSectors();
	$var['message'] = '<span class="green">Success</span> : Succesfully created galaxies.';
}
else if ($submit=='Redo Connections') {
	$galaxy = SmrGalaxy::getGalaxy($var['game_id'],$var['gal_on']);
	if(!$galaxy->setConnectivity($_REQUEST['connect']))
		$var['message'] = '<span class="red">Error</span> : Regenerating connections failed.';
	else
		$var['message'] = '<span class="green">Success</span> : Regenerated connections.';
	SmrSector::saveSectors();
}
elseif ($submit == 'Jump To Galaxy') {
	if(!is_numeric($_REQUEST['jumpgal'])) {
		create_error('New galaxy must be a number.');
	}
	$var['gal_on'] = (int)$_REQUEST['jumpgal'];
}
elseif ($submit == 'Toggle Link') {
	$linkSector = SmrSector::getSector($var['game_id'],$var['sector_id']);
	$linkSector->toggleLink($var['dir']);
	SmrSector::saveSectors();
}
elseif ($submit == 'Modify Sector') {
	if(!empty($_POST['sector_edit'])) {
		$galaxy = SmrGalaxy::getGalaxy($var['game_id'],$var['gal_on']);
		if($galaxy->contains($_POST['sector_edit'])) {
			$var['sector_id'] = $_POST['sector_edit'];
			$var['body'] = '1.6/universe_create_sector_details.php';
		}
		else
			$var['message'] = '<span class="red">Error</span> : That sector does not exist in this galaxy.';
	}
}
elseif ($submit == 'Create Locations') {
	$galSectors = SmrSector::getGalaxySectors($var['game_id'],$var['gal_on']);
	foreach ($galSectors as $galSector) {
		$galSector->removeAllLocations();
	}
	foreach (SmrLocation::getAllLocations() as $location) {
		if (isset($_POST['loc' . $location->getTypeID()])) {
			for ($i=0;$i<$_POST['loc' . $location->getTypeID()];$i++) {
				$randSector = $galSectors[array_rand($galSectors)]; //get random sector from start of gal to end of gal
				//4 per sector max locs and no locations inside fed
				
				while (!checkSectorAllowedForLoc($randSector, $location)) {
					$randSector = $galSectors[array_rand($galSectors)]; //get valid sector
				}
				
				addLocationToSector($location, $randSector);
			}
		}
	}
	$var['message'] = '<span class="green">Success</span> : Succesfully added locations.';
}
elseif ($submit == 'Create Warps') {
	//get all warp info from all gals, some need to be removed, some need to be added
	$galaxy = SmrGalaxy::getGalaxy($var['game_id'],$var['gal_on']);
	$galSectors = $galaxy->getSectors();
	//get totals
	foreach ($galSectors as $galSector) {
		if($galSector->hasWarp()) {
			$galSector->removeWarp();
		}
	}
	//iterate over all the galaxies
	$galaxies = SmrGalaxy::getGameGalaxies($var['game_id']);
	foreach ($galaxies as $eachGalaxy) {
		//do we have a warp to this gal?
		if (isset($_POST['warp' . $eachGalaxy->getGalaxyID()])) {
			// Sanity check the number
			$numWarps = $_POST['warp' . $eachGalaxy->getGalaxyID()];
			if ($numWarps > 10) {
				create_error('Specify no more than 10 warps between two galaxies!');
			}
			//iterate for each warp to this gal
			for ($i=1; $i<=$numWarps; $i++) {
				$galSector = $galSectors[array_rand($galSectors)];
				//only 1 warp per sector
				while ($galSector->hasWarp() || $galSector->offersFederalProtection()) {
					$galSector = $galSectors[array_rand($galSectors)];
				}
				//get other side
				$otherSectors = $eachGalaxy->getSectors();
				$otherSector = $otherSectors[array_rand($otherSectors)];
				//make sure it does not go to itself
				while ($otherSector->hasWarp() || $otherSector->offersFederalProtection() || $otherSector->equals($galSector)) {
					$otherSector = $otherSectors[array_rand($otherSectors)];
				}
				$galSector->setWarp($otherSector);
			}
		}
	}
	SmrSector::saveSectors();
	$var['message'] = '<span class="green">Success</span> : Succesfully added warps.';
}
elseif ($submit == 'Create Planets') {
	$galaxy = SmrGalaxy::getGalaxy($var['game_id'],$var['gal_on']);
	$galSectors = $galaxy->getSectors();
	foreach ($galSectors as $galSector) {
		if($galSector->hasPlanet()) {
			$galSector->removePlanet();
		}
	}
//	$numberOfNpcPlanets = $_POST['NPC'];

	foreach (array_keys(SmrPlanetType::PLANET_TYPES) as $planetTypeID) {
		$numberOfPlanets = $_POST['type' . $planetTypeID];
		for ($i=1;$i<=$numberOfPlanets;$i++) {
			$galSector = $galSectors[array_rand($galSectors)];
			while ($galSector->hasPlanet()) $galSector = $galSectors[array_rand($galSectors)]; //1 per sector
			$galSector->createPlanet($planetTypeID);
		}
	}
	$var['message'] = '<span class="green">Success</span> : Succesfully added planets.';
}
elseif ($submit == 'Create Ports') {
	$totalPorts=0;
	for ($i=1; $i<=SmrPort::MAX_LEVEL; $i++) {
		$totalPorts+=$_REQUEST['port' . $i];
	}

	$totalRaceDist=0;
	$numRacePorts = array();
	foreach (Globals::getRaces() as $race) {
		$racePercent = $_REQUEST['race' . $race['Race ID']];
		if (!empty($racePercent)) {
			$totalRaceDist += $racePercent;
			$numRacePorts[$race['Race ID']] = ceil($racePercent / 100 * $totalPorts);
		}
	}
	$assignedPorts = array_sum($numRacePorts);
	if ($totalRaceDist == 100 || $totalPorts == 0) {
		$galaxy = SmrGalaxy::getGalaxy($var['game_id'],$var['gal_on']);
		$galSectors = $galaxy->getSectors();
		foreach ($galSectors as $galSector) {
			if($galSector->hasPort()) {
				$galSector->removePort();
			}
		}
		//get race for all ports
		while ($totalPorts > $assignedPorts) {
			//this adds extra ports until we reach the requested #
{'fraction_non_alphanumeric': 0.20453601395696602, 'fraction_numerical': 0.004652322007144638, 'mean_word_length': 5.3693121693121695, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 99, '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': '7054582', 'n_tokens_mistral': 4949, 'n_tokens_neox': 4506, 'n_words': 803}