lethak / frostbite-php-framework

BF4: a lightweight PHP Framework wrapping Frostbite's RCON protocol - Lets you trigger admin command from your website or CLI (tested with Battlefield 4 server up to R42)
12 stars 8 forks source link

LethaK's Frostbite-PHP-Framework

Not under active development anymore

We recomand the work of this guy : https://github.com/RobFreiburger/BF4_Webcon

Features

$server = new Lethak_Frostbite_Server("192.168.0.1", 47200, 'myRconPassword');
$server
    ->login()
    ->yell('Hello World !')
    ->rconCommand('vars.friendlyFire 1')
;
// ...
$player->say('Goodbye '.$player->name.' !')->kick();
# library/LethaK/Frostbite/Server.php == class Lethak_ForstBite_Server
# library/LethaK/Frostbite/Player.php == class Lethak_ForstBite_Player

What is working ?

What will be working soon ?

What may be added ahead ?

Example: RCON Login

See more: https://github.com/lethak/frostbite-php-framework/tree/master/examples/server-login.php

Authenticate as the server administrator using the RCON password to be granted access to restricted commands

$server = new Lethak_Frostbite_Server("127.0.0.1", 47200);
$server->login("myRconPassword");

// Fetch the player list,
// it will internaly use admin.playerList if authed, playerList if not authed...
$playerList = $server->players->getList();

// HTML formated rendering of the array list...
echo '<pre>'.print_r($playerList,1).'</pre>'; 

Example: Multiple server instance

Because one does not simply have a single server to manage.

// Server list declaration
$serverList[] = new Lethak_Frostbite_Server("127.0.0.1", 47200, "myRconPassword1");
$serverList[] = new Lethak_Frostbite_Server("127.0.0.2", 47200, "myRconPassword2");
$serverList[] = new Lethak_Frostbite_Server("127.0.0.3", 47200);

// Issuing commands quickly to the server list
foreach($serverList as $server)
{
  // do your stuff ...
  $some_stuff = $server->rconCommand('vars.maxPlayers');
  $server->say('Team killing for vehicles is prohibited');
}

Example: Error handling

See more: https://github.com/lethak/frostbite-php-framework/tree/master/examples/server-login.php

try
{
  $server = new Lethak_Frostbite_Server("127.0.0.1", 47200);
  $some_stuff = $server->rconCommand('this.is-not-a-valid-command');
}
catch(Exception $error)
{
  echo('<pre>'.print_r($error,1).'</pre>');
}

Example: Creating you own preset class

See more: https://github.com/lethak/frostbite-php-framework/tree/master/examples/server-preset.php

For the moment, you have to create your own class like this one. Remember to have the class Lethak_Frostbite_Server_Preset_Abstract included.

class myFastVehiclePreset extends Lethak_Frostbite_Server_Preset_Abstract
{

    public function label()
    {
        return 'Custom Fast Vehicle Preset';
    }

    protected function presetDefinition()
    {
        return array(
            'vars.friendlyFire' => 1,
            'vars.vehicleSpawnAllowed' => 1,
            'vars.vehicleSpawnDelay' => 20,
        );
    }
}

Example: Applying a server preset

See more: https://github.com/lethak/frostbite-php-framework/tree/master/examples/server-preset.php

This example feature 3 possible methods of using a preset.

try
{
    $server = new Lethak_Frostbite_Server("127.0.0.1", 47200, "myRconPassword");

    // Embeded 'Hardcore' Preset
    $hardcorePreset = new Lethak_Frostbite_Server_Preset_Hardcore();

    // Some custom preset, loaded from a database or else
    $customVariables = array(
        'vars.friendlyFire' => 1,
        'vars.killCam' => 0,
    );
    $customPreset = new Lethak_Frostbite_Server_Preset_Custom($customVariables);

    // A custom class preset
    // Remember to have the class myFastVehiclePreset included.
    $myPresetClass = new myFastVehiclePreset();

    // Applying previously defined preset
    $RESULT = array(
        $server->apply($hardcorePreset),
        $server->apply($customPreset),
        $server->apply($myPresetClass),
    );

}
catch(Exception $error)
{
    die('Exception: ['.get_class($error).'] <pre>'.print_r($error->getMessage(), true).'</pre>');
}

echo('FINISHED: <pre>'.print_r($RESULT, true).'</pre>');exit;