Added some base Clans shops.

Added support for Gold

Refactored failed db call code a bit.
This commit is contained in:
Jonathan Williams 2014-12-04 23:41:49 -08:00
parent fbcc774383
commit 3d3f9012ff
9 changed files with 214 additions and 41 deletions

View File

@ -0,0 +1,6 @@
package mineplex.core.common.util;
public interface RunnableStatus
{
public boolean run();
}

View File

@ -11,7 +11,6 @@ import java.util.Iterator;
import mineplex.core.common.util.NautHashMap;
import mineplex.core.database.column.Column;
import mineplex.core.logger.Logger;
import mineplex.core.timing.TimingManager;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
@ -229,14 +228,7 @@ public abstract class RepositoryBase implements Listener
}
catch (Exception exception)
{
Logger.Instance.log(errorMessage + exception.getMessage());
databaseRunnable.incrementFailCount();
synchronized (_queueLock)
{
_failedQueue.put(databaseRunnable, errorMessage);
}
processFailedDatabaseCall(databaseRunnable, exception.getMessage(), errorMessage);
}
}
});
@ -244,6 +236,25 @@ public abstract class RepositoryBase implements Listener
asyncThread.start();
}
protected void processFailedDatabaseCall(DatabaseRunnable databaseRunnable, String errorPreMessage, String runnableMessage)
{
if (databaseRunnable.getFailedCounts() < 4)
{
databaseRunnable.incrementFailCount();
synchronized (_queueLock)
{
_failedQueue.put(databaseRunnable, runnableMessage);
}
Logger.Instance.log(errorPreMessage + runnableMessage);
}
else
{
Logger.Instance.log("Abandoning database call : " + runnableMessage);
}
}
@EventHandler
public void processDatabaseQueue(UpdateEvent event)
{
@ -259,37 +270,8 @@ public abstract class RepositoryBase implements Listener
{
for (Iterator<DatabaseRunnable> runnablesIterator = _failedQueue.keySet().iterator(); runnablesIterator.hasNext();)
{
final DatabaseRunnable databaseRunnable = runnablesIterator.next();
Thread asyncThread = new Thread(new Runnable()
{
public void run()
{
try
{
databaseRunnable.run();
}
catch (Exception exception)
{
Logger.Instance.log(_failedQueue.get(databaseRunnable) + exception.getMessage());
if (databaseRunnable.getFailedCounts() < 4)
{
synchronized (_queueLock)
{
_failedQueue.put(databaseRunnable, _failedQueue.get(databaseRunnable));
}
}
else
{
Logger.Instance.log("Abandoning database call : " + _failedQueue.get(databaseRunnable));
}
}
}
});
runnablesIterator.remove();
asyncThread.start();
DatabaseRunnable databaseRunnable = runnablesIterator.next();
handleDatabaseCall(databaseRunnable, _failedQueue.get(databaseRunnable));
}
}
}

View File

@ -30,6 +30,7 @@ public class DonationManager extends MiniPlugin
private NautHashMap<Player, NautHashMap<String, Integer>> _gemQueue = new NautHashMap<Player, NautHashMap<String, Integer>>();
private NautHashMap<Player, NautHashMap<String, Integer>> _coinQueue = new NautHashMap<Player, NautHashMap<String, Integer>>();
private NautHashMap<Player, NautHashMap<String, Integer>> _goldQueue = new NautHashMap<Player, NautHashMap<String, Integer>>();
public DonationManager(JavaPlugin plugin, String webAddress)
{
@ -302,6 +303,88 @@ public class DonationManager extends MiniPlugin
_coinQueue.clear();
}
public void RewardGold(Callback<Boolean> callback, String caller, String name, UUID uuid, int amount)
{
RewardGold(callback, caller, name, uuid, amount, true);
}
public void RewardGold(final Callback<Boolean> callback, final String caller, final String name, final UUID uuid, final int amount, final boolean updateTotal)
{
_repository.rewardGold(new Callback<Boolean>()
{
public void run(Boolean success)
{
if (success)
{
if (updateTotal)
{
Donor donor = Get(name);
if (donor != null)
{
donor.addGold(amount);
}
}
if (callback != null)
callback.run(true);
}
}
}, caller, name, uuid.toString(), amount);
}
public void RewardGoldLater(final String caller, final Player player, final int amount)
{
if (!_goldQueue.containsKey(player))
_goldQueue.put(player, new NautHashMap<String, Integer>());
int totalAmount = amount;
if (_goldQueue.get(player).containsKey(caller))
totalAmount += _goldQueue.get(player).get(caller);
_goldQueue.get(player).put(caller, totalAmount);
//Do Temp Change
Donor donor = Get(player.getName());
if (donor != null)
donor.addGold(amount);
}
@EventHandler
public void UpdateGoldQueue(UpdateEvent event)
{
if (event.getType() != UpdateType.SLOWER)
return;
for (Player player : _goldQueue.keySet())
{
String caller = null;
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(), player.getUniqueId(), total, false);
System.out.println("Queue Added [" + player + "] with Gold [" + total + "] for [" + caller + "]");
//Clean
_goldQueue.get(player).clear();
}
//Clean
_goldQueue.clear();
}
public void applyKits(String playerName)
{
_repository.applyKits(playerName);

View File

@ -12,6 +12,7 @@ public class Donor
{
private int _gems;
private int _coins;
private int _gold;
private boolean _donated;
private List<Integer> _salesPackagesOwned;
private List<String> _unknownSalesPackagesOwned;
@ -160,8 +161,18 @@ public class Donor
_coins += amount;
}
public void addGold(int amount)
{
_gold += amount;
}
public List<CoinTransactionToken> getCoinTransactions()
{
return _coinTransactions;
}
public int getGold()
{
return _gold;
}
}

