Jump to content
Sign in to follow this  
Hope

RuneLocus Callback Script Integration

Recommended Posts

RuneLocus Callback Script Integration



Hey everyone, RuneLocus needed a basic tutorial on how to implement its unique voting callback feature, so here it is. Please let me know if I left anything out or made any errors.

0.1: Pre-Requirements:
 

  • A RuneLocus MyLocus account. If you do not have one, create one by clicking this link.
  • A place to host your voting script. This is usually done on a web-host.
  • A server on the toplist. If you don't have a server on the toplist, register for a MyLocus account (link above) then proceed to "Add Server" once logged in.



1: Downloading the Script

Having completed the pre-requirements listed above, proceed to download the PHP Callback Example located at this link. Once downloaded, un-zip the archive and go to the next step.

2: Uploading and Configuring the files on your web-host

Proceed to upload form.html and process.php to your webhost in a directory of your choice. Usually, users put these files in the desired voting directory. Example: http://yourserverwebsite.com/vote.

2.1: form.html:

Inside form.html, you will see the following form code.

<form action="http://www.runelocus.com/toplist/index.php" method="get">

<input type="hidden" name="action" value="vote" />

<input type="hidden" name="id" value="YOURSERVERID" />
Username: <input type="text" name="id2" placeholder="username" />
<input type="submit" value="Vote for us" />

</form>



You may use this form however you want (putting it in another file, on your homepage, ect) as long as it stays in a similar format and sends the value vote and an input with a name of id2.

Important Step: Where it says YOURSERVERID, replace that with your toplist server ID, which can be found in the following way: Navigate to your MyLocus account >> Where it lists your server, click on the name of your server which will take you to the toplist advertisement if your server >> examine the URL of the page you're currently on. >> It should list something like http://runelocus.com/toplist/details-NUMBERS-YOURSERVERNAME.html >> Your server ID will be the numbers listed in the url.

Here's another example to make sure you're getting the right number. The following URL is an advertisement of my old server.

