Added Gold saving/retrieval from mysql.

Converted DonationManager to MiniDbClientPlugin.
Fixed bug in DonationRepository with gold.
This commit is contained in:
Jonathan Williams 2014-12-29 06:42:20 -05:00
parent 2480361421
commit 5a57c5f372
3 changed files with 60 additions and 68 deletions

View File

@ -1,5 +1,7 @@
package mineplex.core.donation; package mineplex.core.donation;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -8,38 +10,31 @@ import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.MiniPlugin; import mineplex.core.MiniDbClientPlugin;
import mineplex.core.account.event.ClientUnloadEvent; import mineplex.core.account.CoreClientManager;
import mineplex.core.account.event.ClientWebResponseEvent; import mineplex.core.account.event.ClientWebResponseEvent;
import mineplex.core.common.CurrencyType; import mineplex.core.common.CurrencyType;
import mineplex.core.common.util.Callback; import mineplex.core.common.util.Callback;
import mineplex.core.common.util.NautHashMap; import mineplex.core.common.util.NautHashMap;
import mineplex.core.donation.repository.DonationRepository; import mineplex.core.donation.repository.DonationRepository;
import mineplex.core.donation.repository.token.DonorTokenWrapper; import mineplex.core.donation.repository.token.DonorTokenWrapper;
import mineplex.core.server.remotecall.JsonWebCall;
import mineplex.core.server.util.TransactionResponse; import mineplex.core.server.util.TransactionResponse;
import mineplex.core.updater.UpdateType; import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent; import mineplex.core.updater.event.UpdateEvent;
public class DonationManager extends MiniPlugin public class DonationManager extends MiniDbClientPlugin<Donor>
{ {
private DonationRepository _repository; private DonationRepository _repository;
private NautHashMap<String, Donor> _donors;
private Object _donorLock = new Object();
private NautHashMap<Player, NautHashMap<String, Integer>> _gemQueue = new NautHashMap<Player, NautHashMap<String, Integer>>(); 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>> _coinQueue = new NautHashMap<Player, NautHashMap<String, Integer>>();
private NautHashMap<Player, NautHashMap<String, Integer>> _goldQueue = 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) public DonationManager(JavaPlugin plugin, CoreClientManager clientManager, String webAddress)
{ {
super("Donation", plugin); super("Donation", plugin, clientManager);
_repository = new DonationRepository(plugin, webAddress); _repository = new DonationRepository(plugin, webAddress);
_donors = new NautHashMap<String, Donor>();
} }
@Override @Override
@ -56,31 +51,11 @@ public class DonationManager extends MiniPlugin
LoadDonor(token, event.getUniqueId()); LoadDonor(token, event.getUniqueId());
} }
@EventHandler
public void UnloadDonor(ClientUnloadEvent event)
{
synchronized (_donorLock)
{
_donors.remove(event.GetName());
}
}
private void LoadDonor(DonorTokenWrapper token, UUID uuid) private void LoadDonor(DonorTokenWrapper token, UUID uuid)
{ {
synchronized (_donorLock) Get(token.Name).loadToken(token.DonorToken);
{
_donors.put(token.Name, new Donor(token.DonorToken));
//_repository.updateGemsAndCoins(uuid, Get(token.Name).GetGems(), Get(token.Name).getCoins()); //_repository.updateGemsAndCoins(uuid, Get(token.Name).GetGems(), Get(token.Name).getCoins());
} }
}
public Donor Get(String name)
{
synchronized (_donorLock)
{
return _donors.get(name);
}
}
public void PurchaseUnknownSalesPackage(final Callback<TransactionResponse> callback, final String name, final UUID uuid, final String packageName, final boolean coinPurchase, final int cost, boolean oneTimePurchase) public void PurchaseUnknownSalesPackage(final Callback<TransactionResponse> callback, final String name, final UUID uuid, final String packageName, final boolean coinPurchase, final int cost, boolean oneTimePurchase)
{ {
@ -324,11 +299,15 @@ public class DonationManager extends MiniPlugin
donor.addGold(amount); donor.addGold(amount);
} }
} }
}
else
{
System.out.println("REWARD GOLD FAILED...");
}
if (callback != null) if (callback != null)
callback.run(true); callback.run(true);
} }
}
}, caller, name, uuid.toString(), amount); }, caller, name, uuid.toString(), amount);
} }
@ -388,4 +367,22 @@ public class DonationManager extends MiniPlugin
{ {
_repository.applyKits(playerName); _repository.applyKits(playerName);
} }
@Override
public void processLoginResultSet(String playerName, ResultSet resultSet) throws SQLException
{
Get(playerName).addGold(_repository.retrieveDonorInfo(resultSet).getGold());
}
@Override
public String getQuery(String uuid, String name)
{
return "SELECT gold FROM accounts WHERE uuid = '" + uuid + "';";
}
@Override
protected Donor AddPlayer(String player)
{
return new Donor();
}
} }

