Booster tip system

This commit is contained in:
Shaun Bennett 2016-05-28 09:21:10 -07:00
parent e00009a6d1
commit aebcfc2727
9 changed files with 301 additions and 6 deletions

View File

@ -44,9 +44,9 @@ public class ProfileLoader
return profile;
}
private void addProperties(GameProfile profile)
public static boolean addProperties(GameProfile profile)
{
String uuid = getUUID(skinOwner);
String uuid = profile.getId().toString().replaceAll("-", "");
try
{
// Get the name from SwordPVP
@ -85,11 +85,15 @@ public class ProfileLoader
Bukkit.getLogger().log(Level.WARNING, "Failed to apply auth property", e);
}
}
return true;
}
catch (Exception e)
{
e.printStackTrace();
}
return false;
}
@SuppressWarnings("deprecation")

View File

@ -40,6 +40,7 @@ public class UtilSkull
if (displayHead)
meta.setOwner(playerName);
meta.setDisplayName(itemName);
if (itemLore != null)
meta.setLore(itemLore);
skull.setItemMeta(meta);
return skull;

View File

@ -10,6 +10,8 @@ import java.util.UUID;
*/
public class Booster
{
// TODO
private int _boosterId;
private String _playerName;
private UUID _uuid;
private int _accountId;
@ -22,6 +24,11 @@ public class Booster
{
}
public int getBoosterId()
{
return _boosterId;
}
public String getPlayerName()
{
return _playerName;
@ -85,4 +92,34 @@ public class Booster
", _activationTime=" + _activationTime +
'}';
}
@Override
public boolean equals(Object o)
{
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Booster booster = (Booster) o;
if (_accountId != booster._accountId) return false;
if (_duration != booster._duration) return false;
if (!_playerName.equals(booster._playerName)) return false;
if (!_uuid.equals(booster._uuid)) return false;
if (!_startTime.equals(booster._startTime)) return false;
if (!_endTime.equals(booster._endTime)) return false;
return _activationTime.equals(booster._activationTime);
}
@Override
public int hashCode()
{
int result = _playerName.hashCode();
result = 31 * result + _uuid.hashCode();
result = 31 * result + _accountId;
result = 31 * result + _duration;
result = 31 * result + _startTime.hashCode();
result = 31 * result + _endTime.hashCode();
result = 31 * result + _activationTime.hashCode();
return result;
}
}

View File

@ -3,9 +3,13 @@ package mineplex.core.boosters;
import mineplex.core.MiniPlugin;
import mineplex.core.account.CoreClientManager;
import mineplex.core.boosters.command.BoosterCommand;
import mineplex.core.boosters.event.BoosterEnableEvent;
import mineplex.core.boosters.gui.BoosterShop;
import mineplex.core.boosters.tips.BoosterTipManager;
import mineplex.core.common.util.Callback;
import mineplex.core.common.util.UtilEvent;
import mineplex.core.donation.DonationManager;
import mineplex.core.serverConfig.ServerConfiguration;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import org.bukkit.entity.Player;
@ -24,10 +28,11 @@ public class BoosterManager extends MiniPlugin
private BoosterRepository _repository;
private CoreClientManager _clientManager;
private DonationManager _donationManager;
private BoosterTipManager _tipManager;
private BoosterShop _shop;
private long _cacheLastUpdated;
private Map<String, List<Booster>> _boosterCache = new HashMap<>();
private Map<String, List<Booster>> _boosterCache;
public BoosterManager(JavaPlugin plugin, CoreClientManager clientManager, DonationManager donationManager)
{
@ -36,6 +41,7 @@ public class BoosterManager extends MiniPlugin
_repository = new BoosterRepository();
_clientManager = clientManager;
_donationManager = donationManager;
_tipManager = new BoosterTipManager(plugin, clientManager, donationManager);
_shop = new BoosterShop(this, clientManager, donationManager);
}
@ -47,7 +53,7 @@ public class BoosterManager extends MiniPlugin
public Map<String, List<Booster>> getBoosters()
{
return _boosterCache;
return _boosterCache == null ? new HashMap<>() : _boosterCache;
}
public void getBoostersAsync(Callback<Map<String, List<Booster>>> callback)
@ -59,8 +65,19 @@ public class BoosterManager extends MiniPlugin
Map<String, List<Booster>> boosters = _repository.getBoosters();
long timeTaken = System.currentTimeMillis() - time;
runSync(() -> {
if (_boosterCache != null)
{
for (Map.Entry<String, List<Booster>> entry : boosters.entrySet())
{
List<Booster> current = _boosterCache.get(entry.getKey());
if (current == null || (current.size() < entry.getValue().size()))
getPluginManager().callEvent(new BoosterEnableEvent(entry.getValue().get(0)));
}
}
_cacheLastUpdated = System.currentTimeMillis();
System.out.println("Got Boosters. Took: " + timeTaken + "ms");
// System.out.println("Got Boosters. Took: " + timeTaken + "ms");
_boosterCache = boosters;
if (callback != null) callback.run(boosters);
});
@ -124,12 +141,18 @@ public class BoosterManager extends MiniPlugin
@EventHandler
public void updateCache(UpdateEvent event)
{
// TODO no no !
if (event.getType() == UpdateType.TICK)
{
getBoostersAsync(null);
}
}
public BoosterTipManager getTipManager()
{
return _tipManager;
}
public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException
{
BoosterRepository repository = new BoosterRepository();

View File

@ -0,0 +1,36 @@
package mineplex.core.boosters.event;
import mineplex.core.boosters.Booster;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
/**
* @author Shaun Bennett
*/
public class BoosterEnableEvent extends Event
{
private Booster _booster;
public BoosterEnableEvent(Booster booster)
{
_booster = booster;
}
public Booster getBooster()
{
return _booster;
}
private static final HandlerList _handlers = new HandlerList();
private static HandlerList getHandlerList()
{
return _handlers;
}
@Override
public HandlerList getHandlers()
{
return getHandlerList();
}
}

View File

@ -0,0 +1,108 @@
package mineplex.core.boosters.tips;
import mineplex.core.MiniDbClientPlugin;
import mineplex.core.account.CoreClientManager;
import mineplex.core.boosters.Booster;
import mineplex.core.common.util.Callback;
import mineplex.core.donation.DonationManager;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @author Shaun Bennett
*/
public class BoosterTipManager extends MiniDbClientPlugin<PlayerTipData>
{
private BoosterTipRepository _repository;
private DonationManager _donationManager;
public BoosterTipManager(JavaPlugin plugin, CoreClientManager clientManager, DonationManager donationManager)
{
super("Booster Tips", plugin, clientManager);
_donationManager = donationManager;
_repository = new BoosterTipRepository(plugin);
}
public void addTip(Player player, Booster booster, Callback<TipAddResult> callback)
{
int accountId = ClientManager.getAccountId(player);
// Break out if client manager has a bad account id
if (accountId == -1)
{
callback.run(TipAddResult.INVALID_ACCOUNT_ID);
return;
}
runAsync(() -> {
TipAddResult result;
if (_repository.addTip(accountId, booster.getAccountId(), booster.getBoosterId()))
result = TipAddResult.SUCCESS;
else
result = TipAddResult.ALREADY_TIPPED_BOOSTER;
runSync(() -> callback.run(result));
});
}
/**
* Claim all tips for a player and add those tips to their account (Treasure Shards)
* This will call a database routine that handles the tip process.
*
* The callback will return -1 on a failed attempt or 0 if there was no tips to claim
* @param player The player with tips to claim
* @param callback Callback returning the amount of tips claimed
*/
public void claimTips(Player player, Callback<Integer> callback)
{
String playerName = player.getName();
int accountId = ClientManager.getAccountId(player);
if (accountId == -1)
{
// uh oh, invalid account id!
if (callback != null) callback.run(-1);
}
runAsync(() -> {
int tips = _repository.claimTips(accountId);
runSync(() -> {
if (tips > 0)
{
_donationManager.rewardCoinsUntilSuccess(null, "Tips", playerName, accountId, tips);
}
if (callback != null) callback.run(tips);
});
});
}
@Override
public String getQuery(int accountId, String uuid, String name)
{
return "SELECT tips FROM Account.tips WHERE Account.tips.accountId = " + accountId;
}
@Override
protected PlayerTipData AddPlayer(String player)
{
return new PlayerTipData();
}
@Override
public void processLoginResultSet(String playerName, int accountId, ResultSet resultSet) throws SQLException
{
PlayerTipData data = new PlayerTipData();
while (resultSet.next())
{
data.setTips(resultSet.getInt(1));
}
Set(playerName, data);
}
}

View File

@ -0,0 +1,51 @@
package mineplex.core.boosters.tips;
import mineplex.core.database.MinecraftRepository;
import mineplex.database.routines.ClaimTips;
import mineplex.serverdata.database.DBPool;
import mineplex.serverdata.database.RepositoryBase;
import mineplex.serverdata.database.column.ColumnInt;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
/**
* @author Shaun Bennett
*/
public class BoosterTipRepository extends MinecraftRepository
{
public static String LOG_TIP = "INSERT INTO Account.accountTipLogs (accountId, boosterId) VALUES (?, ?)";
public static String ADD_TIP = "INSERT INTO Account.accountTip (accountId, tips) VALUES (?, ?) ON DUPLICATE KEY UPDATE tips = tips + ?";
public BoosterTipRepository(JavaPlugin plugin)
{
super(plugin, DBPool.getAccount());
}
public boolean addTip(int tipperId, int recieverId, int boosterId)
{
// Add tip to recieverId
// Log tip
return false;
}
public int claimTips(int accountId)
{
ClaimTips claimTips = new ClaimTips();
claimTips.setAccountId_in(accountId);
claimTips.execute(jooq().configuration());
return claimTips.getTipsClaimed();
}
@Override
protected void initialize()
{
}
@Override
protected void update()
{
}
}

View File

@ -0,0 +1,24 @@
package mineplex.core.boosters.tips;
/**
* @author Shaun Bennett
*/
public class PlayerTipData
{
private int _tips;
public PlayerTipData()
{
_tips = 0;
}
public int getTips()
{
return _tips;
}
public void setTips(int tips)
{
_tips = tips;
}
}

View File

@ -0,0 +1,11 @@
package mineplex.core.boosters.tips;
/**
* @author Shaun Bennett
*/
public enum TipAddResult
{
ALREADY_TIPPED_BOOSTER,
INVALID_ACCOUNT_ID,
SUCCESS
}