diff --git a/src/commands/poll/SubCommandResults.java b/src/commands/poll/SubCommandResults.java new file mode 100644 index 0000000..3c8fc3d --- /dev/null +++ b/src/commands/poll/SubCommandResults.java @@ -0,0 +1,33 @@ +package commands.poll; + +import java.util.ArrayList; + +import core.*; +import dataStructures.*; + +public class SubCommandResults extends SubCommand +{ + public SubCommandResults(KittyRole level, KittyRating rating) { super(level, rating); } + + @Override + public SubCommandFormattable OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, String input) + { + String results = ""; + int totalVotes = 0; + ArrayList votes = guild.choices; + + for(int i = 0; i < votes.size(); i++) + { + totalVotes += votes.get(i).votes; + } + + results += "The poll was " + guild.poll + "\n"; + + for(int i = 0; i < votes.size(); i++) + { + results += String.format(LocStrings.stub("PollResultsResponse"), votes.get(i).votes, votes.get(i).choice, (int)(((double)votes.get(i).votes) / ((double)totalVotes) * 100)); + } + + return new SubCommandFormattable (results); + } +} diff --git a/src/commands/poll/SubCommandShow.java b/src/commands/poll/SubCommandShow.java new file mode 100644 index 0000000..eafb1a5 --- /dev/null +++ b/src/commands/poll/SubCommandShow.java @@ -0,0 +1,33 @@ +package commands.poll; + +import core.LocStrings; +import core.SubCommand; +import core.SubCommandFormattable; +import dataStructures.KittyChannel; +import dataStructures.KittyGuild; +import dataStructures.KittyRating; +import dataStructures.KittyRole; +import dataStructures.KittyUser; + +public class SubCommandShow extends SubCommand +{ + public SubCommandShow(KittyRole level, KittyRating rating) { super(level, rating); } + + + @Override + public SubCommandFormattable OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, String input) + { + if(!guild.polling) + { + return new SubCommandFormattable (LocStrings.stub("PollShowNoPoll")); + } + String poll = String.format(LocStrings.stub("PollShowPoll"), guild.poll); + poll += LocStrings.stub("PollShowChoices"); + for(int i = 0; i < guild.choices.size(); i++) + { + poll += (i+1) + ": `" + guild.choices.get(i).choice + "`\n"; + } + + return new SubCommandFormattable (poll); + } +} diff --git a/src/commands/poll/SubCommandVote.java b/src/commands/poll/SubCommandVote.java new file mode 100644 index 0000000..400f50b --- /dev/null +++ b/src/commands/poll/SubCommandVote.java @@ -0,0 +1,47 @@ +package commands.poll; + +import core.LocStrings; +import core.SubCommand; +import core.SubCommandFormattable; +import dataStructures.KittyChannel; +import dataStructures.KittyGuild; +import dataStructures.KittyPoll; +import dataStructures.KittyRating; +import dataStructures.KittyRole; +import dataStructures.KittyUser; + +public class SubCommandVote extends SubCommand +{ + public SubCommandVote(KittyRole level, KittyRating rating) { super(level, rating); } + + @Override + public SubCommandFormattable OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, String input) + { + if(guild.polling) + { + if(guild.hasVoted.contains(user.uniqueID)) + { + return new SubCommandFormattable (LocStrings.stub("PollVoteAlreadyVoted")); + } + try + { + int voteNum = Integer.parseInt(input)-1; + if(voteNum >= guild.choices.size() || voteNum < 0) + { + return new SubCommandFormattable (String.format(LocStrings.stub("PollVoteNotValidVote"), voteNum)); + } + + KittyPoll polled = guild.choices.get(voteNum); + polled.votes++; + guild.hasVoted.add(user.uniqueID); + return new SubCommandFormattable (LocStrings.stub("PollVoteSuccess") + " `" + polled.choice + "`!"); + } + catch (NumberFormatException e) + { + return new SubCommandFormattable (LocStrings.stub("PollVoteNotValidNumber")); + } + } + + return new SubCommandFormattable (LocStrings.stub("PollVoteNoPoll")); + } +} diff --git a/src/core/CommandManager.java b/src/core/CommandManager.java index f811e93..82b8fff 100644 --- a/src/core/CommandManager.java +++ b/src/core/CommandManager.java @@ -47,10 +47,6 @@ import commands.general.CommandTweet; import commands.general.CommandWolfram; import commands.general.CommandYeet; import commands.guildrole.CommandGuildRoleMain; -import commands.poll.CommandPollManage; -import commands.poll.CommandPollResults; -import commands.poll.CommandPollShow; -import commands.poll.CommandPollVote; import commands.raffle.CommandRaffleMain; import dataStructures.KittyChannel; import dataStructures.KittyGuild; @@ -116,7 +112,6 @@ public class CommandManager this.register(LocCommands.stub("indicator"), new CommandChangeIndicator(KittyRole.Admin, KittyRating.Safe)); // Mod - this.register(LocCommands.stub("poll"), new CommandPollManage(KittyRole.Mod, KittyRating.Safe)); this.register(LocCommands.stub("givebeans"), new CommandGiveBeans(KittyRole.Mod, KittyRating.Safe)); this.register(LocCommands.stub("rpg"), new CommandRPG(KittyRole.Mod, KittyRating.Safe)); @@ -131,9 +126,6 @@ public class CommandManager this.register(LocCommands.stub("choose"), new CommandChoose(KittyRole.General, KittyRating.Safe)); this.register(LocCommands.stub("help"), new CommandHelp(KittyRole.General, KittyRating.Safe)); this.register(LocCommands.stub("info, about"), new CommandInfo(KittyRole.General, KittyRating.Safe)); - this.register(LocCommands.stub("vote"), new CommandPollVote(KittyRole.General, KittyRating.Safe)); - this.register(LocCommands.stub("results"), new CommandPollResults(KittyRole.General, KittyRating.Safe)); - this.register(LocCommands.stub("showpoll"), new CommandPollShow(KittyRole.General, KittyRating.Safe)); this.register(LocCommands.stub("wolfram"), new CommandWolfram(KittyRole.General, KittyRating.Safe)); this.register(LocCommands.stub("c++, g++, cplus, cpp"), new CommandColiru(KittyRole.General, KittyRating.Safe)); this.register(LocCommands.stub("java, jdoodle"), new CommandJDoodle(KittyRole.General, KittyRating.Safe));