View File

@ -13,6 +13,7 @@ import mineplex.core.database.column.ColumnVarChar;
import mineplex.core.donation.repository.token.GemRewardToken;
import mineplex.core.donation.repository.token.PurchaseToken;
import mineplex.core.donation.repository.token.UnknownPurchaseToken;
import mineplex.core.logger.Logger;
import mineplex.core.server.remotecall.AsyncJsonWebCall;
import mineplex.core.server.remotecall.JsonWebCall;
import mineplex.core.server.util.TransactionResponse;
@ -23,6 +24,7 @@ public class DonationRepository extends RepositoryBase
private static String CREATE_GEM_TRANSACTION_TABLE = "CREATE TABLE IF NOT EXISTS accountGemTransactions (id INT NOT NULL AUTO_INCREMENT, accounts_uuid VARCHAR(100), reason VARCHAR(100), gems INT, PRIMARY KEY (id), FOREIGN KEY (accounts_uuid) REFERENCES accounts(uuid), INDEX gemUuidIndex (accounts_uuid));";
private static String INSERT_COIN_TRANSACTION = "INSERT INTO accountCoinTransactions(accounts_uuid, reason, coins) VALUES(?, ?, ?);";
private static String UPDATE_ACCOUNT_COINS = "UPDATE accounts SET coins = coins + ? WHERE uuid = ?;";
private static String UPDATE_ACCOUNT_GOLD = "UPDATE accounts SET gold = gold + ? WHERE uuid = ?;";
private static String UPDATE_NULL_ACCOUNT_GEMS_AND_COINS_ = "UPDATE accounts SET gems = ?, coins = ? WHERE uuid = ? AND gems IS NULL AND coins IS NULL;";
private String _webAddress;
@ -175,6 +177,25 @@ public class DonationRepository extends RepositoryBase
}), "Error updating player coin amount in DonationRepository : ");
}
public void rewardGold(final Callback<Boolean> callback, final String giver, final String name, final String uuid, final int gold)
{
final GemRewardToken token = new GemRewardToken();
token.Source = giver;
token.Name = name;
token.Amount = gold;
handleDatabaseCall(new DatabaseRunnable(new Runnable()
{
public void run()
{
if (executeUpdate(UPDATE_ACCOUNT_GOLD, new ColumnInt("gold", gold), new ColumnVarChar("uuid", 100, uuid)) < 1)
{
Logger.Instance.log("Account gold wasn't updated for " + name);
}
}
}), "Error updating player gold amount in DonationRepository : ");
}
@Override
protected void initialize()
{

View File

@ -1,5 +1,7 @@
package mineplex.game.clans.shop;
import org.bukkit.Material;
import mineplex.core.account.CoreClientManager;
import mineplex.core.donation.DonationManager;
import mineplex.core.shop.page.ShopPageBase;
@ -15,6 +17,7 @@ public class BuildingPage extends ShopPageBase<ClansManager, BuildingShop>
@Override
protected void BuildPage()
{
PvpItem item = new PvpItem(Material.STONE, (byte)0, 1, "Stone", 25, 64);
AddButton(1, item, new PvpShopButton<BuildingPage>(this, item));
}
}

View File

@ -0,0 +1,25 @@
package mineplex.game.clans.shop;
import org.bukkit.Material;
import mineplex.core.common.util.C;
import mineplex.core.shop.item.ShopItem;
public class PvpItem extends ShopItem
{
private static String LEFT_CLICK_BUY = C.cWhite + "Left-Click to Buy 1";
public PvpItem(Material type, byte data, int displayAmount, String name, int price, int bulkCount)
{
super(type, data, name, new String[]
{
C.cYellow + C.Bold + name,
C.cWhite + " ",
LEFT_CLICK_BUY,
C.cWhite + "Costs " + C.cGreen + "$" + price,
C.cWhite + " ",
C.cWhite + "Shift Left-Click to Buy " + bulkCount,
C.cWhite + "Costs " + C.cGreen + "$" + (price * bulkCount),
}, 0, false, false);
}
}

View File

@ -0,0 +1,42 @@
package mineplex.game.clans.shop;
import org.bukkit.entity.Player;
import mineplex.core.shop.item.ComplexButton;
import mineplex.core.shop.page.ShopPageBase;
import mineplex.game.clans.clans.ClansManager;
public class PvpShopButton<PageType extends ShopPageBase<ClansManager, BuildingShop>> extends ComplexButton
{
protected PageType Page;
protected PvpItem Item;
public PvpShopButton(PageType page, PvpItem item)
{
Page = page;
Item = item;
}
@Override
public void ClickedLeft(Player player)
{
int balance = Page.getDonationManager().Get(player.getName()).getGold();
}
@Override
public void ClickedShiftLeft(Player player)
{
}
@Override
public void ClickedRight(Player player)
{
}
@Override
public void ClickedShiftRight(Player player)
{
}
}

Binary file not shown.