Use atomic mysql operations for modifying tickets

This commit is contained in:
Shaun Bennett 2015-08-10 01:19:10 -05:00
parent 511cc76bef
commit a0be3fa964
3 changed files with 90 additions and 10 deletions

View File

@ -21,6 +21,7 @@ import mineplex.core.common.util.UtilPlayer;
import mineplex.core.common.util.UtilServer; import mineplex.core.common.util.UtilServer;
import mineplex.core.common.util.UtilParticle.ParticleType; import mineplex.core.common.util.UtilParticle.ParticleType;
import mineplex.core.common.util.UtilParticle.ViewDist; import mineplex.core.common.util.UtilParticle.ViewDist;
import mineplex.core.database.DBPool;
import mineplex.core.donation.DonationManager; import mineplex.core.donation.DonationManager;
import mineplex.core.hologram.Hologram; import mineplex.core.hologram.Hologram;
import mineplex.core.hologram.HologramManager; import mineplex.core.hologram.HologramManager;
@ -30,15 +31,20 @@ import mineplex.core.reward.RewardManager;
import mineplex.core.updater.UpdateType; import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent; import mineplex.core.updater.event.UpdateEvent;
import mineplex.core.votifier.VotifierCommand; import mineplex.core.votifier.VotifierCommand;
import mineplex.database.Tables;
import mineplex.hub.bonuses.animations.AnimationCarl; import mineplex.hub.bonuses.animations.AnimationCarl;
import mineplex.hub.bonuses.commands.AnimationCommand; import mineplex.hub.bonuses.commands.AnimationCommand;
import mineplex.hub.bonuses.commands.GuiCommand; import mineplex.hub.bonuses.commands.GuiCommand;
import mineplex.hub.bonuses.commands.TicketCommand;
import mineplex.hub.bonuses.event.CarlSpinnerEvent; import mineplex.hub.bonuses.event.CarlSpinnerEvent;
import mineplex.hub.bonuses.gui.BonusGui; import mineplex.hub.bonuses.gui.BonusGui;
import mineplex.database.tables.records.BonusRecord; import mineplex.database.tables.records.BonusRecord;
import mineplex.hub.bonuses.gui.SpinGui; import mineplex.hub.bonuses.gui.SpinGui;
import mineplex.hub.poll.PollManager; import mineplex.hub.poll.PollManager;
import mineplex.serverdata.commands.ServerCommandManager; 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.Bukkit;
import org.bukkit.Sound; import org.bukkit.Sound;
@ -157,6 +163,7 @@ public class BonusManager extends MiniClientPlugin<BonusClientData> implements I
{ {
addCommand(new GuiCommand(this)); addCommand(new GuiCommand(this));
addCommand(new AnimationCommand(this)); addCommand(new AnimationCommand(this));
addCommand(new TicketCommand(this));
} }
// Just keeping things up-to-date // Just keeping things up-to-date
@ -411,27 +418,29 @@ public class BonusManager extends MiniClientPlugin<BonusClientData> implements I
if (!event.isCancelled()) if (!event.isCancelled())
{ {
clientData.setTickets(clientData.getTickets() - 1); final int accountId = _clientManager.Get(player).getAccountId();
runAsync(new Runnable() runAsync(new Runnable()
{ {
@Override @Override
public void run() public void run()
{ {
int modified = clientData.getRecord().store(); try
if (modified == 1)
{ {
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() runSync(new Runnable()
{ {
@Override @Override
public void run() public void run()
{ {
clientData.setTickets(newTickets);
new SpinGui(getPlugin(), player, _rewardManager, manager).openInventory(); new SpinGui(getPlugin(), player, _rewardManager, manager).openInventory();
} }
}); });
} }
else catch (Exception e)
{ {
UtilPlayer.message(player, F.main("Carl", "There was an error processing your request")); UtilPlayer.message(player, F.main("Carl", "There was an error processing your request"));
} }
@ -689,16 +698,33 @@ public class BonusManager extends MiniClientPlugin<BonusClientData> implements I
if (tickets > 0) if (tickets > 0)
{ {
bonusClient.setTickets(tickets + bonusClient.getTickets()); final int accountId = _clientManager.Get(player).getAccountId();
runAsync(new Runnable() runAsync(new Runnable()
{ {
@Override @Override
public void run() 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) if (experience > 0)

View File

@ -53,7 +53,7 @@ public class BonusRepository extends RepositoryBase
record.setVoteStreak(0); record.setVoteStreak(0);
record.setMaxVoteStreak(0); record.setMaxVoteStreak(0);
record.setTickets(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()); System.out.println("Loaded record. Daily time: " + record.getDailytime());
return record; return record;
@ -158,7 +158,7 @@ public class BonusRepository extends RepositoryBase
} }
@Deprecated @Deprecated
public void attemptPurchaseSpin(final Player player, final Callback<Boolean> result) public void giveTickets(final Player player, final Callback<Boolean> result)
{ {
final int accountId = _manager.getClientManager().Get(player).getAccountId(); final int accountId = _manager.getClientManager().Get(player).getAccountId();

View File

@ -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<BonusManager>
{
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 <player> <amount>")));
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"));
}
}
}