Fix playwire multithreading and convert to new 5-ticket per 24-hour system

This commit is contained in:
AlexTheCoder 2016-10-29 11:05:36 -04:00 committed by Shaun Bennett
parent 824ef685c0
commit 0070fed6c7
9 changed files with 153 additions and 114 deletions

View File

@ -677,8 +677,8 @@ public class BonusManager extends MiniClientPlugin<BonusClientData> implements I
BonusClientData client = Get(player);
int availableRewards = 0;
if (_playWireManager.canRedeem(player)) availableRewards++;
if (_playWireManager.Get(player) != null && _playWireManager.Get(player).getAccountId() == -1 && _playWireManager.canRedeemTickets(_playWireManager.Get(player))) availableRewards++;
if (_youtubeManager.canYoutube(player)) availableRewards++;
if (canRank(player) && _clientManager.hasRank(player, Rank.ULTRA) && isPastAugust()) availableRewards++;
if (canDaily(player)) availableRewards++;
@ -839,7 +839,7 @@ public class BonusManager extends MiniClientPlugin<BonusClientData> implements I
{
if (Recharge.Instance.use(player, "Carl Inform", 240000, false, false))
{
if(_pollManager.hasPoll(player) || _playWireManager.canRedeem(player) || (canRank(player) && _clientManager.hasRank(player, Rank.ULTRA) && isPastAugust()) || canDaily(player) || PowerPlayClubButton.isAvailable(player, _powerPlayClubRepository))
if(_pollManager.hasPoll(player) || (_playWireManager.Get(player) != null && _playWireManager.Get(player).getAccountId() == -1 && _playWireManager.canRedeemTickets(_playWireManager.Get(player))) || (canRank(player) && _clientManager.hasRank(player, Rank.ULTRA) && isPastAugust()) || canDaily(player) || PowerPlayClubButton.isAvailable(player, _powerPlayClubRepository))
{
if(_showCarl.containsKey(player.getName()))
{

View File

@ -61,7 +61,11 @@ public class PlayWireButton implements GuiItem
@Override
public ItemStack getObject()
{
return _manager.canRedeem(_player) ? ENABLED_ICON : DISABLED_ICON;
if (_manager.Get(_player) == null || _manager.Get(_player).getAccountId() == -1)
{
return DISABLED_ICON;
}
return _manager.canRedeemTickets(_manager.Get(_player)) ? ENABLED_ICON : DISABLED_ICON;
}
@Override

View File

@ -1,53 +1,40 @@
package mineplex.core.playwire;
import mineplex.core.common.util.UtilTime.TimeUnit;
/**
*
*/
public class PlayWireClientData
{
private int _accountId;
private int _ticketsRemaining;
private long _ticketRefresh;
private long _redeemTime;
private int _streak;
public PlayWireClientData(long date)
public PlayWireClientData(int accountId, int ticketsRemaining, long ticketRefresh)
{
_redeemTime = date;
long curr = System.currentTimeMillis();
if (_redeemTime <= 0)
{
_streak = 0;
}
_accountId = accountId;
_ticketsRemaining = ticketsRemaining;
_ticketRefresh = ticketRefresh;
}
public long getRedeemTime()
public int getAccountId()
{
return _redeemTime;
return _accountId;
}
public void setRedeemTime(long date)
public int getTicketsRemaining()
{
long old = _redeemTime;
_redeemTime = date;
if (_redeemTime - old > TimeUnit.DAYS.getMilliseconds() * 2)
{
_streak = 0;
} else
{
setStreak(getStreak() + 1);
}
return _ticketsRemaining;
}
public int getStreak()
public long getTicketRefresh()
{
return _streak;
return _ticketRefresh;
}
public void setStreak(int streak)
public void setTicketsRemaining(int ticketsRemaining)
{
_streak = streak;
_ticketsRemaining = ticketsRemaining;
}
}
public void setTicketRefresh(long ticketRefresh)
{
_ticketRefresh = ticketRefresh;
}
}

View File

@ -4,10 +4,13 @@ import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.Managers;
@ -16,72 +19,100 @@ import mineplex.core.account.CoreClientManager;
import mineplex.core.bonuses.BonusManager;
import mineplex.core.common.util.C;
import mineplex.core.common.util.Callback;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.common.util.UtilTime;
import mineplex.core.common.util.UtilTime.TimeUnit;
import mineplex.core.donation.DonationManager;
import mineplex.core.inventory.InventoryManager;
import mineplex.core.treasure.TreasureType;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import mineplex.serverdata.database.DBPool;
/**
*
*/
public class PlayWireManager extends MiniDbClientPlugin<PlayWireClientData>
{
private final long COOL_DOWN = TimeUnit.HOURS.getMilliseconds();
private static final int MAX_TICKETS_PER_PERIOD = 5;
private final long COOL_DOWN = TimeUnit.HOURS.getMilliseconds() * 24;
private static final int REWARD_MESSAGE_DELAY_SECONDS = 10;
private final CoreClientManager _clientManager;
private final DonationManager _donationManager;
private final PlayWireRepository _repository;
public PlayWireManager(JavaPlugin plugin, CoreClientManager clientManager, DonationManager donationManager)
public PlayWireManager(JavaPlugin plugin, CoreClientManager clientManager)
{
super("PlayWire", plugin, clientManager);
_clientManager = clientManager;
_donationManager = donationManager;
_repository = new PlayWireRepository(this);
getPlugin().getCommand("playwire").setExecutor(new PlayWireCommand(this));
}
@EventHandler
public void onUpdate(UpdateEvent event)
{
if (event.getType() != UpdateType.SEC)
{
return;
}
final Map<PlayWireClientData, Player> update = new HashMap<>();
Bukkit.getOnlinePlayers().stream().filter(player -> Get(player) != null && Get(player).getAccountId() != -1 && Get(player).getTicketRefresh() != -1 && Get(player).getTicketRefresh() < System.currentTimeMillis()).forEach(player ->
{
PlayWireClientData data = Get(player);
data.setTicketRefresh(-1);
data.setTicketsRemaining(MAX_TICKETS_PER_PERIOD);
update.put(data, player);
});
if (!update.isEmpty())
{
runAsync(() ->
{
update.entrySet().forEach(entry ->
{
PlayWireClientData data = entry.getKey();
_repository.attemptPlayWire(data, () ->
{
UtilPlayer.message(entry.getValue(), F.main("Carl", "Your Ad Ticketssssss have refresssshed! Come ssssee me for ssssome " + F.elem("Spin Tickets") + "!"));
}, false);
});
update.clear();
});
}
}
@Override
public String getQuery(int accountId, String uuid, String name)
{
return "SELECT `redeemed`, `streak` FROM `play_wire` WHERE `accountId`=" + accountId + ";";
return "SELECT ticketsRemaining, ticketRefresh FROM play_wire WHERE accountId=" + accountId + ";";
}
public boolean canRedeem(Player player)
public boolean canRedeemTickets(PlayWireClientData data)
{
PlayWireClientData data = Get(player);
long now = System.currentTimeMillis();
return data == null || data.getRedeemTime() == -1 || (now - data.getRedeemTime()) > COOL_DOWN;
return data == null || data.getTicketRefresh() >= now || data.getTicketsRemaining() <= 0;
}
public void attemptRedeem(Player player)
{
if (!canRedeem(player))
final PlayWireClientData client = Get(player);
if (client == null || client.getAccountId() == -1)
{
player.sendMessage(ResponseType.UNCOUNTED.getMessage());
player.sendMessage(ResponseType.UNFILLED.getMessage());
return;
}
PlayWireClientData client = Get(player);
client.setRedeemTime(System.currentTimeMillis());
if(client.getStreak() == 7)
if (!canRedeemTickets(client))
{
//
Managers.get(InventoryManager.class).addItemToInventory(player, TreasureType.ANCIENT.getItemName(), 1);
} else if(client.getStreak() == 14)
{
Managers.get(InventoryManager.class).addItemToInventory(player, TreasureType.ANCIENT.getItemName(), 1);
client.setStreak(0);
player.sendMessage(ResponseType.UNCOUNTED.getMessage(UtilTime.MakeStr(client.getTicketRefresh() - System.currentTimeMillis())));
return;
}
_repository.attemptPlayWire(player, client, () ->
if (client.getTicketsRemaining() >= MAX_TICKETS_PER_PERIOD)
{
client.setTicketRefresh(System.currentTimeMillis() + COOL_DOWN);
}
client.setTicketsRemaining(Math.max(client.getTicketsRemaining() - 1, 0));
_repository.attemptPlayWireAsync(client, () ->
{
_donationManager.RewardCoinsLater("Watching Ad", player, 100);
final int accountId = _clientManager.Get(player).getAccountId();
final Callback<Integer> ticketCallback = new Callback<Integer>()
{
@ -118,7 +149,7 @@ public class PlayWireManager extends MiniDbClientPlugin<PlayWireClientData>
}
});
Managers.get(BonusManager.class).addPendingExplosion(player, player.getName());
Bukkit.getScheduler().runTaskLater(getClientManager().getPlugin(), () -> UtilPlayer.message(player, ResponseType.COUNTED.getMessage()), REWARD_MESSAGE_DELAY_SECONDS * 20L);
Bukkit.getScheduler().runTaskLater(getClientManager().getPlugin(), () -> UtilPlayer.message(player, ResponseType.COUNTED.getMessage(client.getTicketsRemaining() + "")), REWARD_MESSAGE_DELAY_SECONDS * 20L);
});
}
@ -151,18 +182,20 @@ public class PlayWireManager extends MiniDbClientPlugin<PlayWireClientData>
boolean hasRow = resultSet.next();
if (hasRow)
{
PlayWireClientData client = new PlayWireClientData(resultSet.getLong(1));
client.setStreak(resultSet.getInt(2));
PlayWireClientData client = new PlayWireClientData(accountId, resultSet.getInt("ticketsRemaining"), resultSet.getLong("ticketRefresh"));
Set(uuid, client);
} else
}
else
{
Set(uuid, new PlayWireClientData(-1));
PlayWireClientData client = new PlayWireClientData(accountId, MAX_TICKETS_PER_PERIOD, -1L);
Set(uuid, client);
_repository.attemptPlayWire(client, null, false);
}
}
@Override
protected PlayWireClientData addPlayer(UUID uuid)
{
return new PlayWireClientData(-1);
return new PlayWireClientData(-1, -1, -1);
}
}
}

View File

@ -1,20 +1,14 @@
package mineplex.core.playwire;
import mineplex.serverdata.database.DBPool;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* Totally original code I wrote, did not copy it from YouTubeRepo, no sir.
*/
import mineplex.serverdata.database.DBPool;
public class PlayWireRepository
{
private static final String INSERT_LAST_REDEEMED = "INSERT INTO `play_wire` VALUES(?,?,?) ON DUPLICATE KEY UPDATE `redeemed` = ?, `streak` = ?";
private static final String INSERT_LAST_REDEEMED = "INSERT INTO play_wire (accountId, ticketsRemaining, ticketRefresh) VALUES (?,?,?) ON DUPLICATE KEY UPDATE ticketsRemaining=VALUES(ticketsRemaining), ticketRefresh=VALUES(ticketRefresh)";
private final PlayWireManager _manager;
@ -22,31 +16,46 @@ public class PlayWireRepository
{
_manager = manager;
}
public void attemptPlayWire(Player player, PlayWireClientData client, Runnable runnable)
public void attemptPlayWire(final PlayWireClientData client, final Runnable runnable, boolean forceAsync)
{
int accountId = _manager.getClientManager().Get(player).getAccountId();
Bukkit.getScheduler().runTaskAsynchronously(_manager.getPlugin(), () ->
if (client.getAccountId() == -1)
{
return;
}
Runnable doSQL = () ->
{
try (Connection connection = DBPool.getAccount().getConnection())
{
PreparedStatement statement = connection.prepareStatement(INSERT_LAST_REDEEMED);
statement.setInt(1, accountId);
statement.setLong(2, client.getRedeemTime());
statement.setInt(3, client.getStreak());
statement.setLong(4, client.getRedeemTime());
statement.setInt(5, client.getStreak());
statement.setInt(1, client.getAccountId());
statement.setInt(2, client.getTicketsRemaining());
statement.setLong(3, client.getTicketRefresh());
statement.executeUpdate();
runnable.run();
} catch (SQLException e)
if (runnable != null)
{
_manager.runSync(runnable);
}
}
catch (SQLException e)
{
e.printStackTrace();
}
});
};
if (forceAsync)
{
_manager.runAsync(doSQL);
}
else
{
doSQL.run();
}
}
}
public void attemptPlayWireAsync(final PlayWireClientData client, final Runnable runnable)
{
attemptPlayWire(client, runnable, false);
}
}

View File

@ -7,9 +7,8 @@ import mineplex.core.common.util.F;
*/
public enum ResponseType
{
COUNTED(F.main("Carl", "Rewarded " + F.elem("100 Treasure Shards") + " and " + F.elem("1 Carl Spin Ticket") + " for watching the Ad")),
UNCOUNTED(F.main("Carl", "You already watched the Ad within the past hour!")),
COUNTED(F.main("Carl", "Rewarded " + F.elem("1 Carl Spin Ticket") + " for watching the Ad! %elem% tickets remaining for the current 24 hour period!")),
UNCOUNTED(F.main("Carl", "You already received your 5 " + F.elem("Carl Spin Tickets") + " for the current 24 hours! Your available tickets will refresh in %elem%!")),
BLOCKED(F.main("Carl", "You have an AdBlocker on, but tried to watch the Ad! Ssssssslight problem there!")),
UNFILLED(F.main("Carl", "Ssssomething went wrong with the Ad, we'll get it sorted ASAP.")),;
@ -20,9 +19,16 @@ public enum ResponseType
_message = message;
}
public String getMessage()
public String getMessage(String... elements)
{
return _message;
String message = _message;
if (elements.length > 0)
{
for (int i = 0; i < elements.length; i++)
{
message = message.replaceFirst("%elem%", elements[i]);
}
}
return message;
}
}
}

View File

@ -190,7 +190,7 @@ public class HubManager extends MiniPlugin implements IChatMessageFormatter
FacebookManager facebookManager = new FacebookManager(plugin, clientManager, donationManager, inventoryManager);
YoutubeManager youtubeManager = new YoutubeManager(plugin, clientManager, donationManager);
PlayWireManager playWireManager = new PlayWireManager(plugin, clientManager, donationManager);
PlayWireManager playWireManager = new PlayWireManager(plugin, clientManager);
_bonusManager = new BonusManager(plugin, null, playWireManager, clientManager, donationManager, pollManager , npcManager, hologramManager, statsManager, _inventoryManager, petManager, facebookManager, youtubeManager, _gadgetManager, thankManager);
World world = _spawn.getWorld();

View File

@ -218,7 +218,7 @@ public class HubManager extends MiniClientPlugin<HubClient> implements IChatMess
FacebookManager facebookManager = new FacebookManager(plugin, clientManager, donationManager, inventoryManager);
YoutubeManager youtubeManager = new YoutubeManager(plugin, clientManager, donationManager);
PlayWireManager playWireManager = new PlayWireManager(plugin, clientManager, donationManager);
PlayWireManager playWireManager = new PlayWireManager(plugin, clientManager);
_bonusManager = new BonusManager(plugin, null, playWireManager, clientManager, donationManager, pollManager , npcManager, hologramManager, statsManager, _inventoryManager, petManager, facebookManager, youtubeManager, _gadgetManager, thankManager);

View File

@ -336,7 +336,7 @@ public class ArcadeManager extends MiniPlugin implements IRelation
new GameStatManager(this);
FacebookManager facebookManager = new FacebookManager(plugin, clientManager, donationManager, inventoryManager);
YoutubeManager youtubeManager = new YoutubeManager(plugin, clientManager, donationManager);
PlayWireManager playWireManager = new PlayWireManager(plugin, clientManager, donationManager);
PlayWireManager playWireManager = new PlayWireManager(plugin, clientManager);
_bonusManager = new BonusManager(plugin, _gameLobbyManager.getCarl(), playWireManager, clientManager, donationManager, pollManager , npcManager, hologramManager, statsManager, _inventoryManager, petManager, facebookManager, youtubeManager, _cosmeticManager.getGadgetManager(), thankManager);
new GameLootManager(this, petManager, _bonusManager.getRewardManager());