Stub out new Power Play rewards calculations

This commit is contained in:
cnr 2016-09-29 23:55:47 -07:00
parent f0c7b1d6aa
commit 4fe5eb8b45
5 changed files with 243 additions and 152 deletions

View File

@ -1,6 +1,11 @@
package mineplex.core.bonuses.gui.buttons; package mineplex.core.bonuses.gui.buttons;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import mineplex.core.bonuses.BonusManager; import mineplex.core.bonuses.BonusManager;
import mineplex.core.common.util.C; import mineplex.core.common.util.C;
@ -11,6 +16,7 @@ import mineplex.core.gui.GuiItem;
import mineplex.core.inventory.InventoryManager; import mineplex.core.inventory.InventoryManager;
import mineplex.core.powerplayclub.PowerPlayClubRepository; import mineplex.core.powerplayclub.PowerPlayClubRepository;
import mineplex.core.powerplayclub.PowerPlayClubRewards; import mineplex.core.powerplayclub.PowerPlayClubRewards;
import mineplex.core.powerplayclub.PowerPlayData;
import mineplex.core.shop.item.ShopItem; import mineplex.core.shop.item.ShopItem;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Sound; import org.bukkit.Sound;
@ -52,7 +58,7 @@ public class PowerPlayClubButton implements GuiItem
@Override @Override
public void click(ClickType clickType) public void click(ClickType clickType)
{ {
if (isAvailable() && !_bonusManager.getPowerPlayClubRepository().alreadyClaimed(_player)) if (isAvailable())
{ {
_player.closeInventory(); _player.closeInventory();
_player.playSound(_player.getLocation(), Sound.NOTE_PLING, 1, 1.6f); _player.playSound(_player.getLocation(), Sound.NOTE_PLING, 1, 1.6f);
@ -61,9 +67,9 @@ public class PowerPlayClubButton implements GuiItem
else else
{ {
_player.playSound(_player.getLocation(), Sound.ITEM_BREAK, 1, 10); _player.playSound(_player.getLocation(), Sound.ITEM_BREAK, 1, 10);
if (_powerPlayClubRepository.alreadyClaimed(_player)) if (_powerPlayClubRepository.getCachedData(_player).isSubscribed())
{ {
UtilPlayer.message(_player, F.main("Power Play Club", "Already claimed!")); UtilPlayer.message(_player, F.main("Power Play Club", "Already claimed! Come back next month!"));
} }
else else
{ {
@ -80,58 +86,72 @@ public class PowerPlayClubButton implements GuiItem
private void setItem() private void setItem()
{ {
ArrayList<String> lore = new ArrayList<>(); final Material material;
Material material; final String itemName;
byte data = 0; final List<String> lore;
String itemName;
if (isAvailable())
PowerPlayData cached = _powerPlayClubRepository.getCachedData(_player);
List<YearMonth> unclaimed = cached.getUnclaimedMonths();
if (!unclaimed.isEmpty())
{ {
// Player has unclaimed rewards, even if s/he's not currently subscribed
material = Material.GOLD_INGOT; material = Material.GOLD_INGOT;
itemName = C.cGreenB + "Power Play Club"; itemName = C.cGreenB + "Power Play Club";
lore = buildLore(unclaimed);
lore.add(""); lore.add("");
lore.add(C.Reset + "Click to claim!"); lore.add(C.Reset + "Click to claim!");
}
else } else if (cached.isSubscribed())
{ {
// Player is subscribed and has claimed all of his/her rewards
material = Material.REDSTONE_BLOCK; material = Material.REDSTONE_BLOCK;
itemName = C.cRedB + "Power Play Club"; itemName = C.cRedB + "Power Play Club";
}
lore.add(C.cYellow + "Rewards"); lore = new ArrayList<>();
lore.add(" " + C.cWhite + "2 Game Amplifiers");
lore.add(" " + C.cWhite + "1 Omega Chest");
for (PowerPlayClubRewards.PowerPlayClubItem prize : PowerPlayClubRewards.septemberItems())
{
lore.add(" " + C.cWhite + prize.getAmount() + " " + prize.getPrize());
}
//lore.add(" ");
//lore.add(C.cYellow + "Months left: " + C.cWhite + getMonthsLeft(true)); //TODO: figure this out
if (!isAvailable())
{
lore.add(" ");
if (_powerPlayClubRepository.alreadyClaimed(_player))
{
lore.add(C.cRed + "Already claimed!"); lore.add(C.cRed + "Already claimed!");
}
else // TODO: 'come back later!'
} else
{ {
// Player isn't subscribed; show them the rewards for this current month and tell them to subscribe
material = Material.REDSTONE_BLOCK;
itemName = C.cRedB + "Power Play Club";
lore = buildLore(Collections.singletonList(YearMonth.now()));
lore.add("");
lore.add(C.cRed + "Get Power Play Club months at"); lore.add(C.cRed + "Get Power Play Club months at");
lore.add(C.cAqua + "mineplex.com/shop"); lore.add(C.cAqua + "mineplex.com/shop");
} }
_item = new ShopItem(material, (byte)0, itemName, lore.toArray(new String[lore.size()]), 1, false, false);
} }
_item = new ShopItem(material, data, itemName, lore.toArray(new String[0]), 1, false, false); private List<String> buildLore(List<YearMonth> unclaimed)
{
List<String> lore = new ArrayList<>();
lore.add(C.cYellow + "Rewards");
lore.add(" " + C.cWhite + (PowerPlayClubRewards.AMPLIFIERS_PER_MONTH * unclaimed.size()) + " Game Amplifier");
lore.add(" " + C.cWhite + (PowerPlayClubRewards.CHESTS_PER_MONTH * unclaimed.size()) + " Omega Chest");
PowerPlayClubRewards.rewardsForMonths(unclaimed).forEach(prize ->
{
lore.add(" " + C.cWhite + prize.getPrize());
});
return lore;
} }
private boolean isAvailable() private boolean isAvailable()
{ {
return _powerPlayClubRepository.canClaim(_player); return !_powerPlayClubRepository.getCachedData(_player).getUnclaimedMonths().isEmpty();
} }
public static boolean isAvailable(Player player, PowerPlayClubRepository repo) public static boolean isAvailable(Player player, PowerPlayClubRepository repo)
{ {
return repo.canClaim(player); return !repo.getCachedData(player).getUnclaimedMonths().isEmpty();
} }
} }

View File

@ -1,26 +0,0 @@
package mineplex.core.powerplayclub;
public class PPCPlayerData {
private boolean _subscribed;
private boolean _claimed;
public void setSubscribed(boolean subscribed)
{
_subscribed = subscribed;
}
public void setClaimed(boolean claimed)
{
_claimed = claimed;
}
public boolean hasClaimed()
{
return _claimed;
}
public boolean hasSubscribed()
{
return _subscribed;
}
}

View File

@ -1,26 +1,39 @@
package mineplex.core.powerplayclub; package mineplex.core.powerplayclub;
import mineplex.core.MiniClientPlugin;
import mineplex.core.account.CoreClientManager; import mineplex.core.account.CoreClientManager;
import mineplex.core.account.ILoginProcessor; import mineplex.core.account.ILoginProcessor;
import mineplex.serverdata.database.DBPool; import mineplex.serverdata.database.DBPool;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.sql.*; import java.sql.*;
import java.sql.Date;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.UUID; import java.time.YearMonth;
import java.util.*;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
public class PowerPlayClubRepository extends MiniClientPlugin<PPCPlayerData> { public class PowerPlayClubRepository implements Listener {
// Data loaded by the first ILoginProcessor, waiting for the second
private final Map<UUID, List<YearMonth>> _stageOneDataClaims = new HashMap<>();
// Cached data produced by combining the first and second ILoginProcessors.
// This data is not guaranteed to be fresh, and should be recalculated before
// giving a player his/her rewards.
private final Map<UUID, PowerPlayData> _cachedPlayerData = new HashMap<>();
private final CoreClientManager _clientManager; private final CoreClientManager _clientManager;
public PowerPlayClubRepository(JavaPlugin plugin, CoreClientManager clientManager) { public PowerPlayClubRepository(JavaPlugin plugin, CoreClientManager clientManager) {
super("PowerPlayClubRepository", plugin);
_clientManager = clientManager; _clientManager = clientManager;
Bukkit.getPluginManager().registerEvents(this, plugin);
clientManager.addStoredProcedureLoginProcessor(new ILoginProcessor() { clientManager.addStoredProcedureLoginProcessor(new ILoginProcessor() {
@Override @Override
public String getName() { public String getName() {
@ -29,7 +42,12 @@ public class PowerPlayClubRepository extends MiniClientPlugin<PPCPlayerData> {
@Override @Override
public void processLoginResultSet(String playerName, UUID uuid, int accountId, ResultSet resultSet) throws SQLException { public void processLoginResultSet(String playerName, UUID uuid, int accountId, ResultSet resultSet) throws SQLException {
PowerPlayClubRepository.this.Get(uuid).setClaimed(resultSet.next()); List<YearMonth> claims = new ArrayList<>();
while (resultSet.next())
{
claims.add(YearMonth.of(resultSet.getInt("claimYear"), resultSet.getInt("claimMonth")));
}
_stageOneDataClaims.put(uuid, claims);
} }
@Override @Override
@ -46,7 +64,16 @@ public class PowerPlayClubRepository extends MiniClientPlugin<PPCPlayerData> {
@Override @Override
public void processLoginResultSet(String playerName, UUID uuid, int accountId, ResultSet resultSet) throws SQLException { public void processLoginResultSet(String playerName, UUID uuid, int accountId, ResultSet resultSet) throws SQLException {
PowerPlayClubRepository.this.Get(uuid).setSubscribed(resultSet.next()); List<PowerPlayData.Subscription> subscriptions = new ArrayList<>();
while (resultSet.next())
{
LocalDate date = resultSet.getDate("startDate").toLocalDate();
PowerPlayData.SubscriptionDuration duration = PowerPlayData.SubscriptionDuration.valueOf(resultSet.getString("duration").toUpperCase());
subscriptions.add(new PowerPlayData.Subscription(date, duration));
}
// Now that we have the claims from the first processor and subscriptions from this one, combine them
_cachedPlayerData.put(uuid, PowerPlayData.fromSubsAndClaims(subscriptions, _stageOneDataClaims.remove(uuid)));
} }
@Override @Override
@ -56,6 +83,13 @@ public class PowerPlayClubRepository extends MiniClientPlugin<PPCPlayerData> {
}); });
} }
@EventHandler
public void onQuit(PlayerQuitEvent event)
{
_stageOneDataClaims.remove(event.getPlayer().getUniqueId()); // Just in case.
_cachedPlayerData.remove(event.getPlayer().getUniqueId());
}
public CompletableFuture<Void> addSubscription(int accountId, LocalDate date, String duration) public CompletableFuture<Void> addSubscription(int accountId, LocalDate date, String duration)
{ {
return CompletableFuture.supplyAsync(() -> return CompletableFuture.supplyAsync(() ->
@ -101,18 +135,75 @@ public class PowerPlayClubRepository extends MiniClientPlugin<PPCPlayerData> {
}); });
} }
public boolean alreadyClaimed(Player player) public CompletableFuture<PowerPlayData> loadData(Player player)
{ {
return Get(player).hasClaimed(); return loadSubscriptions(player).thenCombine(loadClaimMonths(player), PowerPlayData::fromSubsAndClaims);
} }
public boolean canClaim(Player player) public CompletableFuture<List<YearMonth>> loadClaimMonths(Player player)
{ {
return Get(player).hasSubscribed() && !Get(player).hasClaimed(); int accountId = _clientManager.Get(player).getAccountId();
return CompletableFuture.supplyAsync(() ->
{
try (Connection connection = DBPool.getAccount().getConnection())
{
PreparedStatement statement = connection.prepareStatement("SELECT * FROM powerPlayClaims WHERE accountId = ?");
statement.setInt(1, accountId);
ResultSet resultSet = statement.executeQuery();
List<YearMonth> claims = new ArrayList<>();
while (resultSet.next())
{
claims.add(YearMonth.of(resultSet.getInt("claimYear"), resultSet.getInt("claimMonth")));
}
return claims;
} catch (SQLException e)
{
throw new CompletionException(e);
}
});
} }
@Override public CompletableFuture<List<PowerPlayData.Subscription>> loadSubscriptions(Player player)
protected PPCPlayerData addPlayer(UUID uuid) { {
return new PPCPlayerData(); int accountId = _clientManager.Get(player).getAccountId();
return CompletableFuture.supplyAsync(() ->
{
try (Connection connection = DBPool.getAccount().getConnection())
{
PreparedStatement statement = connection.prepareStatement("SELECT * FROM powerPlaySubs WHERE accountId = ?");
statement.setInt(1, accountId);
ResultSet resultSet = statement.executeQuery();
List<PowerPlayData.Subscription> subscriptions = new ArrayList<>();
while (resultSet.next())
{
LocalDate date = resultSet.getDate("startDate").toLocalDate();
PowerPlayData.SubscriptionDuration duration = PowerPlayData.SubscriptionDuration.valueOf(resultSet.getString("duration").toUpperCase());
subscriptions.add(new PowerPlayData.Subscription(date, duration));
}
return subscriptions;
} catch (SQLException e)
{
throw new CompletionException(e);
}
});
}
public PowerPlayData getCachedData(Player player)
{
return _cachedPlayerData.get(player.getUniqueId());
}
public void putCachedData(Player player, PowerPlayData data)
{
if (player.isOnline())
{
_cachedPlayerData.put(player.getUniqueId(), data);
}
} }
} }

View File

@ -2,8 +2,12 @@ package mineplex.core.powerplayclub;
import java.time.Month; import java.time.Month;
import java.time.Year; import java.time.Year;
import java.time.YearMonth;
import java.util.*; import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import com.google.common.collect.ImmutableMap;
import mineplex.core.common.currency.GlobalCurrency; import mineplex.core.common.currency.GlobalCurrency;
import mineplex.core.common.skin.SkinData; import mineplex.core.common.skin.SkinData;
import mineplex.core.common.util.BukkitFuture; import mineplex.core.common.util.BukkitFuture;
@ -18,65 +22,22 @@ import org.bukkit.inventory.ItemStack;
public class PowerPlayClubRewards public class PowerPlayClubRewards
{ {
private static final Map<RewardMonth, List<PowerPlayClubItem>> rewards = new HashMap<>(); public static final int AMPLIFIERS_PER_MONTH = 2;
public static final int CHESTS_PER_MONTH = 1;
static private static final Map<YearMonth, PowerPlayClubItem> rewards = ImmutableMap.<YearMonth, PowerPlayClubItem>builder()
{ .put(YearMonth.of(2016, Month.SEPTEMBER), new PowerPlayClubItem("Squid Morph", new ItemStack(Material.INK_SACK)))
setRewards(2016, Month.SEPTEMBER, new PowerPlayClubItem("Squid Morph", 1, new ItemStack(Material.INK_SACK))); .put(YearMonth.of(2016, Month.OCTOBER), new PowerPlayClubItem("Witch Morph", SkinData.WITCH.getSkull()))
setRewards(2016, Month.OCTOBER, new PowerPlayClubItem("Witch Morph", 1, SkinData.WITCH.getSkull())); .build();
}
public static List<PowerPlayClubItem> septemberItems() // TODO: figure this out private static class PowerPlayClubItem
{
return rewards.values().iterator().next();
}
private static void setRewards(int year, Month month, PowerPlayClubItem... items)
{
rewards.put(new RewardMonth(Year.of(year), month), Arrays.asList(items));
}
private static class RewardMonth
{
private final Year year;
private final Month month;
static RewardMonth of(Year year, Month month)
{
return new RewardMonth(year, month);
}
RewardMonth(Year year, Month month)
{
this.year = year;
this.month = month;
}
@Override
public boolean equals(Object obj) {
if (obj == null || getClass() != obj.getClass()) return false;
RewardMonth that = (RewardMonth) obj;
return Objects.equals(this.year, that.year) && Objects.equals(this.month, that.month);
}
@Override
public int hashCode() {
return Objects.hash(year, month);
}
}
public static class PowerPlayClubItem
{ {
private final String _prize; private final String _prize;
private final int _amount;
private final ItemStack _display; private final ItemStack _display;
public PowerPlayClubItem(String prize, int amount, ItemStack display) PowerPlayClubItem(String prize, ItemStack display)
{ {
_prize = prize; _prize = prize;
_amount = amount;
_display = display; _display = display;
} }
@ -85,51 +46,45 @@ public class PowerPlayClubRewards
return _prize; return _prize;
} }
public int getAmount()
{
return _amount;
}
public ItemStack getDisplay() public ItemStack getDisplay()
{ {
return _display; return _display;
} }
} }
public static List<PowerPlayClubItem> rewardsForMonths(List<YearMonth> months)
{
return months.stream().map(rewards::get).collect(Collectors.toList());
}
public static void giveAllItems(Player player, DonationManager donationManager, InventoryManager inventoryManager, PowerPlayClubRepository repo) public static void giveAllItems(Player player, DonationManager donationManager, InventoryManager inventoryManager, PowerPlayClubRepository repo)
{ {
UtilPlayer.message(player, F.main("Power Play Club", "Verifying subscription..")); UtilPlayer.message(player, F.main("Power Play Club", "Verifying subscription.."));
repo.attemptClaim(player).thenCompose(BukkitFuture.accept(success -> repo.attemptClaim(player).thenCompose(success ->
{ {
if (!success) if (!success)
{ {
UtilPlayer.message(player, F.main("Power Play Club", "An unexpected error happened!")); UtilPlayer.message(player, F.main("Power Play Club", "An unexpected error happened!"));
return; return CompletableFuture.completedFuture(null);
} }
repo.Get(player).setClaimed(true); PowerPlayData cached = repo.getCachedData(player);
List<PowerPlayClubItem> items = rewardsForMonths(cached.getUnclaimedMonths());
for (PowerPlayClubItem item : septemberItems()) // TODO: figure this out
// Give normal power play items
items.forEach(item ->
{ {
/*Item fItem = manager.getItem(item.getPrize());
if (fItem == null)
{
UtilPlayer.message(player, F.main("Power Play Club", "An unexpected error happened!"));
}
else
{
manager.addItemToInventory(player, fItem.Name, 1);
UtilPlayer.message(player, F.main("Power Play Club", "You received " + item.getAmount() + "x " + F.elem(item.getPrize()) + "."));
}*/
donationManager.PurchaseUnknownSalesPackage(null, player.getName(), donationManager.PurchaseUnknownSalesPackage(null, player.getName(),
donationManager.getClientManager().Get(player).getAccountId(), item.getPrize(), donationManager.getClientManager().Get(player).getAccountId(), item.getPrize(),
GlobalCurrency.TREASURE_SHARD, 0, true); GlobalCurrency.TREASURE_SHARD, 0, true);
UtilPlayer.message(player, F.main("Power Play Club", "You received " + item.getAmount() + "x " + UtilPlayer.message(player, F.main("Power Play Club", "You received the " +
F.elem(item.getPrize()) + ".")); F.elem(item.getPrize()) + "."));
} });
// Give amplifiers and chests
Item gameAmplifier = inventoryManager.getItem("Game Booster"); Item gameAmplifier = inventoryManager.getItem("Game Booster");
if (gameAmplifier == null) if (gameAmplifier == null)
{ {
@ -137,8 +92,8 @@ public class PowerPlayClubRewards
} }
else else
{ {
inventoryManager.addItemToInventory(player, gameAmplifier.Name, 2); inventoryManager.addItemToInventory(player, gameAmplifier.Name, AMPLIFIERS_PER_MONTH * items.size());
UtilPlayer.message(player, F.main("Power Play Club", "You received 2x " + F.elem("Game Boosters") + ".")); UtilPlayer.message(player, F.main("Power Play Club", "You received " + (AMPLIFIERS_PER_MONTH * items.size()) + "x " + F.elem("Game Amplifier") + "."));
} }
Item omegaChest = inventoryManager.getItem("Omega Chest"); Item omegaChest = inventoryManager.getItem("Omega Chest");
if (omegaChest == null) if (omegaChest == null)
@ -147,10 +102,13 @@ public class PowerPlayClubRewards
} }
else else
{ {
inventoryManager.addItemToInventory(player, omegaChest.Name, 1); inventoryManager.addItemToInventory(player, omegaChest.Name, CHESTS_PER_MONTH * items.size());
UtilPlayer.message(player, F.main("Power Play Club", "You received 1x " + F.elem("Omega Chest") + ".")); UtilPlayer.message(player, F.main("Power Play Club", "You received " + (CHESTS_PER_MONTH * items.size()) + "x " + F.elem("Omega Chest") + "."));
} }
}));
// Refresh Power Play data on the server
return repo.loadData(player).thenCompose(BukkitFuture.accept(data -> repo.putCachedData(player, data)));
});
} }
} }

View File

@ -0,0 +1,48 @@
package mineplex.core.powerplayclub;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.List;
public class PowerPlayData
{
private final boolean _subscribed;
private final List<YearMonth> _unclaimedMonths;
static PowerPlayData fromSubsAndClaims(List<Subscription> subscriptions, List<YearMonth> claimMonths)
{
// TODO
return null;
}
static class Subscription {
private final LocalDate _startDate;
private final SubscriptionDuration _duration;
Subscription(LocalDate startDate, SubscriptionDuration duration)
{
_startDate = startDate;
_duration = duration;
}
}
enum SubscriptionDuration {
MONTH, YEAR
}
private PowerPlayData(boolean subscribed, List<YearMonth> unclaimedMonths)
{
_subscribed = subscribed;
_unclaimedMonths = unclaimedMonths;
}
public List<YearMonth> getUnclaimedMonths()
{
return _unclaimedMonths;
}
public boolean isSubscribed()
{
return _subscribed;
}
}