|
| 1 | +package ozaii.apis.managers; |
| 2 | + |
| 3 | + |
| 4 | +import ozaii.managers.CoinManager; |
| 5 | + |
| 6 | +import java.util.concurrent.CompletableFuture; |
| 7 | +import java.util.logging.Logger; |
| 8 | + |
| 9 | +public class CoinApi { |
| 10 | + private final CoinManager coinManager; |
| 11 | + private static final Logger logger = Logger.getLogger(CoinApi.class.getName()); |
| 12 | + |
| 13 | + public CoinApi(CoinManager coinManager) { |
| 14 | + this.coinManager = coinManager; |
| 15 | + } |
| 16 | + |
| 17 | + /** |
| 18 | + * Creates a new bank account for a player. |
| 19 | + * |
| 20 | + * @param playerName The name of the player. |
| 21 | + */ |
| 22 | + public void createAccount(String playerName) { |
| 23 | + coinManager.createBankAccount(playerName) |
| 24 | + .thenRun(() -> logger.info("Account created for player: " + playerName)) |
| 25 | + .exceptionally(ex -> { |
| 26 | + logger.severe("Failed to create account for player " + playerName + ": " + ex.getMessage()); |
| 27 | + return null; |
| 28 | + }); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Deposits coins into a player's account. |
| 33 | + * |
| 34 | + * @param playerName The name of the player. |
| 35 | + * @param amount The amount to deposit. |
| 36 | + */ |
| 37 | + public void depositCoins(String playerName, double amount) { |
| 38 | + coinManager.deposit(playerName, amount) |
| 39 | + .thenRun(() -> logger.info(amount + " coins deposited to " + playerName)) |
| 40 | + .exceptionally(ex -> { |
| 41 | + logger.severe("Failed to deposit coins for " + playerName + ": " + ex.getMessage()); |
| 42 | + return null; |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Withdraws coins from a player's account. |
| 48 | + * |
| 49 | + * @param playerName The name of the player. |
| 50 | + * @param amount The amount to withdraw. |
| 51 | + */ |
| 52 | + public void withdrawCoins(String playerName, double amount) { |
| 53 | + coinManager.remove(playerName, amount) |
| 54 | + .thenRun(() -> logger.info(amount + " coins withdrawn from " + playerName)) |
| 55 | + .exceptionally(ex -> { |
| 56 | + logger.severe("Failed to withdraw coins for " + playerName + ": " + ex.getMessage()); |
| 57 | + return null; |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Transfers coins from one player to another. |
| 63 | + * |
| 64 | + * @param sender The name of the player sending the coins. |
| 65 | + * @param taker The name of the player receiving the coins. |
| 66 | + * @param amount The amount to transfer. |
| 67 | + */ |
| 68 | + public void transferCoins(String sender, String taker, double amount) { |
| 69 | + coinManager.transferAccount(sender, taker, amount) |
| 70 | + .thenRun(() -> logger.info("Transferred " + amount + " coins from " + sender + " to " + taker)) |
| 71 | + .exceptionally(ex -> { |
| 72 | + logger.severe("Failed to transfer coins from " + sender + " to " + taker + ": " + ex.getMessage()); |
| 73 | + return null; |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Gets the current coin balance for a player. |
| 79 | + * |
| 80 | + * @param playerName The name of the player. |
| 81 | + * @return A future containing the coin balance. |
| 82 | + */ |
| 83 | + public CompletableFuture<Double> getBalance(String playerName) { |
| 84 | + return coinManager.getCoins(playerName) |
| 85 | + .thenApply(balance -> { |
| 86 | + logger.info("Balance for " + playerName + ": " + balance); |
| 87 | + return balance; |
| 88 | + }) |
| 89 | + .exceptionally(ex -> { |
| 90 | + logger.severe("Failed to fetch balance for " + playerName + ": " + ex.getMessage()); |
| 91 | + return 0.0; |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Deletes a player's bank account. |
| 97 | + * |
| 98 | + * @param playerName The name of the player. |
| 99 | + */ |
| 100 | + public void deleteAccount(String playerName) { |
| 101 | + coinManager.deleteAccount(playerName) |
| 102 | + .thenRun(() -> logger.info("Deleted account for player: " + playerName)) |
| 103 | + .exceptionally(ex -> { |
| 104 | + logger.severe("Failed to delete account for " + playerName + ": " + ex.getMessage()); |
| 105 | + return null; |
| 106 | + }); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Resets a player's coin balance to zero. |
| 111 | + * |
| 112 | + * @param playerName The name of the player. |
| 113 | + */ |
| 114 | + public void resetCoins(String playerName) { |
| 115 | + coinManager.resetCoins(playerName) |
| 116 | + .thenRun(() -> logger.info("Reset coins for player: " + playerName)) |
| 117 | + .exceptionally(ex -> { |
| 118 | + logger.severe("Failed to reset coins for " + playerName + ": " + ex.getMessage()); |
| 119 | + return null; |
| 120 | + }); |
| 121 | + } |
| 122 | +} |
0 commit comments