add a shard queue to DonationManager, use that for TreasureShardReward

This commit is contained in:
Shaun Bennett 2016-02-02 05:34:33 -05:00
parent 1a9c36d0a1
commit af27944a39
6 changed files with 150 additions and 78 deletions

View File

@ -7,8 +7,6 @@ import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
import java.util.TimeZone;
@ -17,7 +15,6 @@ import mineplex.core.MiniClientPlugin;
import mineplex.core.account.CoreClient;
import mineplex.core.account.CoreClientManager;
import mineplex.core.account.ILoginProcessor;
import mineplex.core.account.IQuerylessLoginProcessor;
import mineplex.core.account.event.ClientUnloadEvent;
import mineplex.core.bonuses.redis.VoteHandler;
import mineplex.core.bonuses.redis.VotifierCommand;
@ -32,6 +29,7 @@ import mineplex.core.common.util.UtilParticle.ParticleType;
import mineplex.core.common.util.UtilParticle.ViewDist;
import mineplex.core.database.DBPool;
import mineplex.core.donation.DonationManager;
import mineplex.core.donation.GiveDonorData;
import mineplex.core.facebook.FacebookManager;
import mineplex.core.giveaway.GiveawayManager;
import mineplex.core.hologram.Hologram;
@ -59,7 +57,6 @@ import mineplex.core.bonuses.gui.SpinGui;
import mineplex.core.poll.PollManager;
import mineplex.serverdata.commands.ServerCommandManager;
import org.jooq.DSLContext;
import org.jooq.SQLDialect;
import org.jooq.impl.DSL;
import org.bukkit.Bukkit;
@ -755,7 +752,7 @@ public class BonusManager extends MiniClientPlugin<BonusClientData> implements I
if (gems > 0)
{
UtilPlayer.message(player, F.main("Carl", "Rewarded " + F.elem(gems + " Gems")));
_gemQueue.add(new GiveDonorData(player.getName(), coreClient.getAccountId(), player.getUniqueId(), gems));
_gemQueue.add(new GiveDonorData(null, player.getName(), "Treasure", player.getUniqueId(), coreClient.getAccountId(), gems));
}
if (gold > 0)
@ -780,7 +777,7 @@ public class BonusManager extends MiniClientPlugin<BonusClientData> implements I
if (coins > 0)
{
UtilPlayer.message(player, F.main("Carl", "Rewarded " + F.elem(coins + " Treasure Shards")));
_coinQueue.add(new GiveDonorData(player.getName(), coreClient.getAccountId(), player.getUniqueId(), coins));
_coinQueue.add(new GiveDonorData(null, player.getName(), "Treasure", player.getUniqueId(), coreClient.getAccountId(), coins));
}
if (tickets > 0)

View File

@ -1,50 +0,0 @@
package mineplex.core.bonuses;
import java.util.UUID;
public class GiveDonorData
{
private String _playerName;
private int _accountId;
private UUID _uuid;
private int _giveAmount;
private int _attempts;
public GiveDonorData(String playerName, int accountId, UUID uuid, int giveAmount)
{
_playerName = playerName;
_accountId = accountId;
_uuid = uuid;
_giveAmount = giveAmount;
}
public String getPlayerName()
{
return _playerName;
}
public int getAccountId()
{
return _accountId;
}
public UUID getUuid()
{
return _uuid;
}
public int getGiveAmount()
{
return _giveAmount;
}
public int getAttempts()
{
return _attempts;
}
public void incrementAttempts()
{
_attempts++;
}
}

View File

