Sunday, March 21, 2010

How To Add A Champions Online Server Status To Your Site

Champions Online is a MMORPG made by Cryptic. You can normally find the server status by looking at the top of the page of their official site or from the launcher (from what I've read). This post will allow you to either (1) embed a Champions Online Server Status page on your site or (2) make your own page using php and XML RPC.

Embed a Server Status on your site
Adding a server status to your site, using the page I provided, is pretty simple. Just add an iframe to your page widget using this code:
<div align="center">
<iframe src="http://motty.000space.com/champions/status.php?css=co.css" allowtransparency="true" align="top" scrolling="auto" frameborder="0" height="50" width="160"></iframe>
</div>
The frame height is set to only contain the "Server Status" and "Up" or "Down". Any additional text from a notice will make the window scrollable.

I've also included a way to choose several other stylesheets or add to add your own. The default stylesheet uses images on a dark background.



To make your own stylesheet, follow this basic template (this is the default CSS)
/* Overall page */
body {
background-color: #222;
margin: 0 auto;
font-family: Verdana, Helvetica, sans-serif;
color: #ddd;
text-align: center;
}
/* wraps contents */
.wrapper {
color: #ddd;
width: 130px;
padding: 5px;
text-align: center;
margin: 0 auto;
overflow-x: hidden;
}

/* Server Status text */
.serverStatus .text {
color: #ddd;
background: url(server-status.png) no-repeat;
text-align: center;
text-indent: -9999em; /* hide text if using images */
margin: 0 auto;
height: 20px;
width: 120px;
}

/* Server Status Up or Down */
.statusUp {
color: #ddd;
background: url(server-up.png) no-repeat;
text-align: center;
text-indent: -9999em; /* hide text if using images */
margin: 0 auto;
height: 20px;
width: 40px;
}
.statusDown {
color: #ddd;
background: url(server-down.png) no-repeat;
text-align: center;
text-indent: -9999em; /* hide text if using images */
margin: 0 auto;
height: 20px;
width: 75px;
}
.notice {
color: #ddd;
font-weight: bold;
}
Then name the file something like "co.css", upload it to your server and finally, point the iframe src css variable to the file:
http://motty.000space.com/champions/status.php?css=http://mysite.com/co.css
Add Your Own Server Status
This method shows you how to add your own server status using php and XML RPC (you'll need this extension installed on your server). I barely know php and I don't know XML RPC at all, so piecing this code together took some work and help from Diamonds who works at Cryptic. I'm just posting the result of that work and to hopefully keep Diamonds from being barraged with more questions about setting up a server status.

Check your phpinfo() and make sure you have the XMLRPC enabled

Here is the basic php required to obtain the server status:
# Using the XML-RPC extension to format the XML package
$request = xmlrpc_encode_request( "wgsLauncher.getServerStatus", $srvr );

$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_URL, "http://www.champions-online.com/xmlrpc.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
$result = curl_exec($ch);
curl_close($ch);

$method = null;
$params = xmlrpc_decode_request($result, &$method);

# server status result = true (up) or false (down)
$status = ($params['status']) ? 'Up' : 'Down';
$notice = ($params['notice'] == "") ? "" : "Notice: " + $params['notice'];