中文博客 (10) 美国陆军 (8) 部队生活 (7) English Blog (4) PHP (4) World of Warcraft (4) XML (2) IT (1) Network (1) Nginx (1) OIF (1) Rogue (1) Win 7 (1) 伊拉克 (1) 假期 (1) 笑话 (1)

Sunday, November 15, 2009

Realm Status PHP module

When I was create my guild website for World of warcraft, I want to have a realm status table in my site, so my guildies wouldn't need to get on wow official website, and check whetherf our realm has been up yet.

Searching through the web, we can find many little programs that people can download to their desktop so that they can monitor the realms status. But that is not what I want. I need a mod I can put on the website. Luckily, I found one from "PHPModule-WoW US Realm Status" from http://www.wowinterface.com.



But it is outdated.


Time to do modification for this piece of source code, so we can use it in the website.

Blizzard has changed couple images, xml data location, and some status code, such as RP/PVP server's code becomes #4. If we want to put server types into an array, it will be:
array(1 => '(Noraml)', 2 => '(PVP)', 3 => '(RP)', 4 => '(RPPVP)');
The up and down arrow images that wow official web site uses are at:
http://www.worldofwarcraft.com/shared/wow-com/images/icons/serverstatus/uparrow.gif
http://www.worldofwarcraft.com/shared/wow-com/images/icons/serverstatus/downarrow.gif

And the least and most important, the file where all realms' status is store at: http://www.worldofwarcraft.com/realmstatus/status.xml, this is where we gonna get all our information from.

New, we can do our magic.
Filename: realmstatus.php, can be download at http://sites.google.com/site/lnakuma/Home/script/realm.php?attredirects=0&d=1


<?php
$showallrealms = $_POST['all'];
?>
<table style = "border: 2px solid #000000;">
<tr>
<td><table width = "550" border = "0" cellpadding = "0" cellspacing = "0" align = "left">
<tr bgcolor = "#000000">
<td width = "100" style = "font-weight: bold; color: #ffffff; text-align: center;"> Status</td>
<td width = "200" style = "font-weight: bold; color: #ffffff;"> Realm Name</td>
<td width = "130" style = "font-weight: bold; color: #ffffff;"> Type</td>
<td width = "120" style = "font-weight: bold; color: #ffffff;"> Load</td>
</tr>
<?PHP
// This is a self contained script. All you need to do is add an inclued
// from the calling page.
//
// START -- User Customization Area
//
// Feel free to add additional realms to the $nmyrealms array
// NOTE --> array elements are case sensitive
$myrealms = array('Twisting Nether');
sort($myrealms);
$j = 0;
$flag = 0;
$realmcnt = 0;
$t = array(4 => '(RPPVP)', 1 => '(Noraml)', 2 => '(PVP)', 3 => '(RP)');
$tcol = array(4 => 'Blue', 1 => 'Green', 2 => 'DarkRed', 3 => 'Orange');
$l = array(1 => 'Low', 2 => 'Medium', 3 => 'High', 4 => 'Max');
$lcol = array(1 => 'Green', 2 => 'Yellow', 3 => 'Orange', 4 => 'Red');
$rawdata = @file_get_contents('http://www.worldofwarcraft.com/realmstatus/status.xml');
if (!$rawdata){
print '<b>Cannot connect to Realm(s)</b><br />';
} else {
$contents = explode("\n", $rawdata);
while (list ($key, $value) = each ($contents)) { // Retrieve the top 15 virus advisory list
if (stristr($value, '<r ')){
$realms[$j] = $value;
$realmcnt++;
$j++;
}
}
$status = range(0, $realmcnt-1);
$realm = range(0, $realmcnt-1);
$type = range(0, $realmcnt-1);
$load = range(0, $realmcnt-1);
for ($i = 0; $i < $realmcnt; $i++){
$contents = explode("\"", $realms[$i]);
$status[$i] = $contents[5];
if ( $status[$i] == 1 ) {
$s = '<img src="http://www.worldofwarcraft.com/shared/wow-com/images/icons/serverstatus/uparrow.gif" border = "0" align = "middle" alt = "Realm Up" />';
} else {
$s = '<img src="http://www.worldofwarcraft.com/shared/wow-com/images/icons/serverstatus/downarrow.gif" border="0" align = "middle" alt = "Realm Down" />';
}
$realm[$i] = $contents[1];
$type[$i] = $contents[3];
$load[$i] = $contents[7];
if ( $showallrealms == 1 ) {
$flag = PrintRealms($flag, $s, $realm[$i], $t[$type[$i]], $tcol[$type[$i]], $l[$load[$i]], $lcol[$load[$i]]);
} else {
for ($j = 0; $j < count($myrealms); $j++){
if ($realm[$i] == $myrealms[$j]){
$flag = PrintRealms($flag, $s, $realm[$i], $t[$type[$i]], $tcol[$type[$i]], $l[$load[$i]], $lcol[$load[$i]]);
}
}
}
}
}
?>
</table>
</td>
</tr>
</table>
<?php
function PrintRealms($flag, $s, $r, $t, $tcol, $l, $lcol){
if ( $flag == 1 ) {
$bg = "#acb6b8";
$flag = 0;
} else {
$bg = "#999999";
$flag = 1;
}
?>
<tr bgcolor="<?php print $bg;?>">
<td height="25" align = "center" style = "vertical-align: middle;"><?php print $s;?> </td>
<td height="25" style = "font-weight: bold; color: #114057; vertical-align: middle;"> <?php print $r;?></td>
<td height="25" style = "font-weight: bold; color: <?php print $tcol;?>; vertical-align: middle;"> <?php print $t;?></td>
<td height="25" style = "font-weight: bold; color: <?php print $tcol;?>; vertical-align: middle;"> <?php print $l;?></td>
</tr>
<?php
return $flag;
}
?>


