Guest User

Untitled

a guest
Aug 27th, 2016
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.90 KB | None | 0 0
  1. package org.ultramine.economy;
  2.  
  3. /**
  4.  * Created by Derelanade on 23.08.2016 (Август).
  5.  * All rights reserved! Copyright by Dreamfinity.org!
  6.  * 18:48 (sys. time: 18:48).
  7.  * Not for free use!
  8.  */
  9.  
  10. import java.sql.Connection;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import javax.sql.DataSource;
  15. import org.apache.logging.log4j.LogManager;
  16. import org.apache.logging.log4j.Logger;
  17. import org.ultramine.server.data.Databases;
  18. import cpw.mods.fml.relauncher.Side;
  19. import cpw.mods.fml.relauncher.SideOnly;
  20. import net.minecraft.command.CommandException;
  21. import net.minecraft.nbt.NBTTagCompound;
  22.  
  23. @SideOnly(Side.SERVER)
  24. public class SpecRubHoldings implements IHoldings {
  25.     private static final Logger log = LogManager.getLogger();
  26.     private static final DataSource ds = Databases.getDataSource("forumdb");
  27.  
  28.     private final Account acc;
  29.     private final Currency cur;
  30.  
  31.     private long balance = -1;
  32.  
  33.     public SpecRubHoldings(Account acc, Currency cur) {
  34.         this.acc = acc;
  35.         this.cur = cur;
  36.     }
  37.  
  38.     public void updateBalance() {
  39.         Connection conn = null;
  40.         try {
  41.             conn = ds.getConnection();
  42.  
  43.             PreparedStatement ps = conn.prepareStatement("SELECT `money` FROM `st_users` WHERE `username`=?");
  44.             ps.setString(1, acc.getName());
  45.             try {
  46.                 ResultSet rs = ps.executeQuery();
  47.                 if (!rs.next())
  48.                     return; //throw new RuntimeException("Player not found: "+acc.getName());
  49.                 long newBalance = rs.getLong("money");
  50.                 if (newBalance != this.balance) {
  51.                     this.balance = newBalance;
  52.                     acc.onHoldingsChange(this);
  53.                 }
  54.             } finally {
  55.                 ps.close();
  56.             }
  57.  
  58.         } catch (SQLException e) {
  59.             log.error("Failed to select balance of player: " + acc.getName(), e);
  60.         } finally {
  61.             if (conn != null)
  62.                 try {
  63.                     conn.close();
  64.                 } catch (SQLException ignored) {
  65.                 }
  66.         }
  67.     }
  68.  
  69.     @Override
  70.     public Account getAccount() {
  71.         return acc;
  72.     }
  73.  
  74.     @Override
  75.     public Currency getCurrency() {
  76.         return cur;
  77.     }
  78.  
  79.     @Override
  80.     public double getBalance() {
  81.         if (this.balance == -1)
  82.             updateBalance();
  83.         if (this.balance == -1)
  84.             updateBalance();
  85.         if (this.balance == -1)
  86.             log.error("Totally failed to select player balance! Giving up");
  87.  
  88.         return this.balance / 100.0d;
  89.     }
  90.  
  91.     @Override
  92.     public void setBalanceSilently(double balance)
  93.     {
  94.         this.balance = (int)(balance*100);
  95.     }
  96.  
  97.     @Override
  98.     public void setBalance(double balance) {
  99.         this.balance = (int)(balance*100);
  100.         acc.onHoldingsChange(this);
  101.     }
  102.  
  103.     @Override
  104.     public void add(double amount) {
  105.         addChecked(amount, "unknown");
  106.     }
  107.  
  108.     @Override
  109.     public void subtract(double amount) {
  110.         subtractChecked(amount); //negative not supported
  111.     }
  112.  
  113.     @Override
  114.     public void subtractChecked(double amount) {
  115.         subtractChecked(amount, "unknown");
  116.     }
  117.  
  118.     private static long ceiling(double arg)
  119.     {
  120.         long i = (long)arg;
  121.         return arg > (double)i ? Math.addExact(i, 1) : i;
  122.     }
  123.  
  124.     private static long floor(double arg)
  125.     {
  126.         long i = (long)arg;
  127.         return arg < (double)i ? Math.subtractExact(i, 1) : i;
  128.     }
  129.  
  130.     public void subtractChecked(double amount, String comment) {
  131.         if (amount <= 0.0d)
  132.             throw new CommandException("economy.fail.negativeamount");
  133.         long amountI = ceiling(amount * 100);
  134.         Connection conn = null;
  135.         try {
  136.             conn = ds.getConnection();
  137.             conn.setAutoCommit(false);
  138.  
  139.             PreparedStatement ps = conn.prepareStatement(
  140.                     "INSERT INTO `st_payment_acc_log` (`user`,`type`,`amount`,`time`,`comment`) VALUES ((SELECT `userID` FROM `st_users` WHERE `username`=?),?,?,NOW(),?)");
  141.             //ps.setString(1, acc.getName());
  142.             //ps.setString(2, "-");
  143.             //ps.setLong(3, amountI);
  144.             //ps.setString(4, ConfigurationHandler.getSettingDir().getParentFile().getCanonicalFile().getName() + " " + comment);
  145.             //try {
  146.             //    ps.executeUpdate();
  147.             //} finally {
  148.             //    ps.close();
  149.             //}
  150.  
  151.             ps = conn.prepareStatement("UPDATE `st_users` SET `money`=`money`-? WHERE `username`=?");
  152.             ps.setLong(1, amountI);
  153.             ps.setString(2, acc.getName());
  154.             try {
  155.                 ps.executeUpdate();
  156.             } finally {
  157.                 ps.close();
  158.             }
  159.  
  160.             conn.commit();
  161.         } catch (Exception e) {
  162.             try {
  163.                 conn.rollback();
  164.             } catch (SQLException ignored) {
  165.             }
  166.             log.error("Failed to subtract money from player: " + acc.getName(), e);
  167.             throw new RuntimeException("Failed to subtract money from player");
  168.         } finally {
  169.             if (conn != null) {
  170.                 try {
  171.                     conn.setAutoCommit(true);
  172.                     conn.close();
  173.                 } catch (SQLException ignored) {
  174.                 }
  175.             }
  176.             updateBalance();//always updating
  177.         }
  178.     }
  179.  
  180.     public void addChecked(double amount, String comment) {
  181.         if (amount <= 0.0d)
  182.             throw new CommandException("economy.fail.negativeamount");
  183.         long amountI = floor(amount * 100);
  184.         Connection conn = null;
  185.         try {
  186.             conn = ds.getConnection();
  187.             conn.setAutoCommit(false);
  188.  
  189.             PreparedStatement ps = conn.prepareStatement(
  190.                     "INSERT INTO `st_payment_acc_log` (`user`,`type`,`amount`,`time`,`comment`) VALUES ((SELECT `userID` FROM `st_users` WHERE `username`=?),?,?,NOW(),?)");
  191.             //ps.setString(1, acc.getName());
  192.             //ps.setString(2, "-");
  193.             //ps.setLong(3, amountI);
  194.             //ps.setString(4, ConfigurationHandler.getSettingDir().getParentFile().getCanonicalFile().getName() + " " + comment);
  195.             //try {
  196.             //    ps.executeUpdate();
  197.             //} finally {
  198.             //    ps.close();
  199.             //}
  200.  
  201.             ps = conn.prepareStatement("UPDATE `st_users` SET `money`=`money`+? WHERE `username`=?");
  202.             ps.setLong(1, amountI);
  203.             ps.setString(2, acc.getName());
  204.             try {
  205.                 ps.executeUpdate();
  206.             } finally {
  207.                 ps.close();
  208.             }
  209.  
  210.             conn.commit();
  211.         } catch (Exception e) {
  212.             try {
  213.                 conn.rollback();
  214.             } catch (SQLException ignored) {
  215.             }
  216.             log.error("Failed to subtract money from player: " + acc.getName(), e);
  217.             throw new RuntimeException("Failed to subtract money from player");
  218.         } finally {
  219.             if (conn != null) {
  220.                 try {
  221.                     conn.setAutoCommit(true);
  222.                     conn.close();
  223.                 } catch (SQLException ignored) {
  224.                 }
  225.             }
  226.             updateBalance();//always updating
  227.         }
  228.     }
  229.  
  230.     @Override
  231.     public void divide(double amount) {
  232.         throw new UnsupportedOperationException();
  233.     }
  234.  
  235.     @Override
  236.     public void multiply(double amount) {
  237.         throw new UnsupportedOperationException();
  238.     }
  239.  
  240.     @Override
  241.     public boolean isNegative() {
  242.         return false; //always false
  243.     }
  244.  
  245.     @Override
  246.     public boolean hasEnough(double amount) {
  247.         return ceiling(amount * 100) <= this.balance;
  248.     }
  249.  
  250.     @Override
  251.     public boolean hasOver(double amount) {
  252.         return ceiling(amount * 100) < this.balance;
  253.     }
  254.  
  255.     @Override
  256.     public void transact(IHoldings to, double amount) {
  257.         transactChecked(to, amount); //negative not supported
  258.     }
  259.  
  260.     @Override
  261.     public void transactChecked(IHoldings to, double amount) {
  262.         if (to.getCurrency() != cur)
  263.             throw new RuntimeException("Tried to transact different currensies");
  264.  
  265.         if (amount <= 0.0d)
  266.             throw new CommandException("economy.fail.negativeamount");
  267.         long amountISub = ceiling(amount * 100);
  268.         long amountIAdd = floor(amount * 100);
  269.         Connection conn = null;
  270.         try {
  271.             conn = ds.getConnection();
  272.             conn.setAutoCommit(false);
  273.  
  274.             PreparedStatement ps = conn.prepareStatement(
  275.                     "INSERT INTO `st_payment_acc_log` (`user`,`type`,`amount`,`time`,`comment`) VALUES ((SELECT `userID` FROM `st_users` WHERE `username`=?),?,?,NOW(),?)");
  276.             ps.setString(1, acc.getName());
  277.             ps.setString(2, "-");
  278.             ps.setLong(3, amountISub);
  279.             ps.setString(4, "Transact to " + to.getAccount().getName());
  280.             try {
  281.                 ps.executeUpdate();
  282.             } catch (SQLException e) {
  283.                 ps.close();
  284.                 throw e;
  285.             }
  286.  
  287.             ps.setString(1, to.getAccount().getName());
  288.             ps.setString(2, "+");
  289.             ps.setLong(3, amountIAdd);
  290.             ps.setString(4, "Transact from " + acc.getName());
  291.             try {
  292.                 ps.executeUpdate();
  293.             } finally {
  294.                 ps.close();
  295.             }
  296.  
  297.             ps = conn.prepareStatement("UPDATE `st_users` SET `money`=`money`-? WHERE `username`=?");
  298.             ps.setLong(1, amountISub);
  299.             ps.setString(2, acc.getName());
  300.             try {
  301.                 ps.executeUpdate();
  302.             } finally {
  303.                 ps.close();
  304.             }
  305.  
  306.             ps = conn.prepareStatement("UPDATE `st_users` SET `money`=`money`+? WHERE `username`=?");
  307.             ps.setLong(1, amountIAdd);
  308.             ps.setString(2, to.getAccount().getName());
  309.             try {
  310.                 ps.executeUpdate();
  311.             } finally {
  312.                 ps.close();
  313.             }
  314.  
  315.             conn.commit();
  316.         } catch (Exception e) {
  317.             try {
  318.                 conn.rollback();
  319.             } catch (SQLException ignored) {
  320.             }
  321.             log.error("Failed to transact money from: " + acc.getName() + " to:" + to.getAccount().getName(), e);
  322.             throw new RuntimeException("Failed to transact money");
  323.         } finally {
  324.             if (conn != null) {
  325.                 try {
  326.                     conn.setAutoCommit(true);
  327.                     conn.close();
  328.                 } catch (SQLException ignored) {
  329.                 }
  330.             }
  331.             updateBalance();//always updating
  332.             ((SpecRubHoldings) to).updateBalance();
  333.         }
  334.     }
  335.  
  336.     @Override
  337.     public void writeToNBT(NBTTagCompound nbt) {
  338.     }
  339.  
  340.     @Override
  341.     public void readFromNBT(NBTTagCompound nbt) {
  342.     }
  343.  
  344. }
Advertisement
Add Comment
Please, Sign In to add comment