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:
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:
/*
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.