I've recently been told that people don't know what goes into some of the stuff I do behind the scenes, so I thought it would be neat to discuss in detail what went into updating the Minecraft stats box, a relatively minor project that still had a good bit of work behind it. If there's interests in posts like these, I might make more in the future.
Restoring the Minecraft Stats BoxSince moving to our new Minecraft server host,
@trader has done the heavy lifting of making the server work and then making it run more efficiently. Thanks to him, I’ve had to do very little to get things going, and I greatly appreciate trader for everything he’s taken on to make the move work, not to mention for providing the hosting to start with!
One of the issues that required my attention though was updating the website code so that the Minecraft stats box on the forum index page would work with our new host. This box displays the server status and the players that are online, so you don’t actually have to open up Minecraft to see. Our old host had a custom REST API to get this data from, but this wasn’t the case with the new host, so we had to go about finding a new way of getting this data.
A few weeks back, trader gave me some links to libraries that use Minecraft’s Query protocol, which allows a server to broadcast basic information about the server. When I started working with them yesterday though, I ran into a big problem: these libraries use sockets to communicate with the Minecraft server, and our webhost (and most shared webhosting for that matter) doesn’t allow the full capacity to write to sockets in order to prevent people from running some demanding types of servers. I had hit a virtual wall.
Finding an Alternative SolutionMy options were to either give up on having server stats on the website or search for a webhost that didn’t have those restrictions. I didn’t like either option, so I went to Google to look for an alternative. Fortunately, I found two websites that do the querying for you and then provide the data in a nice REST API, and I decided to go with the one that allowed the data to be updated the quickest (every five minutes):
https://mcapi.us/Beyond just being the best technical solution, I found another reason for adopting mcapi. In reading the site, I came across the website of the person who created and runs it:
Syfaro, a purple fox who among his other projects has created a tool to upload furry art to multiple furry websites. After reading it, I have a good feeling about him. I’d like to think he’d feel at home in Wintreath, and that makes me more comfortable about using his tool. Maybe someday I’ll contact him in appreciation, or
buy him a cup of coffee.
Coding It All InAll that was to write the code that pulled the data from mcapi and displayed it on the forum page. The main challenge was making sure our website only pulled from the API at least every five minutes and stored that data on our end to use in the meantime. Technically, I could have pulled the data from the API everytime someone came to the forum and received cached data, but that’s hardly considerate to the mcapi server when it’s going to be the same data for five minutes. Ultimately, this is what happens when someone comes to the page:
- The script pulls the MC server data that’s stored on our server. This data includes the last time that the data was updated from the API.
- Using that time, the script checks to see if at least five minutes have passed. If it is not, it returns the stored data to the forum skin, and that’s what you see.
- If at least five minutes has passed, the script pulls fresh data from the API. In the process, it converts the array of player names we get from the API and converts it into a string complete with formatted commas and stores it as well. This is so that this doesn’t have to be done everytime someone comes to the page, instead it’s already stored and ready to go. This data is stored on our website and is what’s used for the next five minutes until we can get fresh data from the API yet again.
And that’s how the Minecraft server stats portion of the index page works.
I also changed it so that the stats are only shown to members that are signed in, in respect of our own server resources. Ever since we got dinged from our webhost for using too many CPU resources during Clickwar, I’ve tried to make sure that our pages run the minimum amount of code needed. The forum front page is the most visited page on the website, and most of the ‘guests’ aren’t even people but bots (usually search bots cataloging the forum). When we already require people to register on the forums to request MC permissions, is it worth the resources to show this to guests? I decided that it wasn’t, so guests will instead see a message asking them to sign in to view the stats.
Oh Yeah, lolHere’s the reason the old code always said the server was online, as
@taulover had already guessed. I guess I never got around to figuring out the entire API the old host was using. Thankfully, this one is much easier to use and is much better documented.
$mcServerStatus = 'Online';
if($mcServerStatus == 'Online')
$mcServerStatus = '<span style="color:green;">' . $mcServerStatus . '</span>';