From 4f6dfdb856d9ab918e1aacd25532236fcbb3bc7d Mon Sep 17 00:00:00 2001 From: Shaun Bennett Date: Sat, 8 Aug 2015 06:35:25 -0500 Subject: [PATCH] Update carl spin button to consume tickets, Updates to Votifier Plugin, Redis Vote Command, Various bug fixes --- .../core/votifier/RedisVotifierCommand.java | 17 --- .../core/votifier/VotifierCommand.java | 23 ++++ .../src/mineplex/hub/bonuses/BonusAmount.java | 1 - .../mineplex/hub/bonuses/BonusManager.java | 105 +++++++++++++++++- .../mineplex/hub/bonuses/BonusRepository.java | 65 +++++++++++ .../mineplex/hub/bonuses/StreakRecord.java | 23 ++++ .../src/mineplex/hub/bonuses/VoteHandler.java | 31 ++++++ .../mineplex/hub/bonuses/gui/BonusGui.java | 2 +- .../bonuses/gui/buttons/CarlSpinButton.java | 43 ++++++- .../bonuses/gui/buttons/DailyBonusButton.java | 12 +- .../hub/bonuses/gui/buttons/VoteButton.java | 13 ++- .../Mineplex.Votifier/Mineplex.Votifier.iml | 1 + .../mineplex/votifier/VotifierManager.java | 46 +++++++- 13 files changed, 342 insertions(+), 40 deletions(-) delete mode 100644 Plugins/Mineplex.Core/src/mineplex/core/votifier/RedisVotifierCommand.java create mode 100644 Plugins/Mineplex.Core/src/mineplex/core/votifier/VotifierCommand.java create mode 100644 Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/StreakRecord.java create mode 100644 Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/VoteHandler.java diff --git a/Plugins/Mineplex.Core/src/mineplex/core/votifier/RedisVotifierCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/votifier/RedisVotifierCommand.java deleted file mode 100644 index 7b92598b3..000000000 --- a/Plugins/Mineplex.Core/src/mineplex/core/votifier/RedisVotifierCommand.java +++ /dev/null @@ -1,17 +0,0 @@ -package mineplex.core.votifier; - -import java.util.UUID; - -import mineplex.serverdata.commands.ServerCommand; - -public class RedisVotifierCommand extends ServerCommand -{ - private UUID _voterUUID; - - - public RedisVotifierCommand() - { - - } - -} diff --git a/Plugins/Mineplex.Core/src/mineplex/core/votifier/VotifierCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/votifier/VotifierCommand.java new file mode 100644 index 000000000..a84087fb0 --- /dev/null +++ b/Plugins/Mineplex.Core/src/mineplex/core/votifier/VotifierCommand.java @@ -0,0 +1,23 @@ +package mineplex.core.votifier; + +import java.util.UUID; + +import mineplex.serverdata.commands.ServerCommand; + +public class VotifierCommand extends ServerCommand +{ + private String _playerName; + + public VotifierCommand(String playerName, String targetServer) + { + super(targetServer); + + _playerName = playerName; + } + + public String getPlayerName() + { + return _playerName; + } + +} diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/BonusAmount.java b/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/BonusAmount.java index 9c5343077..d596a846b 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/BonusAmount.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/BonusAmount.java @@ -143,7 +143,6 @@ public class BonusAmount addLore(lore, getGems(), getBonusGems(), "Gems"); addLore(lore, getGold(), getBonusGold(), "Gold"); addLore(lore, getExperience(), getBonusExperience(), "Experience"); - lore.add(" "); } private void addLore(List lore, int amount, int bonus, String suffix) diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/BonusManager.java b/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/BonusManager.java index bfd1bca5a..b4bd57948 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/BonusManager.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/BonusManager.java @@ -23,10 +23,14 @@ import mineplex.core.npc.NpcManager; import mineplex.core.reward.RewardManager; import mineplex.core.updater.UpdateType; import mineplex.core.updater.event.UpdateEvent; +import mineplex.core.votifier.VotifierCommand; import mineplex.hub.bonuses.commands.GuiCommand; import mineplex.hub.bonuses.gui.BonusGui; import mineplex.database.tables.records.BonusRecord; +import mineplex.hub.bonuses.gui.SpinGui; +import mineplex.serverdata.commands.ServerCommandManager; +import org.bukkit.Bukkit; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; import org.bukkit.entity.LivingEntity; @@ -87,6 +91,10 @@ public class BonusManager extends MiniClientPlugin implements I private RewardManager _rewardManager; private Npc _carlNpc; + // Streak + private StreakRecord _dailyStreak; + private StreakRecord _voteStreak; + public BonusManager(JavaPlugin plugin, CoreClientManager clientManager, DonationManager donationManager, NpcManager npcManager, HologramManager hologramManager, RewardManager rewardManager) { super("Bonus", plugin); @@ -102,7 +110,11 @@ public class BonusManager extends MiniClientPlugin implements I clientManager.addStoredProcedureLoginProcessor(this); + ServerCommandManager.getInstance().registerCommandType("VotifierCommand", VotifierCommand.class, + new VoteHandler(this)); + updateOffSet(); + updateStreakRecord(); } @Override @@ -121,6 +133,51 @@ public class BonusManager extends MiniClientPlugin implements I updateOffSet(); } + @EventHandler + public void updateStreak(UpdateEvent event) + { + if (event.getType() != UpdateType.MIN_16) + return; + + updateStreakRecord(); + } + + private void updateStreakRecord() + { + _repository.getDailyStreakRecord(new Callback() + { + @Override + public void run(StreakRecord data) + { + _dailyStreak = data; + } + }); + + _repository.getVoteStreakRecord(new Callback() + { + @Override + public void run(StreakRecord data) + { + _voteStreak = data; + } + }); + } + + public StreakRecord getDailyStreak() + { + return _dailyStreak; + } + + public StreakRecord getVoteStreak() + { + return _voteStreak; + } + + public void handleVote(Player player) + { + Bukkit.broadcastMessage("Recieved Vote: " + player.getName()); + } + // DAILY BONUS public static final long TIME_BETWEEN_BONUSES = 1000 * 60 * 60 * 20; @@ -186,6 +243,41 @@ public class BonusManager extends MiniClientPlugin implements I } }); } + + public void attemptCarlSpin(final Player player) + { + final BonusClientData clientData = Get(player); + + if (clientData.getTickets() > 0) + { + clientData.setTickets(clientData.getTickets() - 1); + + runAsync(new Runnable() + { + @Override + public void run() + { + int modified = clientData.getRecord().store(); + + if (modified == 1) + { + runSync(new Runnable() + { + @Override + public void run() + { + new SpinGui(getPlugin(), player, _rewardManager).openInventory(); + } + }); + } + else + { + UtilPlayer.message(player, F.main("Carl", "There was an error processing your request")); + } + } + }); + } + } public long timeTillRankBonus(Player player) { @@ -275,8 +367,6 @@ public class BonusManager extends MiniClientPlugin implements I { BonusAmount amount = new BonusAmount(); amount.setTickets(1); - amount.setBonusCoins(100); - amount.setBonusExperience(100); return amount; } @@ -330,7 +420,7 @@ public class BonusManager extends MiniClientPlugin implements I public void awardBonus(final Player player, BonusAmount amount) { - BonusClientData bonusClient = Get(player); + final BonusClientData bonusClient = Get(player); CoreClient coreClient = _clientManager.Get(player); final int gems = amount.getTotalGems(); @@ -399,7 +489,14 @@ public class BonusManager extends MiniClientPlugin implements I if (tickets > 0) { bonusClient.setTickets(tickets + bonusClient.getTickets()); - // TODO database save + runAsync(new Runnable() + { + @Override + public void run() + { + bonusClient.getRecord().store(); + } + }); UtilPlayer.message(player, F.main("Bonus", "Rewarded " + F.elem(tickets + " Carl Spin Ticket"))); } diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/BonusRepository.java b/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/BonusRepository.java index 070e4aec0..8fcc95d6d 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/BonusRepository.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/BonusRepository.java @@ -16,6 +16,8 @@ import mineplex.core.recharge.Recharge; import mineplex.database.Tables; import mineplex.database.tables.records.BonusRecord; import org.jooq.DSLContext; +import org.jooq.Record2; +import org.jooq.TableField; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -57,6 +59,42 @@ public class BonusRepository extends RepositoryBase return record; } + public void getDailyStreakRecord(Callback callback) + { + getStreakRecord(Tables.bonus.maxDailyStreak, callback); + } + + public void getVoteStreakRecord(Callback callback) + { + getStreakRecord(Tables.bonus.maxVoteStreak, callback); + } + + private void getStreakRecord(final TableField field, final Callback callback) + { + Bukkit.getScheduler().runTaskAsynchronously(_manager.getPlugin(), new Runnable() + { + @Override + public void run() + { + final Record2 record = jooq().select(Tables.accounts.name, field) + .from(Tables.bonus.join(Tables.accounts).on(Tables.bonus.accountId.eq(Tables.accounts.id))) + .orderBy(field.desc()).limit(1).fetchOne(); + + Bukkit.getScheduler().runTask(_manager.getPlugin(), new Runnable() + { + @Override + public void run() + { + if (record.value1() != null && record.value2() != null) + { + callback.run(new StreakRecord(record.value1(), record.value2())); + } + } + }); + } + }); + } + public void attemptDailyBonus(final Player player, final Callback result) { if (!Recharge.Instance.usable(player, "AttemptDailyBonus")) { @@ -123,6 +161,33 @@ public class BonusRepository extends RepositoryBase }); } + @Deprecated + public void attemptPurchaseSpin(final Player player, final Callback result) + { + final int accountId = _manager.getClientManager().Get(player).getAccountId(); + + Bukkit.getScheduler().runTaskAsynchronously(_manager.getPlugin(), new Runnable() + { + @Override + public void run() + { + final int newTickets = jooq().update(Tables.bonus).set(Tables.bonus.tickets, Tables.bonus.tickets.sub(-1)). + where(Tables.bonus.accountId.eq(accountId)).returning(Tables.bonus.tickets).fetchOne().getTickets(); + + Bukkit.getScheduler().runTask(_manager.getPlugin(), new Runnable() + { + @Override + public void run() + { + + + } + }); + + } + }); + } + public void attemptRankBonus(final Player player, final Callback result) { if (!Recharge.Instance.usable(player, "AttemptRankBonus")) { diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/StreakRecord.java b/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/StreakRecord.java new file mode 100644 index 000000000..901ef4ad6 --- /dev/null +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/StreakRecord.java @@ -0,0 +1,23 @@ +package mineplex.hub.bonuses; + +public class StreakRecord +{ + private String _playerName; + private int _streak; + + public StreakRecord(String playerName, int streak) + { + _playerName = playerName; + _streak = streak; + } + + public String getPlayerName() + { + return _playerName; + } + + public int getStreak() + { + return _streak; + } +} diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/VoteHandler.java b/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/VoteHandler.java new file mode 100644 index 000000000..73e11305b --- /dev/null +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/VoteHandler.java @@ -0,0 +1,31 @@ +package mineplex.hub.bonuses; + +import org.bukkit.entity.Player; + +import mineplex.core.common.util.UtilPlayer; +import mineplex.core.votifier.VotifierCommand; +import mineplex.serverdata.commands.CommandCallback; +import mineplex.serverdata.commands.ServerCommand; + +public class VoteHandler implements CommandCallback +{ + private BonusManager _bonusManager; + + public VoteHandler(BonusManager bonusManager) + { + _bonusManager = bonusManager; + } + + @Override + public void run(ServerCommand command) + { + VotifierCommand v = ((VotifierCommand) command); + + Player player = UtilPlayer.searchExact(v.getPlayerName()); + + if (player != null) + { + _bonusManager.handleVote(player); + } + } +} diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/gui/BonusGui.java b/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/gui/BonusGui.java index 083288461..65aa961cf 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/gui/BonusGui.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/gui/BonusGui.java @@ -28,7 +28,7 @@ public class BonusGui extends SimpleGui setItem(15, new DailyBonusButton(getPlugin(), player, this, manager)); - setItem(31, new CarlSpinButton(getPlugin(), player, rewardManager)); + setItem(31, new CarlSpinButton(getPlugin(), player, manager, rewardManager)); } @Override diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/gui/buttons/CarlSpinButton.java b/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/gui/buttons/CarlSpinButton.java index e91626a23..c68f95814 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/gui/buttons/CarlSpinButton.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/gui/buttons/CarlSpinButton.java @@ -1,5 +1,7 @@ package mineplex.hub.bonuses.gui.buttons; +import java.util.ArrayList; + import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.entity.Player; @@ -7,22 +9,25 @@ import org.bukkit.event.inventory.ClickType; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.Plugin; +import mineplex.core.common.util.C; import mineplex.core.gui.GuiItem; import mineplex.core.reward.RewardManager; import mineplex.core.shop.item.ShopItem; -import mineplex.hub.bonuses.gui.SpinGui; +import mineplex.hub.bonuses.BonusClientData; +import mineplex.hub.bonuses.BonusManager; public class CarlSpinButton implements GuiItem { - private Plugin _plugin; private Player _player; + private BonusManager _bonusManager; private RewardManager _rewardManager; - public CarlSpinButton(Plugin plugin, Player player, RewardManager rewardManager) + public CarlSpinButton(Plugin plugin, Player player, BonusManager bonusManager, RewardManager rewardManager) { _plugin = plugin; _player = player; + _bonusManager = bonusManager; _rewardManager = rewardManager; } @@ -40,14 +45,40 @@ public class CarlSpinButton implements GuiItem @Override public void click(ClickType clickType) { - new SpinGui(_plugin, _player, _rewardManager).openInventory(); + BonusClientData client = _bonusManager.Get(_player); + int tickets = client.getTickets(); + + if (tickets > 0) + { + _bonusManager.attemptCarlSpin(_player); + } +// new SpinGui(_plugin, _player, _rewardManager).openInventory(); } @Override public ItemStack getObject() { - ShopItem item = new ShopItem(Material.SKULL_ITEM, (byte) 4, "Carl's Spinner", new String[] {ChatColor.RESET + "Try your Luck!" }, 1, false, false); + BonusClientData client = _bonusManager.Get(_player); + int tickets = client.getTickets(); - return item; + String name = (tickets > 0 ? C.cGreen : C.cRed) + C.Bold + "Carl's Spinner"; + ArrayList lore = new ArrayList(); + Material material = Material.SKULL_ITEM; + byte data = (byte) 4; + + lore.add(" "); + if (tickets > 0) + { + lore.add(ChatColor.RESET + "Click to Spin"); + } + else + { + lore.add(ChatColor.RESET + "You need a Carl Spin Ticket to Spin"); + } + + lore.add(" "); + lore.add(ChatColor.YELLOW + "Your Tickets: " + C.cWhite + tickets); + + return new ShopItem(material, data, name, lore.toArray(new String[0]), 1, false, false); } } diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/gui/buttons/DailyBonusButton.java b/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/gui/buttons/DailyBonusButton.java index 4554c8a58..f6534589d 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/gui/buttons/DailyBonusButton.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/gui/buttons/DailyBonusButton.java @@ -19,6 +19,7 @@ import mineplex.core.updater.event.UpdateEvent; import mineplex.hub.bonuses.BonusAmount; import mineplex.hub.bonuses.BonusClientData; import mineplex.hub.bonuses.BonusManager; +import mineplex.hub.bonuses.StreakRecord; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -139,6 +140,7 @@ public class DailyBonusButton implements GuiItem, Listener BonusAmount bonusAmount = _bonusManager.getDailyBonusAmount(_player); bonusAmount.addLore(lore); + lore.add(" "); lore.add(C.cYellow + "Current Streak: " + C.cWhite + client.getDailyStreak()); lore.add(C.cYellow + "Highest Streak: " + C.cWhite + client.getMaxDailyStreak()); @@ -154,9 +156,13 @@ public class DailyBonusButton implements GuiItem, Listener } } - lore.add(" "); - lore.add(C.cYellow + "Record Holder: " + C.cWhite + "Phinary"); - lore.add(C.cYellow + "Streak: " + C.cWhite + "420"); + StreakRecord streakRecord = _bonusManager.getDailyStreak(); + if (streakRecord != null) + { + lore.add(" "); + lore.add(C.cYellow + "Record: " + C.cWhite + streakRecord.getPlayerName()); + lore.add(C.cYellow + "Streak: " + C.cWhite + streakRecord.getStreak()); + } _item = new ShopItem(material, itemName, lore.toArray(new String[0]), 1, false, false); diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/gui/buttons/VoteButton.java b/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/gui/buttons/VoteButton.java index 0552203d7..54021b6f2 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/gui/buttons/VoteButton.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/gui/buttons/VoteButton.java @@ -17,6 +17,8 @@ import mineplex.core.updater.event.UpdateEvent; import mineplex.hub.bonuses.BonusAmount; import mineplex.hub.bonuses.BonusClientData; import mineplex.hub.bonuses.BonusManager; +import mineplex.hub.bonuses.StreakRecord; + import net.minecraft.server.v1_7_R4.ChatSerializer; import net.minecraft.server.v1_7_R4.IChatBaseComponent; import net.minecraft.server.v1_7_R4.PacketPlayOutChat; @@ -134,6 +136,7 @@ public class VoteButton implements GuiItem, Listener { BonusAmount bonusAmount = _bonusManager.getVoteBonusAmount(_player); bonusAmount.addLore(lore); + lore.add(" "); lore.add(C.cYellow + "Current Streak: " + C.cWhite + client.getVoteStreak()); lore.add(C.cYellow + "Highest Streak: " + C.cWhite + client.getMaxVoteStreak()); @@ -149,9 +152,13 @@ public class VoteButton implements GuiItem, Listener { } } - lore.add(" "); - lore.add(C.cYellow + "Record Holder: " + C.cWhite + "Phinary"); - lore.add(C.cYellow + "Streak: " + C.cWhite + "420"); + StreakRecord streakRecord = _bonusManager.getVoteStreak(); + if (streakRecord != null) + { + lore.add(" "); + lore.add(C.cYellow + "Record: " + C.cWhite + streakRecord.getPlayerName()); + lore.add(C.cYellow + "Streak: " + C.cWhite + streakRecord.getStreak()); + } _item = new ShopItem(material, itemName, lore.toArray(new String[0]), 1, false, false); } diff --git a/Plugins/Mineplex.Votifier/Mineplex.Votifier.iml b/Plugins/Mineplex.Votifier/Mineplex.Votifier.iml index 8d37a4051..b7d71d202 100644 --- a/Plugins/Mineplex.Votifier/Mineplex.Votifier.iml +++ b/Plugins/Mineplex.Votifier/Mineplex.Votifier.iml @@ -16,6 +16,7 @@ + diff --git a/Plugins/Mineplex.Votifier/src/mineplex/votifier/VotifierManager.java b/Plugins/Mineplex.Votifier/src/mineplex/votifier/VotifierManager.java index 30646a1f5..ecef96339 100644 --- a/Plugins/Mineplex.Votifier/src/mineplex/votifier/VotifierManager.java +++ b/Plugins/Mineplex.Votifier/src/mineplex/votifier/VotifierManager.java @@ -8,27 +8,63 @@ import com.vexsoftware.votifier.model.VotifierEvent; import mineplex.core.MiniPlugin; import mineplex.core.account.CoreClientManager; import mineplex.core.donation.DonationManager; -import mineplex.core.votifier.RedisVotifierCommand; +import mineplex.core.votifier.VotifierCommand; +import mineplex.serverdata.Region; +import mineplex.serverdata.commands.ServerCommandManager; +import mineplex.serverdata.data.PlayerStatus; +import mineplex.serverdata.redis.RedisDataRepository; +import mineplex.serverdata.servers.ServerManager; /** * Created by shaun on 15-08-05. */ public class VotifierManager extends MiniPlugin { + private RedisDataRepository _usPlayerRepo; + private RedisDataRepository _euPlayerRepo; + public VotifierManager(JavaPlugin plugin, CoreClientManager clientManager, DonationManager donationManager) { super("Votifier", plugin); + + _usPlayerRepo = new RedisDataRepository(ServerManager.getMasterConnection(), ServerManager.getSlaveConnection(), + Region.US, PlayerStatus.class, "playerStatus"); + _euPlayerRepo = new RedisDataRepository(ServerManager.getMasterConnection(), ServerManager.getSlaveConnection(), + Region.EU, PlayerStatus.class, "playerStatus"); + + ServerCommandManager.getInstance().registerCommandType("VotifierCommand", VotifierCommand.class); } - - @EventHandler public void handleVote(VotifierEvent event) { Vote vote = event.getVote(); - RedisVotifierCommand command = new RedisVotifierCommand(); - System.out.println("New Vote: " + vote.getUsername()); + + PlayerStatus usStatus = _usPlayerRepo.getElement(vote.getUsername()); +// VotifierCommand command = new VotifierCommand(vote.getUsername(), "PhiTest-1"); +// command.publish(); + + if (usStatus != null) + { + System.out.println("Found on US Server: " + usStatus.getServer()); + VotifierCommand command = new VotifierCommand(vote.getUsername(), usStatus.getServer()); + command.publish(); + } + else + { + System.out.println("Not found on US Server!"); + } + + PlayerStatus euStatus = _euPlayerRepo.getElement(vote.getUsername()); + if (euStatus != null) + { + System.out.println("Found on EU Server: " + euStatus.getServer()); + } + else + { + System.out.println("Not found on EU Server!"); + } } } \ No newline at end of file