[RSPS TUT] Add a 508/525 Vote4cash

.Judgement

Onyx user!
Reputation
0
First off, make a folder called mysql in the codeusa folder. Inside the folder create a new class named Vote4cash.java inside that class paste this


Code:
/*
 * Class Vote4cash
 *
 * @author Taylor
 */

package net.com.codeusa.mysql;

import java.sql.*;
import net.com.codeusa.model.Player;
import java.security.MessageDigest;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.net.Socket;
import java.util.*;

public class Vote4cash {

	public static Connection con = null;
	public static Statement stm;

	public static void createConnection() {
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			con = DriverManager.getConnection("jdbc:mysql://127.0.0.1/vote", "root", "password");
			stm = con.createStatement();
		} 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 static void destroyCon() {
		try {
			stm.close();
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static boolean checkVotes(String username)
	{
		try {
			Statement statement = con.createStatement();
			String query = "SELECT * FROM Votes WHERE username = '" + username + "'";
			ResultSet results = statement.executeQuery(query);
			while(results.next()) {
				int recieved = results.getInt("recieved");
				if(recieved == 0)
				{
				return true;
				}
				
			}
		} catch(SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	public static boolean voteGiven(String username)
	{
		try
		{
			query("UPDATE Votes SET recieved = 1 WHERE username = '" + username + "'");
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
}

in Rs2LoginProtocol.java import


Code:
import net.com.codeusa.mysql.*;

Paste this in your login method after your character loads

Code:
				       if(Vote4cash.checkVotes(p.username)) {
            if(Vote4cash.voteGiven(p.username)) {
				p.vote =+ 1;
						for (Player allsee : Server.engine.players) {
							p.getActionSender().sendMessage(allsee, ""+p.username+"has voted "+p.vote+" times and been                                          rewarded.");					
                                Engine.playerItems.addItem(p, 995, 10000000); 
               p.getActionSender().sendMessage(p, "Thank you for voting for us!");
           }  
       }
   }

in Player.java declare


Code:
public int vote = 0;

Open Filemanager.java under the saveCharacter void paste


Code:
stream.writeString("vote:" + p.vote);

and in the loadCharacter void paste

Code:
						else if (line.startsWith("vote:"))
                    p.vote = Integer.parseInt(line.substring(5));
In Engine.java Import


Code:
import net.com.codeusa.mysql.*;

Search

Code:
public Engine()

and under that paste


Code:
Vote4cash.createConnection();

In your build folder you need to insert the com folder which you can get here http://www.mediafire.com/?sr9j79v26r0thch


Open up phpmyadmin and create a database named 'vote' and run this

Code:
CREATE TABLE IF NOT EXISTS `votes` (
  `username` char(50) DEFAULT NULL,
  `ip` char(50) DEFAULT NULL,
  `time` int(50) DEFAULT NULL,
  `recieved` int(1) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Now you are going to need the php & html files so create a file named vote.php
inside that file paste


Code:
<?php

$host="";
$username="root";
$password="password";
$db_name="vote";

$link = mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$username=$_POST['username'];
$voteid = ($_POST['votenum']);

$username = stripslashes($username);
$username = mysql_real_escape_string($username);

$time = time();
$ip = $_SERVER['REMOTE_ADDR'];

	if(clean_requests($time))
	{
		if(vote_entries($ip) == 0)
		{
			mysql_query("INSERT INTO Votes (username, ip, time, recieved) VALUES ('$username', '$ip', '$time', '0')", $link) or die("An internal error has occured please try again later.");
			header("Location: ".votenow($voteid)."");
		}
		else
		{
			die("You have already voted once today or you have port 80 open (usually skype) close it.<br/>To go back click <a href='index.php'><b>HERE</b></a>");
		}
	}
	else
	{
		die("An internal error has occured please try again later.<br/>To go back click <a href='index.php'><b>HERE</b></a>");
	}

function clean_requests($time)
{	
	$query = mysql_query ( "SELECT * FROM Votes");
	$i = 0;
	while($row = mysql_fetch_array($query))
	{
		$i++;
		$timerequested = $row['time'];
		if($time-$timerequested > 12*3600)
		{
			if (!mysql_query( "DELETE FROM Votes WHERE time='$timerequested'"))
			{
				return false;
			}
		}		
	}
	return true;	
}

function vote_entries($ip)
{	$entries = mysql_query ( "SELECT * FROM Votes WHERE ip = '$ip'" );
	if ( !$entries ) {
		die ( "Unable to get number of entries: " . mysql_error () );
	}
	return mysql_num_rows ( $entries );
}

function votenow($votenum)
{
	$url = "";
	if($votenum)
	{
		switch($votenum)
		{
			case 1:
				$url = "vote link here";
			break;

		default: die("Error!"); break;
		}
	}
	return $url;
}

?>

now create another file named vote.html and paste this into there



Code:
<html>
<title>server name here - Vote</title>
<center><img src="banner here" alt="server name here!"/>
<b><h3>Vote For server name here</h3></b>
<form name="form" method="post" action="vote.php">
Username:  <input name="username" type="text" id="username">
Vote #  <select name="votenum">
<option value="1">Vote for rewards!</option>
</select><br/><br/>
<input type="submit" value="Vote!"/>
</html>

Put those 2 files on your webhost/htdocs depending on what you are using.

When go to your vote.html page and enter their name in, it takes them to the toplist and once they vote & relog they will be rewarded.
 
Back
Top