use of com.sx4.bot.managers.EconomyManager in project Sx4 by sx4-discord-bot.
the class AdventCalendarCommand method onCommand.
public void onCommand(Sx4CommandEvent event) {
ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC);
int day = now.getDayOfMonth();
if (now.getMonthValue() != 12 || day > 24) {
event.replyFormat("There's no advent calendar box for the %s %s :no_entry:", NumberUtility.getSuffixed(day), now.getMonth().getDisplayName(TextStyle.FULL, Locale.UK)).queue();
return;
}
EconomyManager manager = event.getBot().getEconomyManager();
event.getMongo().withTransaction(session -> {
Document data = event.getMongo().getUsers().findOneAndUpdate(session, Filters.eq("_id", event.getAuthor().getIdLong()), Updates.addToSet("economy.opened", day), new FindOneAndUpdateOptions().upsert(true));
List<Integer> opened = data == null ? Collections.emptyList() : data.getEmbedded(List.of("economy", "opened"), Collections.emptyList());
if (opened.contains(day)) {
long secondsTillTomorrow = now.toLocalDate().atStartOfDay(ZoneOffset.UTC).plusDays(1).toEpochSecond() - now.toEpochSecond();
event.replyFormat("You've already opened today's box on your advent calendar%s :no_entry:", day != 24 ? ", you can open tomorrows in **" + TimeUtility.LONG_TIME_FORMATTER.parse(Duration.of(secondsTillTomorrow, ChronoUnit.SECONDS)) + "**" : "").queue();
session.abortTransaction();
return null;
}
List<Item> items = manager.getItems();
items.sort(Comparator.comparingLong(Item::getPrice).reversed());
Item item = items.get(items.size() - 1);
for (Item winnableItem : items) {
if (winnableItem instanceof Tool) {
continue;
}
int equation = (int) Math.ceil(winnableItem.getPrice() / Math.pow(day * 3, 2));
if (manager.getRandom().nextInt(equation + 1) == 0) {
item = winnableItem;
break;
}
}
if (opened.size() == 23) {
Crate present = manager.getItemById(PRESENT_CRATE, Crate.class);
List<Bson> update = List.of(Operators.set("item", present.toData()), Operators.set("amount", Operators.add(Operators.ifNull("$amount", 0L), 1L)));
event.getMongo().getItems().updateOne(session, Filters.and(Filters.eq("userId", event.getAuthor().getIdLong()), Filters.eq("item.id", item.getId())), update, new UpdateOptions().upsert(true));
}
List<Bson> update = List.of(Operators.set("item", item.toData()), Operators.set("amount", Operators.add(Operators.ifNull("$amount", 0L), 1L)));
event.getMongo().getItems().updateOne(session, Filters.and(Filters.eq("userId", event.getAuthor().getIdLong()), Filters.eq("item.id", item.getId())), update, new UpdateOptions().upsert(true));
return "You opened your advent calendar for the " + NumberUtility.getSuffixed(day) + " and got **" + item.getName() + "**" + (opened.size() == 23 ? " and a **Present Crate**" : "") + " :christmas_tree:";
}).whenComplete((content, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception) || content == null) {
return;
}
event.reply(content).queue();
});
}
Aggregations