Search in sources :

Example 1 with EconomyManager

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();
    });
}
Also used : Document(org.bson.Document) ZonedDateTime(java.time.ZonedDateTime) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) Filters(com.mongodb.client.model.Filters) Bson(org.bson.conversions.Bson) Item(com.sx4.bot.entities.economy.item.Item) Locale(java.util.Locale) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) TimeUtility(com.sx4.bot.utility.TimeUtility) Duration(java.time.Duration) ZoneOffset(java.time.ZoneOffset) UpdateOptions(com.mongodb.client.model.UpdateOptions) Tool(com.sx4.bot.entities.economy.item.Tool) Operators(com.sx4.bot.database.mongo.model.Operators) TextStyle(java.time.format.TextStyle) Sx4Command(com.sx4.bot.core.Sx4Command) NumberUtility(com.sx4.bot.utility.NumberUtility) EconomyManager(com.sx4.bot.managers.EconomyManager) Updates(com.mongodb.client.model.Updates) ModuleCategory(com.sx4.bot.category.ModuleCategory) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) Crate(com.sx4.bot.entities.economy.item.Crate) Comparator(java.util.Comparator) Collections(java.util.Collections) Item(com.sx4.bot.entities.economy.item.Item) ZonedDateTime(java.time.ZonedDateTime) EconomyManager(com.sx4.bot.managers.EconomyManager) Crate(com.sx4.bot.entities.economy.item.Crate) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) List(java.util.List) Document(org.bson.Document) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) UpdateOptions(com.mongodb.client.model.UpdateOptions) Tool(com.sx4.bot.entities.economy.item.Tool)

Aggregations

Filters (com.mongodb.client.model.Filters)1 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)1 UpdateOptions (com.mongodb.client.model.UpdateOptions)1 Updates (com.mongodb.client.model.Updates)1 ModuleCategory (com.sx4.bot.category.ModuleCategory)1 Sx4Command (com.sx4.bot.core.Sx4Command)1 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)1 Operators (com.sx4.bot.database.mongo.model.Operators)1 Crate (com.sx4.bot.entities.economy.item.Crate)1 Item (com.sx4.bot.entities.economy.item.Item)1 Tool (com.sx4.bot.entities.economy.item.Tool)1 EconomyManager (com.sx4.bot.managers.EconomyManager)1 ExceptionUtility (com.sx4.bot.utility.ExceptionUtility)1 NumberUtility (com.sx4.bot.utility.NumberUtility)1 TimeUtility (com.sx4.bot.utility.TimeUtility)1 Duration (java.time.Duration)1 ZoneOffset (java.time.ZoneOffset)1 ZonedDateTime (java.time.ZonedDateTime)1 TextStyle (java.time.format.TextStyle)1 ChronoUnit (java.time.temporal.ChronoUnit)1