Search in sources :

Example 1 with EpicFreeGame

use of com.sx4.bot.entities.info.EpicFreeGame in project Sx4 by sx4-discord-bot.

the class FreeGamesCommand method upcoming.

@Command(value = "upcoming", description = "Lists the free games on Epic Games coming in the future")
@CommandId(482)
@Examples({ "free games upcoming" })
public void upcoming(Sx4CommandEvent event) {
    FreeGameUtility.retrieveUpcomingFreeGames(event.getHttpClient(), freeGames -> {
        if (freeGames.isEmpty()) {
            event.replyFailure("There are currently no upcoming free games").queue();
            return;
        }
        PagedResult<EpicFreeGame> paged = new PagedResult<>(event.getBot(), freeGames).setSelect().setPerPage(1).setCustomFunction(page -> {
            EmbedBuilder embed = new EmbedBuilder();
            embed.setFooter("Game " + page.getPage() + "/" + page.getMaxPage());
            page.forEach((game, index) -> this.setGameEmbed(embed, game));
            return new MessageBuilder().setEmbeds(embed.build());
        });
        paged.execute(event);
    });
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) EpicFreeGame(com.sx4.bot.entities.info.EpicFreeGame) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command)

Example 2 with EpicFreeGame

use of com.sx4.bot.entities.info.EpicFreeGame in project Sx4 by sx4-discord-bot.

the class EpicFreeGame method fromData.

public static EpicFreeGame fromData(Document data) {
    String id = data.getString("id");
    String title = data.getString("title");
    String description = data.getString("description");
    String publisher = data.getEmbedded(List.of("seller", "name"), String.class);
    String url = data.getString("offerType").equals("DLC") ? data.getString("urlSlug") : data.getString("productSlug");
    Document promotion = FreeGameUtility.getPromotionalOffer(data);
    OffsetDateTime start = OffsetDateTime.parse(promotion.getString("startDate"));
    OffsetDateTime end = OffsetDateTime.parse(promotion.getString("endDate"));
    List<Document> keyImages = data.getList("keyImages", Document.class);
    String image = keyImages.stream().filter(d -> d.getString("type").equals(publisher.equals("Epic Dev Test Account") && title.equals("Mystery Game") ? "VaultClosed" : "OfferImageWide")).map(d -> d.getString("url")).findFirst().orElse(null);
    if (image == null) {
        image = keyImages.stream().filter(d -> !d.getString("type").equals("VaultClosed")).map(d -> d.getString("url")).findFirst().orElse(null);
    }
    Document priceInfo = data.getEmbedded(List.of("price", "totalPrice"), Document.class);
    int originalPrice = priceInfo.getInteger("originalPrice");
    int discountPrice = priceInfo.getInteger("discountPrice");
    return new EpicFreeGame(id, title, description, publisher, image, url, originalPrice, discountPrice, start, end);
}
Also used : Document(org.bson.Document) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) FreeGameUtility(com.sx4.bot.utility.FreeGameUtility) ZoneOffset(java.time.ZoneOffset) Instant(java.time.Instant) OffsetDateTime(java.time.OffsetDateTime) Document(org.bson.Document)

Example 3 with EpicFreeGame

use of com.sx4.bot.entities.info.EpicFreeGame in project Sx4 by sx4-discord-bot.

the class FreeGameManager method ensureEpicFreeGames.

