1. 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.

    [​IMG] [​IMG]


    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:
    Code:
    PLUGIN.Title = "MOTD"
    PLUGIN.Description = "Message of the day using web requests tutorial."
    PLUGIN.Author = "Norn"
    PLUGIN.Version = V(1, 0, 0)
    
    I then created a bunch of variables:
    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.
    
    Init:
    Code:
    function PLUGIN:Init()
        self:LoadDefaultConfig()
        command.AddChatCommand("motd", self.Plugin, "cmdMOTD")
    end
    
    Simply loading/generating configuration and adding the command "/motd"

    Now for the configuration:
    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
    
    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.

    MOTD command:
    Code:
    function PLUGIN:cmdMOTD(player, cmd, args)
        rust.SendChatMessage(player, "[" .. self.Title.. "] " .. self.Config.MOTD)
    end
    
    Now for the actual web request:
    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
    
    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.

    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)
    Code:
    function PLUGIN:OnServerInitialized()
        self:CallMOTD()
    endfunction PLUGIN:OnPluginUnloaded() -- Destroy timer
        if rtimer then
            rtimer:Destroy()
        end
    end
    
    Now for the web side of things:

    rd.php

    Code:
    <?php
        $file_name = "rust.motd";
        if (file_exists($file_name)) {
            echo(file_get_contents($file_name, true));
        }
    ?>
    
    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.

    Editing the motd from webpage (index.php)

    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>
    
    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.

    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 :)
     

    Attached Files:

    Last edited by a moderator: Aug 13, 2015
  2. Could you give me an example about running a console command ?
     
  3. If I could remember the actual way to send a console command in LUA I could give you a quick example yeah, I can't quite recall it atm haha.
     
  4. What i need is just send a command once someone presses a button on the website. Like once he presses "I Agree" then website will send the command "whitelist.add <playerName>" and then i handle the other stuff.
     
  5. I see I see, I can think of several concepts in my head. The question is really what way do you want to generate the data dynamically in php and which way you want to read the dynamically generated data in your lua plugin. That's personal preference really, if I was going to do something like that I would make two timers, one for reading and one for writing. Sending a string back and forth that you parse in the plugin/php script.

    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.
     
  6. 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
     
  7. Depends on preference and scale of the project I guess.
     
  8. 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
     
  9. Oh of course, but I'm referring to my example as it's just for a simple script

    Also the idea is to work on any free web host that allows php, a lot of them have stuff like that disabled.
     
  10. 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
     
  11. This is only a warning not an error.
     
  12. 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: