Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. package mysql.donate;
  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.  
  9. import com.ruseps.world.entity.impl.player.Player;
  10. import com.ruseps.util.Misc;
  11.  
  12. /**
  13. * Using this class:
  14. * To call this class, it's best to make a new thread. You can do it below like so:
  15. * new Thread(new Donation(player)).start();
  16. */
  17. public class Donation implements Runnable {
  18.  
  19. public static final String HOST = "162.218.48.215"; // website ip address
  20. public static final String USER = "rapidpkn_store";
  21. public static final String PASS = "Australia77";
  22. public static final String DATABASE = "rapidpkn_store";
  23.  
  24. private static int minimum = 500; // must match what u have set on the website!
  25. private static int maximum = 25000;
  26. private static double pricePerToken = 0.01; // must match what u have set on the website!
  27.  
  28. private Player player;
  29. private Connection conn;
  30. private Statement stmt;
  31.  
  32. public Donation(Player player) {
  33. this.player = player;
  34. }
  35.  
  36. @Override
  37. public void run() {
  38. try {
  39. if (!connect(HOST, DATABASE, USER, PASS)) {
  40. System.out.println("1");
  41. return;
  42. }
  43. System.out.println("2");
  44. String name = Misc.formatText(player.getUsername());
  45. ResultSet rs = executeQuery("SELECT * FROM donations WHERE user='"+name+"' AND claimed=0");
  46.  
  47. while (rs.next()) {
  48. System.out.println("3");
  49. int quantity = rs.getInt("quantity");
  50. double total = rs.getDouble("amount");
  51. double ratio = total / quantity;
  52.  
  53. if (quantity < minimum || quantity > maximum || ratio != pricePerToken) {
  54. System.out.println("Invalid purchase detected for "+name+"!");
  55. continue;
  56. }
  57. player.getPointsHandler().incrementDonatorPoints(quantity);
  58. player.getPointsHandler().refreshPanel();
  59. player.getPacketSender().sendMessage("Thank you for your donation! You have received "+Misc.format(quantity)+" Donator Points!");
  60. }
  61.  
  62. destroy();
  63. } catch (Exception e) {
  64. e.printStackTrace();
  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