Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.52 KB | None | 0 0
  1. package com.vencillio.core.network.mysql;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.concurrent.ExecutorService;
  9. import java.util.concurrent.Executors;
  10.  
  11. import com.vencillio.VencillioConstants;
  12. import com.vencillio.core.util.Utility;
  13. import com.vencillio.rs2.entity.World;
  14. import com.vencillio.rs2.entity.item.Item;
  15. import com.vencillio.rs2.entity.player.Player;
  16. import com.vencillio.rs2.entity.player.net.Client;
  17. import com.vencillio.rs2.entity.player.net.out.impl.SendMessage;
  18. public class VoteUpdater implements Runnable {
  19.  
  20.     public static final String HOST = "";
  21.     public static final String USER = "";
  22.     public static final String PASS = "";
  23.     public static final String DATABASE = "";
  24.  
  25.     private Player player;
  26.     private Connection conn;
  27.     private Statement stmt;
  28.  
  29.     public VoteUpdater(Player player) {
  30.         this.player = player;
  31.     }
  32.  
  33.     @Override
  34.     public void run() {
  35.     if ((System.currentTimeMillis() - player.getLastRequestedLookup()) < 30000) {
  36.         player.send(new SendMessage("Sorry, you can only check your votes once per 30 seconds."));
  37.         return;
  38.     }
  39.         try {
  40.             if (!connect(HOST, DATABASE, USER, PASS)) {
  41.                 return;
  42.             }
  43.             player.setLastRequestedLookup(System.currentTimeMillis());
  44.             String name = player.getUsername().replace(" ", "_");
  45.             ResultSet rs = executeQuery("SELECT * FROM fx_votes WHERE username='"+name+"' AND claimed=0 AND callback_date IS NOT NULL");
  46.             while (rs.next()) {
  47.                 String timestamp = rs.getTimestamp("callback_date").toString();
  48.                 String ipAddress = rs.getString("ip_address");
  49.                 int siteId = rs.getInt("site_id");
  50.                 player.voteAmount ++;
  51.                 System.out.println("Vote claimed by "+name+". (sid: "+siteId+", ip: "+ipAddress+", time: "+timestamp+")");
  52.                 rs.updateInt("claimed", 1); // do not delete otherwise they can reclaim!
  53.                 rs.updateRow();
  54.             }
  55.             if (player.voteAmount >= 1) {
  56.                 player.send(new SendMessage("You have received "+player.voteAmount+" vote tickets for voting!"));
  57.                 player.getInventory().add(4067, player.voteAmount);
  58.                 player.voteAmount = 0;
  59.                 World.sendGlobalMessage("<img=1><col=FFDF00>"+ player.getUsername() + " has voted and claimed their reward!</col>");
  60.             }
  61.             destroy();
  62.         } catch (Exception e) {
  63.             e.printStackTrace();
  64.         }
  65.     }
  66.  
  67.  
  68.     public boolean connect(String host, String database, String user, String pass) {
  69.         try {
  70.             this.conn = DriverManager.getConnection("jdbc:mysql://"+host+":3306/"+database, user, pass);
  71.             return true;
  72.         } catch (SQLException e) {
  73.             System.out.println("Failing connecting to database!");
  74.             return false;
  75.         }
  76.     }
  77.  
  78.     public void destroy() {
  79.         try {
  80.             conn.close();
  81.             conn = null;
  82.             if (stmt != null) {
  83.                 stmt.close();
  84.                 stmt = null;
  85.             }
  86.         } catch(Exception e) {
  87.             e.printStackTrace();
  88.         }
  89.     }
  90.  
  91.     public int executeUpdate(String query) {
  92.         try {
  93.             this.stmt = this.conn.createStatement(1005, 1008);
  94.             int results = stmt.executeUpdate(query);
  95.             return results;
  96.         } catch (SQLException ex) {
  97.             ex.printStackTrace();
  98.         }
  99.         return -1;
  100.     }
  101.  
  102.     public ResultSet executeQuery(String query) {
  103.         try {
  104.             this.stmt = this.conn.createStatement(1005, 1008);
  105.             ResultSet results = stmt.executeQuery(query);
  106.             return results;
  107.         } catch (SQLException ex) {
  108.             ex.printStackTrace();
  109.         }
  110.         return null;
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement