Reload ThankData when a player is /thank - Allows rewards to appear immediately at carl

This commit is contained in:
Shaun Bennett 2016-08-03 18:22:36 -05:00
parent e113bedea5
commit 85c4c181ec
3 changed files with 86 additions and 16 deletions

View File

@ -5,7 +5,7 @@ package mineplex.core.thank;
*/
public class ThankData
{
private final int _thankToClaim;
private int _thankToClaim;
public ThankData(int thankToClaim)
{
@ -16,4 +16,9 @@ public class ThankData
{
return _thankToClaim;
}
public void setThankToClaim(int thankToClaim)
{
_thankToClaim = thankToClaim;
}
}

View File

@ -14,6 +14,9 @@ import org.bukkit.plugin.java.JavaPlugin;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* This class handles /thank and amplifier thanks, along with allowing players to claim the rewards they earn
*/
public class ThankManager extends MiniDbClientPlugin<ThankData>
{
public static final int DEFAULT_RECEIVER_REWARD = 5;
@ -38,6 +41,18 @@ public class ThankManager extends MiniDbClientPlugin<ThankData>
addCommand(new ThankCommand(this));
}
/**
* Attempt to thank a player. This can be used to distribute rewards to players, give players rewards for using
* amplifiers, and allow players to thank anyone inside a game.
*
* @param receiver The player who is being thanked
* @param sender The player thanking receiver
* @param receiverReward The Treasure Shard reward for the receiver
* @param senderReward The Treasure Shard reward for the sender
* @param reason The reason that player is being thanked
* @param ignoreCooldown Should we ignore all thank cooldowns
* @param callback Callback for processing the result
*/
public void thankPlayer(Player receiver, Player sender, int receiverReward, int senderReward, String reason, boolean ignoreCooldown, Callback<ThankResult> callback)
{
if (!Recharge.Instance.use(sender, "Thank Player", 1000 * 5, false, false))
@ -49,9 +64,32 @@ public class ThankManager extends MiniDbClientPlugin<ThankData>
int senderAccountId = ClientManager.getAccountId(sender);
int receiverAccountId = ClientManager.getAccountId(receiver);
thankPlayer(receiver.getName(), receiverAccountId, sender.getName(), senderAccountId, receiverReward, senderReward, reason, ignoreCooldown, callback);
thankPlayer(receiver.getName(), receiverAccountId, sender.getName(), senderAccountId, receiverReward, senderReward, reason, ignoreCooldown, result ->
{
if (result == ThankResult.SUCCESS)
{
// Reload their thank data if the player is online!
runAsync(() -> {
try
{
Set(receiver, _thankRepository.getThankData(receiverAccountId));
} catch (SQLException e)
{
e.printStackTrace();
}
});
}
callback.run(result);
});
}
/**
* Called when a player wants to "claim" all pending rewards that they haven't claimed yet
*
* @param player The player claiming their thank rewards
* @param callback Callback with the result of the claim
*/
public void claimThanks(Player player, Callback<ClaimThankResult> callback)
{
int accountId = ClientManager.getAccountId(player);
@ -79,6 +117,20 @@ public class ThankManager extends MiniDbClientPlugin<ThankData>
});
}
/**
* Attempt to thank a player. This can be used to distribute rewards to players, give players rewards for using
* amplifiers, and allow players to thank anyone inside a game.
*
* @param receiverName Name of the player being thanked
* @param receiverAccountId Account id of the player being thanked
* @param senderName Name of the player sending the thanks
* @param senderAccountId Account id of the player sending the thanks
* @param receiverReward The Treasure Shard reward for the receiver
* @param senderReward The Treasure Shard reward for the sender
* @param reason The reason that player is being thanked
* @param ignoreCooldown Should we ignore all thank cooldowns
* @param callback Callback for processing the result
*/
public void thankPlayer(String receiverName, int receiverAccountId, String senderName, int senderAccountId, int receiverReward, int senderReward, String reason, boolean ignoreCooldown, Callback<ThankResult> callback)
{
// Break out on bad account id

View File

@ -4,11 +4,18 @@ import mineplex.core.database.MinecraftRepository;
import mineplex.database.routines.AddThank;
import mineplex.database.routines.ClaimThank;
import mineplex.serverdata.database.DBPool;
import mineplex.serverdata.database.ResultSetCallable;
import mineplex.serverdata.database.column.ColumnInt;
import org.bukkit.plugin.java.JavaPlugin;
import org.jooq.Configuration;
import java.sql.ResultSet;
import java.sql.SQLException;
public class ThankRepository extends MinecraftRepository
{
private static final String GET_THANK_DATA = "SELECT SUM(thankAmount) FROM accountThankTransactions WHERE receiverId = ? AND claimed = FALSE";
public ThankRepository(JavaPlugin plugin)
{
super(plugin, DBPool.getAccount());
@ -25,15 +32,6 @@ public class ThankRepository extends MinecraftRepository
}
/**
*
* @param receiverAccountId
* @param senderAccountId
* @param thankAmount
* @param reason
* @param ignoreCooldown
* @return
*/
public boolean thank(int receiverAccountId, int senderAccountId, int thankAmount, String reason, boolean ignoreCooldown)
{
AddThank addThank = new AddThank();
@ -47,11 +45,6 @@ public class ThankRepository extends MinecraftRepository
return addThank.getSuccess() == 1;
}
/**
*
* @param accountId
* @return
*/
public ClaimThankResult claimThank(int accountId)
{
ClaimThank claimThank = new ClaimThank();
@ -59,4 +52,24 @@ public class ThankRepository extends MinecraftRepository
claimThank.execute(jooq().configuration());
return new ClaimThankResult(claimThank.getAmountClaimed(), claimThank.getUniqueThank());
}
/**
* Get ThankData for an accountId. This is used to reload ThankData when a player is thanked on live servers
* @param accountId The account id of the player
* @return {@link ThankData} for that player
*/
public ThankData getThankData(int accountId) throws SQLException
{
ThankData thankData = new ThankData(0);
executeQuery(GET_THANK_DATA, resultSet ->
{
if (resultSet != null && resultSet.next())
{
thankData.setThankToClaim(resultSet.getInt(1));
}
}, new ColumnInt("receiverId", accountId));
return thankData;
}
}