This is a read only copy without any forum functionality of the old Modcraft forum.
If there is anything that you would like to have removed, message me on Discord via Kaev#5208.
Big thanks to Alastor for making this copy!

Menu

Author Topic: [TUTORIAL] Creating a Server-Status String for your Webpage  (Read 2081 times)

Valkryst

  • Moderators
  • Model Change Addict
  • *****
  • Posts: 224
    • View Profile
    • http://valkryst.com/blog/
[TUTORIAL] Creating a Server-Status String for your Webpage
« on: September 04, 2014, 07:05:36 pm »
Although this is quite simple, I felt that I should release it for those who may not have the time required to be able to setup something like this. I cannot fully support any questions regarding php as I only know enough to get by, but I can answer most questions regarding HTML and CSS and some Bash questions. If anyone notices an error in this guide or even some piece of code that can be optimized/changed, just comment and I'll do my best to keep this thread updated. Special thanks to my friend Tiq for helping me with all of the, admittedly, stupid errors and bugs that I encountered.

  • Just in case anyone wants to know what I'm running:
       
  • Windows 8, x64 -- Home
       
  • Debian 7 x32 -- Server
       
  • TrinityCore -- Latest version as of 18/August/2014
       
  • WoTLK 3.3.5a
As this guide is quite a bit more advanced than the simple DBC editing guides I've written before, I'm going to assume that anyone reading this has at least a basic knowledge of HTML, CSS, Bash, Linux have a web server up and running with php, are running MySQL for your servers database, and have a webpage already ready for the code I'm going to supply you with. I will not be going too in-depth with this guide as it only requires a few simple steps.

-----------------------------------------------------------------------------------------

The first thing you should do is navigate to, or create, a folder where you'll be storing both the script to gather the information required and the PHP file generated by the script. In this case I'll be using /path/to/valks/files/. Wherever you see /path/to/valks/files/, you'll need to replace it with the path to the directory you're using. I suggest saving the script file as UpdateStatistics.sh. The generated PHP file will be named currentStatistics.php.
Code: [Select]
#!/bin/bash

# Set the MySQL login info for ease of use:
mysqlUsername="putMySQLUsernameHere";
mysqlPassword="putMySQLPasswordHere";

# Get the total number of, currently online, players and then get the total number of players.
onlinePlayers=$(mysql --user=$mysqlUsername --password=$mysqlPassword --database=auth --skip-column-names --execute="SELECT COUNT(online) FROM account WHERE online = 1;");

totalPlayers=$(mysql --user=$mysqlUsername --password=$mysqlPassword --database=auth --skip-column-names --execute="SELECT COUNT(id) FROM account;");

# Get the unix time of when the server was last launched, and then get how many seconds the server
# has been online for, and then get the current unix time.
starttime=$(mysql --user=$mysqlUsername --password=$mysqlPassword --database=auth --skip-column-names --execute="SELECT MAX(starttime) FROM uptime;");

uptime=$(mysql --user=$mysqlUsername --password=$mysqlPassword --database=auth --skip-column-names --execute="SELECT uptime FROM uptime WHERE starttime = (SELECT MAX(starttime) FROM auth.uptime);");

currentUnixTime=$(date +%s);

##############################################################################33

# Create a string for the current number of online players.
stringA="There are currently $onlinePlayers / $totalPlayers players online.";

# Calculate the last time when the server was online based on starttime + uptime.
# Then subtract that from the current time and then decide whether or not to say
# if the server is online.
tempInt=$((starttime + uptime));
tempInt=$((currentUnixTime - tempInt));
tempInt=$((tempInt / 60));

# If the server was online less than 1 minutes ago then say that it's online.
# The uptime field of the uptime table updates every 10 minutes normally, but
# you can change that in worldserver.conf. Mine is set to update every minute.
stringB="Server Status: <span style="color: ";

if [ $tempInt -gt 2 ]
then
stringB+="#FF0000">Offline</span>"; # The hex value is for the color to display the word "Offline" as.
else
stringB+="#00FF00">Online</span>"; # The hex value is for the color to display the word "Online" as.
fi

# Construct the final String to be displayed on the webpage.
stringC="<p style="color:#FFCC00;text-align:center">$stringA || $stringB</p>";

# Output the constructed HTML into the specified php file.
echo "$stringC" > /path/to/valks/files/currentStatistics.php


After you've finished creating your UpdateStatistics.sh file, you'll need to do chmod a+x UpdateStatistics.sh to give it the necessary permissions to run. Now type crontab -e and enter the following line at the bottom of the file and save, but remember to change the path to point to your UpdateStatistics.sh file. The cronjob line below will run once per-minute, but you can change that to your liking.
Code: [Select]
* * * * * /path/to/valks/files/UpdateStatistics.sh
The next step is to get the generated string to appear on your webpage. Just enter the following line into your PHP webpage wherever you want the string to show up. If you have any issues with it not showing up then try removing the first slash at the beginning of the path.
Code: [Select]
&lt;?php include("/path/to/valks/files/currentStatistics.php"); ?&gt;Now for the final step. Open up your worldserver.conf file and set UpdateTimeInterval to 1. This will make sure that the script always has the latest data as-of a minute ago.
Code: [Select]
#
#    UpdateUptimeInterval
#        Description: Update realm uptime period (in minutes).
#        Default:     10 - (10 minutes)
#                     1+

UpdateUptimeInterval=1


After you've finished everything above, you should have a string that looks something like the following picture. If you wish to edit the colors of the text then just edit the hex color-values in the Bash script.

-----------------------------------------------------------------------------------------

After you've finished everything above, you should have a string that looks something like the following picture. If you wish to edit the colors of the text then just edit the hex color-values in the Bash script.



-----------------------------------------------------------------------------------------

If you want to stay up-to-date with my latest tutorials or if you just want an easier way to view all of my tutorials and releases in one place then take a look at my blog.
« Last Edit: January 01, 1970, 01:00:00 am by Admin »
MY BLOG IS NOW HERE.