Search in sources :

Example 6 with ItemStack

use of com.sx4.bot.entities.economy.item.ItemStack in project Sx4 by sx4-discord-bot.

the class FactoryCommand method buy.

@Command(value = "buy", description = "Buy a factory with some materials")
@CommandId(396)
@Examples({ "factory buy 5 Shoe Factory", "factory buy Shoe Factory", "factory buy all" })
@BotPermissions(permissions = { Permission.MESSAGE_EMBED_LINKS })
public void buy(Sx4CommandEvent event, @Argument(value = "factories", endless = true) @AlternativeOptions("all") Alternative<ItemStack<Factory>> option) {
    ItemStack<Factory> stack = option.getValue();
    event.getMongo().withTransaction(session -> {
        Bson userFilter = Filters.eq("userId", event.getAuthor().getIdLong()), filter;
        List<Factory> factories;
        if (stack == null) {
            filter = Filters.and(userFilter, Filters.eq("item.type", ItemType.MATERIAL.getId()));
            factories = event.getBot().getEconomyManager().getItems(Factory.class);
        } else {
            Factory factory = stack.getItem();
            filter = Filters.and(userFilter, Filters.eq("item.id", factory.getCost().getItem().getId()));
            factories = List.of(factory);
        }
        List<Document> materials = event.getMongo().getItems().find(session, filter).projection(Projections.include("amount", "item.id")).into(new ArrayList<>());
        List<ItemStack<Factory>> boughtFactories = new ArrayList<>();
        Factories: for (Factory factory : factories) {
            ItemStack<Material> cost = factory.getCost();
            Material costMaterial = cost.getItem();
            for (Document material : materials) {
                int id = material.getEmbedded(List.of("item", "id"), Integer.class);
                if (costMaterial.getId() == id) {
                    long buyableAmount = (long) Math.floor((double) material.getLong("amount") / cost.getAmount());
                    long amount = stack == null ? buyableAmount : stack.getAmount();
                    if (amount == 0 || amount > buyableAmount) {
                        continue Factories;
                    }
                    event.getMongo().getItems().updateOne(session, Filters.and(userFilter, Filters.eq("item.id", id)), Updates.inc("amount", -amount * cost.getAmount()));
                    List<Bson> update = List.of(Operators.set("item", factory.toData()), Operators.set("amount", Operators.add(Operators.ifNull("$amount", 0L), amount)));
                    event.getMongo().getItems().updateOne(session, Filters.and(userFilter, Filters.eq("item.id", factory.getId())), update, new UpdateOptions().upsert(true));
                    boughtFactories.add(new ItemStack<>(factory, amount));
                }
            }
        }
        return boughtFactories;
    }).whenComplete((factories, exception) -> {
        if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        if (factories.isEmpty()) {
            event.replyFailure("You cannot afford " + (stack == null ? "any factories" : "`" + stack.getAmount() + " " + stack.getItem().getName() + "`")).queue();
            return;
        }
        String factoriesBought = factories.stream().sorted(Collections.reverseOrder(Comparator.comparingLong(ItemStack::getAmount))).map(ItemStack::toString).collect(Collectors.joining("\n• "));
        EmbedBuilder embed = new EmbedBuilder().setColor(event.getMember().getColor()).setAuthor(event.getAuthor().getName(), null, event.getAuthor().getEffectiveAvatarUrl()).setDescription("With all your materials you have bought the following factories\n\n• " + factoriesBought);
        event.reply(embed.build()).queue();
    });
}
Also used : Document(org.bson.Document) EconomyUtility(com.sx4.bot.utility.EconomyUtility) java.util(java.util) Command(com.jockie.bot.core.command.Command) Permission(net.dv8tion.jda.api.Permission) Projections(com.mongodb.client.model.Projections) CommandId(com.sx4.bot.annotations.command.CommandId) PagedResult(com.sx4.bot.paged.PagedResult) Filters(com.mongodb.client.model.Filters) Bson(org.bson.conversions.Bson) Alternative(com.sx4.bot.entities.argument.Alternative) com.sx4.bot.entities.economy.item(com.sx4.bot.entities.economy.item) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) TimeUtility(com.sx4.bot.utility.TimeUtility) AlternativeOptions(com.sx4.bot.annotations.argument.AlternativeOptions) BotPermissions(com.sx4.bot.annotations.command.BotPermissions) UpdateOptions(com.mongodb.client.model.UpdateOptions) Argument(com.jockie.bot.core.argument.Argument) Operators(com.sx4.bot.database.mongo.model.Operators) Sx4Command(com.sx4.bot.core.Sx4Command) Updates(com.mongodb.client.model.Updates) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Collectors(java.util.stream.Collectors) ModuleCategory(com.sx4.bot.category.ModuleCategory) Examples(com.sx4.bot.annotations.command.Examples) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) Document(org.bson.Document) UpdateOptions(com.mongodb.client.model.UpdateOptions) Bson(org.bson.conversions.Bson) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) BotPermissions(com.sx4.bot.annotations.command.BotPermissions) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command) CommandId(com.sx4.bot.annotations.command.CommandId) Examples(com.sx4.bot.annotations.command.Examples)

Example 7 with ItemStack

use of com.sx4.bot.entities.economy.item.ItemStack in project Sx4 by sx4-discord-bot.

the class FactoryCommand method shop.

@Command(value = "shop", description = "View all the factories you can buy")
@CommandId(395)
@Examples({ "factory shop" })
@BotPermissions(permissions = { Permission.MESSAGE_EMBED_LINKS })
public void shop(Sx4CommandEvent event) {
    List<Factory> factories = event.getBot().getEconomyManager().getItems(Factory.class);
    PagedResult<Factory> paged = new PagedResult<>(event.getBot(), factories).setPerPage(15).setCustomFunction(page -> {
        EmbedBuilder embed = new EmbedBuilder().setAuthor("Factory Shop", null, event.getSelfUser().getEffectiveAvatarUrl()).setTitle("Page " + page.getPage() + "/" + page.getMaxPage()).setDescription("Factories are a good way to make money from materials you have gained through mining").setFooter(PagedResult.DEFAULT_FOOTER_TEXT);
        page.forEach((factory, index) -> {
            ItemStack<Material> cost = factory.getCost();
            embed.addField(factory.getName(), "Price: " + cost.getAmount() + " " + cost.getItem().getName(), true);
        });
        return new MessageBuilder().setEmbeds(embed.build());
    });
    paged.execute(event);
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) PagedResult(com.sx4.bot.paged.PagedResult) BotPermissions(com.sx4.bot.annotations.command.BotPermissions) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command) CommandId(com.sx4.bot.annotations.command.CommandId) Examples(com.sx4.bot.annotations.command.Examples)

Example 8 with ItemStack

use of com.sx4.bot.entities.economy.item.ItemStack in project Sx4 by sx4-discord-bot.

the class FishingRodCommand method shop.

@Command(value = "shop", description = "View all the fishing rods you are able to buy or craft")
@CommandId(379)
@Examples({ "fishing rod shop" })
@BotPermissions(permissions = { Permission.MESSAGE_EMBED_LINKS })
public void shop(Sx4CommandEvent event) {
    List<Rod> rods = event.getBot().getEconomyManager().getItems(Rod.class);
    PagedResult<Rod> paged = new PagedResult<>(event.getBot(), rods).setPerPage(12).setCustomFunction(page -> {
        EmbedBuilder embed = new EmbedBuilder().setAuthor("Fishing Rod Shop", null, event.getSelfUser().getEffectiveAvatarUrl()).setTitle("Page " + page.getPage() + "/" + page.getMaxPage()).setDescription("Fishing rods are a good way to gain some money").setFooter(PagedResult.DEFAULT_FOOTER_TEXT);
        page.forEach((rod, index) -> {
            List<ItemStack<CraftItem>> items = rod.getCraft();
            String craft = items.isEmpty() ? "None" : items.stream().map(ItemStack::toString).collect(Collectors.joining("\n"));
            embed.addField(rod.getName(), String.format("Price: $%,d\nCraft: %s\nDurability: %,d", rod.getPrice(), craft, rod.getMaxDurability()), true);
        });
        return new MessageBuilder().setEmbeds(embed.build());
    });
    paged.execute(event);
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) Rod(com.sx4.bot.entities.economy.item.Rod) ItemStack(com.sx4.bot.entities.economy.item.ItemStack) PagedResult(com.sx4.bot.paged.PagedResult) BotPermissions(com.sx4.bot.annotations.command.BotPermissions) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command) CommandId(com.sx4.bot.annotations.command.CommandId) Examples(com.sx4.bot.annotations.command.Examples)

Example 9 with ItemStack

use of com.sx4.bot.entities.economy.item.ItemStack in project Sx4 by sx4-discord-bot.

