GoldManager now uses stored procedure.

Deductions automatically process gold queue and return if transaction succeeded.
This commit is contained in:
Jonathan Williams 2015-07-19 19:32:46 -07:00
parent 3660b348cd
commit 73aea85876
6 changed files with 101 additions and 63 deletions

View File

@ -1,5 +1,6 @@
package mineplex.core.database; package mineplex.core.database;
import java.sql.CallableStatement;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;

View File

@ -295,12 +295,12 @@ public class DonationManager extends MiniDbClientPlugin<Donor>
_coinQueue.clear(); _coinQueue.clear();
} }
public void RewardGold(Callback<Boolean> callback, String caller, String name, int accountId, int amount) public void rewardGold(Callback<Boolean> callback, String caller, String name, int accountId, int amount)
{ {
RewardGold(callback, caller, name, accountId, amount, true); rewardGold(callback, caller, name, accountId, amount, true);
} }
public void RewardGold(final Callback<Boolean> callback, final String caller, final String name, final int accountId, final int amount, final boolean updateTotal) public void rewardGold(final Callback<Boolean> callback, final String caller, final String name, final int accountId, final int amount, final boolean updateTotal)
{ {
_repository.rewardGold(new Callback<Boolean>() _repository.rewardGold(new Callback<Boolean>()
{ {
@ -324,7 +324,7 @@ public class DonationManager extends MiniDbClientPlugin<Donor>
} }
if (callback != null) if (callback != null)
callback.run(true); callback.run(success);
} }
}, caller, name, accountId, amount); }, caller, name, accountId, amount);
} }
@ -349,37 +349,42 @@ public class DonationManager extends MiniDbClientPlugin<Donor>
} }
@EventHandler @EventHandler
public void UpdateGoldQueue(UpdateEvent event) public void updateGoldQueue(UpdateEvent event)
{ {
if (event.getType() != UpdateType.SLOWER) if (event.getType() != UpdateType.SLOWER)
return; return;
for (Player player : _goldQueue.keySet()) for (Player player : _goldQueue.keySet())
{ {
String caller = null; updateGoldQueue(player);
int total = 0;
for (String curCaller : _goldQueue.get(player).keySet())
{
caller = curCaller;
total += _goldQueue.get(player).get(curCaller);
}
if (caller == null)
continue;
//Actually Add Gold
RewardGold(null, caller, player.getName(), ClientManager.Get(player).getAccountId(), total, false);
System.out.println("Queue Added [" + player + "] with Gold [" + total + "] for [" + caller + "]");
//Clean
_goldQueue.get(player).clear();
} }
//Clean //Clean
_goldQueue.clear(); _goldQueue.clear();
} }
public void updateGoldQueue(Player player)
{
String caller = null;
int total = 0;
for (String curCaller : _goldQueue.get(player).keySet())
{
caller = curCaller;
total += _goldQueue.get(player).get(curCaller);
}
if (caller == null)
return;
//Actually Add Gold
rewardGold(null, caller, player.getName(), ClientManager.Get(player).getAccountId(), total, false);
System.out.println("Queue Added [" + player + "] with Gold [" + total + "] for [" + caller + "]");
//Clean
_goldQueue.get(player).clear();
}
public void applyKits(String playerName) public void applyKits(String playerName)
{ {

View File

@ -1,7 +1,10 @@
package mineplex.core.donation.repository; package mineplex.core.donation.repository;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Types;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -190,12 +193,30 @@ public class DonationRepository extends RepositoryBase
{ {
public void run() public void run()
{ {
if (executeUpdate(UPDATE_ACCOUNT_GOLD, new ColumnInt("gold", gold), new ColumnInt("id", accountId)) < 1) try (
Connection connection = getConnection();
CallableStatement statement = connection.prepareCall("{call accountGoldChange(?, ?, ?)}");
)
{ {
callback.run(false); statement.setInt(1, accountId);
statement.setInt(2, gold);
statement.registerOutParameter(3, Types.BOOLEAN);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next())
{
callback.run(resultSet.getBoolean(1));
}
}
catch (SQLException exception)
{
exception.printStackTrace();
}
catch (Exception exception)
{
exception.printStackTrace();
} }
else
callback.run(true);
} }
}), "Error updating player gold amount in DonationRepository : "); }), "Error updating player gold amount in DonationRepository : ");
} }