public void ensureEpicFreeGames() {
    FreeGameUtility.retrieveFreeGames(this.bot.getHttpClient(), games -> {
        OffsetDateTime now = OffsetDateTime.now(ZoneOffset.UTC);
        List<EpicFreeGame> upcomingGames = games.stream().filter(game -> game.getPromotionStart().isAfter(now)).sorted(Comparator.comparing(game -> Duration.between(now, game.getPromotionStart()))).collect(Collectors.toList());
        EpicFreeGame newestGame = upcomingGames.get(0);
        OffsetDateTime promotionStart = newestGame.getPromotionStart();
        if (!promotionStart.isAfter(now)) {
            // re-request games as they have not been refreshed on the API
            this.epicExecutor.schedule(this::ensureEpicFreeGames, 10, TimeUnit.MINUTES);
            return;
        }
        List<EpicFreeGame> currentGames = games.stream().filter(Predicate.not(this::isAnnounced)).filter(game -> !game.getPromotionStart().isAfter(now) && game.getPromotionEnd().isAfter(now)).collect(Collectors.toList());
        if (!currentGames.isEmpty()) {
            this.sendFreeGameNotifications(currentGames).whenComplete(MongoDatabase.exceptionally());
        }
        this.epicExecutor.schedule(this::ensureEpicFreeGames, Duration.between(now, promotionStart).toSeconds() + 5, TimeUnit.SECONDS);
    });
}
Also used : Document(org.bson.Document) java.util(java.util) JsonFormatter(com.sx4.bot.formatter.JsonFormatter) Permission(net.dv8tion.jda.api.Permission) MongoDatabase(com.sx4.bot.database.mongo.MongoDatabase) EpicFreeGame(com.sx4.bot.entities.info.EpicFreeGame) TextChannel(net.dv8tion.jda.api.entities.TextChannel) FreeGame(com.sx4.bot.entities.info.FreeGame) WebhookClient(com.sx4.bot.entities.webhook.WebhookClient) Bson(org.bson.conversions.Bson) Formatter(com.sx4.bot.formatter.Formatter) Sx4(com.sx4.bot.core.Sx4) SteamFreeGame(com.sx4.bot.entities.info.SteamFreeGame) Element(org.jsoup.nodes.Element) Duration(java.time.Duration) FreeGameUtility(com.sx4.bot.utility.FreeGameUtility) com.mongodb.client.model(com.mongodb.client.model) FutureUtility(com.sx4.bot.utility.FutureUtility) ZoneOffset(java.time.ZoneOffset) FreeGameType(com.sx4.bot.entities.info.FreeGameType) ReadonlyMessage(com.sx4.bot.entities.webhook.ReadonlyMessage) Operators(com.sx4.bot.database.mongo.model.Operators) Request(okhttp3.Request) HttpCallback(com.sx4.bot.http.HttpCallback) java.util.concurrent(java.util.concurrent) Predicate(java.util.function.Predicate) MessageUtility(com.sx4.bot.utility.MessageUtility) Collectors(java.util.stream.Collectors) HttpException(club.minnced.discord.webhook.exception.HttpException) OkHttpClient(okhttp3.OkHttpClient) OffsetDateTime(java.time.OffsetDateTime) WebhookMessage(club.minnced.discord.webhook.send.WebhookMessage) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) Jsoup(org.jsoup.Jsoup) OffsetDateTime(java.time.OffsetDateTime) EpicFreeGame(com.sx4.bot.entities.info.EpicFreeGame)

Aggregations

EpicFreeGame (com.sx4.bot.entities.info.EpicFreeGame)2 FreeGameUtility (com.sx4.bot.utility.FreeGameUtility)2 OffsetDateTime (java.time.OffsetDateTime)2 ZoneOffset (java.time.ZoneOffset)2 Document (org.bson.Document)2 HttpException (club.minnced.discord.webhook.exception.HttpException)1 WebhookMessage (club.minnced.discord.webhook.send.WebhookMessage)1 Command (com.jockie.bot.core.command.Command)1 com.mongodb.client.model (com.mongodb.client.model)1 Sx4 (com.sx4.bot.core.Sx4)1 Sx4Command (com.sx4.bot.core.Sx4Command)1 MongoDatabase (com.sx4.bot.database.mongo.MongoDatabase)1 Operators (com.sx4.bot.database.mongo.model.Operators)1 FreeGame (com.sx4.bot.entities.info.FreeGame)1 FreeGameType (com.sx4.bot.entities.info.FreeGameType)1 SteamFreeGame (com.sx4.bot.entities.info.SteamFreeGame)1 ReadonlyMessage (com.sx4.bot.entities.webhook.ReadonlyMessage)1 WebhookClient (com.sx4.bot.entities.webhook.WebhookClient)1 Formatter (com.sx4.bot.formatter.Formatter)1 JsonFormatter (com.sx4.bot.formatter.JsonFormatter)1