package org.royaldev.thehumanity.commands.impl;
import com.google.common.base.Preconditions;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.kitteh.irc.client.library.element.User;
import org.kitteh.irc.client.library.event.channel.ChannelMessageEvent;
import org.royaldev.thehumanity.TheHumanity;
import org.royaldev.thehumanity.cards.types.WhiteCard;
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 xyz.cardstock.cardstock.commands.CallInfo;
import xyz.cardstock.cardstock.commands.Command;
import xyz.cardstock.cardstock.configuration.Server;
import java.util.ArrayList;
import java.util.List;
@Command(
name = "reboottheuniverse",
description = "Allows players to sacrifice one point to return cards to the deck and draw back to ten cards.",
aliases = {"rbtu", "reboot"},
usage = "<command> [card#...]"
)
public class RebootTheUniverseCommand extends InGameCommand {
public RebootTheUniverseCommand(final TheHumanity instance) {
super(instance);
}
/**
* Gets WhiteCards from the given Player's Hand, given their numbers. If any number is invalid, null will be returned.
*
* @param args Numbers of cards
* @param p Player to get cards from
* @param u User that the Player represents
* @return A list of WhiteCards of null
*/
@Nullable
private List<WhiteCard> getCardsFromNumbers(@NotNull final List<String> args, @NotNull final TheHumanityPlayer p, @NotNull final User u) {
final List<WhiteCard> cardsToRemove = new ArrayList<>();
for (final String index : args) {
try {
cardsToRemove.add(p.getHand().get(Integer.parseInt(index) - 1));
} catch (final NumberFormatException ex) {
u.sendNotice(index + " is not a number. Please specify only card numbers separated by spaces. No cards or points have been removed.");
return null;
}
}
return cardsToRemove;
}
@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 (!game.hasHouseRule(HouseRule.REBOOTING_THE_UNIVERSE)) {
u.sendNotice("The house rule \"Rebooting the Universe\" must be enabled to use this command.");
if (this.isHostOrOp(player, game)) {
final Server server = this.humanity.getClientServerMap().get(event.getClient());
Preconditions.checkNotNull(server);
u.sendNotice("Try " + server.getPrefix() + "game hr list.");
}
return;
}
if (arguments.size() < 1) {
u.sendNotice("Usage: " + this.getUsage().replace("<command>", callInfo.getLabel()));
return;
}
if (player.getScore() < 1) {
u.sendNotice("You must have points to use this command.");
return;
}
final List<WhiteCard> cardsToRemove = this.getCardsFromNumbers(arguments, player, u);
if (cardsToRemove == null) return;
cardsToRemove.forEach(player.getHand()::remove);
game.deal(player);
player.removeWin(player.getWins().iterator().next());
game.showCards(player);
u.sendNotice("In exchange for a point, you have replaced " + cardsToRemove.size() + " card" + (cardsToRemove.size() == 1 ? " with a new one." : "s with new ones."));
}
}