Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


  Show Posts
Pages: [1]
1  Liberty Unleashed / Liberty Unleashed Chat / Liberty Unleashed official Discord server on: February 08, 2017, 09:53:46 pm



LU also has to keep up with the times a bit, and with the approval of VRocker, I have set up a Discord server for it.

For those unfamiliar, Discord is a chat application that aims to combine the best functions of IRC, TeamSpeak and Skype into one application. It is quickly taking over the gaming world and for good reason.

Feel free to join -and accompany me and my bots :) -



Current channels

  • #general - General discussion channel for everything LU
  • #scripting - Scripting discussion
  • #support - Channel for all your support questions
  • #rhytz-server - Experimental server echo channel



Bots
  • Lazlow - Experimental LU server echo bot (#rhytz-server)
  • Liberty Tree - Checks the forum for new posts on regular basis



Feel free to join and say hi!

2  Scripting / Script Help / Pointer to function within class on: January 31, 2017, 03:18:32 pm
A lot of LU functions take a string or pointer to a callback function. However, there seems to be no way to point to a function within a class.

Take the code below for example:
Code: [Select]
class Example {
mainSocket = null;

constructor(port){
mainSocket = NewSocket( this.ReceiveData );
mainSocket.SetNewConnFunc( this.Connected );
mainSocket.Start( port, 64 );
::print("Loaded example class and opened socket");
}

function Connected( socket, clientID, clientIP, clientPort ){
::print("Socket connected");
}

function ReceiveData( socket, clientID, data ){
::print("Received data");
}
}
ExampleClass <- Example(2345);

The socket is opened and can be connected to. However, the functions Connected and ReceiveData never get called. Script generates no errors. I've tried to make every possible reference to these functions I could think of, but nothing happens. Am I missing something, or is this just impossible to do?
3  Scripting / Script Snippets / Squirrel functions for encoding/decoding JSON on: November 16, 2016, 12:14:09 am
These functions, which are also included with my PHP/Web SDK allow you to encode Squirrel tables/arrays into JSON, and vice versa.

Code: [Select]
/*
PHP SDK server for Liberty Unleashed, by Rhytz
json_functions.nut - Contains custom JSON encoding and decoding functions
(c) 2016
*/


/*
json_encode( table/array )

Takes a table/array and encodes it into JSON
*/
function json_encode( table ){
if(typeof(table) == "table" || typeof(table)  == "array"){
json <-  "{";
json_recurse( table );
json += "}";
return json;
}else{
return false;
}
}

/*
json_recurse( table/array )

Recursive function that loops through all layers of a table/array and encodes it.
Used by json_encode().
*/
function json_recurse( table ){
local len = table.len();
local i = 1;
local typ = typeof(table);

//Loop through all keys in the supplied table
foreach (key, value in table) {
//Switch datatype of 'value' for appropriate handling
switch (typeof(value)){
//If the datatype is another array or table, run the function again
case "array":
json += "\"" + key + "\": [";
json_recurse( value );
json += "]";
break;

case "table":
json += "\"" + key + "\": {";
json_recurse( value );
json += "}";
break;

//Handling of other datatypes
case "bool":
local boolvalue = value ? "true" : "false";
//Depending on whether the current current 'table' is an array or table, change markup.
typ == "array" ? json += boolvalue : json += "\"" + key + "\": " + boolvalue;
break;

case "integer":
typ == "array" ? json += value : json += "\"" + key + "\": " + value;
break;

case "float":
typ == "array" ? json += value : json += "\"" + key + "\": " + value;
break;

case "null":
typ == "array" ? json += "null" : json += "\"" + key + "\": " + "null";
break;

case "string":
typ == "array" ? json += "\"" + json_escape(value) + "\"" : json += "\"" + key + "\":\"" + json_escape(value) + "\"";
break;

//Return false if the datatype is unsupported
default:
typ == "array" ? json += "false" : json += "\"" + key + "\": " + "false";
break;
}

//Append a comma at the end if it isn't the last item in the current table/array
if(i < len){
json += ",";
}
i++;
}
}

/*
json_escape( unescaped string )

Puts a backslash in front of any double quotes in the input string.
*/
function json_escape( string ){

//Split the input string on any double quotes
local breaks = split( string, "\"" );
local outputstring = "";

foreach (key, value in breaks) {
if(key > 0){
//Append backslash
outputstring += "\\\"" + value;
}else{
outputstring += value;
}
}

return outputstring;
}

/*
json_decode( JSON string )

Squirrel supports JSON syntax to create tables.
This function compiles the input string to Squirrel code.
Use with EXTREME caution and properly filter any user input, because it will also compile and execute any unescaped malicious code within the string.
*/
function json_decode( string ){
string = strip( string );
local json = ::compilestring("return " + string);
return json();
}
4  Scripting / Script Snippets / Squirrel functions for encoding GET/POST data on: November 16, 2016, 12:09:19 am
My PHP/Web SDK allows you do query your server from your website. But querying your website from your LU server in real time was not yet possible. Until now!

I've written some new functions that allow you to encode Squirrel strings and tables into "GET/POST" data which your webserver can understand.

postdata_encode( table ) - Converts a Squirrel table or array to a POST-able string
urlencode( string ) - Percent-encodes reserved URI characters in given string, and returns encoded string

Sample code:

Code: [Select]
function onScriptLoad(){



dofile( "Scripts/Rhytz/phpsdk/encode_functions.nut" );

SendSocket <- NewSocket( "ReceiveData" );

SendSocket.Connect("192.168.2.243", 2037 );

//SetLostConnFunc doesn't seem to work properly now, if SetNewConnFunc is set as well. Switching them around makes only SetLostConnFunc work and vice versa.
//SendSocket.SetLostConnFunc( "Failure" );
SendSocket.SetNewConnFunc( "Connected" );

}

function Connected( socket ){
local ServerData = {
GamemodeName = GetGamemodeName(),
MapName = GetMapName(),
ServerName = GetServerName(),
MaxPlayers = GetMaxPlayers(),
Players = GetPlayers(),
Password = GetPassword(),
Port = GetPort(),
AnArray = ["test1", "test2", "test3", ["arraywithinarray1","arraywithinarray2","arraywithinarray3"]],
MTUSize = GetMTUSize(),
falsevalue = false,
nullvalue = null
}
local data = postdata_encode(ServerData);
local path = "/rhytz/lu2.class.php";

    SendSocket.Send("POST " + path + " HTTP/1.0\r\n");
    SendSocket.Send("Content-Length: " + data.len() + "\r\n");
SendSocket.Send("Content-Type: application/x-www-form-urlencoded\r\n");
SendSocket.Send("\r\n");
SendSocket.Send(data);
}

function ReceiveData( socket, data ){
print(data); //Handle the returned webserver data here
}

The scripting functions:

Code: [Select]
/*
PHP SDK server for Liberty Unleashed, by Rhytz
encode_functions.nut - Contains custom URL encoding functions
(c) 2016
*/

/*
postdata_encode( table )

Converts a Squirrel table or array to POST-able data
*/
function postdata_encode( table ) {
if(typeof(table) == "table" || typeof(table)  == "array"){
postdata <-  "";
keyname <- "";
postdata_recurse( table );

return postdata;
}else{
return false;
}
}

/*
postdata_recurse( table )

Recursive function that loops through the table/array and encodes it.
Complementary function to postdata_encode().
*/
function postdata_recurse( table, depth = 0 ) {
local len = table.len();
local i = 1;
local typ = typeof(table);
//Loop through all keys in the supplied table
foreach (key, value in table) {
//Switch datatype of 'value' for appropriate handling
switch (typeof(value)){
//If the datatype is another array or table, run the function again
case "array":
case "table":
depth == 0 ? keyname  = key : keyname += "[" + key + "]";
postdata_recurse( value , depth + 1);
break;

//Handling of other datatypes
case "bool":
local boolvalue = value ? "true" : "false";
depth == 0 ? postdata += key + "=" + boolvalue : postdata += keyname + "[" + key + "]=" + boolvalue;
break;

case "integer":
case "float":
depth == 0 ? postdata += key + "=" + value : postdata += keyname + "[" + key + "]=" + value;
break;

case "null":
depth == 0 ? postdata += key + "=0"  : postdata += keyname + "[" + key + "]=0";
break;

case "string":
depth == 0 ? postdata += key + "=" + urlencode(value) : postdata += keyname + "[" + key + "]=" + urlencode(value);
break;

//Return false if the datatype is unsupported
default:
depth == 0 ? postdata += key + "=false" : postdata += keyname + "[" + key + "]=false";
break;
}

//Append a ampersand at the end if this isnt the last item in the table
if(i < len){
postdata += "&";
}

i++;
}
}


/*
urlencode( string )

Percent-encodes reserved URI characters in given string, and returns encoded string
*/
function urlencode( string ) {
local out = "";
for(local i=0; i<string.len(); i++){
local c = string.slice(i, i+1);
switch(c){
case " ":
out += "%20";
break;
case "!":
out += "%21";
break;
case "#":
out += "%23";
break;
case "$":
out += "%24";
break;
case "&":
out += "%26";
break;
case "'":
out += "%27";
break;
case "(":
out += "%28";
break;
case ")":
out += "%29";
break;
case "*":
out += "%2A";
break;
case "+":
out += "%2B";
break;
case ",":
out += "%2C";
break;
case "/":
out += "%2F";
break;
case ":":
out += "%3A";
break;
case ";":
out += "%3B";
break;
case "=":
out += "%3D";
break;
case "?":
out += "%3F";
break;
case "@":
out += "%40";
break;
case "[":
out += "%5B";
break;
case "]":
out += "%5D";
break;
default:
out += c;
break;
}
}
return out;
}

You can now access the $_POST parameter in your PHP script and handle the data.
5  Scripting / Script Releases / [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!
Pages: [1]
© Liberty Unleashed Team.