Liberty Unleashed
Scripting => Script Releases => Topic started 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:
- Sync the login system of your server to your (WordPress?) website and vice versa
- Create a web-based shop to buy items ingame
- Create a web-based admin panel
- Make a web-based map editor
- Show a real-time map of player positions on your website
- Show detailed realtime statistics on your website
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
$server = new LU("123.123.123.123", 2302, "SecureKey123");
Setting the weather ingame
$server->SetWeather(2); //Sets rainy weather
Getting details about the server
$server->ServerInfo();
Will return:
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: <script folder="Rhytz" />
3. Open Scripts/Rhytz/server.nut and edit the following constants to your needs:
//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:
require_once("lu.class.php");
7. Create a new instance of the LU class in your PHP script, like so:
$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:
$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.
CallFunc( scriptPath, funcName, array( Params ))
Sample usage:
$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.
CallClientFunc(playerID, scriptPath, funcName, array( Params ))
Sample usage:
$server->CallClientFunc($playerID, "Scripts/Rhytz/phpsdk/remotefunc.nut", "thisIsARemoteFunc", array($param1, $param2))
ServerInfo - Returns an object with details about the LU server.
Sample usage:
$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:
$server->CurrentPlayers();
GetWeather/SetWeather - Gets or sets the current weather.
Sample usage:
$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:
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
- This will probably the first and last release of this script, unless it has some major problems. It is up to you to extend it to your needs.
- I use PHP, but the script could easily be ported to other programming languages. The communication between the servers consists of packets containing JSON.
- You may freely use this script, but some credit would be nice!
-
This is really neat, I was looking for something like this. Thanks
-
I've made some small changes.
- Cleaned up some code in the JSON functions
- Commented out the socket timeout function (because SetLostConnFunc doesnt work, and there is no way to tell if the socket was already disconnected before the timeout function runs)
- Fixed the sample Cash() function which didn't work when a ID of 0 was supplied
For better compatibility it might be better to use fsockopen instead of socket_create in the PHP class.
-
Great release Rhytz! :)
-
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
-
Awesome work!