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);
});
}
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);
}
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);
});
}
Aggregations