package net.uomc.mineshaft.resources.market; import java.util.List; import com.mouldycheerio.dbot.CustomBot; import com.mouldycheerio.dbot.commands.CommandDetails; import com.mouldycheerio.dbot.commands.DetailedCommand; import com.mouldycheerio.dbot.util.PeelingUtils; import com.vdurmont.emoji.EmojiParser; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.MessageEmbed.Field; import net.dv8tion.jda.api.entities.emoji.Emoji; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.uomc.mineshaft.resources.Resource; import net.uomc.mineshaft.resources.ResourceManager; public class MarketCommand extends DetailedCommand { private ResourceManager resourceManager; private MarketManager marketManager; public MarketCommand(MarketManager marketManager) { this.marketManager = marketManager; this.resourceManager = marketManager.getResourceManager(); setCommandDetails(CommandDetails.from("market,m", "Buy and sell items at a given quantity on the market", "market sell [items] for [price]")); } @Override public void execute(MessageReceivedEvent e, CustomBot op, String[] args) { market(e, op, args); } private void market(MessageReceivedEvent e, CustomBot op, String[] args) { marketManager.processOffers(e.getGuild()); if (args.length == 0) { EmbedBuilder embedBuilder = new EmbedBuilder(); embedBuilder.setTitle("Market"); embedBuilder.setColor(op.color); List embedFields = marketManager.getEmbedFields(e.getGuild()); if (embedFields.size() == 0) { embedBuilder.setDescription("**:x: There are no available offers!**\n" + "Create one with `market sell`"); e.getMessage().replyEmbeds(embedBuilder.build()).queue(); } else { embedBuilder.setDescription( "Use `" + op.getPrefixManager().getPrefix(e.getGuild()) + "market buy [id]` to accept an offer\n" + "Use `" + op.getPrefixManager().getPrefix(e.getGuild()) + "market sell` to create an offer\n"); e.getMessage().addReaction(Emoji.fromUnicode(EmojiParser.parseToUnicode(":white_check_mark:"))) .queue((r) -> { PeelingUtils.pagesEmbed(e.getAuthor(), e.getChannel(), embedBuilder, embedFields); }); } } else if (args.length > 0) { if (args[0].equals("buy")) { if (args.length > 1) { buy(e, op, args); } else { op.sendErrorMessage(e,":x: please specify an id!"); } } else if (args[0].equals("sell")) { sell(e, op, args); } } } private void sell(MessageReceivedEvent e, CustomBot op, String[] args) { if (args.length > 5) { try { long sellQ = Math.abs(Long.parseLong(args[1])); long buyQ = Math.abs(Long.parseLong(args[4])); String sellRname = args[2]; Resource sellR = resourceManager.getResource(sellRname); String buyRname = args[5]; Resource buyR = resourceManager.getResource(buyRname); if (sellR != null || buyR != null) { if (sellR.get(e.getMember()) >= sellQ) { Offer offer = new Offer(); offer.setUserID(e.getAuthor().getId()); offer.setBuyingQuantity(buyQ); offer.setBuyingResource(buyR); offer.setID(Offer.highest + 1); offer.setSellingQuantity(sellQ); offer.setSellingResource(sellR); marketManager.addOffer(e.getGuild(), offer); op.sendSuccessMessage(e, ":white_check_mark: successfully listed **" + offer.getAsString(resourceManager) + "** offer id: `" + offer.getID() + "`"); } else { op.sendErrorMessage(e, ":x: You don't have enough " + sellR + " to offer"); } } else { sendSellUsage(e, op); } } catch (NumberFormatException ex) { sendSellUsage(e, op); } } else { sendSellUsage(e, op); } } private void sendSellUsage(MessageReceivedEvent e, CustomBot op) { List listResources = resourceManager.listResources(); Resource resource1 = listResources.get(listResources.size() - 1); Resource resource2 = listResources.get(0); int q1 = (int) (Math.random() * 10); int q2 = (int) (Math.random() * 10); op.sendErrorMessage( e, ":x: usage: `market sell [items] for [price]`\ni.e: `market sell " + q1 + " " + resource1.getName() + " for " + q2 + " " + resource2.getName() + "`" ); } private void buy(MessageReceivedEvent e, CustomBot op, String[] args) { String id = args[1]; Offer offer = marketManager.getOffer(e.getGuild(), id); if (offer != null) { offer.getMember(e.getGuild(), seller -> { if (e.getMember().equals(seller)) { op.sendErrorMessage(e, ":x: You cannot accept your own offer"); } else { long buyingQuantity = offer.getBuyingQuantity(); Resource buyingResource = offer.getBuyingResource(resourceManager); if (buyingResource.get(e.getMember()) >= buyingQuantity) { long sellingQuantity = offer.getSellingQuantity(); Resource sellingResource = offer.getSellingResource(resourceManager); if (sellingResource.get(seller) < sellingQuantity) { op.sendErrorMessage(e, ":x: This offer no longer exists as " + seller.getAsMention() + "does not have enough " + sellingResource.getName()); marketManager.removeOffer(e.getGuild(), offer); return; } buyingResource.increment(e.getMember(), -buyingQuantity); sellingResource.increment(e.getMember(), sellingQuantity); if (seller != null) { buyingResource.increment(seller, buyingQuantity); sellingResource.increment(seller, -sellingQuantity); } seller.getUser().openPrivateChannel().queue(pc -> { pc.sendMessage( "**" + e.getAuthor().getAsTag() + "** has accepted your market offer: **" + offer.getAsString(resourceManager) + "** [" + offer.getID() + "]" ) .queue(); }); String from = " from " + seller.getAsMention(); marketManager.removeOffer(e.getGuild(), offer); e.getMessage().addReaction(Emoji.fromUnicode(EmojiParser.parseToUnicode(":white_check_mark:"))).queue((r) -> { op.sendMessageRaw( e, ":white_check_mark: successfully bought **" + sellingResource.prettyValue(sellingQuantity) + "** for **" + buyingResource.prettyValue(buyingQuantity) + "**" + from ); }); } else { long have = buyingResource.get(e.getMember()); long more = buyingQuantity - have; op.sendErrorMessage(e, ":x: You need **" + buyingResource.prettyValue(more) + "** more to buy this"); } } }); } else { op.sendErrorMessage(e, ":x: Please specify a valid id!"); } } }