summaryrefslogtreecommitdiff
path: root/src/main/java/net/uomc/mineshaft/resources/market/MarketCommand.java
blob: 2b6922310a4dfd1b96d61b9b9090a9602f4903a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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<Field> 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<Resource> 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!");
        }
    }
}