Two method we can display our realm status.
1) Display all realms
2) Display specific realms

At the beginning,

<?php
$showallrealms = $_GET['all'];
?>

We will read through the URL to determine which way we would display our status. all=1, we display every realms, and all=0, we display the realm(s) which we select.
find in line 21:

$myrealms = array('Twisting Nether');

This is where we define which realm(s) we want to show individually. If we want more that one realm, we can simply add more realms name into the array:

$myrealms = array('Twisting Nether', 'Anvilmar');

Remember, some realm's name has a ' sign in it (Anub'arak), we need put like \' (Anub\'arak) to avoid confusion and error reading source code.

The PHP module is done, now we need it update itself repeatedly. AJAX will do the trick.
Download "Prototype.js" library from "http://www.prototypejs.org", we will use its Ajax.PeriodicalUpdater to implement periodic data update.

function selectRealmStatusLoader() {
var pars = 'all=0';
new Ajax.PeriodicalUpdater('realmStatus', 'realmstatus.php', {
method: 'post',
parameters: pars,
frequency: 2,
});
return;
}
function allRealmStatusLoader() {
var pars = 'all=1';
new Ajax.PeriodicalUpdater('realmStatus', 'realmstatus.php', {
method: 'post',
parameters: pars,
frequency: 2,
});
return;
}

As we can see, two function which selectRealmStatusLoader() will displays selected realms and allRealmStatusLoader() displays all.
Depends which method we want to use, we can put following code into our html file.

For selected realms: Demo: http://bamf.x10hosting.com

<script type="text/javascript">
//<![CDATA[
Event.observe(window, 'load', selectRealmStatusLoader, false);
//]]>
</script>
<div id = "realmStatus"></div>


And for all realms: Demo: http://bamf.x10hosting.com/?function=realmstatus

<script type="text/javascript">
//<![CDATA[
Event.observe(window, 'load', allRealmStatusLoader, false);
//]]>
</script>
<div id = "realmStatus"></div>


Now, we can display realm status in our guild website.





--------------------------
个人网页:http://akuma.x10hosting.com
魔兽公会网站:http://bamf.x10hosting.com

No comments:

Post a Comment