Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

Web [TUT] Online Player List

Joined
Dec 15, 2009
Messages
1,387
Reaction score
236
This is the end of product:


Things you'll need to know before getting started:
1. Java
2. PHP
3. MySQL
3. A brain
4. It's Ducking simple.

--

Let's begin!

1. Open up your MySQL workbench and navigate to your datebase. Next, go to the setting of 'characters'.



  • Add a new column. Name it as loginState and set default field as 0.



  • Apply and that's done. All characters should have a new column.
  • Now we need to update that column to define whether a character is logged in or offline.
  • 0 = offline and 1 = online.


2. Insert below codes into MapleClient.java (you might ask where heck to put them? Where? ON WHICH FKING LINE!? Continue to read all the instructions and you'll discover it on your own.)

Code:
    public void charLoginState(int charLoginNewState) {
        try {
            Connection con = DatabaseConnection.getConnection();
            PreparedStatement ps = con.prepareStatement("UPDATE characters SET loginState = ? WHERE id = ?"); //update specific character's row based on character id
            ps.setInt(1, charLoginNewState); //charLoginNewState = 0 or 1
            ps.setInt(2, player.getId()); //get character id
            ps.executeUpdate();
            ps.close(); 
        } catch (SQLException e) {
            e.printStackTrace();
        }         
    }


3. Now we need to call charLoginState() for every time a user log in. Open up PlayerLoggedinHandler.java or similar named file depends of which repacks/source you are using. My case, I'll be using MoopleDev.

  • Add below codes into that particular file. Look for the proper lines to place the code. READ THROUGH THE ENTIRE CODES AND YOU'LL KNOW.

Code:
        c.charLoginState(1); //call for charLoginState() and define charLoginNewState as 1 which is online.


4. Now that if a character logs in, the column loginState will be set as 1. However, it will remains as 1, forever because no script is added to define whether the character has logged out or not.


5. Thus, open up MapleClient.java again. Look for player.logOff(); which live under public final void disconnect.

  • Obviously.....
Code:
charLoginState(0); //set column 'loginState' as 0 which is offline.


6. Keep in mind, the column will only update to 0 if the player logs out successfully and properly. So what happens if the server shutdown or dc? I'll give you hints.

  • Server.java
  • You'll need to figure out where to duplicate the codes and place them on the right spots to get the job done.


7. Oh right! Now, we'll need to output the characters which is online. I'll be using MapleBits. It should works on any CMS.

  • So if you're using MapleBits as well, navigate to sidebar.php. Use search if you don't know where it is located.
  • However, for those who uses any other CMS, you'll have to locate Server Info and find the appropriate file.
  • Look up for Server Info and insert the codes below inside a php tag.

Code:
$onlinePlayers = array(); //create an empty array
$sql = $mysqli->query("SELECT name from characters where loginState = 1"); //pull out characters which fulfill loginState as 1 which is online. 
$sonline = $sql->num_rows; //I've replaced the original $sonline from MapleBits so that it works more precisely. What this does is, look up for the number of rows which is online.

if ($sonline != 0) { //if online players not equals 0, the codes will execute.
while ($row = $sql->fetch_assoc()) { //looping through the rows
array_push($onlinePlayers, $row['name']); //pushing online players into array which we just created
}
}
$onlinePlayers = implode(", ", $onlinePlayers); //returns a string from the elements of an array and join each of them with a comma(,)


8. Finally, we'll echo out the online players.

Code:
<?php echo $onlinePlayers; ?>
 

Attachments

You must be registered for see attachments list
Last edited:
Mythic Archon
Joined
Jul 2, 2013
Messages
723
Reaction score
70

Nice Tutorial! This is good for smaller servers, but once the server is more popular I would suggest moving it towards its own page. Also if this were to load 30-50+ I would assume it would take a lot of time and memory to load right?
 
Joined
Dec 15, 2009
Messages
1,387
Reaction score
236
Nice Tutorial! This is good for smaller servers, but once the server is more popular I would suggest moving it towards its own page. Also if this were to load 30-50+ I would assume it would take a lot of time and memory to load right?
Not sure about that. Maybe it might. If so, setup a cronjob and run the script in X intervals. It will no longer be real time.

==
Alternate solutions would be restrict the script to loop 10 times only. If online user > 10, print "MORE" and add hyperlinks. Direct to new page > print out all online characters.
 
Last edited:
Mythic Archon
Joined
Jul 2, 2013
Messages
723
Reaction score
70
Not sure about that. Maybe it might. If so, setup a cronjob and run the script in X intervals. It will no longer be real time.

==
Alternate solutions would be restrict the script to loop 10 times only. If online user > 10, print "MORE" and add hyperlinks. Direct to new page > print out all online characters.


Another possibility is to have it auto update on its own (Example time: every 5 mins) and display what was updated "x" amount of minutes ago and have it posted at the bottom how many minutes its been since its updated and how many more till it updates again. This way its like they are loading just a normal php script without calling any superficial amount of data. We are speaking in terms of 20-30+ online.
 
Back
Top