[FONT=comic sans ms][SIZE=2][COLOR=#000000]http://runelocus.com/toplist/details-38108-Meridian.html
[/COLOR][/SIZE][/FONT]



In this case, my server ID would be 38108.

Don't forget to save your file after editing!

2.2: Creating a Database

For web-hosts that use c-panel only:

Navigate to your http://yourwebsite.com/cpanel and login. Scroll down until you see the category similar to the picture below:

After you find the "Databases" category, click on MySQL Databases (which is the first option). Once on that page, find where it says "Create New Database". I've named my new database as "vote" (shown by the picture below).
 


Finish by clicking "Create Database". Staying on the same page as before, locate at the bottom where it says "Create New User". Fill in your desired username (i've used "vote" once again) along with a password of your choice (shown below).



Finish by clicking "Create User". Staying on the same page once again, locate where it says "Add User to Database" which should be right below where you created your user. Select the vote database you created, and the matching username you created for it (shown below).


Finish by clicking "Add". It should direct you to a new page and ask for what privileges you'd like to assign. Simply click the "All Privileges" button located at the top and it should check mark everything. After done, click "Make Changes" at the bottom. Congratulations! You've made a database.

2.3: Creating a table for your database to use

In order for you to keep track of your voters, you'll need to create a table in your database which organizes and holds voter information. Here's how to do it!

Begin by navigating to your cPanel home (just like before) and in the "Databases" category (once again), click on "phpMyAdmin", which is the third button in the databases category. You will be redirected to a page that looks like this:



Once there, look on the left hand side. On the left, mine says "world987" which is the username of my host. Yours will be different of course depending on your website name/choice of username. Click the "+" which should open a list of your databases. You should see the new database you just created. It should read something like yourusername_vote, for example, mine reads "world987_vote". Click on that and since it's a new database it should look like this:


For the "name", enter "players". For the number of columns, enter the number "2". After clicking "Ok" at the bottom right, it should take you to a page that you might look confusing. That's alright! Simply copy what's in the below image and make your page look the same as mine.


After done, click "Save" in the bottom right. Congratulations once again! You've just created a place to store information for your validated voters.

The 'username' variable is self-explanatory. It holds the player's username.
The 'tokens' field holds how many validated votes the player has made.

3: Process.php:

Next, open up process.php. You should see the following code:
 

<?php


$con = mysql_connect("localhost","DATABASE_USERNAME","DATABASE_PASSWORD");
if (!$con) {
die("Could not connect to database: " . mysql_error());
}


mysql_select_db("DATABASE_NAME", $con);


$username = mysql_escape_string($_GET['usr']);


if (isset($_GET['usr'])) {
mysql_query("UPDATE players SET tokens = tokens + 10 WHERE username = '$username'");
// Above is an example, would add 10 vote tokens to the user.
}

mysql_close($con);

?>



In order for this code to work, we're going to have to edit a few things.

Locate:

$con = mysql_connect("localhost","DATABASE_USERNAME","DATABASE_PASSWORD");



If you remember your database username and password, place it in the corresponding places. If not, simply revisit the MySQL Databases page to collect your information. Here's what mine looks like:
 

$con = mysql_connect("localhost","world987_vote","password");



Notice how the username is placed before the database name. "world987_vote" Yours will be a different username however. Once you edit that scroll down and find:
 

mysql_select_db("DATABASE_NAME", $con);



Enter your database name. This is what mine looks like:
 

mysql_select_db("world987_vote", $con);



We're almost done! Hang in there.

In the same file, where it says:
 

if (isset($_GET['usr'])) {
mysql_query("UPDATE players SET tokens = tokens + 10 WHERE username = '$username'");
// Above is an example, would add 10 vote tokens to the user.
}



Replace that with:
 

 if (isset($_GET['usr'])) {
$result = mysql_query("SELECT * FROM players WHERE username = '$username'");
if(mysql_num_rows($result) == 0) {
mysql_query("INSERT INTO players (username, tokens) VALUES ('$username', 1);");
} else {
mysql_query("UPDATE players SET tokens = tokens + 1 WHERE username = '$username'");
}
}



What this code does: Whenever Runelocus sends a validation back to your process.php document, it will search for any entries in your database corresponding with the playername. If there is one in there, it will add +1 to the token count, and if there isn't any entries in it, it will insert the player's name along with 1 token into the database.

You're done this part, don't forget to save process.php!

4: Setting the callback url:

Navigate to your MyLocus account once again. Click on "Edit" next to your server listing. Scroll down and find the box where it says "Incentive Callback URL". This is where the URL to your process will be placed. For example, this link would be wherever you uploaded your process.php. Example:

This is how mine would be, since I placed my process.php in my 'vote' directory.

http://meridianrsps.com/vote/process.php



Once that's done, click save.

Congratulations! You now have a voting system that authenticates users. Direct the users to the page containing the form.html you edited earlier. They will proceed to enter their username and click the "vote" button which will redirect them to runelocus. After they have voted, RuneLocus will send information to your process.php file and update your database.


5: Claiming the Votes on your server:

Next, we have to set up our server so that players can claim their votes as a reward. Begin by going into your source and creating a new class called Vote. Copy and paste the below code into it.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;




public class Vote {


private static Connection con = null;
private static Statement stmt;

public static void createConnection() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String IP="WEB HOST IP";
String DB="DATABASE NAME";
String User="DATABASE USERNAME";
String Pass="DATABASE PASSWORD";
con = DriverManager.getConnection("jdbc:mysql://"+IP+"/"+DB, User, Pass);
stmt = con.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void claimVote(Player player) {
createConnection();
if (checkVote(player.getUsername())) {
int tokens = getVotes(player.getUsername()); //Number of times this user has voted. Number of unclaimed votes.

//methods for giving rewards to players goes here.
//for example, I might give a user 50,000 coins per 1 time they voted, so I would do:
player.getInventory().add(new Item(995, 50000 * tokens)); // adds 50,000 * token amount of coins to the player's inventory.


//remove votes after user as claimed.
removeVotes(player.getUsername());
} else {
player.sendMessage("You either have not voted, already claimed your reward, or there was an error.");
player.sendMessage("Either try again in a few minutes, or contact a staff member.");
}
}

private static ResultSet query(String s) throws SQLException {
try {
if (s.toLowerCase().startsWith("select")) {
ResultSet rs = stmt.executeQuery(s);
return rs;
} else {
stmt.executeUpdate(s);
}
return null;
} catch (Exception e) {
destroyConnection();
createConnection();
}
return null;
}


private static void destroyConnection() {
try {
stmt.close();
con.close();
} catch (Exception e) {


}
}


private static boolean checkVote(String playerName) {
try {
String name2 = playerName.replaceAll("_", " ");
Statement statement = con.createStatement();
String query = "SELECT * FROM players WHERE username = '" + name2 + "'";
ResultSet results = statement.executeQuery(query);
while(results.next()) {
int tokens = results.getInt("tokens");
if(tokens >= 0) {
return true;
}
}
} catch(SQLException e) {
e.printStackTrace();
}
return false;
}

private static int getVotes(String playerName) {
try {
String name2 = playerName.replaceAll("_", " ");
Statement statement = con.createStatement();
String query = "SELECT * FROM players WHERE username = '" + name2 + "'";
ResultSet results = statement.executeQuery(query);
while(results.next()) {
int tokens = results.getInt("tokens");
if(tokens >= 1) {
return tokens;
}
}
} catch(SQLException e) {
e.printStackTrace();
}
return 0;
}

private static boolean removeVotes(String playerName) {
try {
String name2 = playerName.replaceAll("_", " ");
query("DELETE FROM `players` WHERE username = '"+name2+"';");
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}

}



The above code is an example of a class that connects to your website database, checks to see if the user voted, and if he/she has, then it handles the rewards.

Begin by locating the following piece of code:
 

String IP="WEB HOST IP";
String DB="DATABASE NAME";
String User="DATABASE USERNAME";
String Pass="DATABASE PASSWORD";



Just as you did in process.php on your webhost, enter the correct details corresponding to your database. For the WEB HOST IP, simply use the IP of your web-host. If you do not know it, it is on the left hand side of the main page of your cPanel.

After that, scroll down and find the method claimVote, which should look like this:
 

public static void claimVote(Player player) {
createConnection();
if (checkVote(player.getUsername())) {
int tokens = getVotes(player.getUsername()); //Number of times this user has voted. Number of unclaimed votes.

//methods for giving rewards to players goes here.
//for example, I might give a user 50,000 coins per 1 time they voted, so I would do:
player.getInventory().add(new Item(995, 50000 * tokens)); // adds 50,000 * token amount of coins to the player's inventory.


//remove votes after user as claimed.
removeVotes(player.getUsername());
} else {
player.sendMessage("You either have not voted, already claimed your reward, or there was an error.");
player.sendMessage("Either try again in a few minutes, or contact a staff member.");
}
}



You'll need to properly change the syntax of certain things (such as the parameters of the method depending on what your sources uses as the player class, and the function of how to get the player's username.

Here's how you add rewards for the player:

Find:
 

int tokens = getVotes(player.getUsername()); //Number of times this user has voted. Number of unclaimed votes.

//methods for giving rewards to players goes here.
//for example, I might give a user 50,000 coins per 1 time they voted, so I would do:
player.getInventory().add(new Item(995, 50000 * tokens)); // adds 50,000 * token amount of coins to the
player's inventory.



It grabs the player's tokens from the database (if any), then stores it as an integer. You can use this any way you want to. For example, you'll see that right after it, I made the system add 50,000 coins times the number of votes in the database. The methods and classes in which your server operates upon will be different, so you'll need to customize this method a bit. It helps to use an IDE (like eclipse).

We're almost done! There's only a few steps left to do.

After saving Vote.class navigate to your webhost and go to the main page of the cPanel. Just as before, scroll down to the "Databases" category and select the "Remote MySQL" button. You'll see something like this:
 



In the box, enter the IP upon which your server is hosted. If you have a VPS, it will be the IP address of that, if you're hosting it on your home computer, you can put the IP address of your network. What does this do? It whitelists the IP so that connects can be made and your server can access the database files.

That's it!!!!

Generally users create a command called "claim" or "vote" that calls the Vote.claimVote(player); method, but feel free to do it any way you'd like.

Thanks,
Please let me know if I left out anything or if there's any errors.

Share this post


Link to post
Share on other sites
Hope to see it finished, looks great so far.

Share this post


Link to post
Share on other sites
[quote name='Cart']Hope to see it finished, looks great so far.[/QUOTE]

Just got off my lazy bum and finished it actually. :P

Share this post


Link to post
Share on other sites
Rarely see a tutorial like this, nicely done. Edited by Fall Out

Share this post


Link to post
Share on other sites
Maybe I missed it somewhere but you should also include the jar plugin for the SQL server sided :P If you did include it, my apologies.

Anyway, I like how this goes along with your suggestion on the 'Top Tutorials'. Looks great (y)

Share this post


Link to post
Share on other sites
[quote name='Citellum']Maybe I missed it somewhere but you should also include the jar plugin for the SQL server sided :P If you did include it, my apologies.

Anyway, I like how this goes along with your suggestion on the 'Top Tutorials'. Looks great (y)[/QUOTE]

Thanks!

Also, SQL management is part of the java libraries, so there's no external jar needed.

[code]
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
[/code]

[url]http://docs.oracle.com/javase/7/docs/api/java/sql/package-summary.html[/url]

Share this post


Link to post
Share on other sites
[quote name='Hope']Thanks!

Also, SQL management is part of the java libraries, so there's no external jar needed.

[code]
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
[/code]

[url]http://docs.oracle.com/javase/7/docs/api/java/sql/package-summary.html[/url][/QUOTE]

Oooooh didn't know this.

Share this post


Link to post
Share on other sites
im getting this error.

[QUOTE]src\com\rs2hd\packethandler\CommandPacketHandler.java:2766: error: cannot find s
ymbol
if(Vote.claimVote(player())) {
^
symbol: variable Vote
location: class CommandPacketHandler[/QUOTE]

Share this post


Link to post
Share on other sites
[quote name='dwall710']im getting this error.[/QUOTE]

It's because you haven't imported the class Vote to CommandPacketHandler. Or Vote.class doesn't exist. Either import it manually or get an IDE such as eclipse that will do it automatically for you.

Share this post


Link to post
Share on other sites
so I saved it as Vote.Java So that needs to bee imported to a .class file so ill have the Vote.Java and Vote.Class? If this is what your saying how would I do this manually? Feel free to tell me to google it, but i figured it would be easier to get an answer straight from the source.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×