Skip to content

Commit

Permalink
Fix for coins with spaces-dashes in name
Browse files Browse the repository at this point in the history
  • Loading branch information
Evirth committed Mar 13, 2018
1 parent c59a3fb commit 5bdbaa5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,18 @@ public Player(String name) {
*/
public void withdraw(int amount, String crypto) {
try {
if (!this.account.getBalance().containsKey(crypto)) {
CoinMarket coin = CoinMarketCap.ticker(crypto);
if (coin == null) {
this.player.sendMessage(String.format(CryptoMarket.resourceManager.getResource("CouldNotFindCoin"), crypto));
return;
}

if (!this.account.getBalance().containsKey(coin.getName())) {
this.player.sendMessage(String.format(CryptoMarket.resourceManager.getResource("DontHaveCoin"), crypto));
return;
}

CoinMarket coin = CoinMarketCap.ticker(crypto);
int diamondsFromBalance = CoinHelper.calculateAmountOfDiamondsFromCoins(coin.getPriceUSD(), this.account.getBalance().get(crypto).getAmount());
int diamondsFromBalance = CoinHelper.calculateAmountOfDiamondsFromCoins(coin.getPriceUSD(), this.account.getBalance().get(coin.getName()).getAmount());
if (diamondsFromBalance >= amount) {
double amountOfCrypto = CoinHelper.calculateAmountOfCryptoFromDiamonds(amount, coin.getPriceUSD());
this.changeBalance(coin, -amountOfCrypto);
Expand Down Expand Up @@ -195,8 +200,14 @@ public void deposit(int amountOfDiamonds, String crypto) {
*/
public void exchange(String fromCrypto, String amount, String toCrypto) {
try {
if (!this.account.getBalance().containsKey(fromCrypto)) {
this.player.sendMessage(String.format(CryptoMarket.resourceManager.getResource("DontHaveCoin"), fromCrypto));
CoinMarket fromCoin = CoinMarketCap.ticker(fromCrypto);
if (fromCoin == null) {
this.player.sendMessage(String.format(CryptoMarket.resourceManager.getResource("CouldNotFindCoin"), toCrypto));
return;
}

if (!this.account.getBalance().containsKey(fromCoin.getName())) {
this.player.sendMessage(String.format(CryptoMarket.resourceManager.getResource("DontHaveCoin"), fromCoin.getName()));
return;
}

Expand All @@ -206,9 +217,8 @@ public void exchange(String fromCrypto, String amount, String toCrypto) {
return;
}

Coin fromC = this.account.getBalance().get(fromCrypto);
CoinMarket fromCoin = CoinMarketCap.ticker(fromC.getId());

Coin fromC = this.account.getBalance().get(fromCoin.getName());
double amountOfCrypto;
int amountOfDiamonds;

Expand Down Expand Up @@ -484,7 +494,7 @@ public void removeBalance(String crypto, String amount, String executorName) thr
}

if (!this.account.getBalance().containsKey(coin.getName())) {
throw new IllegalArgumentException(String.format(CryptoMarket.resourceManager.getResource("PlayerDoesntHaveCoin"), this.name, crypto));
throw new IllegalArgumentException(String.format(CryptoMarket.resourceManager.getResource("PlayerDoesntHaveCoin"), this.name, coin.getName()));
}

int amountOfDiamonds;
Expand All @@ -501,7 +511,7 @@ public void removeBalance(String crypto, String amount, String executorName) thr
amountOfDiamonds = CoinHelper.calculateAmountOfDiamondsFromCoins(coin.getPriceUSD(), amountOfCrypto);
}

int diamondsFromBalance = CoinHelper.calculateAmountOfDiamondsFromCoins(coin.getPriceUSD(), this.account.getBalance().get(crypto).getAmount());
int diamondsFromBalance = CoinHelper.calculateAmountOfDiamondsFromCoins(coin.getPriceUSD(), this.account.getBalance().get(coin.getName()).getAmount());
if (diamondsFromBalance < amountOfDiamonds) {
throw new IllegalArgumentException(String.format(CryptoMarket.resourceManager.getResource("PlayerDoesntHaveThatManyCoins"), this.name, coin.getName()));
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: CryptoMarket
version: 1.0
version: 1.0.1
description: Cryptocurrency market simulator
author: Evirth
main: main.java.pl.csrv.divinecraft.evirth.cryptomarket.CryptoMarket
Expand Down

0 comments on commit 5bdbaa5

Please sign in to comment.