@ -2,6 +2,8 @@ package mineplex.core.donation;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.UUID;
import org.bukkit.Bukkit;
@ -30,17 +32,23 @@ import mineplex.core.updater.event.UpdateEvent;
public class DonationManager extends MiniDbClientPlugin<Donor>
{
private final int MAX_GIVE_ATTEMPTS = 10;
private DonationRepository _repository;
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>>();
private Queue<GiveDonorData> _coinAttemptQueue;
public DonationManager(JavaPlugin plugin, CoreClientManager clientManager, String webAddress)
{
super("Donation", plugin, clientManager);
_repository = new DonationRepository(plugin, webAddress);
_coinAttemptQueue = new LinkedList<>();
}
@Override
@ -204,6 +212,11 @@ public class DonationManager extends MiniDbClientPlugin<Donor>
_gemQueue.clear();
}
public void rewardCoinsUntilSuccess(Callback<Boolean> callback, String caller, String name, int accountId, int amount)
{
_coinAttemptQueue.add(new GiveDonorData(callback, name, caller, accountId, amount));
}
public void RewardCoins(Callback<Boolean> callback, String caller, String name, int accountId, int amount)
{
RewardCoins(callback, caller, name, accountId, amount, true);
@ -298,6 +311,57 @@ public class DonationManager extends MiniDbClientPlugin<Donor>
//Clean
_coinQueue.clear();
}
@EventHandler
public void processCoinAttemptQueue(UpdateEvent event)
{
if (event.getType() != UpdateType.FAST)
return;
GiveDonorData data = _coinAttemptQueue.poll();
if (data != null)
{
_repository.rewardCoins(new Callback<Boolean>()
{
@Override
public void run(Boolean success)
{
if (success)
{
Donor donor = Get(data.getPlayerName());
if (donor != null)
{
donor.addCoins(data.getGiveAmount());
}
if (data.getCallback() != null) data.getCallback().run(true);
System.out.println("Successfully rewarded shards to player " + data.getPlayerName());
}
else
{
data.incrementAttempts();
if (data.getAttempts() >= MAX_GIVE_ATTEMPTS)
{
// Admit Defeat!
if (data.getCallback() != null) data.getCallback().run(false);
System.out.println("Gave up giving shards to player " + data.getPlayerName());
}
else
{
// Add again to the back of queue
_coinAttemptQueue.add(data);
System.out.println("Failed to reward shards to player " + data.getPlayerName() + ". Attempts: " + data.getAttempts());
}
}
}
}, data.getCaller(), data.getPlayerName(), data.getAccountId(), data.getGiveAmount());
}
}
public void rewardGold(Callback<Boolean> callback, String caller, Player player, int amount)
{

View File

@ -0,0 +1,71 @@
package mineplex.core.donation;
import mineplex.core.common.util.Callback;
import java.util.UUID;
public class GiveDonorData
{
private final String _playerName;
private final String _caller;
private final UUID _uuid;
private final int _accountId;
private final int _giveAmount;
private final Callback<Boolean> _callback;
private int _attempts;
public GiveDonorData(Callback<Boolean> callback, String playerName, String caller, UUID uuid, int accountId, int giveAmount)
{
_callback = callback;
_playerName = playerName;
_caller = caller;
_uuid = uuid;
_accountId = accountId;
_giveAmount = giveAmount;
}
public GiveDonorData(Callback<Boolean> callback, String playerName, String caller, int accountId, int giveAmount)
{
this(callback, playerName, caller, null, accountId, giveAmount);
}
public UUID getUuid()
{
return _uuid;
}
public Callback<Boolean> getCallback()
{
return _callback;
}
public String getPlayerName()
{
return _playerName;
}
public String getCaller()
{
return _caller;
}
public int getAccountId()
{
return _accountId;
}
public int getGiveAmount()
{
return _giveAmount;
}
public int getAttempts()
{
return _attempts;
}
public void incrementAttempts()
{
_attempts++;
}
}

View File

@ -513,7 +513,7 @@ public class RewardManager
if (reward.canGiveReward(player))
return reward;
else
return new TreasureShardReward(_donationManager, reward, 1, reward.getRarity());
return new TreasureShardReward(_clientManager, _donationManager, reward, 1, reward.getRarity());
}
}
}

View File

@ -1,5 +1,6 @@
package mineplex.core.reward.rewards;
import mineplex.core.account.CoreClientManager;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
@ -14,15 +15,16 @@ import mineplex.core.reward.RewardType;
public class TreasureShardReward extends Reward
{
private CoreClientManager _clientManager;
private DonationManager _donationManager;
private Reward _otherReward;
private int _shards = 0, _task;
private boolean _success = true;
private int _shards = 0;
public TreasureShardReward(DonationManager donationManager, Reward otherReward, int weight, RewardRarity rarity)
public TreasureShardReward(CoreClientManager clientManager, DonationManager donationManager, Reward otherReward, int weight, RewardRarity rarity)
{
super(rarity, weight, 0);
_clientManager = clientManager;
_donationManager = donationManager;
_otherReward = otherReward;
@ -35,33 +37,21 @@ public class TreasureShardReward extends Reward
RewardData fakeData = _otherReward.getFakeRewardData(player);
RewardData rewardData = new RewardData(fakeData.getHeader(), fakeData.getFriendlyName(), fakeData.getDisplayItem(), fakeData.getRarity(), _shards);
_task = Bukkit.getScheduler().scheduleSyncRepeatingTask(_donationManager.getPlugin(), new Runnable()
int accountId = _clientManager.getAccountId(player);
// Give shards 5 seconds later for better effect
Bukkit.getScheduler().runTaskLater(_donationManager.getPlugin(), new Runnable()
{
@Override
public void run()
{
if(hasRewarded(fakeData, player))
Bukkit.getScheduler().cancelTask(_task);
_donationManager.rewardCoinsUntilSuccess(null, "Treasure", player.getName(), accountId, _shards);
}
}, 0, 3);
}, 100);
return rewardData;
}
public boolean hasRewarded(RewardData fakeData, Player player)
{
_donationManager.RewardCoins(new Callback<Boolean>()
{
@Override
public void run(Boolean data)
{
_success = data;
}
}, "Treasure Chest", player.getName(), _donationManager.getClientManager().Get(player).getAccountId(), _shards);
return _success;
}
@Override
public RewardData getFakeRewardData(Player player)
{