Use separate gold balances for each clans server

This commit is contained in:
cnr 2016-07-27 15:46:26 -05:00
parent 031a9575ae
commit dfbebe11f4
3 changed files with 62 additions and 34 deletions

View File

@ -267,7 +267,6 @@ public class ClansManager extends MiniClientPlugin<ClientClan>implements IRelati
_blacklist = new ClansBlacklist(plugin);
_goldManager = new GoldManager(this, _clientManager, donationManager);
_gearManager = gearManager;
_lootManager = new LootManager(gearManager, _goldManager);
_disguiseManager = new DisguiseManager(plugin, packetHandler);
@ -384,13 +383,15 @@ public class ClansManager extends MiniClientPlugin<ClientClan>implements IRelati
_playTracker = new Playtime(this, statsManager);
_legacyTutorial = new mineplex.game.clans.legacytutorial.TutorialManager(plugin, _playTracker, _goldManager, _taskManager, donationManager, preferencesManager, this, packetHandler);
_scoreboard = new ClansScoreboardManager(plugin, this, _warManager, _worldEvent, _tutorial, clientManager, donationManager);
_clanDataAccess = new ClansDataAccessLayer(this, _scoreboard);
_bannerManager = new BannerManager(plugin);
_goldManager = new GoldManager(this, _clientManager, donationManager, _clanDataAccess);
_legacyTutorial = new mineplex.game.clans.legacytutorial.TutorialManager(plugin, _playTracker, _goldManager, _taskManager, donationManager, preferencesManager, this, packetHandler);
for (ClanToken token : _clanDataAccess.getRepository().retrieveClans())
{
loadClan(token);

View File

@ -8,6 +8,7 @@ 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;
@ -41,6 +42,7 @@ public class GoldManager extends MiniDbClientPlugin<GoldData>
public static GoldManager getInstance() { return _instance; }
private DonationManager _donationManager;
private final int _serverId;
private TransferTracker _transferTracker;
private Set<Item> _itemSet;
private Map<Player, Integer> _playerPickupMap;
@ -48,17 +50,18 @@ public class GoldManager extends MiniDbClientPlugin<GoldData>
private final GoldRepository _repository;
public GoldManager(ClansManager plugin, CoreClientManager clientManager, DonationManager donationManager)
public GoldManager(ClansManager plugin, CoreClientManager clientManager, DonationManager donationManager, ClansDataAccessLayer dataAccessLayer)
{
super("Clans Gold", plugin.getPlugin(), clientManager);
_instance = this;
_donationManager = donationManager;
_serverId = dataAccessLayer.getRepository().getServerId();
_transferTracker = new TransferTracker();
_itemSet = new HashSet<Item>();
_playerPickupMap = new HashMap<Player, Integer>();
_bankShop = new BankShop(plugin, clientManager, donationManager);
_repository = new GoldRepository(plugin.getPlugin());
_repository = new GoldRepository(_serverId);
}
@Override
@ -396,7 +399,7 @@ public class GoldManager extends MiniDbClientPlugin<GoldData>
@Override
public String getQuery(int accountId, String uuid, String name) {
return "SELECT gold FROM accounts WHERE id = '" + accountId + "';";
return "SELECT gold FROM clansGold WHERE id = '" + accountId + "' AND serverId=" + _serverId + ";";
}
@Override

View File

@ -1,31 +1,47 @@
package mineplex.game.clans.economy;
import mineplex.core.common.util.Callback;
import mineplex.core.database.MinecraftRepository;
import mineplex.serverdata.database.DBPool;
import mineplex.serverdata.database.DatabaseRunnable;
import mineplex.serverdata.database.column.ColumnInt;
import org.bukkit.plugin.java.JavaPlugin;
public class GoldRepository extends MinecraftRepository {
private static final String UPDATE_ACCOUNT_GOLD = "UPDATE accounts SET gold = gold + ? WHERE id = ? && gold >= ?;";
private static final String SET_ACCOUNT_GOLD = "UPDATE accounts SET gold = ? WHERE id = ?;";
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public GoldRepository(JavaPlugin plugin) {
super(plugin, DBPool.getAccount());
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)
{
handleDatabaseCall(new DatabaseRunnable(new Runnable()
try (Connection connection = DBPool.getAccount().getConnection())
{
public void run()
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)
{
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);
callback.run(true);
}
}), "Error updating player gold amount in DonationRepository : ");
} catch (SQLException e) {
e.printStackTrace();
if (callback != null)
{
callback.run(false);
}
}
}
public void setGold(final Callback<Boolean> callback, final int accountId, final int gold)
@ -34,19 +50,27 @@ public class GoldRepository extends MinecraftRepository {
{
throw new IllegalArgumentException("gold cannot be negative");
}
handleDatabaseCall(new DatabaseRunnable(new Runnable()
try (Connection connection = DBPool.getAccount().getConnection())
{
public void run()
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)
{
boolean success = executeUpdate(SET_ACCOUNT_GOLD, new ColumnInt("gold", gold), new ColumnInt("id", accountId)) > 0;
callback.run(success);
callback.run(true);
}
}), "Error updating player gold amount in DonationRepository : ");
} catch (SQLException e) {
e.printStackTrace();
if (callback != null)
{
callback.run(false);
}
}
}
@Override
protected void initialize() {}
@Override
protected void update() {}
}