Show Posts
|
Pages: 1 [2] 3
|
19
|
Scripting / Script Help / Re: CreateVehicle
|
on: November 16, 2016, 05:54:08 pm
|
I've tested your code and it works just fine. Maybe some other script is causing trouble in combination with this one?
|
|
|
20
|
Scripting / Script Snippets / Re: Squirrel functions for encoding GET/POST data
|
on: November 16, 2016, 12:35:16 am
|
To separate the HTTP header from the data returned by your webserver, you can do something like this:
function ReceiveData( socket, data ){ local newline = data.find("\r\n\r\n"); local header = data.slice(0, newline); local content = data.slice(newline + 4, data.len()); print(content); }
|
|
|
21
|
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.
/* 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(); }
|
|
|
22
|
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:
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.
|
|
|
24
|
Scripting / Script Releases / Re: [REL]PHP/Web SDK 0.1 - Interact with your LU server through PHP!
|
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.
|
|
|
25
|
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 samplesConnecting 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 upNote that some knowledge of Squirrel and PHP would be very useful... 1. Download the Squirrel script and PHP Class2.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 functionsAlthough 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. 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 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!
|
|
|
30
|
Archive / Archived Items / Re: Latest News
|
on: January 12, 2008, 10:51:16 pm
|
I just have one question. When is the LU realesed? I can't wait for it too be. I have seen soo many movies about it. Then I register too Liberty-unleashed.co.uk and what? Liberty Unleashed 20%??? When ?? Btw I love the idea
i dont think the release date is known. a release date cant be set, because bugs will always be found at the last moment.
|
|
|
|