Refactor Clans gold to be set from Core

This commit is contained in:
AlexTheCoder 2017-02-09 23:32:25 -05:00 committed by cnr
parent 9591485bb6
commit a4c875b4bd
10 changed files with 180 additions and 161 deletions

View File

@ -19,6 +19,7 @@ import mineplex.core.account.event.ClientWebResponseEvent;
import mineplex.core.common.currency.GlobalCurrency;
import mineplex.core.donation.command.GemCommand;
import mineplex.core.donation.command.ShardCommand;
import mineplex.core.donation.gold.GoldRepository;
import mineplex.core.donation.repository.DonationRepository;
import mineplex.core.donation.repository.token.DonorTokenWrapper;
import mineplex.core.server.util.TransactionResponse;
@ -43,12 +44,14 @@ public class DonationManager extends MiniClientPlugin<Donor>
private final CoreClientManager _clientManager = require(CoreClientManager.class);
private final DonationRepository _repository;
private final GoldRepository _goldRepository;
private DonationManager()
{
super("Donation");
_repository = new DonationRepository();
_goldRepository = new GoldRepository();
UtilScheduler.runEvery(UpdateType.FAST, this::processCoinAttemptQueue);
}
@ -67,6 +70,11 @@ public class DonationManager extends MiniClientPlugin<Donor>
Get(event.getUniqueId()).loadToken(token.DonorToken);
}
public GoldRepository getGoldRepository()
{
return _goldRepository;
}
/**
* Adds an unknown sales package to the specified {@link Player}

View File

@ -1,87 +1,84 @@
package mineplex.game.clans.economy;
import mineplex.core.common.util.Callback;
import mineplex.core.common.util.UtilServer;
import mineplex.serverdata.database.DBPool;
import org.bukkit.Bukkit;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class GoldRepository {
private static final String CREATE_TABLE = "CREATE TABLE clansGold (serverId int(11) not null, id int(11) not null, gold int not null, primary key (serverId, id), foreign key (serverId) references clanServer(id), foreign key (id) references accounts(id))";
private static final String UPDATE_ACCOUNT_GOLD = "INSERT INTO clansGold (serverId, id, gold) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE gold=gold+?";
private static final String SET_ACCOUNT_GOLD = "INSERT INTO clansGold (serverId, id, gold) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE gold=?";
private final int _serverId;
public GoldRepository(int serverId) {
_serverId = serverId;
}
public void rewardGold(final Callback<Boolean> callback, final int accountId, final int gold)
{
Bukkit.getScheduler().runTaskAsynchronously(UtilServer.getPlugin(), () ->
{
try (Connection connection = DBPool.getAccount().getConnection())
{
PreparedStatement statement = connection.prepareStatement(UPDATE_ACCOUNT_GOLD);
statement.setInt(1, _serverId);
statement.setInt(2, accountId);
statement.setInt(3, gold);
statement.setInt(4, gold);
statement.executeUpdate();
if (callback != null)
{
Bukkit.getScheduler().runTask(UtilServer.getPlugin(), () -> callback.run(true));
}
} catch (SQLException e)
{
e.printStackTrace();
if (callback != null)
{
Bukkit.getScheduler().runTask(UtilServer.getPlugin(), () -> callback.run(false));
}
}
});
}
public void setGold(final Callback<Boolean> callback, final int accountId, final int gold)
{
if (gold < 0)
{
throw new IllegalArgumentException("gold cannot be negative");
}
Bukkit.getScheduler().runTaskAsynchronously(UtilServer.getPlugin(), () ->
{
try (Connection connection = DBPool.getAccount().getConnection())
{
PreparedStatement statement = connection.prepareStatement(SET_ACCOUNT_GOLD);
statement.setInt(1, _serverId);
statement.setInt(2, accountId);
statement.setInt(3, gold);
statement.setInt(4, gold);
statement.executeUpdate();
if (callback != null)
{
Bukkit.getScheduler().runTask(UtilServer.getPlugin(), () -> callback.run(true));
}
} catch (SQLException e)
{
e.printStackTrace();
if (callback != null)
{
Bukkit.getScheduler().runTask(UtilServer.getPlugin(), () -> callback.run(false));
}
}
});
}
}
package mineplex.core.donation.gold;
import mineplex.core.common.util.Callback;
import mineplex.core.common.util.UtilServer;
import mineplex.serverdata.database.DBPool;
import org.bukkit.Bukkit;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class GoldRepository
{
private static final String CREATE_TABLE = "CREATE TABLE clansGold (serverId int(11) not null, id int(11) not null, gold int not null, primary key (serverId, id), foreign key (serverId) references clanServer(id), foreign key (id) references accounts(id))";
private static final String UPDATE_ACCOUNT_GOLD = "INSERT INTO clansGold (serverId, id, gold) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE gold=gold+?";
private static final String SET_ACCOUNT_GOLD = "INSERT INTO clansGold (serverId, id, gold) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE gold=?";
public GoldRepository() {}
public void rewardGold(final Callback<Boolean> callback, final int serverId, final int accountId, final int gold)
{
Bukkit.getScheduler().runTaskAsynchronously(UtilServer.getPlugin(), () ->
{
try (Connection connection = DBPool.getAccount().getConnection())
{
PreparedStatement statement = connection.prepareStatement(UPDATE_ACCOUNT_GOLD);
statement.setInt(1, serverId);
statement.setInt(2, accountId);
statement.setInt(3, gold);
statement.setInt(4, gold);
statement.executeUpdate();
if (callback != null)
{
Bukkit.getScheduler().runTask(UtilServer.getPlugin(), () -> callback.run(true));
}
}
catch (SQLException e)
{
e.printStackTrace();
if (callback != null)
{
Bukkit.getScheduler().runTask(UtilServer.getPlugin(), () -> callback.run(false));
}
}
});
}
public void setGold(final Callback<Boolean> callback, final int serverId, final int accountId, final int gold)
{
if (gold < 0)
{
throw new IllegalArgumentException("gold cannot be negative");
}
Bukkit.getScheduler().runTaskAsynchronously(UtilServer.getPlugin(), () ->
{
try (Connection connection = DBPool.getAccount().getConnection())
{
PreparedStatement statement = connection.prepareStatement(SET_ACCOUNT_GOLD);
statement.setInt(1, serverId);
statement.setInt(2, accountId);
statement.setInt(3, gold);
statement.setInt(4, gold);
statement.executeUpdate();
if (callback != null)
{
Bukkit.getScheduler().runTask(UtilServer.getPlugin(), () -> callback.run(true));
}
}
catch (SQLException e)
{
e.printStackTrace();
if (callback != null)
{
Bukkit.getScheduler().runTask(UtilServer.getPlugin(), () -> callback.run(false));
}
}
});
}
}

View File

@ -4,6 +4,7 @@ import mineplex.core.common.currency.Currency;
import mineplex.core.common.util.C;
import org.bukkit.Material;
public class ClansCurrency {
public class ClansCurrency
{
public static final Currency GOLD = new Currency("Gold", "Gold", C.cGold, Material.GOLD_NUGGET);
}

View File

@ -4,8 +4,6 @@ import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.bukkit.entity.Player;
import mineplex.serverdata.data.Data;
public class GemTransfer implements Data

View File

@ -1,12 +1,12 @@
package mineplex.game.clans.economy;
import mineplex.core.account.CoreClient;
import org.bukkit.entity.Player;
import mineplex.core.command.CommandBase;
import mineplex.core.common.Rank;
import mineplex.core.common.util.Callback;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilPlayer;
import org.bukkit.entity.Player;
public class GoldCommand extends CommandBase<GoldManager>
{
@ -38,7 +38,9 @@ public class GoldCommand extends CommandBase<GoldManager>
Plugin.getClientManager().loadClientByName(targetName, client ->
{
if (client != null)
{
rewardGold(caller, null, targetName, client.getAccountId(), goldString);
}
else
{
UtilPlayer.message(caller, F.main("Gold", "Could not find player " + F.name(targetName)));

View File

@ -4,15 +4,18 @@ public class GoldData
{
private int balance;
public void addBalance(int amount) {
public void addBalance(int amount)
{
balance += amount;
}
public void setBalance(int amount) {
public void setBalance(int amount)
{
balance = amount;
}
public int getBalance() {
public int getBalance()
{
return balance;
}
}
}

View File

@ -1,17 +1,14 @@
package mineplex.game.clans.economy;
import mineplex.core.MiniDbClientPlugin;
import mineplex.core.account.CoreClientManager;
import mineplex.core.common.currency.GlobalCurrency;
import mineplex.core.common.util.*;
import mineplex.core.donation.DonationManager;
import mineplex.core.donation.Donor;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import mineplex.game.clans.clans.ClansDataAccessLayer;
import mineplex.game.clans.clans.ClansManager;
import mineplex.game.clans.items.economy.GoldToken;
import mineplex.game.clans.shop.bank.BankShop;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.bukkit.Location;
import org.bukkit.Material;
@ -28,9 +25,22 @@ import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.util.Vector;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import mineplex.core.MiniDbClientPlugin;
import mineplex.core.account.CoreClientManager;
import mineplex.core.common.currency.GlobalCurrency;
import mineplex.core.common.util.C;
import mineplex.core.common.util.Callback;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilAction;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.donation.DonationManager;
import mineplex.core.donation.Donor;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import mineplex.game.clans.clans.ClansDataAccessLayer;
import mineplex.game.clans.clans.ClansManager;
import mineplex.game.clans.items.economy.GoldToken;
import mineplex.game.clans.shop.bank.BankShop;
public class GoldManager extends MiniDbClientPlugin<GoldData>
{
@ -53,8 +63,6 @@ public class GoldManager extends MiniDbClientPlugin<GoldData>
private Map<Player, Integer> _playerPickupMap;
private BankShop _bankShop;
private final GoldRepository _repository;
public GoldManager(ClansManager plugin, CoreClientManager clientManager, DonationManager donationManager, ClansDataAccessLayer dataAccessLayer)
{
super("Clans Gold", plugin.getPlugin(), clientManager);
@ -66,7 +74,6 @@ public class GoldManager extends MiniDbClientPlugin<GoldData>
_itemSet = new HashSet<Item>();
_playerPickupMap = new HashMap<Player, Integer>();
_bankShop = new BankShop(plugin, clientManager, donationManager);
_repository = new GoldRepository(_serverId);
}
@Override
@ -319,43 +326,46 @@ public class GoldManager extends MiniDbClientPlugin<GoldData>
public void setGold(final Callback<Boolean> callback, final String caller, final String name, final int accountId, final int amount, final boolean updateTotal)
{
_repository.setGold(new Callback<Boolean>()
_donationManager.getGoldRepository().setGold(success ->
{
public void run(Boolean success)
if (success)
{
if (success)
if (updateTotal)
{
if (updateTotal)
{
GoldData data = Get(name);
GoldData data = Get(name);
if (data != null)
{
data.setBalance(amount);
}
if (data != null)
{
data.setBalance(amount);
}
}
else
{
System.out.println("SET GOLD FAILED...");
}
if (callback != null)
callback.run(success);
}
}, accountId, amount);
else
{
System.out.println("SET GOLD FAILED...");
}
if (callback != null)
{
callback.run(success);
}
}, _serverId, accountId, amount);
}
public void addGold(Player player, int amount)
{
if (amount >= 0)
{
rewardGold(null, player, amount, true);
}
}
public void deductGold(Callback<Boolean> resultCallback, Player player, int amount)
{
if (amount > 0)
{
rewardGold(resultCallback, player, -amount, true);
}
}
public void rewardGold(final Callback<Boolean> callback, final Player player, final int amount, final boolean updateTotal)
@ -374,33 +384,31 @@ public class GoldManager extends MiniDbClientPlugin<GoldData>
return;
}
_repository.rewardGold(new Callback<Boolean>()
_donationManager.getGoldRepository().rewardGold(success ->
{
public void run(Boolean success)
if (success)
{
if (success)
if (updateTotal)
{
if (updateTotal)
{
GoldData data = Get(name);
GoldData data = Get(name);
if (data != null)
{
data.addBalance(amount);
}
if (data != null)
{
data.addBalance(amount);
}
}
else
{
System.out.println("REWARD GOLD FAILED...");
}
if (callback != null)
callback.run(success);
}
}, accountId, amount);
}
else
{
System.out.println("REWARD GOLD FAILED...");
}
if (callback != null)
{
callback.run(success);
}
}, _serverId, accountId, amount);
}
@Override
public String getQuery(int accountId, String uuid, String name)
@ -440,4 +448,4 @@ public class GoldManager extends MiniDbClientPlugin<GoldData>
{
return new GoldData();
}
}
}

View File

@ -9,7 +9,8 @@ import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
public class GoldPurchaseProcessor implements ConfirmationProcessor {
public class GoldPurchaseProcessor implements ConfirmationProcessor
{
private final Player _player;
private final int _price;
private final GoldManager _goldManager;
@ -24,7 +25,8 @@ public class GoldPurchaseProcessor implements ConfirmationProcessor {
}
@Override
public void init(Inventory inventory) {
public void init(Inventory inventory)
{
inventory.setItem(4, new ItemBuilder(ClansCurrency.GOLD.getDisplayMaterial()).setTitle(ClansCurrency.GOLD.getPrefix()).addLore(C.cGray + _price + " " + ClansCurrency.GOLD.getPrefix() + " will be", C.cGray + "deducted from your account balance").build());
}
@ -33,7 +35,9 @@ public class GoldPurchaseProcessor implements ConfirmationProcessor {
int goldCount = _goldManager.Get(_player).getBalance();
if (_price > goldCount)
{
showResults(callback, TransactionResponse.InsufficientFunds);
}
else
{
_goldManager.rewardGold(data ->
@ -71,4 +75,4 @@ public class GoldPurchaseProcessor implements ConfirmationProcessor {
break;
}
}
}
}

View File

@ -1,14 +1,12 @@
package mineplex.game.clans.economy;
import mineplex.core.account.CoreClient;
import org.bukkit.entity.Player;
import mineplex.core.command.CommandBase;
import mineplex.core.common.Rank;
import mineplex.core.common.util.Callback;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.donation.DonationManager;
import org.bukkit.entity.Player;
public class SetGoldCommand extends CommandBase<GoldManager>
{
@ -67,4 +65,4 @@ public class SetGoldCommand extends CommandBase<GoldManager>
}
}, caller.getName(), targetName, accountId, gold, true);
}
}
}

View File

@ -34,4 +34,4 @@ public class TransferTracker
GemTransfer transfer = _repository.getElement(player.getName());
return transfer != null && transfer.transferWasToday();
}
}
}