Skip to content

Instantly share code, notes, and snippets.

@thedava
Last active July 29, 2018 17:07
Show Gist options
  • Save thedava/90a566c34b3409c40fbcf7c067fcf510 to your computer and use it in GitHub Desktop.
Save thedava/90a566c34b3409c40fbcf7c067fcf510 to your computer and use it in GitHub Desktop.
<?php
namespace Games\Game\Planetbase;
use Wa72\HtmlPageDom\HtmlPageCrawler;
/**
* @see https://games-blog.de/planetbase-cheats/4566/
*/
class CheatManager
{
/** @var Savegame */
protected $savegame;
/**
* @param Savegame $savegame
*/
public function __construct(Savegame $savegame)
{
$this->savegame = $savegame;
}
/**
* @param int $credits
*
* @return $this
*/
public function setCredits($credits)
{
$amount = $this->savegame
->getFileContent()
->filter('inmaterial-resources')
->filter('amount');
if ($amount->filter('resource-type')->getAttribute('value') === 'Coins') {
$creditAmount = $amount->filter('amount > amount');
$creditAmount->setAttribute('value', $credits);
}
return $this;
}
/**
* @param $moduleType
* @param $sizeIndex
*
* @return array|HtmlPageCrawler[]
*/
protected function getModules($moduleType, $sizeIndex)
{
$modules = [];
$this->savegame
->getFileContent()
->filter('construction')
->each(function (HtmlPageCrawler $node) use ($moduleType, $sizeIndex, &$modules) {
if (
$node->getAttribute('type') === 'Module'
&& $node->filter('module-type')->getAttribute('value') === $moduleType
&& $node->filter('size-index')->getAttribute('value') == $sizeIndex
) {
$modules[] = $node;
}
});
return $modules;
}
/**
* Refill all energy collectors
*
* @return $this
*/
public function fillEnergyCollectors()
{
foreach (Planetbase::getEnergyCollectorsConfig() as $energyCollector) {
foreach ($this->getModules('ModuleTypePowerCollector', $energyCollector['size-index']) as $module) {
$module->filter('power-storage')->setAttribute('value', $energyCollector['max-capacity']);
}
}
return $this;
}
public function fillWaterTanks()
{
foreach (Planetbase::getWaterTanksConfig() as $waterTank) {
foreach ($this->getModules('ModuleTypeWaterTank', $waterTank['size-index']) as $module) {
$module->filter('water-storage')->setAttribute('value', $waterTank['max-capacity']);
}
}
return $this;
}
/**
* @param string $technology
*
* @return $this
*/
public function unlockTechnology($technology)
{
$this->savegame
->getFileContent()
->filter('techs')
->append(sprintf('<tech value="%s" />', $technology));
return $this;
}
/**
* Unlocks all known technologies
*
* @return $this
*/
public function unlockTechnologies()
{
foreach (Planetbase::getTechnologies() as $technology) {
$this->unlockTechnology($technology);
}
return $this;
}
/**
* Reset all disasters
*
* @return $this
*/
public function resetDisasters()
{
// TODO: implement
return $this;
}
/**
* Heal all colonials
*
* @return $this
*/
public function healColonials()
{
$this->savegame
->getFileContent()
->filter('condition')
->each(function (HtmlPageCrawler $node) {
if ($node->getAttribute('type') === 'ConditionTraumaInjuries') {
$node->remove();
}
});
return $this;
}
}
<?php
namespace Games\Game\Planetbase;
class Planetbase
{
// Technologies
const TECHNOLOGY_GOLIATH_TURBINE = 'TechGoliathTurbine';
const TECHNOLOGY_DRILLER_BOT = 'TechDrillerBot';
const TECHNOLOGY_COLOSSAL_PANEL = 'TechColossalPanel';
const TECHNOLOGY_CONSTRUCTOR_BOT = 'TechConstructorBot';
const TECHNOLOGY_MASSIVE_STORAGE = 'TechMassiveStorage';
const TECHNOLOGY_MEGA_COLLECTOR = 'TechMegaCollector';
const TECHNOLOGY_SUPER_EXTRACTOR = 'TechSuperExtractor';
const TECHNOLOGY_FARM_DOME = 'TechFarmDome';
const TECHNOLOGY_GM_TOMATOES = 'TechGmTomatoes';
const TECHNOLOGY_GM_ONIONS = 'TechGmOnions';
// Disasters
const DISASTER_SANDSTORM = 'sandstorm';
const DISASTER_BLIZZARD = 'blizzard';
const DISASTER_SOLAR_FLARE = 'solar-flare';
// Energy Collectors
const ENERGY_COLLECTOR_SMALL = 'energy-collector-0';
const ENERGY_COLLECTOR_MEDIUM = 'energy-collector-1';
const ENERGY_COLLECTOR_LARGE = 'energy-collector-2';
// Water Tanks
const WATER_TANK_SMALL = 'water-tank-0';
const WATER_TANK_MEDIUM = 'water-tank-1';
/**
* @return array
*/
public static function getTechnologies()
{
return [
self::TECHNOLOGY_GOLIATH_TURBINE,
self::TECHNOLOGY_DRILLER_BOT,
self::TECHNOLOGY_COLOSSAL_PANEL,
self::TECHNOLOGY_CONSTRUCTOR_BOT,
self::TECHNOLOGY_MASSIVE_STORAGE,
self::TECHNOLOGY_MEGA_COLLECTOR,
self::TECHNOLOGY_SUPER_EXTRACTOR,
self::TECHNOLOGY_FARM_DOME,
self::TECHNOLOGY_GM_TOMATOES,
self::TECHNOLOGY_GM_ONIONS,
];
}
/**
* @return array
*/
public static function getDisasters()
{
return [
self::DISASTER_SANDSTORM,
self::DISASTER_BLIZZARD,
self::DISASTER_SOLAR_FLARE,
];
}
/**
* @return array
*/
public static function getEnergyCollectorsConfig()
{
return [
self::ENERGY_COLLECTOR_SMALL => [
'size-index' => 0,
'max-capacity' => 5000000,
],
self::ENERGY_COLLECTOR_MEDIUM => [
'size-index' => 1,
'max-capacity' => 7500000,
],
self::ENERGY_COLLECTOR_LARGE => [
'size-index' => 2,
'max-capacity' => '1.25E+07',
],
];
}
/**
* @return array
*/
public static function getWaterTanksConfig()
{
return [
self::WATER_TANK_SMALL => [
'size-index' => 0,
'max-capacity' => 400000,
],
self::WATER_TANK_MEDIUM => [
'size-index' => 1,
'max-capacity' => 600000,
],
];
}
}
<?php
namespace Games\Game\Planetbase;
use DOMDocument;
use Wa72\HtmlPageDom\HtmlPageCrawler;
class Savegame
{
/** @var string */
protected $filePath;
/** @var HtmlPageCrawler */
protected $fileContent;
/**
* @param string $filePath
*/
public function __construct(string $filePath)
{
$this->filePath = $filePath;
}
/**
* @return string
*/
public function getFilePath()
{
return $this->filePath;
}
/**
* @return HtmlPageCrawler
*/
public function getFileContent()
{
if (!$this->fileContent) {
$this->load();
}
return $this->fileContent;
}
/**
* @return $this
*/
public function load()
{
$content = file_get_contents($this->getFilePath());
$this->fileContent = new HtmlPageCrawler($content);
return $this;
}
/**
* @return string
*/
public function getFileContentRaw()
{
$content = $this->getFileContent()->saveHTML();
$xml = new DOMDocument();
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true;
$xml->loadXML($content);
return str_replace('/>', ' />', $xml->saveXML());
}
/**
* @return $this
*/
public function save()
{
file_put_contents($this->getFilePath(), $this->getFileContentRaw());
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment