Author Topic: [REL]PHP/Web SDK 0.1 - Interact with your LU server through PHP!  (Read 3392 times)

Rhytz

  • Newbie
  • *
  • Posts: 30
  • Karma: +13/-7
    • View Profile
    • Rhytz's Website
[REL]PHP/Web SDK 0.1 - Interact with your LU server through PHP!
« 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




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

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 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 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
  • 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!
« Last Edit: November 19, 2016, 11:16:24 pm by Rhytz »
Meet everyone on the Liberty Unleashed Discord || Check out my website/portal!

JamesConway

  • Newbie
  • *
  • Posts: 39
  • Karma: +9/-3
    • View Profile
Re: [REL]PHP/Web SDK 0.1 - Interact with your LU server through PHP!
« Reply #1 on: November 12, 2016, 10:17:54 pm »
This is really neat, I was looking for something like this. Thanks

Rhytz

  • Newbie
  • *
  • Posts: 30
  • Karma: +13/-7
    • View Profile
    • Rhytz's Website
Re: [REL]PHP/Web SDK 0.1 - Interact with your LU server through PHP!
« Reply #2 on: November 14, 2016, 01:03:29 pm »
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.
Meet everyone on the Liberty Unleashed Discord || Check out my website/portal!

Theremin

  • Full Member
  • ***
  • Posts: 154
  • Karma: +48/-18
  • Worst Server Owner
    • View Profile
    • Visit my YouTube channel
Re: [REL]PHP/Web SDK 0.1 - Interact with your LU server through PHP!
« Reply #3 on: November 15, 2016, 05:55:59 pm »
Great release Rhytz! :)

Rhytz

  • Newbie
  • *
  • Posts: 30
  • Karma: +13/-7
    • View Profile
    • Rhytz's Website
Re: [REL]PHP/Web SDK - Interact with your LU server through PHP!
« Reply #4 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
Meet everyone on the Liberty Unleashed Discord || Check out my website/portal!

SugarD

  • Argonath RPG Dev/Manager
  • Tester
  • Sr. Member
  • ****
  • Posts: 820
  • Karma: +37/-75
  • STOP IN THE NAME OF THE COLESLAW!
    • View Profile
    • Clan Xperience
Re: [REL]PHP/Web SDK 0.1 - Interact with your LU server through PHP!
« Reply #5 on: January 19, 2017, 07:34:31 pm »
Awesome work!

 

© Liberty Unleashed Team.