Advertisement
Guest User

Store.java

a guest
Jan 29th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.86 KB | None | 0 0
  1. import java.io.BufferedWriter;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.text.DateFormat;
  10. import java.text.SimpleDateFormat;
  11. import java.util.Calendar;
  12.  
  13. import com.ventyz.server.character.Client;
  14.  
  15. // TODO: Auto-generated Javadoc
  16. /**
  17.  * The Class Store.
  18.  */
  19. public class Store {
  20.  
  21.     /*
  22.     * @author: Pax M
  23.     */
  24.  
  25.     /** The connection. */
  26.     private static Connection connection;
  27.  
  28.     /** The las connection. */
  29.     private static long lasConnection = System.currentTimeMillis();
  30.     static {
  31.         init();
  32.     }
  33.  
  34.     /**
  35.      * Claim payment.
  36.      *
  37.      * @param p
  38.      *            the p
  39.      * @param name
  40.      *            the name
  41.      */
  42.     public static void claimPayment(final Client p, final String name) {
  43.         try {
  44.             if (System.currentTimeMillis() - lasConnection > 10000) {
  45.                 destroyConnection();
  46.                 init();
  47.                 lasConnection = System.currentTimeMillis();
  48.             }
  49.             Statement s = connection.createStatement();
  50.             String name2 = name.replaceAll(" ", "_");
  51.             String query = "SELECT * FROM itemstore WHERE username = '" + name2
  52.                     + "'";
  53.             ResultSet rs = s.executeQuery(query);
  54.             boolean claimed = false;
  55.             while (rs.next()) {
  56.                 int prod = Integer.parseInt(rs.getString("productid"));
  57.                 int price = Integer.parseInt(rs.getString("price"));
  58.                 if (prod == 1 && price == 5) {
  59.                     /*
  60.                     * RETURN WITH REWARD 1
  61.                     */
  62.                     claimed = true;
  63.                 } else if (prod == 2 && price == 20) {
  64.                     /*
  65.                     * RETURN WITH REWARD 2
  66.                     */
  67.                     claimed = true;
  68.                 } else if (prod == 3 && price == 30) {
  69.                     /*
  70.                     * RETURN WITH REWARD 3
  71.                     */
  72.                     claimed = true;
  73.                 if (claimed) {
  74.                     s.execute("DELETE FROM `itemstore` WHERE `username` = '"
  75.                             + name2 + "';");
  76.  
  77.                     final String FILE_PATH = "data/logs/orders/orders.txt";
  78.                     try {
  79.                         Calendar cal = Calendar.getInstance();
  80.                         BufferedWriter writer = new BufferedWriter(
  81.                                 new FileWriter(FILE_PATH, true));
  82.                         writer.write("[" + cal.getTime() + "] - $" + price);
  83.                         writer.newLine();
  84.                         writer.flush();
  85.                         writer.close();
  86.                     } catch (IOException e) {
  87.                         System.err.println(e);
  88.                     }
  89.                 }
  90.             }
  91.         } catch (Exception e) {
  92.             e.printStackTrace();
  93.         }
  94.     }
  95.  
  96.     /**
  97.      * Destroy connection.
  98.      */
  99.     public static void destroyConnection() {
  100.         try {
  101.             connection.close();
  102.         } catch (SQLException e) {
  103.             e.printStackTrace();
  104.         }
  105.     }
  106.  
  107.     /**
  108.      * Inits the.
  109.      */
  110.     public static void init() {
  111.         try {
  112.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  113.  
  114.             connection = DriverManager.getConnection(
  115.                     "jdbc:mysql://scape-deadman.com/scape-hs",
  116.                     "scape-hs", "p~mr4sWVu3PW");
  117.         } catch (Exception e) {
  118.             e.printStackTrace();
  119.             System.err.println("Could not connect to Store SQL Database.");
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement