package org.royaldev.thehumanity.commands.impl;
import org.jetbrains.annotations.NotNull;
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.cards.packs.CAHCardPack;
import org.royaldev.thehumanity.game.TheHumanityGame;
import org.royaldev.thehumanity.player.TheHumanityPlayer;
import xyz.cardstock.cardstock.commands.BaseCommand;
import xyz.cardstock.cardstock.commands.CallInfo;
import xyz.cardstock.cardstock.commands.Command;
import java.util.List;
@Command(
name = "startgame",
description = "Starts a new game of Cards Against Humanity.",
aliases = {"start"},
usage = "<command> (packs)"
)
public class StartGameCommand extends BaseCommand {
private final TheHumanity humanity;
public StartGameCommand(final TheHumanity instance) {
this.humanity = instance;
}
/**
* Adds default packs to the given list of packs if it is empty of if useDefaults is true.
*
* @param cardPacks List of packs to add to
*/
private void addDefaults(final List<CAHCardPack> cardPacks) {
if (!cardPacks.isEmpty()) return;
this.humanity.getDefaultPacks().stream().map(this.humanity::getCardPack).filter(cp -> cp != null).forEach(cardPacks::add);
if (cardPacks.isEmpty()) cardPacks.addAll(this.humanity.getLoadedCardPacks());
}
/**
* Tries to skip the countdown and rush the TheHumanityGame to start.
*
* @param e Event
* @param u User trying to rush
*/
private void rush(final ChannelMessageEvent e, final User u) {
final TheHumanityGame g = this.humanity.getGameRegistrar().find(e.getChannel());
if (g == null) {
u.sendNotice("There is no game in this channel.");
return;
}
final TheHumanityPlayer p = g.getPlayer(u);
if (p == null || !p.equals(g.getHost()) && !this.humanity.hasChannelMode(g.getChannel(), p.getUser(), 'o')) {
u.sendNotice("There is already a game in this channel.");
return;
}
u.sendNotice("The countdown was" + (g.skipCountdown() ? "" : " not") + " skipped.");
}
@Override
public void run(@NotNull ActorEvent<User> event, @NotNull CallInfo callInfo, @NotNull List<String> arguments) {
if (!(event instanceof ChannelMessageEvent)) return;
final User u = event.getActor();
final ChannelMessageEvent e = (ChannelMessageEvent) event;
if (this.humanity.getGameRegistrar().find(e.getChannel()) != null) {
this.rush(e, u);
return;
}
final List<CAHCardPack> cardPacks = this.humanity.getCardPacksFromArguments(arguments.toArray(new String[arguments.size()]));
if (cardPacks.size() < 1) {
this.addDefaults(cardPacks);
}
final TheHumanityGame g = this.humanity.getGameRegistrar().on(e.getChannel());
g.start(cardPacks);
final TheHumanityPlayer p = g.createPlayer(u);
if (p == null) {
u.sendNotice("You could not be set as host due to an internal error.");
return;
}
g.setHost(p);
}
}