Liberty Unleashed

Scripting => Script Releases => Topic started by: Rhytz on November 12, 2016, 03:58:42 pm

Title: [REL]PHP/Web SDK 0.1 - Interact with your LU server through PHP!
Post by: Rhytz on November 12, 2016, 03:58:42 pm
Hi guys,

The past few days I have been working on a solid way to make my LU server "talk" to my website in real time, to allow for all kinds of fun web based scripts that interact with the server.

Just think of the possibilities. You could for example:
So on and so forth...

The SDK is a simple PHP class and Squirrel script in which I have already tackled the major issues and brainteasers. I have added some sample functions to give you an idea how to use it. But it is up to you to extend the script and class, and work out your own awesome ideas.


GitHub (https://github.com/Rhytz/LU-WebSDK)




PHP code samples
Connecting to the server
Code: [Select]
$server = new LU("123.123.123.123", 2302, "SecureKey123");
Setting the weather ingame
Code: [Select]
$server->SetWeather(2); //Sets rainy weather
Getting details about the server
Code: [Select]
$server->ServerInfo();
Will return:
Code: [Select]
stdClass Object
(
    [MTUSize] => 576
    [MapName] => Liberty City
    [Players] => 0
    [GamemodeName] => Deathmatch
    [Port] => 2301
    [MaxPlayers] => 128
    [Password] => thepassword
    [ServerName] => Rhytz's Scripting test server
)





Setting it up

Note that some knowledge of Squirrel and PHP would be very useful...

1. Download the Squirrel script and PHP Class (https://rhytz.rocks/lu/lu_phpsdk_0.1.zip)

2.Copy the Rhytz folder in the zip file to your Scripts folder, and edit LU/content.xml.  Include a reference to the script at the bottom of this file, like so:
Code: [Select]
<script folder="Rhytz" />
3. Open Scripts/Rhytz/server.nut and edit the following constants to your needs:

Code: [Select]
//Replace SecureKey123 with a custom hash/password to prevent others from accessing your server
const SECURE_KEY = "SecureKey123";

//Replace with the IP of your webserver
const SECURE_IP = "123.123.123.123";

//On what port should the server listen to requests from your PHP script?
const LISTEN_PORT = 2302;

//Path to the script files
const FILE_PATH = "Scripts/Rhytz/phpsdk/";

4. Be sure LISTEN_PORT is open and accepts incoming TCP traffic. It also needs to be open for outgoing traffic on your webhost, which is not always the case. You may need to contact your webhosting provider about this.

5. Copy lu.class.php to your webhosting wherever you want to use it.

6. Include the class in your PHP script. Do something like this:
Code: [Select]
require_once("lu.class.php");
7. Create a new instance of the LU class in your PHP script, like so:
Code: [Select]
$server = new LU("123.123.123.123", 2302, "SecureKey"); You obviously replace the IP with the IP of your LU server, and the Port and securekey with the values you have set up in step 3.

8. Interact with your server! If you did everything correctly, you should now be able to communicate with your server through your php script. To test your success, you could do something like this:
Code: [Select]
$serverinfo = $server->ServerInfo();
echo "<pre>";
print_r($serverinfo);
echo "</pre>";
This code should return an object with all the details about your server.







Built-in functions

Although it is up to you to extend the class to your needs, I have created some necessary and sample functions.

CallFunc - Calls a function in another Squirrel script file. Makes a call to CallFunc (http://liberty-unleashed.co.uk/LUWiki/Squirrel/Server/Functions/Scripts/CallFunc) on your server.
Code: [Select]
CallFunc( scriptPath, funcName, array( Params ))
Sample usage:
Code: [Select]
$server->CallFunc("Scripts/Rhytz/phpsdk/remotefunc.nut", "thisIsARemoteFunc", array($param1, $param2))
CallClientFunc - Similar to CallFunc, but allows you to call a function on in a clientside script, for a specific client. Makes a call to CallClientFunc (http://liberty-unleashed.co.uk/LUWiki/Squirrel/Server/Functions/Scripts/CallClientFunc) on your server.
Code: [Select]
CallClientFunc(playerID, scriptPath, funcName, array( Params ))
Sample usage:
Code: [Select]
$server->CallClientFunc($playerID, "Scripts/Rhytz/phpsdk/remotefunc.nut", "thisIsARemoteFunc", array($param1, $param2))
ServerInfo - Returns an object with details about the LU server.

Sample usage:
Code: [Select]
$serverinfo = $server->ServerInfo();
echo $serverinfo->MapName; //Returns the current map name
 

CurrentPlayers - Returns an object with player ID's and names of players currently in the server.

Sample usage:
Code: [Select]
$server->CurrentPlayers();

GetWeather/SetWeather - Gets or sets the current weather.

Sample usage:
Code: [Select]
$weatherIDs = array(
0 => "Sunny",
1 => "Cloudy",
2 => "Rainy",
3 => "Foggy"
);

$server->SetWeather(2);

echo "The weather in Rhytz's server is currently: ". $weatherIDs[$server->GetWeather()->WeatherID]; //Returns "Rainy"


Cash - Gets player Cash, or sets it when a value parameter is supplied.

Sample usage:

Code: [Select]
echo $server->Cash(1); //Returns the amount of cash of player with ID 1

foreach($server->CurrentPlayers() as $id => $name){
     $server->Cash($id, 1000); //Sets the cash of all players in the server to 1000
}


Notes
Title: Re: [REL]PHP/Web SDK 0.1 - Interact with your LU server through PHP!
Post by: JamesConway on November 12, 2016, 10:17:54 pm
This is really neat, I was looking for something like this. Thanks
Title: Re: [REL]PHP/Web SDK 0.1 - Interact with your LU server through PHP!
Post by: Rhytz on November 14, 2016, 01:03:29 pm
I've made some small changes.


For better compatibility it might be better to use fsockopen instead of socket_create in the PHP class.
Title: Re: [REL]PHP/Web SDK 0.1 - Interact with your LU server through PHP!
Post by: Theremin on November 15, 2016, 05:55:59 pm
Great release Rhytz! :)
Title: Re: [REL]PHP/Web SDK - Interact with your LU server through PHP!
Post by: Rhytz on November 20, 2016, 09:45:14 pm
Updated and uploaded to GitHub. The script now includes functions to make requests from your LU server to your website, and I have made it more flexible.

Feel free to contribute!

https://github.com/Rhytz/LU-WebSDK
Title: Re: [REL]PHP/Web SDK 0.1 - Interact with your LU server through PHP!
Post by: SugarD on January 19, 2017, 07:34:31 pm
Awesome work!