What exactly does this mean?
I'm going to teach you how you can create simple php scripts and plugins that allow you to do some pretty cool stuff.
What's useful about that?
Well for example you can do stuff like give players items, teleport them, do whatever essentially directly from your website which doesn't need to be hosted on the same server as your gameserver.
What am I going to achieve? This.
Difficulty Level: **
The example I will be using in this tutorial will be a MOTD (message of the day) that you can edit via your web page and load directly in game. It pulls a string from the web page and then saves it in the motd configuration file until the timer is called and updates the string again. Players will be able to access the motd by typing "/motd".
I will show you the process I used to create this plugin & php script but I'm not going to get in depth about PHP as it's irrelevant. PHP is heavily documented.
I started with a basic plugin:
I then created a bunch of variables:Code:PLUGIN.Title = "MOTD" PLUGIN.Description = "Message of the day using web requests tutorial." PLUGIN.Author = "Norn" PLUGIN.Version = V(1, 0, 0)
Init:Code:local response_string = nil -- This is the string that the response (MOTD) will be stored in. local receiveinterval = 10800 -- Timer interval, currently 3 hours. local default_motd = "MOTD hasn't been loaded to webserver." -- Default motd on plugin load. local url = "http://127.0.0.1/index/rd.php" -- URL which gets requested. local rtimer = nil -- Used for identifying timer for destruction.
Simply loading/generating configuration and adding the command "/motd"Code:function PLUGIN:Init() self:LoadDefaultConfig() command.AddChatCommand("motd", self.Plugin, "cmdMOTD") end
Now for the configuration:
The configuration I used was just a quick simple example, essentially there are two variables. MOTD and FirstUse. FirstUse is just set to default (true) when the plugin creates the configuration file.Code:function PLUGIN:LoadDefaultConfig() self.Config.FirstUse = self.Config.FirstUse or "true" self.Config.MOTD = self.Config.MOTD or default_motd if(self.Config.FirstUse == "true") then self.Config.FirstUse = "false" self:SaveConfig() end end
MOTD command:
Now for the actual web request:Code:function PLUGIN:cmdMOTD(player, cmd, args) rust.SendChatMessage(player, "[" .. self.Title.. "] " .. self.Config.MOTD) end
This function gets called once the entire script as when it's called a timer is started (at the receiveinterval), it checks FirstUse, if not then run timer. This is just an extra precaution.Code:function PLUGIN:CallMOTD() if(self.Config.FirstUse == "false") then rtimer = timer.Repeat(receiveinterval, 0, function() webrequests.EnqueueGet(url, function(code, response) if response == nil or code ~= 200 then print("Could not retrieve MOTD from webserver.") return end response_string = tostring(response) print("MOTD has been loaded from web server: "..response_string) self.Config.MOTD = response_string self:SaveConfig() end, self.Plugin) end, self.Plugin) end end
After the request calls successfully, it stores the data in response_string. It then updates the MOTD config variable with said string before saving configuration.
Call timer on ServerInit (not plugin load, just incase plugin is loaded at startup)
Now for the web side of things:Code:function PLUGIN:OnServerInitialized() self:CallMOTD() endfunction PLUGIN:OnPluginUnloaded() -- Destroy timer if rtimer then rtimer:Destroy() end end
rd.php
Probably the easiest part, literally it does is open the file I store the message of the day in (rust.motd) and outputs it. This is what will be read by the plugin.Code:<?php $file_name = "rust.motd"; if (file_exists($file_name)) { echo(file_get_contents($file_name, true)); } ?>
Editing the motd from webpage (index.php)
What this does is checks if "btnSubmit" is clicked, if so it then checks if the textbox (txtMOTD) is not empty. If it's not empty then write the contents of txtMOTD to our "rust.motd" file. I also have a check to see if the "rust.motd" file exists upon loading of "index.php" if not, create it.Code:<?php $file_name = "rust.motd"; if(isset($_POST['btnSubmit'])) { if(strlen($_POST['txtMOTD']) >= 1) { $username = $_POST['txtMOTD']; $file = fopen($file_name,"w"); fwrite($file, "[".date("Y-m-d")."] ".$_POST['txtMOTD']); fclose($file); } } if(!file_exists($file_name)) { // Default Configuration $file = fopen($file_name,"w"); fwrite($file,"MOTD has not been set."); fclose($file); } else { $motd = file_get_contents($file_name, true); } ?><form action="" method="post"> <input id="txtMOTD" name="txtMOTD" class="_input" type="text"> <button id="btnSubmit" name="btnSubmit" class="_button">Edit MOTD</button> </form>
That's it, very simple yet very effective. If you get into a habit of using this sort of method you can create some pretty interesting stuff. I have scripts where I can give items/bps to people that are currently ingame and online, set hostname etc. Possibilities are endless.
I hope some of you find this interesting, if interest ensues I'll maybe post a more in depth tutorial. Perhaps interacting with live players using requests.
I've also attached my full plugin including the web script below
[TUTORIAL] Execute Actions from WebRequest [LUA/PHP]
Discussion in 'Rust Development' started by Norn, Jul 14, 2015.
-
Attached Files:
Last edited by a moderator: Aug 13, 2015 -
-
Could you give me an example about running a console command ?
-
-
-
That's pretty vague I know, I can maybe try a small example when I get on my pc later.
EDIT:
Or it can be done on one timer, just need to remove the old playername from whatever the php is echoing.
The goal here would be to make the php script do as much work as possible, to take strain off the gameserver. -
Norn.....why not just use pear scripts....im sure php has an ftp script and there are several RCON classes available - this could be done total;ly web side aapart from maybe getting the list of plugins
-
-
im sure if you are wanting to offload from the game server, youd be better using requests from the webserver rather than the game server
-
Also the idea is to work on any free web host that allows php, a lot of them have stuff like that disabled. -
I had a problem in the installation when I run the script. someone can give a tip?
Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in C:\xampp\htdocs\index\index.php on line 7 -
-
I configured with Zend Server and seems to work right, but that does not send messages in the chat. may be you need to install something else on windows?
Attached Files:
-