From 573f71164f603efa629141534becddfd25740664 Mon Sep 17 00:00:00 2001 From: Shaun Bennett Date: Wed, 27 Aug 2014 14:07:22 -0500 Subject: [PATCH] Add a way to view poll statistics in game, load polls that aren't enabled for stat tracking --- .../src/mineplex/hub/poll/Poll.java | 9 +- .../src/mineplex/hub/poll/PollManager.java | 12 +- .../src/mineplex/hub/poll/PollRepository.java | 62 +++++- .../src/mineplex/hub/poll/PollStats.java | 78 +++++++ .../hub/poll/command/PollCommand.java | 196 +++++++++++++++--- 5 files changed, 315 insertions(+), 42 deletions(-) create mode 100644 Plugins/Mineplex.Hub/src/mineplex/hub/poll/PollStats.java diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/poll/Poll.java b/Plugins/Mineplex.Hub/src/mineplex/hub/poll/Poll.java index 3150d7caa..ebff218f9 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/poll/Poll.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/poll/Poll.java @@ -6,13 +6,15 @@ package mineplex.hub.poll; public class Poll { private int _id; + private boolean _enabled; private int _coinReward; private String _question; private String[] _answers; - public Poll(int id, int coinReward, String question, String answerA, String answerB, String answerC, String answerD) + public Poll(int id, boolean enabled, int coinReward, String question, String answerA, String answerB, String answerC, String answerD) { _id = id; + _enabled = enabled; _coinReward = coinReward; _question = question; _answers = new String[4]; @@ -42,6 +44,11 @@ public class Poll return number > 0 && number <= _answers.length && _answers[number - 1] != null && _answers[number - 1].length() > 0; } + public boolean isEnabled() + { + return _enabled; + } + public int getCoinReward() { return _coinReward; diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/poll/PollManager.java b/Plugins/Mineplex.Hub/src/mineplex/hub/poll/PollManager.java index e770295d9..e3d47524c 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/poll/PollManager.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/poll/PollManager.java @@ -108,7 +108,7 @@ public class PollManager extends MiniClientPlugin { for (Poll poll : _polls) { - if (!pollData.hasAnswered(poll)) + if (poll.isEnabled() && !pollData.hasAnswered(poll)) return poll; } @@ -207,6 +207,16 @@ public class PollManager extends MiniClientPlugin return null; } + public PollStats getPollStats(int pollId) + { + return _repository.getPollStats(pollId); + } + + public List getPolls() + { + return _polls; + } + @Override public void AddCommands() { diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/poll/PollRepository.java b/Plugins/Mineplex.Hub/src/mineplex/hub/poll/PollRepository.java index 0a2aa1190..f96c2a875 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/poll/PollRepository.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/poll/PollRepository.java @@ -21,9 +21,10 @@ public class PollRepository extends RepositoryBase private static String CREATE_POLL_TABLE = "CREATE TABLE IF NOT EXISTS polls (id INT NOT NULL AUTO_INCREMENT, enabled BIT(1), question VARCHAR(256) NOT NULL, answerA VARCHAR(256) NOT NULL, answerB VARCHAR(256), answerC VARCHAR(256), answerD VARCHAR(256), coinReward INT NOT NULL, PRIMARY KEY (id));"; private static String CREATE_RELATION_TABLE = "CREATE TABLE IF NOT EXISTS accountPolls (id INT NOT NULL AUTO_INCREMENT, accountId INT NOT NULL, pollId INT NOT NULL, value TINYINT(1) NOT NULL, PRIMARY KEY (id), FOREIGN KEY (accountId) REFERENCES accounts(id), FOREIGN KEY (pollId) REFERENCES polls(id), UNIQUE INDEX accountPollIndex (accountId, pollId));"; - private static String RETRIEVE_POLLS = "SELECT id, question, answerA, answerB, answerC, answerD, coinReward FROM polls WHERE enabled IS TRUE ORDER BY coinReward DESC"; + private static String RETRIEVE_POLLS = "SELECT id, enabled, question, answerA, answerB, answerC, answerD, coinReward FROM polls ORDER BY coinReward DESC"; private static String RETRIEVE_PLAYER_DATA = "SELECT pollId, value FROM accountPolls INNER JOIN accounts ON accountPolls.accountId = accounts.id WHERE accounts.uuid = ?;"; private static String INSERT_POLL_ANSWER = "INSERT INTO accountPolls (accountId, pollId, value) SELECT accounts.id, ?, ? FROM accounts WHERE accounts.uuid = ?;"; + private static String RETRIEVE_POLL_STATS = "SELECT value, COUNT(*) FROM accountPolls WHERE pollId=? GROUP BY value;"; public PollRepository(JavaPlugin plugin) { @@ -55,14 +56,15 @@ public class PollRepository extends RepositoryBase while (resultSet.next()) { int pollId = resultSet.getInt(1); - String question = resultSet.getString(2); - String answerA = resultSet.getString(3); - String answerB = resultSet.getString(4); - String answerC = resultSet.getString(5); - String answerD = resultSet.getString(6); - int coinReward = resultSet.getInt(7); + boolean enabled = resultSet.getBoolean(2); + String question = resultSet.getString(3); + String answerA = resultSet.getString(4); + String answerB = resultSet.getString(5); + String answerC = resultSet.getString(6); + String answerD = resultSet.getString(7); + int coinReward = resultSet.getInt(8); - Poll poll = new Poll(pollId, coinReward, question, answerA, answerB, answerC, answerD); + Poll poll = new Poll(pollId, enabled, coinReward, question, answerA, answerB, answerC, answerD); polls.add(poll); } @@ -95,4 +97,48 @@ public class PollRepository extends RepositoryBase int update = executeUpdate(INSERT_POLL_ANSWER, new ColumnInt("pollId", pollId), new ColumnInt("answer", answer), new ColumnVarChar("uuid", 100, uuid.toString())); return update == 1; } + + public PollStats getPollStats(final int pollId) + { + final PollStats stats = new PollStats(); + + executeQuery(RETRIEVE_POLL_STATS, new ResultSetCallable() + { + @Override + public void processResultSet(ResultSet resultSet) throws SQLException + { + int aCount = 0; + int bCount = 0; + int cCount = 0; + int dCount = 0; + + while (resultSet.next()) + { + int responseCount = resultSet.getInt(2); + switch (resultSet.getInt(1)) + { + case 1: + aCount = responseCount; + break; + case 2: + bCount = responseCount; + break; + case 3: + cCount = responseCount; + break; + case 4: + dCount = responseCount; + break; + } + } + + stats.setACount(aCount); + stats.setBCount(bCount); + stats.setCCount(cCount); + stats.setDCount(dCount); + } + }, new ColumnInt("pollId", pollId)); + + return stats; + } } diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/poll/PollStats.java b/Plugins/Mineplex.Hub/src/mineplex/hub/poll/PollStats.java new file mode 100644 index 000000000..591302c01 --- /dev/null +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/poll/PollStats.java @@ -0,0 +1,78 @@ +package mineplex.hub.poll; + +/** + * Created by Shaun on 8/26/2014. + */ +public class PollStats +{ + private int _aCount; + private int _bCount; + private int _cCount; + private int _dCount; + + public int getTotal() + { + return _aCount + _bCount + _cCount + _dCount; + } + + public int getACount() + { + return _aCount; + } + + public int getBCount() + { + return _bCount; + } + + public int getCCount() + { + return _cCount; + } + + public int getDCount() + { + return _dCount; + } + + public double getAPercent() + { + return (double) _aCount / getTotal(); + } + + public double getBPercent() + { + return (double) _bCount / getTotal(); + } + + public double getCPercent() + { + return (double) _cCount / getTotal(); + } + + public double getDPercent() + { + return (double) _dCount / getTotal(); + } + + public void setACount(int aCount) + { + _aCount = aCount; + } + + public void setBCount(int bCount) + { + _bCount = bCount; + } + + public void setCCount(int cCount) + { + _cCount = cCount; + } + + public void setDCount(int dCount) + { + _dCount = dCount; + } + +} diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/poll/command/PollCommand.java b/Plugins/Mineplex.Hub/src/mineplex/hub/poll/command/PollCommand.java index 64cfcb6d5..123c3af16 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/poll/command/PollCommand.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/poll/command/PollCommand.java @@ -1,14 +1,22 @@ package mineplex.hub.poll.command; +import java.text.DecimalFormat; +import java.util.List; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; import org.bukkit.entity.Player; import mineplex.core.command.CommandBase; import mineplex.core.common.Rank; +import mineplex.core.common.jsonchat.JsonMessage; +import mineplex.core.common.util.C; import mineplex.core.common.util.F; import mineplex.core.common.util.UtilPlayer; import mineplex.hub.poll.PlayerPollData; import mineplex.hub.poll.Poll; import mineplex.hub.poll.PollManager; +import mineplex.hub.poll.PollStats; /** * Created by Shaun on 8/17/2014. @@ -21,51 +29,175 @@ public class PollCommand extends CommandBase } @Override - public void Execute(Player caller, String[] args) + public void Execute(final Player caller, String[] args) { PlayerPollData pollData = Plugin.Get(caller); - if (args == null || args.length != 2) + if (args == null || args.length < 1) { UtilPlayer.message(caller, F.main("Poll", "There was an error processing your poll request")); return; } - int pollId = 0; - int answer = 0; - try + if (args[0].equalsIgnoreCase("list") && CommandCenter.GetClientManager().Get(caller).GetRank().Has(Rank.MODERATOR)) { - pollId = Integer.parseInt(args[0]); - answer = Integer.parseInt(args[1]); + if (args.length == 1) + { + UtilPlayer.message(caller, F.main("Poll", "Listing Polls;")); + + List polls = Plugin.getPolls(); + for (int i = 0; i < polls.size(); i++) + { + Poll poll = polls.get(i); + new JsonMessage(ChatColor.GREEN + poll.getQuestion()) + .hover("show_text", "Poll id: " + poll.getId()) + .click("run_command", "/poll list " + poll.getId()) + .sendToPlayer(caller); + } + + UtilPlayer.message(caller, F.main("Poll", "Click a poll to view responses!")); + } + else + { + int id = 0; + try + { + id = Integer.parseInt(args[1]); + } + catch (NumberFormatException e) + { + UtilPlayer.message(caller, F.main("Poll", "Invalid Integer: " + F.elem(args[1]))); + return; + } + + final int pollId = id; + final Poll poll = Plugin.getPoll(pollId); + + if (poll == null) + { + UtilPlayer.message(caller, F.main("Poll", "Could not find a poll with that id. Try again!")); + return; + } + + UtilPlayer.message(caller, "Fetching Poll Stats...."); + Bukkit.getScheduler().runTaskAsynchronously(Plugin.GetPlugin(), new Runnable() + { + @Override + public void run() + { + final PollStats stats = Plugin.getPollStats(pollId); + Bukkit.getScheduler().runTask(Plugin.GetPlugin(), new Runnable() + { + @Override + public void run() + { + for (int i = 0; i < 5; i++) + { + UtilPlayer.message(caller, ""); + } + + UtilPlayer.message(caller, "Question: " + C.cYellow + poll.getQuestion()); + UtilPlayer.message(caller, "Enabled: " + C.cYellow + poll.isEnabled()); + UtilPlayer.message(caller, "Poll Id: " + C.cYellow + poll.getId()); + UtilPlayer.message(caller, "Reward: " + C.cYellow + poll.getCoinReward()); + UtilPlayer.message(caller, ""); + + DecimalFormat decimalFormat = new DecimalFormat("#.##"); + double aPercent = stats.getAPercent(); + double bPercent = stats.getBPercent(); + double cPercent = stats.getCPercent(); + double dPercent = stats.getDPercent(); + for (int i = 0; i < poll.getAnswers().length; i++) + { + String answer = poll.getAnswers()[i]; + if (answer != null) + { + double percent = 0; + if (i == 0) + percent = aPercent; + if (i == 1) + percent = bPercent; + if (i == 2) + percent = cPercent; + if (i == 3) + percent = dPercent; + + UtilPlayer.message(caller, answer); + UtilPlayer.message(caller, getProgressBar(percent, 60) + " " + C.cWhite + "(" + C.cYellow + decimalFormat.format(percent) + "%" + C.cWhite + ")"); + } + } + UtilPlayer.message(caller, " "); + UtilPlayer.message(caller, "Total Responses: " + C.cYellow + stats.getTotal()); + } + }); + } + }); + } } - catch (NumberFormatException e) + else if (args.length == 2) { - UtilPlayer.message(caller, F.main("Poll", "Failed to parse your response. Please try again later.")); - return; + int pollId = 0; + int answer = 0; + try + { + pollId = Integer.parseInt(args[0]); + answer = Integer.parseInt(args[1]); + } + catch (NumberFormatException e) + { + UtilPlayer.message(caller, F.main("Poll", "Failed to parse your response. Please try again later.")); + return; + } + + Poll poll = Plugin.getPoll(pollId); + + if (poll == null) + { + UtilPlayer.message(caller, F.main("Poll", "That poll no longer exists. Sorry!")); + return; + } + + if (!poll.hasAnswer(answer)) + { + UtilPlayer.message(caller, F.main("Poll", "That is not a valid response for that poll")); + return; + } + + if (!poll.isEnabled()) + { + UtilPlayer.message(caller, F.main("Poll", "That poll is no longer enabled!")); + return; + } + + if (pollData.hasAnswered(poll)) + { + UtilPlayer.message(caller, F.main("Poll", "You already answered that poll!")); + return; + } + + Plugin.answerPoll(caller, poll, answer); + // They answered the poll, queue up the next poll for 5 seconds from now + pollData.setPollCooldown(5000); + } + else + { + UtilPlayer.message(caller, F.main("Poll", "Please click in chat using your mouse to answer!")); + } + } + + private String getProgressBar(double percent, int barCount) + { + int greenCount = (int) (barCount * percent); + StringBuilder sb = new StringBuilder(C.cBlue + "["); + + for (int i = 0; i < barCount; i++) + { + String color = (i < greenCount ? C.cGreen : C.cGray); + sb.append(color + "|"); } - Poll poll = Plugin.getPoll(pollId); + sb.append(C.cBlue + "]"); - if (poll == null) - { - UtilPlayer.message(caller, F.main("Poll", "That poll no longer exists. Sorry!")); - return; - } - - if (!poll.hasAnswer(answer)) - { - UtilPlayer.message(caller, F.main("Poll", "That is not a valid response for that poll")); - return; - } - - if (pollData.hasAnswered(poll)) - { - UtilPlayer.message(caller, F.main("Poll", "You already answered that poll!")); - return; - } - - Plugin.answerPoll(caller, poll, answer); - // They answered the poll, queue up the next poll for 5 seconds from now - pollData.setPollCooldown(5000); + return sb.toString(); } }