diff --git a/FredBoat/src/main/java/fredboat/command/music/control/VoteShuffleCommand b/FredBoat/src/main/java/fredboat/command/music/control/VoteShuffleCommand new file mode 100644 index 000000000..3efc79e57 --- /dev/null +++ b/FredBoat/src/main/java/fredboat/command/music/control/VoteShuffleCommand @@ -0,0 +1,185 @@ +/* + * MIT License + * + * Copyright (c) 2017 Frederik Ar. Mikkelsen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +package fredboat.command.music.control; + +import fredboat.audio.player.GuildPlayer; +import fredboat.commandmeta.abs.CommandContext; +import fredboat.commandmeta.abs.ICommandRestricted; +import fredboat.commandmeta.abs.IMusicCommand; +import fredboat.commandmeta.abs.JCommand; +import fredboat.definitions.PermissionLevel; +import fredboat.messaging.internal.Context; + +import javax.annotation.Nonnull; + +import static fredboat.main.LauncherKt.getBotController; + + +public class VoteShuffleCommand extends JCommand implements IMusicCommand, ICommandRestricted{ + + public VoteShuffleCommand(String name, String... aliases) { + super(name, aliases); + } + @Override + public void onInvoke(@Nonnull CommandContext context) { + GuildPlayer player = getBotController().getPlayerRegistry().getOrCreate(context.getGuild()); + if (context.memeber.voiceChannel == null){ + context.reply(context.i18n("playerUserNotInChannel")); + } + val player = Launcher.botController.playerRegistry.getExisting(context.guild); + if (player == null || player.isQueueEmpty){ + context.reply(context.i18n("shuffleEmpty")); + } + + if (!context.hasArguments()){ + reponse = if (isUnvote) removeVoteWithResponse(context) else addVoteWithResponse(context); + + actualMinShuffle = if (player.humanUsersInCurrentVC.size < 3) 1.0f else MIN_SKIP_PERCENTAGE; + + shufflePercentage = getShufflePercentage(context.guild, player); + if (shufflePercentage >= actualMinShuffle) { + atc = player.playingTrack; + if (atc == null){ + context.reply(context.i18n("skipTrackNotFound")); + else + shufflePerc = "`" + TextUtils.formatPercent(shufflePercentage.toDouble()) + "`"; + context.reply(response + "\n" + context.i18nFormat("voteShuffleNotEnough", shufflePerc); + + player.setShuffle(!player.isShuffle()); + + if (player.isShuffle()) { + context.reply(context.i18n("shuffleOn")); + } else { + context.reply(context.i18n("shuffleOff")); + } + else { + shufflePerc = "`" + TextUtils.formatPercent(shufflePercentage.toDouble()) + "`"; + minShufflePerc = "`" + TextUtils.formatPercent(actualMinShuffle.toDouble()) + "`"; + context.reply(response + "\n" + context.i18nFormat("voteShuffleNotEnough", shufflePerc, minShufflePerc); + } + else if (context.args[0].toLowerCase() == "list"){ + displayVoteList(context, player); + } + else { + HelpCommand.sendFormattedCommandHelp(context); + } + } + private String addVoteWithResponse(context: CommandContext){ + user = context.user; + voters MutableSet? = guildShuffleVotes[context.guild.id]?.toMutableSet(); + + if (voters == null){ + voters = HashSet(); + voters.add(user.id); + guildShuffleVotes[context.guild.id] = voters; + return context.i18n("voteShuffleAdded"); + } + + return if (voters.contains(user.id)) { + context.i18n("voteShuffleAlreadyAdded"); + } + else { + voters.add(user.id); + guildShuffleVotes[context.guild.id] = voters; + context.i18n("voteShuffleAdded"); + } + } + private String removeVoteWithResponse(context: CommandContext){ + user = context.user; + voters MutableSet? = guildShuffleVotes[context.guild.id]?.toMutableSet(); + + if (voters == null){ + voters = HashSet(); + return context.i18n("voteShuffleNotFound"); + } + + return if (voters.contains(user.id)) { + voters.remove(user.id); + guildShuffleVotes[context.guild.id] = voters; + context.i18n("voteShuffleRemoved"); + } + else { + context.i18n("voteShuffleNotFound"); + } + } + private double getShufflePercentage(guild, player){ + int vcMembers = player.humanUsersInCurrentVC; + int votes = 0; + + for (vcMember in vcMembers) { + if (hasVoted(guild, vcMember)){ + votes++; + } + } + double percentage = votes * 1.0f / vcMembers.size(); + + return if (java.lang.Float.isNaN(percentage)){ + 0f + } + else percentage; + } + private Boolean hasVoted(guild, member){ + voters = guildShuffleVotes[context.guild.id]; + + if (voters == null || voters.isEmpty()){ + context.reply(context.i18n("voteShuffleEmbedNoVotes")); + return; + } + + String field1 = StringBuilder(); + String field2 = StringBuilder(); + + for ((i, userId) in voters.withIndex) { + String field = field1 + if (i % 2 == 1){ + field = field2; + } + String member = context.guild.getMember(userId); + if (member != null){ + field.append("| ").append(TextUtils.escapeAndDefuse(member.effectiveName)).append("\n"); + } + } + } + @Nonnull + @Override + public String help(@Nonnull Context context) { + if (isUnvote) { + return "{0}{1}\n#" + context.i18n("helpUnvoteShuffle"); + } + else{ + return "{0}{1}\n#" + context.i18n("helpVoteShuffle"); + } + } + } + + @Nonnull + @Override + public PermissionLevel getMinimumPerms() { + return PermissionLevel.DJ; + } + + +}