package org.royaldev.thehumanity.server.services.game; import com.google.common.base.Preconditions; import org.jetbrains.annotations.NotNull; import org.kitteh.irc.client.library.Client; import org.kitteh.irc.client.library.element.Channel; import org.royaldev.thehumanity.TheHumanity; import org.royaldev.thehumanity.game.TheHumanityGame; import org.royaldev.thehumanity.server.services.channel.ChannelService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Set; import java.util.stream.Collectors; @Service public class HumanityGameService implements GameService { @Autowired private TheHumanity humanity; @Autowired private ChannelService channelService; @Override public Set<TheHumanityGame> getAll() { return this.humanity.getGameRegistrar().all(); } @Override public Set<TheHumanityGame> getAllForClient(@NotNull final Client client) { return this.humanity.getGameRegistrar().all().stream().filter(game -> client.getChannels().contains(game.getChannel())).collect(Collectors.toSet()); } @Override public TheHumanityGame getFromChannel(@NotNull final Channel channel) { Preconditions.checkNotNull(channel, "channel was null"); return this.humanity.getGameRegistrar().find(channel); } @Override public TheHumanityGame getFromChannelName(@NotNull final Client client, @NotNull final String channel) { final Channel actualChannel = client.getChannel(channel).orElse(null); if (actualChannel == null) { return null; } return this.getFromChannel(actualChannel); } }