the class ItemsCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "user", endless = true, nullDefault = true) Member member) {
    Member effectiveMember = member == null ? event.getMember() : member;
    List<Bson> usersPipeline = List.of(Aggregates.match(Filters.eq("_id", effectiveMember.getIdLong())), Aggregates.project(Projections.computed("balance", Operators.ifNull("$economy.balance", 0L))), Aggregates.addFields(new Field<>("name", "balance")));
    List<Bson> pipeline = List.of(Aggregates.match(Filters.and(Filters.eq("userId", effectiveMember.getIdLong()), Filters.ne("amount", 0))), Aggregates.project(Projections.fields(Projections.computed("name", "$item.name"), Projections.computed("type", "$item.type"), Projections.include("item", "amount"))), Aggregates.sort(Sorts.descending("amount")), Aggregates.unionWith("users", usersPipeline));
    event.getMongo().aggregateItems(pipeline).whenComplete((items, exception) -> {
        if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        EmbedBuilder embed = new EmbedBuilder().setAuthor("Items", null, effectiveMember.getUser().getEffectiveAvatarUrl()).setColor(effectiveMember.getColorRaw());
        if (items.isEmpty()) {
            event.replyFailure("That user does not have any items").queue();
            return;
        }
        String footerText = "If a category isn't shown it means you have no items in that category | Balance: $";
        StringBuilder footer = new StringBuilder(footerText);
        Map<ItemType, StringJoiner> types = new HashMap<>();
        for (Document item : items) {
            String name = item.getString("name");
            if (name.equals("balance")) {
                if (items.size() == 1) {
                    event.replyFailure("That user does not have any items").queue();
                    return;
                }
                footer.append(String.format("%,d", item.get("balance", 0L)));
                continue;
            }
            ItemType type = ItemType.fromId(item.getInteger("type"));
            ItemStack<?> stack = new ItemStack<>(event.getBot().getEconomyManager(), item);
            types.compute(type, (key, value) -> (value == null ? new StringJoiner("\n") : value).add(stack.toString()));
        }
        if (footer.length() == footerText.length()) {
            footer.append("0");
        }
        types.forEach((type, joiner) -> embed.addField(type.getName(), joiner.toString(), true));
        embed.setFooter(footer.toString());
        event.reply(embed.build()).queue();
    });
}
Also used : ItemType(com.sx4.bot.entities.economy.item.ItemType) Document(org.bson.Document) Bson(org.bson.conversions.Bson) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) ItemStack(com.sx4.bot.entities.economy.item.ItemStack) Member(net.dv8tion.jda.api.entities.Member)

Example 10 with ItemStack

use of com.sx4.bot.entities.economy.item.ItemStack in project Sx4 by sx4-discord-bot.

the class AxeCommand method shop.

@Command(value = "shop", description = "View all the axes you are able to buy or craft")
@CommandId(388)
@Examples({ "axe shop" })
@BotPermissions(permissions = { Permission.MESSAGE_EMBED_LINKS })
public void shop(Sx4CommandEvent event) {
    List<Axe> axes = event.getBot().getEconomyManager().getItems(Axe.class);
    PagedResult<Axe> paged = new PagedResult<>(event.getBot(), axes).setPerPage(12).setCustomFunction(page -> {
        EmbedBuilder embed = new EmbedBuilder().setAuthor("Axe Shop", null, event.getSelfUser().getEffectiveAvatarUrl()).setTitle("Page " + page.getPage() + "/" + page.getMaxPage()).setDescription("Axes are a good way to get some wood for crafting").setFooter(PagedResult.DEFAULT_FOOTER_TEXT);
        page.forEach((axe, index) -> {
            List<ItemStack<CraftItem>> items = axe.getCraft();
            String craft = items.isEmpty() ? "None" : items.stream().map(ItemStack::toString).collect(Collectors.joining("\n"));
            embed.addField(axe.getName(), String.format("Price: $%,d\nCraft: %s\nDurability: %,d", axe.getPrice(), craft, axe.getMaxDurability()), true);
        });
        return new MessageBuilder().setEmbeds(embed.build());
    });
    paged.execute(event);
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) ItemStack(com.sx4.bot.entities.economy.item.ItemStack) PagedResult(com.sx4.bot.paged.PagedResult) Axe(com.sx4.bot.entities.economy.item.Axe) BotPermissions(com.sx4.bot.annotations.command.BotPermissions) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command) CommandId(com.sx4.bot.annotations.command.CommandId) Examples(com.sx4.bot.annotations.command.Examples)

Aggregations

EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)11 Sx4Command (com.sx4.bot.core.Sx4Command)10 Command (com.jockie.bot.core.command.Command)7 CommandId (com.sx4.bot.annotations.command.CommandId)7 Examples (com.sx4.bot.annotations.command.Examples)7 Document (org.bson.Document)7 Bson (org.bson.conversions.Bson)7 BotPermissions (com.sx4.bot.annotations.command.BotPermissions)6 ItemStack (com.sx4.bot.entities.economy.item.ItemStack)6 PagedResult (com.sx4.bot.paged.PagedResult)6 MessageBuilder (net.dv8tion.jda.api.MessageBuilder)6 ModuleCategory (com.sx4.bot.category.ModuleCategory)5 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)5 Operators (com.sx4.bot.database.mongo.model.Operators)5 EconomyUtility (com.sx4.bot.utility.EconomyUtility)5 ExceptionUtility (com.sx4.bot.utility.ExceptionUtility)5 Permission (net.dv8tion.jda.api.Permission)5 Filters (com.mongodb.client.model.Filters)4 UpdateOptions (com.mongodb.client.model.UpdateOptions)4 List (java.util.List)4