Advertisement
Guest User

StandardEconomyService,java

a guest
May 7th, 2015
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.15 KB | None | 0 0
  1. package me.ekranos.misc.economy;
  2.  
  3. import com.google.common.base.Optional;
  4. import me.ekranos.misc.Misc;
  5. import org.spongepowered.api.entity.player.Player;
  6. import sun.reflect.generics.reflectiveObjects.NotImplementedException;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.PreparedStatement;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.util.HashSet;
  13. import java.util.Set;
  14. import java.util.UUID;
  15.  
  16. public class StandardEconomyService implements EconomyService {
  17.  
  18.     private String majorCurrencyName;
  19.     private String majorCurrencySign;
  20.     private String minorCurrencyName;
  21.     private String minorCurrencySign;
  22.     private String shortFormat;
  23.     private String longFormat;
  24.  
  25.     public StandardEconomyService() {
  26.         majorCurrencyName = "Euro";
  27.         majorCurrencySign = "€";
  28.         minorCurrencyName = "Cent";
  29.         minorCurrencySign = "ct";
  30.         shortFormat = "{majorValue},{minorValue} {majorName}";
  31.         longFormat = "{majorValue} {majorName} {minorValue} {minorName}";
  32.         getDeposit("server", true);
  33.  
  34.         recalculateAllDeposits();
  35.     }
  36.  
  37.     private void recalculateAllDeposits() {
  38.         Optional<Set<Deposit>> deposits = getDeposits();
  39.         for (Deposit deposit : deposits.get()) {
  40.             ((StandardDeposit) deposit).recalculate();
  41.         }
  42.     }
  43.  
  44.     @Override
  45.     public Transaction execute(Transaction transaction) {
  46.         return null;
  47.     }
  48.  
  49.     @Override
  50.     public Optional<Transaction> execute(Player source, Player target, double amount, String description) {
  51.         return execute(source.getUniqueId(), target.getUniqueId(), amount, description);
  52.     }
  53.  
  54.     @Override
  55.     public Optional<Transaction> execute(UUID source, UUID target, double amount, String description) {
  56.         return execute("player:" + source.toString(), "player:" + target.toString(), amount, description);
  57.     }
  58.  
  59.     @Override
  60.     public Optional<Transaction> execute(String source, String target, double amount, String description) {
  61.         if (test(source, target, amount)) {
  62.             StandardDeposit sourceDeposit = (StandardDeposit) getDeposit(source).get();
  63.             StandardDeposit targetDeposit = (StandardDeposit) getDeposit(target).get();
  64.             StandardTransaction transaction = new StandardTransaction(sourceDeposit, targetDeposit, amount, description);
  65.  
  66.             Optional<Transaction> result = Optional.of(transaction.save());
  67.             sourceDeposit.recalculate();
  68.             targetDeposit.recalculate();
  69.  
  70.             return result;
  71.         }
  72.  
  73.         return Optional.absent();
  74.     }
  75.  
  76.     @Override
  77.     public Optional<Transaction> execute(Player target, double amount, String description) {
  78.         return execute(target.getUniqueId(), amount, description);
  79.     }
  80.  
  81.     @Override
  82.     public Optional<Transaction> execute(UUID target, double amount, String description) {
  83.         return execute("player:" + target.toString(), amount, description);
  84.     }
  85.  
  86.     @Override
  87.     public Optional<Transaction> execute(String target, double amount, String description) {
  88.         if (test(target, amount)) {
  89.             StandardDeposit targetDeposit = (StandardDeposit) getDeposit(target).get();
  90.             StandardTransaction transaction = new StandardTransaction(getDeposit("server").get(), targetDeposit, amount, description);
  91.  
  92.             Optional<Transaction> result = Optional.of(transaction.save());
  93.  
  94.             targetDeposit.recalculate();
  95.  
  96.             return result;
  97.         }
  98.  
  99.         return Optional.absent();
  100.     }
  101.  
  102.     @Override
  103.     public boolean test(Player source, Player target, double amount) {
  104.         Deposit sourceDeposit = getDeposit(source).get();
  105.         Deposit targetDeposit = getDeposit(target).get();
  106.  
  107.         return sourceDeposit.getAmount() - amount >= 0.0d && targetDeposit.getAmount() + amount >= 0.0d;
  108.     }
  109.  
  110.     @Override
  111.     public boolean test(UUID source, UUID target, double amount) {
  112.         return false;
  113.     }
  114.  
  115.     @Override
  116.     public boolean test(String source, String target, double amount) {
  117.         return false;
  118.     }
  119.  
  120.     @Override
  121.     public boolean test(Player target, double amount) {
  122.         return test(target.getUniqueId(), amount);
  123.     }
  124.  
  125.     @Override
  126.     public boolean test(UUID target, double amount) {
  127.         return test("player:" + target.toString(), amount);
  128.     }
  129.  
  130.     @Override
  131.     public boolean test(String target, double amount) {
  132.         return getDeposit(target).get().getAmount() + amount >= 0.0d;
  133.     }
  134.  
  135.     @Override
  136.     public Optional<Transaction> rollback(Transaction transaction) {
  137.         throw new NotImplementedException();
  138.     }
  139.  
  140.     @Override
  141.     public Optional<Deposit> getDeposit(Player player) {
  142.         return getDeposit(player.getUniqueId());
  143.     }
  144.  
  145.     @Override
  146.     public Optional<Deposit> getDeposit(UUID uuid) {
  147.         return getDeposit("player:" + uuid.toString(), true);
  148.     }
  149.  
  150.     @Override
  151.     public Optional<Deposit> getDeposit(String name) {
  152.         return getDeposit(name, false);
  153.     }
  154.  
  155.     @Override
  156.     public Optional<Deposit> getDeposit(String name, boolean create) {
  157.         try (Connection connection = getConnection()) {
  158.             PreparedStatement statement = connection.prepareStatement("SELECT id FROM deposits WHERE name = ?");
  159.             statement.setString(1, name);
  160.             ResultSet result = statement.executeQuery();
  161.  
  162.             if (result.next()) {
  163.                 return Optional.of(new StandardDeposit(result.getInt(1)));
  164.             } else {
  165.                 if (create) {
  166.                     return Optional.of(new StandardDeposit(name).save());
  167.                 }
  168.             }
  169.         } catch (SQLException e) {
  170.             e.printStackTrace();
  171.         }
  172.  
  173.         return Optional.absent();
  174.     }
  175.  
  176.     @Override
  177.     public Optional<Set<Deposit>> getDeposits() {
  178.         Set<Deposit> deposits = new HashSet<>();
  179.  
  180.         try (Connection connection = getConnection()) {
  181.             ResultSet result = connection.prepareStatement("SELECT id FROM deposits").executeQuery();
  182.  
  183.             while (result.next()) {
  184.                 deposits.add(new StandardDeposit(result.getInt(1)));
  185.             }
  186.         } catch (SQLException e) {
  187.             e.printStackTrace();
  188.         }
  189.  
  190.         return Optional.of(deposits);
  191.     }
  192.  
  193.     @Override
  194.     public EconomyService setMajorCurrency(String name, String sign) {
  195.         majorCurrencyName = name;
  196.         majorCurrencySign = sign;
  197.         return this;
  198.     }
  199.  
  200.     @Override
  201.     public EconomyService setMinorCurrency(String name, String sign) {
  202.         minorCurrencyName = name;
  203.         minorCurrencySign = sign;
  204.         return this;
  205.     }
  206.  
  207.     @Override
  208.     public EconomyService setShortFormat(String format) {
  209.         shortFormat = format;
  210.         return this;
  211.     }
  212.  
  213.     @Override
  214.     public EconomyService setFullFormat(String format) {
  215.         longFormat = format;
  216.         return this;
  217.     }
  218.  
  219.     @Override
  220.     public double sanitize(double amount) {
  221.         return Math.floor(amount * 100) / 100;
  222.     }
  223.  
  224.     @Override
  225.     public String format(double amount) {
  226.         return format(amount, Format.SHORT);
  227.     }
  228.  
  229.     @Override
  230.     public String format(double amount, Format format) {
  231.         return format == Format.SHORT ? format(amount, shortFormat) : format(amount, longFormat);
  232.     }
  233.  
  234.     private String format(double amount, String format) {
  235.         double value = Math.floor(amount * 100) / 100;
  236.         int majorValue = new Double(amount).intValue();
  237.         int minorValue = new Double(amount * 100).intValue() - (majorValue * 100);
  238.  
  239.         return format
  240.                 .replace("{value}", Double.toString(value).replace('.', ','))
  241.                 .replace("{majorValue}", Integer.toString(majorValue))
  242.                 .replace("{minorValue}", Integer.toString(minorValue))
  243.                 .replace("{majorName}", majorCurrencyName)
  244.                 .replace("{majorSign}", majorCurrencySign)
  245.                 .replace("{minorName}", minorCurrencyName)
  246.                 .replace("{minorSign}", minorCurrencySign);
  247.     }
  248.  
  249.     public static Connection getConnection() throws SQLException {
  250.         return Misc.get().getConnection();
  251.     }
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement