Queue up polls for players instead of constantly checking for next poll
This commit is contained in:
parent
4ba7da9a54
commit
43fd604695
@ -1,5 +1,5 @@
|
||||
<component name="ArtifactManager">
|
||||
<artifact type="jar" build-on-make="true" name="Mineplex.Hub:test">
|
||||
<artifact type="jar" name="Mineplex.Hub:test">
|
||||
<output-path>$PROJECT_DIR$/../Testing/Hub/plugins</output-path>
|
||||
<root id="archive" name="Hub.jar">
|
||||
<element id="module-output" name="Mineplex.Core" />
|
||||
|
@ -1,13 +1,15 @@
|
||||
package mineplex.core;
|
||||
|
||||
import mineplex.core.account.event.ClientUnloadEvent;
|
||||
import mineplex.core.account.event.RetrieveClientInformationEvent;
|
||||
import mineplex.core.common.util.NautHashMap;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import mineplex.core.account.event.ClientUnloadEvent;
|
||||
import mineplex.core.account.event.RetrieveClientInformationEvent;
|
||||
import mineplex.core.common.util.NautHashMap;
|
||||
|
||||
public abstract class MiniClientPlugin<DataType extends Object> extends MiniPlugin
|
||||
{
|
||||
private static Object _clientDataLock = new Object();
|
||||
@ -48,6 +50,11 @@ public abstract class MiniClientPlugin<DataType extends Object> extends MiniPlug
|
||||
{
|
||||
return Get(player.getName());
|
||||
}
|
||||
|
||||
protected Collection<DataType> GetValues()
|
||||
{
|
||||
return _clientData.values();
|
||||
}
|
||||
|
||||
protected void Set(Player player, DataType data)
|
||||
{
|
||||
|
@ -1,5 +1,8 @@
|
||||
package mineplex.hub.poll;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
import mineplex.core.common.util.NautHashMap;
|
||||
|
||||
/**
|
||||
@ -11,11 +14,13 @@ public class PlayerPollData
|
||||
|
||||
private Poll _currentPoll;
|
||||
private long _lastPollTime;
|
||||
private Queue<Poll> _pollQueue;
|
||||
private NautHashMap<Integer, Integer> _pollAnswers;
|
||||
|
||||
public PlayerPollData()
|
||||
{
|
||||
_pollAnswers = new NautHashMap<Integer, Integer>();
|
||||
_pollQueue = new LinkedList<Poll>();
|
||||
}
|
||||
|
||||
public boolean isInPoll()
|
||||
@ -58,4 +63,9 @@ public class PlayerPollData
|
||||
return System.currentTimeMillis() - _lastPollTime > POLL_COOLDOWN;
|
||||
}
|
||||
|
||||
public synchronized Queue<Poll> getPollQueue()
|
||||
{
|
||||
return _pollQueue;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package mineplex.hub.poll;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Sound;
|
||||
@ -29,8 +30,6 @@ import mineplex.hub.poll.command.PollCommand;
|
||||
*/
|
||||
public class PollManager extends MiniClientPlugin<PlayerPollData>
|
||||
{
|
||||
private Object _pollLock = new Object();
|
||||
|
||||
private PollRepository _repository;
|
||||
private DonationManager _donationManager;
|
||||
private List<Poll> _polls;
|
||||
@ -52,12 +51,14 @@ public class PollManager extends MiniClientPlugin<PlayerPollData>
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
synchronized (_pollLock)
|
||||
_polls = _repository.retrievePolls();
|
||||
|
||||
for (PlayerPollData pollData : GetValues())
|
||||
{
|
||||
_polls = _repository.retrievePolls();
|
||||
updatePollQueue(pollData);
|
||||
}
|
||||
}
|
||||
}, 1L, 300L);
|
||||
}, 1L, 1200L);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -85,6 +86,7 @@ public class PollManager extends MiniClientPlugin<PlayerPollData>
|
||||
public void join(PlayerJoinEvent event)
|
||||
{
|
||||
PlayerPollData pollData = Get(event.getPlayer());
|
||||
updatePollQueue(pollData);
|
||||
pollData.setLastPollTime(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
@ -102,9 +104,9 @@ public class PollManager extends MiniClientPlugin<PlayerPollData>
|
||||
{
|
||||
if (pollData.shouldPoll())
|
||||
{
|
||||
Poll nextPoll = getNextPoll(pollData);
|
||||
Poll nextPoll = pollData.getPollQueue().poll();
|
||||
if (nextPoll != null)
|
||||
sendPoll(player, nextPoll);
|
||||
givePoll(player, nextPoll);
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -114,7 +116,19 @@ public class PollManager extends MiniClientPlugin<PlayerPollData>
|
||||
}
|
||||
}
|
||||
|
||||
public void sendPoll(Player player, Poll poll)
|
||||
public void updatePollQueue(PlayerPollData pollData)
|
||||
{
|
||||
Queue<Poll> pollQueue = pollData.getPollQueue();
|
||||
pollQueue.clear();
|
||||
|
||||
for (Poll poll : _polls)
|
||||
{
|
||||
if (!pollData.hasAnswered(poll))
|
||||
pollQueue.add(poll);
|
||||
}
|
||||
}
|
||||
|
||||
public void givePoll(Player player, Poll poll)
|
||||
{
|
||||
displayPoll(player, poll);
|
||||
player.playSound(player.getEyeLocation(), Sound.ORB_PICKUP, 2f, 0f);
|
||||
@ -174,25 +188,6 @@ public class PollManager extends MiniClientPlugin<PlayerPollData>
|
||||
return Get(player).isInPoll();
|
||||
}
|
||||
|
||||
public Poll getNextPoll(PlayerPollData pollData)
|
||||
{
|
||||
synchronized (_pollLock)
|
||||
{
|
||||
Poll nextPoll = null;
|
||||
|
||||
for (Poll poll : _polls)
|
||||
{
|
||||
if (!pollData.hasAnswered(poll))
|
||||
{
|
||||
nextPoll = poll;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return nextPoll;
|
||||
}
|
||||
}
|
||||
|
||||
public void answerPoll(final Player player, final Poll poll, final int answer)
|
||||
{
|
||||
final String name = player.getName();
|
||||
|
Loading…
Reference in New Issue
Block a user