Skip to content
This repository has been archived by the owner on Nov 25, 2019. It is now read-only.

Cleanup polls code #67

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open

Cleanup polls code #67

wants to merge 28 commits into from

Conversation

cswhite2000
Copy link
Member

No description provided.

Copy link

@Electroid Electroid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome job! It will be nice to have refactored polls so more features can implement them. Mostly minor fixes and changes.


public Poll(PollManager pollManager, Server server, String initiator) {
protected final PollManager pollManager;
protected final String initiator;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest changing the initiator to be a PlayerId so that you can fix the bug where nicked players are exposed if they create a poll.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that feels like it would be a bit restrictive, if other systems want to use polls (say the veto system for tournaments) forcing it into a player id may not be that good

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tournaments are an entirely different system. Besides, if you were to refactor this for teams, it would just make more complicated. It's a reasonable assumption that a poll is initiated by a person and not something else.

}
if (sender instanceof Player) {
Player player = (Player) sender;
if (TokenUtil.getUser(player).maptokens() < 1) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a util method for TokenUtil.getUser(CommandSender)

throw new TranslatableCommandException("poll.map.notallowed");
}

if (PGM.get().getServer().getOnlinePlayers().size() * 4 / 5 > nextMap.getDocument().max_players() && !sender.hasPermission("poll.next.override")) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of PGM.get()... inject OnlinePlayers and use its methods.

throw new TranslatableCommandException("poll.map.notallowed");
}

if (PGM.get().getServer().getOnlinePlayers().size() * 4 / 5 > nextMap.getDocument().max_players() && !sender.hasPermission("poll.next.override")) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the 4/5ths?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It accounts for some observers being afk.


if (args.getSuggestionContext() != null) {
Collection<Mutation> availableMutations = Sets.newHashSet(Mutation.values());
availableMutations.removeAll(mutationQueue.mutations());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a convenience method for mutationsAvailable

Mutation mutation = StringUtils.bestFuzzyMatch(mutationString, Sets.newHashSet(Mutation.values()), 0.9);
if(mutation == null) {
throw new TranslatableCommandException("command.mutation.error.find", mutationString);
} else if(MutationCommands.getInstance().getMutationQueue().mutations().contains(mutation)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid statically referencing other injected commands like MutationsCommands. All you need is the MutationQueue object, which you already have as a local variable in mutationQueue

PollCustom create(String text, CommandSender initiator);
}

private String text;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of passing a text object, can that just be a method?

getReasonString() or getPollString()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where would you put the method? PollCustom needs it to be able to display the message they player used.


@Override
public void executeAction() {
if(player != null) player.kickPlayer(ChatColor.DARK_RED + "You were poll-kicked.");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There needs to be a better explanation for the kick. Also, I would implement this via an off-record punishment so they get all the formatting.

Although, you may need to make sure off-record punishments, instead of saying ...appeal here... they say ...punishment does not count... or something.

@StratusNetwork StratusNetwork deleted a comment from Pablete1234 Jan 7, 2018
@StratusNetwork StratusNetwork deleted a comment from Electroid Jan 7, 2018
Copy link

@PotatoStealer PotatoStealer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a job well done on its own, I only have some little things here and there, but thanks for picking up after me :)


private String text;

@AssistedInject PollCustom(@Assisted String text, @Assisted CommandSender initiator, PollManager pollManager, Audiences audiences) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Poll class already defines an @Injected constructor, I believe @AssistedInject is correct here

@@ -21,33 +21,32 @@
PollKick create(PlayerId initiator, Player player);
}

private final Player player;
private final PlayerId player;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be named playerId

}

@Override
public void executeAction() {
PlayerId kicked = userStore.playerId(player);
punishmentCreator.create(null, kicked, "The poll to kick " + kicked.username() + " succeeded", PunishmentDoc.Type.KICK, null, false, false, true);
punishmentCreator.create(null, player, "The poll to kick " + player.username() + " succeeded", PunishmentDoc.Type.KICK, null, false, false, true);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe make this translatable instead of a hardcoded string

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, It doesn't really make sense to translate a punishment message, as it cannot be translated per user.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about translations?

audiences.localServer().sendMessage(new Component(ChatColor.DARK_AQUA).translate("poll.kick.success", new PlayerComponent(identityProvider.currentIdentity(player))));
}

@Override
public String getActionString() {
return normalize + "Kick: " + boldAqua + this.player;
return normalize + "Kick: " + boldAqua + this.player.username();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, would prefer a translatable component.

@@ -94,34 +105,31 @@ public void pollKick(CommandContext args, CommandSender sender) throws CommandEx
return CommandUtils.completeMapName(mapName);
}

if (!Config.Poll.enabled()) {
if(!Config.Poll.enabled()) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you let the poll package have its own configuration class? I think it would be better than referencing it from the Config class.


private String text;

@AssistedInject PollCustom(@Assisted String text, @Assisted CommandSender initiator, PollManager pollManager, Audiences audiences) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Poll class already defines an @Injected constructor, I believe @AssistedInject is correct here


import javax.inject.Inject;

public class PollCommands implements Commands {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a CommandBinder

huddle.globalChatDisabled = Global chat is disabled during team huddle

poll.vote.value.invalid = Accepted values: yes|no

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

poll.vote.invalidValue


public static String boldAqua = ChatColor.BOLD + "" + ChatColor.AQUA;
public static String normalize = ChatColor.RESET + "" + ChatColor.DARK_AQUA;
public static String seperator = ChatColor.RESET + " | ";
public static String separator = ChatColor.RESET + " | ";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constant

final PluginFacetBinder facets = new PluginFacetBinder(binder());
facets.register(PollCommands.class);
facets.register(PollManager.class);
facets.register(PollListener.class);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bindListener

PlayerId voter = CommandUtils.senderToMatchPlayer(sender).getPlayerId();
Poll currentPoll = pollManager.getPoll();
if(currentPoll != null) {
if(args.getString(0).equalsIgnoreCase("yes")) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe have a cleaner way to deal with values. Perhaps use an enum to hold the possible values of a vote.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the check is fine. Polls, at least for now, are either yes or no.

public void veto(CommandContext args, CommandSender sender) throws CommandException {
if(pollManager.isPollRunning()) {
pollManager.endPoll(PollEndReason.Cancelled);
audiences.localServer().sendMessage(new Component(ChatColor.RED).translate("poll.veto", new PlayerComponent(identityProvider.currentIdentity(sender))));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preferably make this 2 lines instead of one for visibility

Player initiator = tc.oc.commons.bukkit.commands.CommandUtils.senderToPlayer(sender);
Player player = tc.oc.commons.bukkit.commands.CommandUtils.findOnlinePlayer(args, sender, 0);

if(player.hasPermission("pgm.poll.kick.exempt") && !initiator.hasPermission("pgm.poll.kick.override")) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Declare the permission strings as a constant at the top of the class, just so other classes may use it for whatever purposes

return StringUtils.complete(args.getSuggestionContext().getPrefix(), mutationQueue.mutationsAvailable().stream().map(mutation -> mutation.name().toLowerCase()));
}

if(!Config.Poll.enabled()) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have a class to hold the configuration values, i.e. a specific PollConfiguration

parts.add(new Component(new TranslatableComponent("punishment.screen.rules", Links.rulesLink())));

parts.add(new Component(new TranslatableComponent("punishment.screen.appeal", Links.appealLink())));
if (!punishment.off_record()) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if(punishment.off_record()) {
     // parts.add("off_record");
} else {
     // parts.add("others");
}

if (lines == null) return;
ImmutableList.Builder<PGMMap> maps = ImmutableList.builder();
for(String line : lines) {
if (line.contains("#")) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this for?

PlayerId voter = CommandUtils.senderToMatchPlayer(sender).getPlayerId();
Poll currentPoll = pollManager.getPoll();
if(currentPoll != null) {
if(args.getString(0).equalsIgnoreCase("yes")) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the check is fine. Polls, at least for now, are either yes or no.

this.pollConfig = pollConfig;
}


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra new lines here

)
@CommandPermissions("poll.kick")
public void pollKick(CommandContext args, CommandSender sender) throws CommandException {
Player initiator = tc.oc.commons.bukkit.commands.CommandUtils.senderToPlayer(sender);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to consider statically importing the sendToPlayer method since you use it frequently.

@CommandPermissions("poll.next")
public List<String> pollNext(CommandContext args, CommandSender sender) throws CommandException {
final String mapName = args.argsLength() > 0 ? args.getJoinedStrings(0) : "";
if(args.getSuggestionContext() != null) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of these single-if checks can just be put as one-liners.

if(args.getSuggestionContext() != null) return CommandUtils.completeMapName(mapName);
if(!pollConfig.enabled()) throw new TranslatableCommandException("poll.disabled");
// etc...

throw new TranslatableCommandException("poll.map.notallowed");
}

if(onlinePlayers.count() * 4 / 5 > nextMap.getDocument().max_players() && !sender.hasPermission("poll.next.override")) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reasoning behind the 4/5ths? Is there a standard place we can put this logic? If a match is too full?

}

if(onlinePlayers.count() * 4 / 5 > nextMap.getDocument().max_players() && !sender.hasPermission("poll.next.override")) {
throw new TranslatableCommandException("poll.map.toomanyplayers");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For translation keys, use camel case for multi-words:
poll.map.tooManyPlayers

public String getDescriptionMessage() {
return boldAqua + text;
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New line


public class PollNextMap extends Poll {

public interface Factory {
PollNextMap create(CommandSender sender, PGMMap nextMap, PlayerId playerId);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the CommandSender is the same as the PlayerId, maybe just provide the sender and derive the player? or the other way around?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I say that because when instantiating this factory, the senderToPlayer method is invoked frequently, and might just be more useful to be put inside the construction.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants