Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
12 kB
3
Indexable
Never
package osv.services.database;

import osv.model.entity.player.Player;
import osv.model.entity.player.PlayerHandler;

import java.sql.*;

/**
 * Using this class:
 * To call this class, it's best to make a new thread. You can do it below like so:
 * new Thread(new Donation(player)).start();
 */
public class StripeStore implements Runnable {

    public static final String HOST = "198.12.12.226"; // website ip address
    public static final String USER = "rigourst_store";
    public static final String PASS = "G{Vo(pR[)cFHx$#3lo";//";erjoKj(xi&{";
    public static final String DATABASE = "rigourst_webstore";

    private Player player;
    private Connection conn;
    private Statement stmt;

    /**
     * The constructor
     *
     * @param player
     */
    public StripeStore(Player player) {
        this.player = player;
    }

    @Override
    public void run() {
        try {
            if (!connect(HOST, DATABASE, USER, PASS)) {
                return;
            }
            boolean announced = false;
            boolean donated = false;
            String name = player.playerName.replace("_", " ");
            ResultSet rs = executeQuery("SELECT * FROM payments WHERE player_name='" + name + "' AND status='paid' AND claimed=0");

            while (rs.next()) {
                int item_number = rs.getInt("item_number");
                int quantity = rs.getInt("quantity");

                switch (item_number) {// add products according to their ID in the ACP

                    case 1: // $10 bond
                        player.getItems().addItemUnderAnyCircumstance(13190, quantity);
                        player.sendMessage("Thanks for your donation! You have received: " + quantity + "x 10 Bond.");
                        break;
                    case 2: // $25 bond
                        player.getItems().addItemUnderAnyCircumstance(23135, quantity);
                        player.sendMessage("Thanks for your donation! You have received: " + quantity + "x 25 Bond.");
                        break;
                    case 3: // $50 bond
                        player.getItems().addItemUnderAnyCircumstance(23136, quantity);
                        player.sendMessage("Thanks for your donation! You have received: " + quantity + "x $50 Bond.");
                        break;
                    case 4: // $100 bond
                        player.getItems().addItemUnderAnyCircumstance(23138, quantity);
                        player.sendMessage("Thanks for your donation! You have received: " + quantity + "x $100 Bond.");
                        break;
                    case 5: // $250 bond
                        player.getItems().addItemUnderAnyCircumstance(23139, quantity);
                        player.sendMessage("Thanks for your donation! You have received: " + quantity + "x $250 Bond.");
                        break;
                    case 6: // $500 bond
                        player.getItems().addItemUnderAnyCircumstance(23140, quantity);
                        player.sendMessage("Thanks for your donation! You have received: " + quantity + "x $500 Bond.");
                        break;
                    case 101: // mbox x5
                        player.getItems().addItemUnderAnyCircumstance(6199, 5 * quantity);
                        player.sendMessage("Thanks for your donation! You have received: " + quantity * 5 + "x rigour box.");
                        player.amDonated += 20 * quantity;
                        break;
                    case 107: // wildy key x5
                        player.getItems().addItemUnderAnyCircumstance(13302, 5 * quantity);
                        player.sendMessage("Thanks for your donation! You have received: " + 5 * quantity + "x wilderness chest key.");
                        player.amDonated += 20 * quantity;
                        break;
                    case 102: // mbox x10
                        player.getItems().addItemUnderAnyCircumstance(6199, 10 * quantity);
                        player.sendMessage("Thanks for your donation! You have received: " + 10 * quantity + "x rigour box.");
                        player.amDonated += 40 * quantity;
                        break;
                    case 108: // wildy key
                        player.getItems().addItemUnderAnyCircumstance(13302, 10 * quantity);
                        player.sendMessage("Thanks for your donation! You have received: " + 10 * quantity + "x wilderness chest key.");
                        player.amDonated += 40 * quantity;
                        break;
                    case 7: // easter mbox x5
                        player.getItems().addItemUnderAnyCircumstance(25206, quantity * 5);
                        player.sendMessage("Thanks for your donation! You have received: " + 5 * quantity + "x easter mystery box.");
                        player.amDonated += 20 * quantity;
                        break;
                    case 8: // easter mbox x10
                        player.getItems().addItemUnderAnyCircumstance(25206, 10 * quantity);
                        player.sendMessage("Thanks for your donation! You have received: " + 10 * quantity + "x easter mystery box.");
                        player.amDonated += 40 * quantity;
                        break;
                    case 1043: // void set
                        player.getItems().addItemUnderAnyCircumstance(8839, quantity);
                        player.getItems().addItemUnderAnyCircumstance(8842, quantity);
                        player.getItems().addItemUnderAnyCircumstance(8840, quantity);
                        player.getItems().addItemUnderAnyCircumstance(11663, quantity);
                        player.getItems().addItemUnderAnyCircumstance(11664, quantity);
                        player.getItems().addItemUnderAnyCircumstance(11665, quantity);
                        player.sendMessage("Thanks for your donation! You have received: " + quantity + "x void set.");
                        player.amDonated += 50 * quantity;
                        break;
                    case 1021:
                        player.getItems().addItemUnderAnyCircumstance(11863, quantity);
                        player.sendMessage("Thanks for your donation! You have received: " + quantity + "x rainbow party hat.");
                        player.amDonated += 150;
                        break;
                    case 1033:
                        player.getItems().addItemUnderAnyCircumstance(11862, quantity);
                        player.sendMessage("Thanks for your donation! You have received: " + quantity + "x black party hat.");
                        player.amDonated += 180;
                        break;
                    case 3049:
                        player.getItems().addItemUnderAnyCircumstance(608, quantity);
                        player.sendMessage("Thanks for your donation! You have received: " + quantity + "x custom title scroll.");
                        player.amDonated += 10 * quantity;
                        break;
                    case 103:
                        player.getItems().addItemUnderAnyCircumstance(25164, quantity * 5);
                        player.sendMessage("Thanks for your donation! You have received: " + quantity * 5 + "x pet mystery box.");
                        player.amDonated += 35 * quantity;
                        break;
                    case 104:
                        player.getItems().addItemUnderAnyCircumstance(25164, quantity * 10);
                        player.sendMessage("Thanks for your donation! You have received: " + quantity * 10 + "x pet mystery box.");
                        player.amDonated += 70 * quantity;
                        break;
                    case 105:
                        player.getItems().addItemUnderAnyCircumstance(25165, quantity * 5);
                        player.sendMessage("Thanks for your donation! You have received: " + quantity * 5 + "x cosmetic mystery box.");
                        player.amDonated += 20 * quantity;
                        break;
                    case 106:
                        player.getItems().addItemUnderAnyCircumstance(25165, quantity * 10);
                        player.sendMessage("Thanks for your donation! You have received: " + quantity * 10 + "x cosmetic mystery box.");
                        player.amDonated += 40 * quantity;
                        break;
                    case 3054:
                        player.getItems().addItemUnderAnyCircumstance(12954, quantity);
                        player.sendMessage("Thanks for your donation! You have received: " + quantity + "x dragon defender.");
                        player.amDonated += 5 * quantity;
                        break;
                    case 3055:
                        player.getItems().addItemUnderAnyCircumstance(7462, quantity);
                        player.sendMessage("Thanks for your donation! You have received: " + quantity + "x barrows gloves.");
                        player.amDonated += 5 * quantity;
                        break;
                }
                donated = true;
                rs.updateInt("claimed", 1); // do not delete otherwise they can reclaim!
                rs.updateRow();
            }
            if (donated) {
                PlayerHandler.announcementMessage(player, 37, "Donations", "has just donated to help support the server! <col=006600>::store</col>");
                player.getDH().sendNpcChat2("All donations succesfully claimed!", "Your new amount donated is: $@red@" + player.amDonated, 5792, "Party pete");
                donated = false;
                player.updateRank();
            } else {
                player.getDH().sendNpcChat1("You have no awaiting claims...", 5792, "Party pete");
            }
            player.nextChat = -1;
            destroy();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param host     the host ip address or url
     * @param database the name of the database
     * @param user     the user attached to the database
     * @param pass     the users password
     * @return true if connected
     */
    public boolean connect(String host, String database, String user, String pass) {
        try {
            this.conn = DriverManager.getConnection("jdbc:mysql://" + host + ":3306/" + database, user, pass);
            return true;
        } catch (SQLException e) {
            System.out.println("Failing connecting to database!");
            return false;
        }
    }

    /**
     * Disconnects from the MySQL server and destroy the connection
     * and statement instances
     */
    public void destroy() {
        try {
            conn.close();
            conn = null;
            if (stmt != null) {
                stmt.close();
                stmt = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Executes an update query on the database
     *
     * @param query
     * @see {@link Statement#executeUpdate}
     */
    public int executeUpdate(String query) {
        try {
            this.stmt = this.conn.createStatement(1005, 1008);
            int results = stmt.executeUpdate(query);
            return results;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return -1;
    }

    /**
     * Executres a query on the database
     *
     * @param query
     * @return the results, never null
     * @see {@link Statement#executeQuery(String)}
     */
    public ResultSet executeQuery(String query) {
        try {
            this.stmt = this.conn.createStatement(1005, 1008);
            ResultSet results = stmt.executeQuery(query);
            return results;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return null;
    }
}