View File

@ -69,22 +69,27 @@ public class GoldManager extends MiniPlugin
@EventHandler @EventHandler
public void onPlayerDeath(PlayerDeathEvent event) public void onPlayerDeath(PlayerDeathEvent event)
{ {
Player player = event.getEntity(); final Player player = event.getEntity();
Player killer = player.getKiller(); final Player killer = player.getKiller();
int gold = getGold(player); int gold = getGold(player);
int droppedGold = (int) (gold * DEATH_TAX); final int droppedGold = (int) (gold * DEATH_TAX);
if (droppedGold > 0) if (droppedGold > 0)
{ {
deductGold(player, droppedGold); deductGold(new Callback<Boolean>()
notify(player, String.format("You dropped %d gold on your death!", droppedGold));
if (killer != null)
{ {
addGold(killer, droppedGold); public void run(Boolean success)
notify(killer, String.format("You looted %d gold off of %s's corpse!", droppedGold, player.getName())); {
} GoldManager.notify(player, String.format("You dropped %d gold on your death!", droppedGold));
if (killer != null)
{
addGold(killer, droppedGold);
GoldManager.notify(killer, String.format("You looted %d gold off of %s's corpse!", droppedGold, player.getName()));
}
}
},player, droppedGold);
} }
} }
@ -170,14 +175,15 @@ public class GoldManager extends MiniPlugin
return !_transferTracker.hasTransferredToday(player); return !_transferTracker.hasTransferredToday(player);
} }
public void addGold(Callback<Boolean> resultCallback, Player player, int amount) public void addGold(Player player, int amount)
{ {
_donationManager.RewardGoldLater("GoldManager", player, amount); _donationManager.RewardGoldLater("GoldManager", player, amount);
} }
public void deductGold(Callback<Boolean> resultCallback, Player player, int amount) public void deductGold(Callback<Boolean> resultCallback, Player player, int amount)
{ {
addGold(resultCallback, player, -amount); _donationManager.RewardGoldLater("GoldManager", player, amount);
_donationManager.updateGoldQueue(player);
} }
public void cashIn(Player player, GoldToken token) public void cashIn(Player player, GoldToken token)
@ -210,21 +216,21 @@ public class GoldManager extends MiniPlugin
} }
} }
public void purchaseToken(Player player, int tokenValue) public void purchaseToken(final Player player, final int tokenValue)
{ {
GoldToken token = new GoldToken(tokenValue); final GoldToken token = new GoldToken(tokenValue);
deductGold(new Callback<Boolean>() deductGold(new Callback<Boolean>()
{ {
public void run(Boolean success) public void run(Boolean success)
{ {
if (sucess) if (success)
{ {
player.getInventory().addItem(token.toItemStack()); player.getInventory().addItem(token.toItemStack());
notify(player, String.format("You have purchased a gold token worth %dg!", tokenValue)); GoldManager.notify(player, String.format("You have purchased a gold token worth %dg!", tokenValue));
} }
else else
{ {
notify(player, String.format("You have purchased a gold token worth %dg!", tokenValue)); GoldManager.notify(player, String.format("You have purchased a gold token worth %dg!", tokenValue));
} }
} }
}, player, tokenValue); }, player, tokenValue);

View File

@ -52,7 +52,7 @@ public class PvpShopButton<PageType extends ShopPageBase<ClansManager, ?>> imple
} }
else else
{ {
Page.getDonationManager().RewardGold(new Callback<Boolean>() Page.getDonationManager().rewardGold(new Callback<Boolean>()
{ {
public void run(Boolean result) public void run(Boolean result)
{ {
@ -95,7 +95,7 @@ public class PvpShopButton<PageType extends ShopPageBase<ClansManager, ?>> imple
final int creditAmount = removed * Item.getPrice() / 2; final int creditAmount = removed * Item.getPrice() / 2;
System.out.println("Crediting " + player.getName() + " with " + creditAmount + " gold."); System.out.println("Crediting " + player.getName() + " with " + creditAmount + " gold.");
Page.getDonationManager().RewardGold(new Callback<Boolean>() Page.getDonationManager().rewardGold(new Callback<Boolean>()
{ {
public void run(Boolean result) public void run(Boolean result)
{ {

View File

@ -1,7 +1,5 @@
package mineplex.game.clans.shop.farming; package mineplex.game.clans.shop.farming;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.craftbukkit.v1_7_R4.inventory.CraftInventory; import org.bukkit.craftbukkit.v1_7_R4.inventory.CraftInventory;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.ClickType;
@ -11,18 +9,12 @@ import mineplex.core.common.util.Callback;
import mineplex.core.common.util.F; import mineplex.core.common.util.F;
import mineplex.core.common.util.InventoryUtil; import mineplex.core.common.util.InventoryUtil;
import mineplex.core.common.util.UtilPlayer; import mineplex.core.common.util.UtilPlayer;
import mineplex.core.donation.DonationManager;
import mineplex.core.shop.item.IButton; import mineplex.core.shop.item.IButton;
import mineplex.core.shop.page.ShopPageBase; import mineplex.core.shop.page.ShopPageBase;
import mineplex.game.clans.clans.ClanInfo;
import mineplex.game.clans.clans.ClansManager;
import mineplex.game.clans.economy.GoldManager; import mineplex.game.clans.economy.GoldManager;
import mineplex.game.clans.items.CustomItem;
import mineplex.game.clans.items.GearManager;
import mineplex.game.clans.items.economy.GoldToken;
import mineplex.game.clans.shop.PvpItem; import mineplex.game.clans.shop.PvpItem;
public class ShopItemButton<T extends ShopPageBase> implements IButton public class ShopItemButton<T extends ShopPageBase<?, ?>> implements IButton
{ {
private int _buyPrice; private int _buyPrice;
private int _sellPrice; private int _sellPrice;
@ -38,7 +30,7 @@ public class ShopItemButton<T extends ShopPageBase> implements IButton
} }
@Override @Override
public void onClick(Player player, ClickType clickType) public void onClick(final Player player, ClickType clickType)
{ {
boolean shiftClick = (clickType == ClickType.SHIFT_LEFT || clickType == ClickType.SHIFT_RIGHT); boolean shiftClick = (clickType == ClickType.SHIFT_LEFT || clickType == ClickType.SHIFT_RIGHT);
@ -70,16 +62,29 @@ public class ShopItemButton<T extends ShopPageBase> implements IButton
} }
else if (clickType == ClickType.SHIFT_LEFT || clickType == ClickType.LEFT) else if (clickType == ClickType.SHIFT_LEFT || clickType == ClickType.LEFT)
{ {
int amount = shiftClick ? 64 : 1; final int amount = shiftClick ? 64 : 1;
int cost = amount * _buyPrice; final int cost = amount * _buyPrice;
int goldCount = GoldManager.getInstance().getGold(player); int goldCount = GoldManager.getInstance().getGold(player);
if (goldCount >= cost) if (goldCount >= cost)
{ {
GoldManager.getInstance().deductGold(player, cost); GoldManager.getInstance().deductGold(new Callback<Boolean>()
giftItem(player, amount); {
GoldManager.notify(player, String.format("You have purchased %d items for %dg!", amount, cost)); public void run(Boolean success)
_page.playAcceptSound(player); {
if (success)
{
giftItem(player, amount);
GoldManager.notify(player, String.format("You have purchased %d items for %dg!", amount, cost));
_page.playAcceptSound(player);
}
else
{
GoldManager.notify(player, "You cannot afford that item! Please relog to update your gold count!");
_page.playDenySound(player);
}
}
}, player, cost);
} }
else else
{ {