Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.ultramine.economy;
- /**
- * Created by Derelanade on 23.08.2016 (Август).
- * All rights reserved! Copyright by Dreamfinity.org!
- * 18:48 (sys. time: 18:48).
- * Not for free use!
- */
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import javax.sql.DataSource;
- import org.apache.logging.log4j.LogManager;
- import org.apache.logging.log4j.Logger;
- import org.ultramine.server.data.Databases;
- import cpw.mods.fml.relauncher.Side;
- import cpw.mods.fml.relauncher.SideOnly;
- import net.minecraft.command.CommandException;
- import net.minecraft.nbt.NBTTagCompound;
- @SideOnly(Side.SERVER)
- public class SpecRubHoldings implements IHoldings {
- private static final Logger log = LogManager.getLogger();
- private static final DataSource ds = Databases.getDataSource("forumdb");
- private final Account acc;
- private final Currency cur;
- private long balance = -1;
- public SpecRubHoldings(Account acc, Currency cur) {
- this.acc = acc;
- this.cur = cur;
- }
- public void updateBalance() {
- Connection conn = null;
- try {
- conn = ds.getConnection();
- PreparedStatement ps = conn.prepareStatement("SELECT `money` FROM `st_users` WHERE `username`=?");
- ps.setString(1, acc.getName());
- try {
- ResultSet rs = ps.executeQuery();
- if (!rs.next())
- return; //throw new RuntimeException("Player not found: "+acc.getName());
- long newBalance = rs.getLong("money");
- if (newBalance != this.balance) {
- this.balance = newBalance;
- acc.onHoldingsChange(this);
- }
- } finally {
- ps.close();
- }
- } catch (SQLException e) {
- log.error("Failed to select balance of player: " + acc.getName(), e);
- } finally {
- if (conn != null)
- try {
- conn.close();
- } catch (SQLException ignored) {
- }
- }
- }
- @Override
- public Account getAccount() {
- return acc;
- }
- @Override
- public Currency getCurrency() {
- return cur;
- }
- @Override
- public double getBalance() {
- if (this.balance == -1)
- updateBalance();
- if (this.balance == -1)
- updateBalance();
- if (this.balance == -1)
- log.error("Totally failed to select player balance! Giving up");
- return this.balance / 100.0d;
- }
- @Override
- public void setBalanceSilently(double balance)
- {
- this.balance = (int)(balance*100);
- }
- @Override
- public void setBalance(double balance) {
- this.balance = (int)(balance*100);
- acc.onHoldingsChange(this);
- }
- @Override
- public void add(double amount) {
- addChecked(amount, "unknown");
- }
- @Override
- public void subtract(double amount) {
- subtractChecked(amount); //negative not supported
- }
- @Override
- public void subtractChecked(double amount) {
- subtractChecked(amount, "unknown");
- }
- private static long ceiling(double arg)
- {
- long i = (long)arg;
- return arg > (double)i ? Math.addExact(i, 1) : i;
- }
- private static long floor(double arg)
- {
- long i = (long)arg;
- return arg < (double)i ? Math.subtractExact(i, 1) : i;
- }
- public void subtractChecked(double amount, String comment) {
- if (amount <= 0.0d)
- throw new CommandException("economy.fail.negativeamount");
- long amountI = ceiling(amount * 100);
- Connection conn = null;
- try {
- conn = ds.getConnection();
- conn.setAutoCommit(false);
- PreparedStatement ps = conn.prepareStatement(
- "INSERT INTO `st_payment_acc_log` (`user`,`type`,`amount`,`time`,`comment`) VALUES ((SELECT `userID` FROM `st_users` WHERE `username`=?),?,?,NOW(),?)");
- //ps.setString(1, acc.getName());
- //ps.setString(2, "-");
- //ps.setLong(3, amountI);
- //ps.setString(4, ConfigurationHandler.getSettingDir().getParentFile().getCanonicalFile().getName() + " " + comment);
- //try {
- // ps.executeUpdate();
- //} finally {
- // ps.close();
- //}
- ps = conn.prepareStatement("UPDATE `st_users` SET `money`=`money`-? WHERE `username`=?");
- ps.setLong(1, amountI);
- ps.setString(2, acc.getName());
- try {
- ps.executeUpdate();
- } finally {
- ps.close();
- }
- conn.commit();
- } catch (Exception e) {
- try {
- conn.rollback();
- } catch (SQLException ignored) {
- }
- log.error("Failed to subtract money from player: " + acc.getName(), e);
- throw new RuntimeException("Failed to subtract money from player");
- } finally {
- if (conn != null) {
- try {
- conn.setAutoCommit(true);
- conn.close();
- } catch (SQLException ignored) {
- }
- }
- updateBalance();//always updating
- }
- }
- public void addChecked(double amount, String comment) {
- if (amount <= 0.0d)
- throw new CommandException("economy.fail.negativeamount");
- long amountI = floor(amount * 100);
- Connection conn = null;
- try {
- conn = ds.getConnection();
- conn.setAutoCommit(false);
- PreparedStatement ps = conn.prepareStatement(
- "INSERT INTO `st_payment_acc_log` (`user`,`type`,`amount`,`time`,`comment`) VALUES ((SELECT `userID` FROM `st_users` WHERE `username`=?),?,?,NOW(),?)");
- //ps.setString(1, acc.getName());
- //ps.setString(2, "-");
- //ps.setLong(3, amountI);
- //ps.setString(4, ConfigurationHandler.getSettingDir().getParentFile().getCanonicalFile().getName() + " " + comment);
- //try {
- // ps.executeUpdate();
- //} finally {
- // ps.close();
- //}
- ps = conn.prepareStatement("UPDATE `st_users` SET `money`=`money`+? WHERE `username`=?");
- ps.setLong(1, amountI);
- ps.setString(2, acc.getName());
- try {
- ps.executeUpdate();
- } finally {
- ps.close();
- }
- conn.commit();
- } catch (Exception e) {
- try {
- conn.rollback();
- } catch (SQLException ignored) {
- }
- log.error("Failed to subtract money from player: " + acc.getName(), e);
- throw new RuntimeException("Failed to subtract money from player");
- } finally {
- if (conn != null) {
- try {
- conn.setAutoCommit(true);
- conn.close();
- } catch (SQLException ignored) {
- }
- }
- updateBalance();//always updating
- }
- }
- @Override
- public void divide(double amount) {
- throw new UnsupportedOperationException();
- }
- @Override
- public void multiply(double amount) {
- throw new UnsupportedOperationException();
- }
- @Override
- public boolean isNegative() {
- return false; //always false
- }
- @Override
- public boolean hasEnough(double amount) {
- return ceiling(amount * 100) <= this.balance;
- }
- @Override
- public boolean hasOver(double amount) {
- return ceiling(amount * 100) < this.balance;
- }
- @Override
- public void transact(IHoldings to, double amount) {
- transactChecked(to, amount); //negative not supported
- }
- @Override
- public void transactChecked(IHoldings to, double amount) {
- if (to.getCurrency() != cur)
- throw new RuntimeException("Tried to transact different currensies");
- if (amount <= 0.0d)
- throw new CommandException("economy.fail.negativeamount");
- long amountISub = ceiling(amount * 100);
- long amountIAdd = floor(amount * 100);
- Connection conn = null;
- try {
- conn = ds.getConnection();
- conn.setAutoCommit(false);
- PreparedStatement ps = conn.prepareStatement(
- "INSERT INTO `st_payment_acc_log` (`user`,`type`,`amount`,`time`,`comment`) VALUES ((SELECT `userID` FROM `st_users` WHERE `username`=?),?,?,NOW(),?)");
- ps.setString(1, acc.getName());
- ps.setString(2, "-");
- ps.setLong(3, amountISub);
- ps.setString(4, "Transact to " + to.getAccount().getName());
- try {
- ps.executeUpdate();
- } catch (SQLException e) {
- ps.close();
- throw e;
- }
- ps.setString(1, to.getAccount().getName());
- ps.setString(2, "+");
- ps.setLong(3, amountIAdd);
- ps.setString(4, "Transact from " + acc.getName());
- try {
- ps.executeUpdate();
- } finally {
- ps.close();
- }
- ps = conn.prepareStatement("UPDATE `st_users` SET `money`=`money`-? WHERE `username`=?");
- ps.setLong(1, amountISub);
- ps.setString(2, acc.getName());
- try {
- ps.executeUpdate();
- } finally {
- ps.close();
- }
- ps = conn.prepareStatement("UPDATE `st_users` SET `money`=`money`+? WHERE `username`=?");
- ps.setLong(1, amountIAdd);
- ps.setString(2, to.getAccount().getName());
- try {
- ps.executeUpdate();
- } finally {
- ps.close();
- }
- conn.commit();
- } catch (Exception e) {
- try {
- conn.rollback();
- } catch (SQLException ignored) {
- }
- log.error("Failed to transact money from: " + acc.getName() + " to:" + to.getAccount().getName(), e);
- throw new RuntimeException("Failed to transact money");
- } finally {
- if (conn != null) {
- try {
- conn.setAutoCommit(true);
- conn.close();
- } catch (SQLException ignored) {
- }
- }
- updateBalance();//always updating
- ((SpecRubHoldings) to).updateBalance();
- }
- }
- @Override
- public void writeToNBT(NBTTagCompound nbt) {
- }
- @Override
- public void readFromNBT(NBTTagCompound nbt) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment