Make sure a user has enough gold to make a purchase.

This commit is contained in:
Conrad S 2016-04-08 10:48:55 -04:00
parent 2c05743f62
commit 8a2efa2f0b
1 changed files with 30 additions and 25 deletions

View File

@ -27,20 +27,20 @@ public class DonationRepository extends MinecraftRepository
private static String CREATE_GEM_TRANSACTION_TABLE = "CREATE TABLE IF NOT EXISTS accountGemTransactions (id INT NOT NULL AUTO_INCREMENT, accountId INT, reason VARCHAR(100), gems INT, PRIMARY KEY (id), FOREIGN KEY (accountId) REFERENCES accounts(id));";
private static String INSERT_COIN_TRANSACTION = "INSERT INTO accountCoinTransactions(accountId, reason, coins) VALUES(?, ?, ?);";
private static String UPDATE_ACCOUNT_COINS = "UPDATE accounts SET coins = coins + ? WHERE id = ?;";
private static String UPDATE_ACCOUNT_GOLD = "UPDATE accounts SET gold = gold + ? WHERE id = ?;";
private static String UPDATE_ACCOUNT_GOLD = "UPDATE accounts SET gold = gold + ? WHERE id = ? && gold >= ?;";
private static String SET_ACCOUNT_GOLD = "UPDATE accounts SET gold = ? WHERE id = ?;";
private static String UPDATE_NULL_ACCOUNT_GEMS_AND_COINS_ = "UPDATE accounts SET gems = ?, coins = ? WHERE id = ? AND gems IS NULL AND coins IS NULL;";
private String _webAddress;
public DonationRepository(JavaPlugin plugin, String webAddress)
{
super(plugin, DBPool.getAccount());
_webAddress = webAddress;
}
public void PurchaseKnownSalesPackage(final Callback<TransactionResponse> callback, String name, final String uuid, final int cost, final int salesPackageId)
public void PurchaseKnownSalesPackage(final Callback<TransactionResponse> callback, String name, final String uuid, final int cost, final int salesPackageId)
{
final PurchaseToken token = new PurchaseToken();
token.AccountName = name;
@ -61,7 +61,7 @@ public class DonationRepository extends MinecraftRepository
});
}
};
handleDatabaseCall(new DatabaseRunnable(new Runnable()
{
public void run()
@ -70,7 +70,7 @@ public class DonationRepository extends MinecraftRepository
}
}), "Error purchasing known sales package in DonationRepository : ");
}
public void PurchaseUnknownSalesPackage(final Callback<TransactionResponse> callback, final String name, final int accountId, final String packageName, final CurrencyType currencyType, final int cost)
{
final UnknownPurchaseToken token = new UnknownPurchaseToken();
@ -96,7 +96,7 @@ public class DonationRepository extends MinecraftRepository
executeUpdate(UPDATE_ACCOUNT_GOLD, new ColumnInt("gold", -cost), new ColumnInt("id", accountId));
}
}
Bukkit.getServer().getScheduler().runTask(getPlugin(), new Runnable()
{
@Override
@ -107,7 +107,7 @@ public class DonationRepository extends MinecraftRepository
});
}
};
handleDatabaseCall(new DatabaseRunnable(new Runnable()
{
public void run()
@ -116,18 +116,18 @@ public class DonationRepository extends MinecraftRepository
}
}), "Error purchasing unknown sales package in DonationRepository : ");
}
public void gemReward(final Callback<Boolean> callback, final String giver, String name, final String uuid, final int greenGems)
{
final GemRewardToken token = new GemRewardToken();
token.Source = giver;
token.Name = name;
token.Amount = greenGems;
final Callback<Boolean> extraCallback = new Callback<Boolean>()
{
public void run(final Boolean response)
{
{
Bukkit.getServer().getScheduler().runTask(getPlugin(), new Runnable()
{
@Override
@ -138,7 +138,7 @@ public class DonationRepository extends MinecraftRepository
});
}
};
handleDatabaseCall(new DatabaseRunnable(new Runnable()
{
public void run()
@ -147,14 +147,14 @@ public class DonationRepository extends MinecraftRepository
}
}), "Error updating player gem amount in DonationRepository : ");
}
public void rewardCoins(final Callback<Boolean> callback, final String giver, String name, final int accountId, final int coins)
{
final GemRewardToken token = new GemRewardToken();
token.Source = giver;
token.Name = name;
token.Amount = coins;
final Callback<Boolean> extraCallback = new Callback<Boolean>()
{
public void run(final Boolean response)
@ -164,7 +164,7 @@ public class DonationRepository extends MinecraftRepository
//executeUpdate(UPDATE_ACCOUNT_COINS, new ColumnInt("coins", coins), new ColumnInt("id", accountId));
//executeUpdate(INSERT_COIN_TRANSACTION, new ColumnInt("id", accountId), new ColumnVarChar("reason", 100, "Rewarded by " + giver), new ColumnInt("coins", coins));
}
Bukkit.getServer().getScheduler().runTask(getPlugin(), new Runnable()
{
@Override
@ -175,7 +175,7 @@ public class DonationRepository extends MinecraftRepository
});
}
};
handleDatabaseCall(new DatabaseRunnable(new Runnable()
{
public void run()
@ -184,21 +184,26 @@ public class DonationRepository extends MinecraftRepository
}
}), "Error updating player coin amount in DonationRepository : ");
}
public void rewardGold(final Callback<Boolean> callback, final String giver, final String name, final int accountId, final int gold)
{
{
handleDatabaseCall(new DatabaseRunnable(new Runnable()
{
public void run()
{
boolean success = executeUpdate(UPDATE_ACCOUNT_GOLD, new ColumnInt("gold", gold), new ColumnInt("id", accountId)) > 0;
ColumnInt min = new ColumnInt("gold", gold < 0 ? -gold : 0);
boolean success = executeUpdate(UPDATE_ACCOUNT_GOLD, new ColumnInt("gold", gold), new ColumnInt("id", accountId), min) > 0;
callback.run(success);
}
}), "Error updating player gold amount in DonationRepository : ");
}
public void setGold(final Callback<Boolean> callback, final String giver, final String name, final int accountId, final int gold)
{
{
if (gold < 0)
{
throw new IllegalArgumentException("gold cannot be negative");
}
handleDatabaseCall(new DatabaseRunnable(new Runnable()
{
public void run()
@ -208,7 +213,7 @@ public class DonationRepository extends MinecraftRepository
}
}), "Error updating player gold amount in DonationRepository : ");
}
@Override
protected void initialize()
{
@ -240,12 +245,12 @@ public class DonationRepository extends MinecraftRepository
public Donor retrieveDonorInfo(ResultSet resultSet) throws SQLException
{
Donor donor = new Donor();
while (resultSet.next())
{
donor.setGold(resultSet.getInt(1));
}
return donor;
}
}