Purpose: To create a Server Status page like no other! See picture: [Only registered and activated users can see links. ]
Difficulty: 5?
Assumed Knowledge: Java, If you do not how MySQL works please do not go any futher.
Classes Modified: Process? Server? What ever your main loop thread is in...
Ok so first of all you will need the PHP file on your server and also connecting to your MySQL Database.
Code:
<?php
/**
* @author Stuart
* @copyright 2010
*/
$status = new status();
$status->getStatus();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Server Status Page</title>
<link rel="stylesheet" href="style.css" media="screen" />
</head>
<body>
<div id="wrapper">
<table width="100%" border="0" cellspacing="1" cellpadding="3" class="stats_table">
<tr class="stats_head">
<th class="stats_th" colspan="4">Server Status for SERVERNAME HERE</th>
</tr>
<tr>
<th class="stats_th">Server Status:</th>
<td class="stats_td"><?php echo $status->status; ?></td>
<th class="stats_th">Server Uptime:</th>
<td class="stats_td"><?php echo $status->uptime; ?></td>
</tr>
<tr>
<th class="stats_th">Online Players:</th>
<td class="stats_td"><?php echo $status->players; ?></td>
<th class="stats_th">Online Peak:</th>
<td class="stats_td"><?php echo $status->peek; ?></td>
</table>
<?php if($status->status != "offline" && $status->players != 0) { ?>
<table width="100%" border="0" cellspacing="1" cellpadding="2" class="stats_table">
<tr class="stats_head">
<th class="stats_th" colspan="6">Online Players</th>
</tr>
<tr>
<th class="stats_th">Name</th>
<th class="stats_th">Combat Level</th>
<th class="stats_th">Total Level</th>
<th class="stats_th">Total EXP</th>
<th class="stats_th">Rank</th>
<th class="stats_th">Highscores</th>
</tr>
<?php echo $status->getOnline(); ?>
</table>
<?php } ?>
</div>
</body>
</html>
<?php
class status
{
var $connected;
var $time;
var $status;
var $uptime;
var $players;
var $peek;
function status()
{
$this->connected = false;
$this->time = time();
}
function getStatus()
{
$query = $this->query("SELECT * FROM `status`");
$row = mysql_fetch_array($query);
$status = $row['status'];
$timerequested = $row['time'];
if ($this->time - $timerequested > 600) {
$this->updateStatus();
$query = $this->query("SELECT * FROM `status`");
$row = mysql_fetch_array($query);
$status = $row['status'];
}
$this->peek = $row['peek'];
if ($status == 0) {
$this->status = "offline";
$this->uptime = "N/A";
$this->players = "N/A";
} else {
$this->status = "online";
$this->uptime = $row['servertime'];
$this->players = $row['online'];
}
}
function updateStatus()
{
$checkport = @fsockopen("127.0.0.1", 3424, $errnum, $errstr, 2);
if ($checkport) {
$this->query("UPDATE status SET status = '1', time = '" . $this->time . "'");
} else {
$this->query("UPDATE status SET status = '0', time = '" . $this->time . "'");
}
}
function getOnline()
{
$content = "";
$result = $this->query("SELECT * FROM playersonline");
while($players = mysql_fetch_assoc($result)) {
$content .= "\t\t\t<tr>\r";
$content .= "\t\t\t\t<td class=\"stats_td\">".$players['name']."</td>\r";
$content .= "\t\t\t\t<td class=\"stats_td\">".$players['combat']."</td>\r";
$content .= "\t\t\t\t<td class=\"stats_td\">".$players['total']."</td>\r";
$content .= "\t\t\t\t<td class=\"stats_td\">".$players['exp']."</td>\r";
if($players['level'] == 0) {
$content .= "\t\t\t\t<td class=\"stats_td\">Player</td>\r";
} elseif($players['level'] == 1) {
$content .= "\t\t\t\t<td class=\"stats_td\">Moderator</td>\r";
} elseif($players['level'] == 2) {
$content .= "\t\t\t\t<td class=\"stats_td\">Admin</td>\r";
} else {
$content .= "\t\t\t\t<td class=\"stats_td\">Owner</td>\r";
}
$content .= "\t\t\t\t<td class=\"stats_td\"><a href=\"http://domain.com/highscores/index.php?name=".$players['name']."\">Highscores ".$players['name']."</a></td>\r";
$content .= "\t\t\t</tr>\r";
}
return $content;
}
function connect()
{
if ([email protected]_connect("127.0.0.1", "username", "password")) {
$this->error("Error connecting to database!");
}
if ([email protected]_select_db("databasename")) {
$this->error("Could not select database!");
}
$this->connected = true;
}
function query($query)
{
if (!$this->connected)
$this->connect();
$qd = mysql_query($query);
if (!$qd)
$this->error("Could not run query: " . $query);
return $qd;
}
function error($err)
{
die($err);
}
}
?>
You will need to edit the function:
Code:
function connect()
{
if ([email protected]_connect("127.0.0.1", "username", "password")) {
$this->error("Error connecting to database!");
}
if ([email protected]_select_db("databasename")) {
$this->error("Could not select database!");
}
$this->connected = true;
}
Replace username, password, databasename with your MySQL login details
Once you have that on your server you are going to require the tables in which the script and server work with to store information, I suggest using phpmyadmin, mysql console or something which will enable you to run querys.
Code:
CREATE TABLE `playersonline` (
`name` varchar(12) NOT NULL,
`total` int(20) NOT NULL,
`exp` int(20) NOT NULL,
`level` mediumint(1) NOT NULL,
`combat` int(3) NOT NULL
)
INSERT INTO `playersonline` (`name`, `total`, `exp`, `level`, `combat`) VALUES
('noob', 0, 0, 0, 0);
CREATE TABLE `status` (
`status` int(1) default NULL,
`time` int(15) default NULL,
`servertime` varchar(255) NOT NULL,
`online` int(10) NOT NULL,
`peek` int(10) NOT NULL
)
INSERT INTO `status` (`status`, `time`, `servertime`, `online`, `peek`) VALUES
(1, 1268827223, '0 Minutes', 0, 0);
Once you have done that the script you have uploaded to your server should work.
Put this style.css in the same directory as the script change the colors / look to what you want...
Now to actually update these stats server sided...
In server define:
Code:
public static MySQL sql = new MySql();
This creates a basic refrance to the class we are going to use.
Next the class itself
Code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MySQL {
public static Connection con = null;
public static Statement stm;
public static void destroyCon() {
try {
stm.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static ResultSet query(String s) throws SQLException {
try {
if (s.toLowerCase().startsWith("select")) {
ResultSet rs = stm.executeQuery(s);
return rs;
} else {
stm.executeUpdate(s);
}
return null;
} catch (Exception e) {
Misc.println("MySQL Error:" + s);
e.printStackTrace();
}
return null;
}
public MySQL() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1/databasename", "username", "password");
stm = con.createStatement();
updateStats();
} catch (Exception e) {
e.printStackTrace();
}
}
public void updateStats() {
try {
query("UPDATE status SET servertime = '"
+ Misc.getUptime(Settings.START_TIME) + "', online = '"
+ PlayerHandler.getPlayerCount() + "'");
Statement statement = con.createStatement();
String query = "SELECT * FROM status";
ResultSet results = statement.executeQuery(query);
while (results.next()) {
int amount = results.getInt("peek");
if (amount < PlayerHandler.getPlayerCount())
query("UPDATE status SET peek = '"
+ PlayerHandler.getPlayerCount() + "'");
}
query("DELETE FROM playersonline");
for (Player p : PlayerHandler.players) {
if (p == null || !p.isActive)
continue;
Client temp = (Client) p;
if ((temp != null) && !temp.disconnected && p.isActive) {
query("INSERT INTO playersonline values('"
+ temp.playerName + "', '" + temp.getTotalLevel()
+ "', '" + temp.getTotalXP() + "', '"
+ temp.playerRights + "', '" + temp.combatLevel
+ "')");
// HIGH SCORES IF YOU HAVE THEM ?saveHighScore(temp);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
You will need to edit:
Code:
con = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1/databasename", "username", "password");
Place your mysql details.
That's about it now this alters on how you want to do it, to update the stats I simply have used grahams event manager...
Code:
// in server .java then run it when the server starts...
public static void serverTimer() {
EventManager.getSingleton().addEvent(new Event() {
public void execute(EventContainer c) {
//System.gc();
Server.sql.updateStats();
//ConnectionHandler.clearConnections();
}
}, 600000);
}
You may need to declare these / change the current functions to these
Code:
public int getTotalLevel() {
int level = 0;
for (int i = 0; i < 21; i++) {
level += getLevelForXP(playerXP[i]);
}
sendQuest("Total Lvl: " + total + "", 3984);
return level;
}
public int getTotalXP() {
int exp = 0;
for (int i = 0; i < 21; i++) {
exp += getXPForLevel(playerXP[i]);
}
return exp;
}
MySQL driver: [Only registered and activated users can see links. ]
No other RuneScape server has anything close to this, I entirely coded this myself, feel free to leech or change to how you want, but if you repost this then please do leave the full url to this post and my name "Stuart" / "RogueX" in the post.
Need any help post and ill try to assist you.
NOTE: YOU MAY WANT TO REJECT YOUR WEBSERVER URL ON YOUR SERVER AS IT MAY CAUSE NULLING ON CRAPPY SERVERS! if(connection.contains(hostname)) socket.close();
I personally do not feel the need to make this look attractive to users of the community it is there for simply viewing and as a demo illustration of what the script achives. Like I said change to how you like it the back end is there for you all to use...