package org.royaldev.thehumanity.commands.impl.game.subcommands;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.kitteh.irc.client.library.util.Format;
import org.kitteh.irc.client.library.element.User;
import org.kitteh.irc.client.library.event.channel.ChannelMessageEvent;
import org.kitteh.irc.client.library.event.helper.ActorEvent;
import org.royaldev.thehumanity.TheHumanity;
import org.royaldev.thehumanity.commands.InGameCommand;
import org.royaldev.thehumanity.game.HouseRule;
import org.royaldev.thehumanity.game.TheHumanityGame;
import org.royaldev.thehumanity.player.TheHumanityPlayer;
import org.royaldev.thehumanity.util.ConversionHelper;
import xyz.cardstock.cardstock.commands.CallInfo;
import xyz.cardstock.cardstock.commands.Command;
import java.util.Arrays;
import java.util.List;
@Command(
name = "houserules",
description = "Manages house rules.",
aliases = {"houserule", "hr", "rules"}
)
public class HouseRulesSubCommand extends InGameCommand {
public HouseRulesSubCommand(final TheHumanity instance) {
super(instance);
}
private void add(final ActorEvent<User> event, final TheHumanityGame g, final TheHumanityPlayer p, final List<String> args) {
final User u = p.getUser();
if (!this.isHostOrOp(p, g)) {
u.sendNotice("You must be either the host or an operator to add house rules.");
return;
}
if (args.size() < 1) {
u.sendNotice("Please provide a house rule.");
return;
}
final HouseRule hr = this.getHouseRule(Joiner.on(' ').join(args));
if (hr == null) {
u.sendNotice("No such house rule.");
return;
}
if (g.hasHouseRule(hr)) {
u.sendNotice("This house rule is already being used.");
return;
}
g.addHouseRule(hr);
ConversionHelper.respond(event, "Added house rule: " + Format.BOLD + hr + Format.RESET + ".");
}
@Nullable
private HouseRule getHouseRule(@NotNull final String name) {
Preconditions.checkNotNull(name, "name was null");
try {
return HouseRule.valueOf(name.toUpperCase());
} catch (final IllegalArgumentException ex) {
return HouseRule.getByFriendlyName(name);
}
}
private void list(final ActorEvent<User> event, final TheHumanityGame g) {
final StringBuilder sb = new StringBuilder();
Arrays.stream(HouseRule.values()).sorted().forEach(hr -> {
sb.append(g.hasHouseRule(hr) ? Format.GREEN : Format.RED);
sb.append(hr);
sb.append(Format.RESET).append(", ");
});
ConversionHelper.respond(event, sb.substring(0, sb.length() - 2));
}
private void remove(final ActorEvent<User> event, final TheHumanityGame g, final TheHumanityPlayer p, final List<String> args) {
final User u = p.getUser();
if (!this.isHostOrOp(p, g)) {
u.sendNotice("You must be either the host or an operator to remove house rules.");
return;
}
if (args.size() < 1) {
u.sendNotice("Please provide a house rule.");
return;
}
final HouseRule hr = this.getHouseRule(Joiner.on(' ').join(args));
if (hr == null) {
u.sendNotice("No such house rule.");
return;
}
if (!g.hasHouseRule(hr)) {
u.sendNotice("This house rule is not being used.");
return;
}
g.removeHouseRule(hr);
ConversionHelper.respond(event, "Removed house rule: " + Format.BOLD + hr + Format.RESET + ".");
}
@Override
public void run(@NotNull ChannelMessageEvent event, @NotNull CallInfo callInfo, @NotNull TheHumanityGame game, @NotNull TheHumanityPlayer player, @NotNull List<String> arguments) {
final User u = player.getUser();
if (arguments.size() < 1) {
u.sendNotice("Not enough arguments.");
u.sendNotice("Subcommands: (add, a), (remove, r), (list, l)");
return;
}
final List<String> newArgs = arguments.subList(1, arguments.size());
switch (arguments.get(0).toLowerCase()) {
case "add":
case "a":
this.add(event, game, player, newArgs);
break;
case "remove":
case "r":
this.remove(event, game, player, newArgs);
break;
case "list":
case "l":
this.list(event, game);
break;
default:
u.sendNotice("Unknown subcommand.");
break;
}
}
}