Skip to content

Commit

Permalink
Use same sorting logic as in python script
Browse files Browse the repository at this point in the history
  • Loading branch information
xPaw committed Jun 26, 2018
1 parent dd48636 commit 5847188
Showing 1 changed file with 29 additions and 51 deletions.
80 changes: 29 additions & 51 deletions cheat.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@
$RepositoryScriptHash = GetRepositoryScriptHash( $RepositoryScriptETag, $LocalScriptHash );

$WaitTime = 110;
$KnownPlanets = [];
$SkippedPlanets = [];
$ZonePaces = [];

Msg( "\033[37;44mWelcome to SalienCheat for SteamDB\033[0m" );
Expand Down Expand Up @@ -83,7 +81,7 @@

do
{
$BestPlanetAndZone = GetBestPlanetAndZone( $SkippedPlanets, $KnownPlanets, $ZonePaces, $WaitTime );
$BestPlanetAndZone = GetBestPlanetAndZone( $ZonePaces, $WaitTime );
}
while( !$BestPlanetAndZone && sleep( 5 ) === 0 );

Expand Down Expand Up @@ -117,7 +115,7 @@

do
{
$BestPlanetAndZone = GetBestPlanetAndZone( $SkippedPlanets, $KnownPlanets, $ZonePaces, $WaitTime );
$BestPlanetAndZone = GetBestPlanetAndZone( $ZonePaces, $WaitTime );
}
while( !$BestPlanetAndZone && sleep( 5 ) === 0 );

Expand Down Expand Up @@ -159,7 +157,7 @@

do
{
$BestPlanetAndZone = GetBestPlanetAndZone( $SkippedPlanets, $KnownPlanets, $ZonePaces, $WaitTime );
$BestPlanetAndZone = GetBestPlanetAndZone( $ZonePaces, $WaitTime );
}
while( !$BestPlanetAndZone && sleep( 5 ) === 0 );

Expand Down Expand Up @@ -439,7 +437,7 @@ function GetPlanetState( $Planet, &$ZonePaces, $WaitTime )
];
}

function GetBestPlanetAndZone( &$SkippedPlanets, &$KnownPlanets, &$ZonePaces, $WaitTime )
function GetBestPlanetAndZone( &$ZonePaces, $WaitTime )
{
$Planets = SendGET( 'ITerritoryControlMinigameService/GetPlanets', 'active_only=1&language=english' );

Expand All @@ -452,6 +450,8 @@ function GetBestPlanetAndZone( &$SkippedPlanets, &$KnownPlanets, &$ZonePaces, $W

foreach( $Planets as &$Planet )
{
$Planet[ 'sort_key' ] = 0;

if( empty( $Planet[ 'state' ][ 'capture_progress' ] ) )
{
$Planet[ 'state' ][ 'capture_progress' ] = 0.0;
Expand All @@ -462,8 +462,6 @@ function GetBestPlanetAndZone( &$SkippedPlanets, &$KnownPlanets, &$ZonePaces, $W
$Planet[ 'state' ][ 'current_players' ] = 0;
}

$KnownPlanets[ $Planet[ 'id' ] ] = true;

if( !isset( $ZonePaces[ $Planet[ 'id' ] ] ) )
{
$ZonePaces[ $Planet[ 'id' ] ] =
Expand All @@ -481,7 +479,6 @@ function GetBestPlanetAndZone( &$SkippedPlanets, &$KnownPlanets, &$ZonePaces, $W
if( $Zone === false )
{
$ZonePaces[ $Planet[ 'id' ] ] = [];
$SkippedPlanets[ $Planet[ 'id' ] ] = true;
$Planet[ 'high_zones' ] = 0;
$Planet[ 'medium_zones' ] = 0;
$Planet[ 'low_zones' ] = 0;
Expand Down Expand Up @@ -521,59 +518,40 @@ function GetBestPlanetAndZone( &$SkippedPlanets, &$KnownPlanets, &$ZonePaces, $W

return $Planet;
}
}
}

// https://bugs.php.net/bug.php?id=71454
unset( $Planet );

$Priority = [ 'high_zones', 'medium_zones', 'low_zones' ];
if( $Planet[ 'low_zones' ] > 0 )
{
$Planet[ 'sort_key' ] += 99 - $Planet[ 'low_zones' ];
}

usort( $Planets, function( $a, $b ) use ( $Priority )
{
// Sort planets by least amount of zones
for( $i = 0; $i < 3; $i++ )
{
$Key = $Priority[ $i ];
if( $Planet[ 'medium_zones' ] > 0 )
{
$Planet[ 'sort_key' ] += pow( 10, 2 ) * ( 99 - $Planet[ 'medium_zones' ] );
}

if( $a[ $Key ] !== $b[ $Key ] )
if( $Planet[ 'high_zones' ] > 0 )
{
return $a[ $Key ] - $b[ $Key ];
$Planet[ 'sort_key' ] += pow( 10, 4 ) * ( 99 - $Planet[ 'high_zones' ] );
}
}
}

return $a[ 'id' ] - $b[ 'id' ];
} );

// Loop three times - first loop tries to find planet with hard zones, second loop - medium zones, and then easies
for( $i = 0; $i < 3; $i++ )
foreach( $Planets as &$Planet )
usort( $Planets, function( $a, $b )
{
if( isset( $SkippedPlanets[ $Planet[ 'id' ] ] ) )
{
continue;
}
return $b[ 'sort_key' ] - $a[ 'sort_key' ];
} );

if( !$Planet[ $Priority[ $i ] ] )
{
continue;
}
$Planet = $Planets[ 0 ];

if( !$Planet[ 'state' ][ 'captured' ] )
{
Msg(
'>> Best Zone is {yellow}' . $Planet[ 'best_zone' ][ 'zone_position' ] .
'{normal} (Captured: {yellow}' . number_format( $Planet[ 'best_zone' ][ 'capture_progress' ] * 100, 2 ) . '%' .
'{normal} - Difficulty: {yellow}' . GetNameForDifficulty( $Planet[ 'best_zone' ] ) .
'{normal}) on Planet {green}' . $Planet[ 'id' ] .
' (' . $Planet[ 'state' ][ 'name' ] . ')'
);

return $Planet;
}
}
Msg(
'>> Best Zone is {yellow}' . $Planet[ 'best_zone' ][ 'zone_position' ] .
'{normal} (Captured: {yellow}' . number_format( $Planet[ 'best_zone' ][ 'capture_progress' ] * 100, 2 ) . '%' .
'{normal} - Difficulty: {yellow}' . GetNameForDifficulty( $Planet[ 'best_zone' ] ) .
'{normal}) on Planet {green}' . $Planet[ 'id' ] .
' (' . $Planet[ 'state' ][ 'name' ] . ')'
);

return $Planets[ 0 ];
return $Planet;
}

function LeaveCurrentGame( $Token, $LeaveCurrentPlanet = 0 )
Expand Down

0 comments on commit 5847188

Please sign in to comment.