View File

@ -14,14 +14,16 @@ public class Donor
private int _coins; private int _coins;
private int _gold; private int _gold;
private boolean _donated; private boolean _donated;
private List<Integer> _salesPackagesOwned; private List<Integer> _salesPackagesOwned = new ArrayList<Integer>();
private List<String> _unknownSalesPackagesOwned; private List<String> _unknownSalesPackagesOwned = new ArrayList<String>();
private List<TransactionToken> _transactions; private List<TransactionToken> _transactions = new ArrayList<TransactionToken>();
private List<CoinTransactionToken> _coinTransactions; private List<CoinTransactionToken> _coinTransactions = new ArrayList<CoinTransactionToken>();
private boolean _update = true; private boolean _update = true;
public Donor(DonorToken token) public Donor() { }
public void loadToken(DonorToken token)
{ {
_gems = token.Gems; _gems = token.Gems;
_coins = token.Coins; _coins = token.Coins;
@ -31,26 +33,6 @@ public class Donor
_unknownSalesPackagesOwned = token.UnknownSalesPackages; _unknownSalesPackagesOwned = token.UnknownSalesPackages;
_transactions = token.Transactions; _transactions = token.Transactions;
_coinTransactions = token.CoinRewards; _coinTransactions = token.CoinRewards;
if (_salesPackagesOwned == null)
{
_salesPackagesOwned = new ArrayList<Integer>();
}
if (_unknownSalesPackagesOwned == null)
{
_unknownSalesPackagesOwned = new ArrayList<String>();
}
if (_transactions == null)
{
_transactions = new ArrayList<TransactionToken>();
}
if (_coinTransactions == null)
{
_coinTransactions = new ArrayList<CoinTransactionToken>();
}
} }
public int GetGems() public int GetGems()

View File

@ -1,5 +1,7 @@
package mineplex.core.donation.repository; package mineplex.core.donation.repository;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -10,6 +12,7 @@ import mineplex.core.database.DatabaseRunnable;
import mineplex.core.database.RepositoryBase; import mineplex.core.database.RepositoryBase;
import mineplex.core.database.column.ColumnInt; import mineplex.core.database.column.ColumnInt;
import mineplex.core.database.column.ColumnVarChar; import mineplex.core.database.column.ColumnVarChar;
import mineplex.core.donation.Donor;
import mineplex.core.donation.repository.token.GemRewardToken; import mineplex.core.donation.repository.token.GemRewardToken;
import mineplex.core.donation.repository.token.PurchaseToken; import mineplex.core.donation.repository.token.PurchaseToken;
import mineplex.core.donation.repository.token.UnknownPurchaseToken; import mineplex.core.donation.repository.token.UnknownPurchaseToken;
@ -179,11 +182,6 @@ public class DonationRepository extends RepositoryBase
public void rewardGold(final Callback<Boolean> callback, final String giver, final String name, final String uuid, final int gold) 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() handleDatabaseCall(new DatabaseRunnable(new Runnable()
{ {
public void run() public void run()
@ -191,7 +189,10 @@ public class DonationRepository extends RepositoryBase
if (executeUpdate(UPDATE_ACCOUNT_GOLD, new ColumnInt("gold", gold), new ColumnVarChar("uuid", 100, uuid)) < 1) 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); Logger.Instance.log("Account gold wasn't updated for " + name);
callback.run(false);
} }
else
callback.run(true);
} }
}), "Error updating player gold amount in DonationRepository : "); }), "Error updating player gold amount in DonationRepository : ");
} }
@ -223,4 +224,16 @@ public class DonationRepository extends RepositoryBase
{ {
new AsyncJsonWebCall(_webAddress + "PlayerAccount/ApplyKits").Execute(playerName); new AsyncJsonWebCall(_webAddress + "PlayerAccount/ApplyKits").Execute(playerName);
} }
public Donor retrieveDonorInfo(ResultSet resultSet) throws SQLException
{
Donor donor = new Donor();
while (resultSet.next())
{
donor.addGold(resultSet.getInt(1));
}
return donor;
}
} }