From a0be3fa96480a559d804a6d850c04dbab0d59ea8 Mon Sep 17 00:00:00 2001 From: Shaun Bennett Date: Mon, 10 Aug 2015 01:19:10 -0500 Subject: [PATCH] Use atomic mysql operations for modifying tickets --- .../mineplex/hub/bonuses/BonusManager.java | 42 ++++++++++++--- .../mineplex/hub/bonuses/BonusRepository.java | 4 +- .../hub/bonuses/commands/TicketCommand.java | 54 +++++++++++++++++++ 3 files changed, 90 insertions(+), 10 deletions(-) create mode 100644 Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/commands/TicketCommand.java diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/BonusManager.java b/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/BonusManager.java index a63556c77..c997deaf8 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/BonusManager.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/BonusManager.java @@ -21,6 +21,7 @@ import mineplex.core.common.util.UtilPlayer; import mineplex.core.common.util.UtilServer; 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.hologram.Hologram; import mineplex.core.hologram.HologramManager; @@ -30,15 +31,20 @@ import mineplex.core.reward.RewardManager; import mineplex.core.updater.UpdateType; import mineplex.core.updater.event.UpdateEvent; import mineplex.core.votifier.VotifierCommand; +import mineplex.database.Tables; import mineplex.hub.bonuses.animations.AnimationCarl; import mineplex.hub.bonuses.commands.AnimationCommand; import mineplex.hub.bonuses.commands.GuiCommand; +import mineplex.hub.bonuses.commands.TicketCommand; import mineplex.hub.bonuses.event.CarlSpinnerEvent; import mineplex.hub.bonuses.gui.BonusGui; import mineplex.database.tables.records.BonusRecord; import mineplex.hub.bonuses.gui.SpinGui; import mineplex.hub.poll.PollManager; import mineplex.serverdata.commands.ServerCommandManager; +import org.jooq.DSLContext; +import org.jooq.SQLDialect; +import org.jooq.impl.DSL; import org.bukkit.Bukkit; import org.bukkit.Sound; @@ -157,6 +163,7 @@ public class BonusManager extends MiniClientPlugin implements I { addCommand(new GuiCommand(this)); addCommand(new AnimationCommand(this)); + addCommand(new TicketCommand(this)); } // Just keeping things up-to-date @@ -411,27 +418,29 @@ public class BonusManager extends MiniClientPlugin implements I if (!event.isCancelled()) { - clientData.setTickets(clientData.getTickets() - 1); + final int accountId = _clientManager.Get(player).getAccountId(); runAsync(new Runnable() { @Override public void run() { - int modified = clientData.getRecord().store(); - - if (modified == 1) + try { + final int newTickets = DSL.using(DBPool.ACCOUNT, SQLDialect.MYSQL).update(Tables.bonus).set(Tables.bonus.tickets, Tables.bonus.tickets.sub(1)). + where(Tables.bonus.accountId.eq(accountId)).returning(Tables.bonus.tickets).fetchOne().getTickets(); + runSync(new Runnable() { @Override public void run() { + clientData.setTickets(newTickets); new SpinGui(getPlugin(), player, _rewardManager, manager).openInventory(); } }); } - else + catch (Exception e) { UtilPlayer.message(player, F.main("Carl", "There was an error processing your request")); } @@ -689,16 +698,33 @@ public class BonusManager extends MiniClientPlugin implements I if (tickets > 0) { - bonusClient.setTickets(tickets + bonusClient.getTickets()); + final int accountId = _clientManager.Get(player).getAccountId(); runAsync(new Runnable() { @Override public void run() { - bonusClient.getRecord().store(); + try + { + final int newTickets = DSL.using(DBPool.ACCOUNT, SQLDialect.MYSQL).update(Tables.bonus).set(Tables.bonus.tickets, Tables.bonus.tickets.add(tickets)). + where(Tables.bonus.accountId.eq(accountId)).returning(Tables.bonus.tickets).fetchOne().getTickets(); + runSync(new Runnable() + { + @Override + public void run() + { + bonusClient.setTickets(newTickets); + UtilPlayer.message(player, F.main("Bonus", "Rewarded " + F.elem(tickets + " Carl Spin Ticket"))); + } + }); + } + catch (Exception e) + { + System.out.println("Failed to award ticket to player: " + player); + e.printStackTrace(); + } } }); - UtilPlayer.message(player, F.main("Bonus", "Rewarded " + F.elem(tickets + " Carl Spin Ticket"))); } if (experience > 0) diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/BonusRepository.java b/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/BonusRepository.java index f34c71296..2f53ae3ac 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/BonusRepository.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/BonusRepository.java @@ -53,7 +53,7 @@ public class BonusRepository extends RepositoryBase record.setVoteStreak(0); record.setMaxVoteStreak(0); record.setTickets(0); -// record.store(); // Todo - is this necessary? + record.store(); // Todo - is this necessary? } System.out.println("Loaded record. Daily time: " + record.getDailytime()); return record; @@ -158,7 +158,7 @@ public class BonusRepository extends RepositoryBase } @Deprecated - public void attemptPurchaseSpin(final Player player, final Callback result) + public void giveTickets(final Player player, final Callback result) { final int accountId = _manager.getClientManager().Get(player).getAccountId(); diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/commands/TicketCommand.java b/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/commands/TicketCommand.java new file mode 100644 index 000000000..14afed1e8 --- /dev/null +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/bonuses/commands/TicketCommand.java @@ -0,0 +1,54 @@ +package mineplex.hub.bonuses.commands; + +import mineplex.core.command.CommandBase; +import mineplex.core.common.Rank; +import mineplex.core.common.util.F; +import mineplex.core.common.util.UtilPlayer; +import mineplex.hub.bonuses.BonusManager; + +import org.bukkit.entity.Player; + +public class TicketCommand extends CommandBase +{ + public TicketCommand(BonusManager plugin) + { + super(plugin, Rank.DEVELOPER, "ticket"); + } + + @Override + public void Execute(final Player caller, String[] args) + { + if (args.length < 2) + { + UtilPlayer.message(caller, F.main("Carl", "Missing Args: " + F.elem("/ticket "))); + return; + } + + final String targetName = args[0]; + final String ticketString = args[1]; + Player target = UtilPlayer.searchExact(targetName); + + + rewardTickets(caller, target, target.getName(), ticketString); + } + + private void rewardTickets(final Player caller, final Player target, final String targetName, String ticketString) + { + try + { + int tickets = Integer.parseInt(ticketString); + Plugin.Get(target).setTickets(Plugin.Get(targetName).getTickets() + tickets); + + UtilPlayer.message(caller, F.main("Carl", "You gave " + F.elem(tickets + " Carl Tickets") + " to " + F.name(targetName) + ".")); + + if (target != null) + { + UtilPlayer.message(target, F.main("Carl", F.name(caller.getName()) + " gave you " + F.elem(tickets + " Carl Tickets") + ".")); + } + } + catch (Exception e) + { + UtilPlayer.message(caller, F.main("Carl", "Invalid Ticket Amount")); + } + } +}