Search in sources :

Example 1 with InteractiveOperation

use of net.kodehawa.mantarobot.core.listeners.operations.InteractiveOperation in project MantaroBot by Mantaro.

the class MoneyCmds method gamble.

@Command
public static void gamble(CommandRegistry cr) {
    RateLimiter rateLimiter = new RateLimiter(TimeUnit.SECONDS, 15);
    Random r = new Random();
    cr.register("gamble", new SimpleCommand(Category.CURRENCY) {

        @Override
        public void call(GuildMessageReceivedEvent event, String content, String[] args) {
            String id = event.getAuthor().getId();
            Player player = MantaroData.db().getPlayer(event.getMember());
            if (!rateLimiter.process(id)) {
                event.getChannel().sendMessage(EmoteReference.STOPWATCH + "Halt! You're gambling so fast that I can't print enough money!").queue();
                return;
            }
            if (player.getMoney() <= 0) {
                event.getChannel().sendMessage(EmoteReference.ERROR2 + "You're broke. Search for some credits first!").queue();
                return;
            }
            if (player.getMoney() > (long) (Integer.MAX_VALUE) * 3) {
                event.getChannel().sendMessage(EmoteReference.ERROR2 + "You have too much money! Maybe transfer or buy items?").queue();
                return;
            }
            double multiplier;
            long i;
            int luck;
            try {
                switch(content) {
                    case "all":
                    case "everything":
                        i = player.getMoney();
                        multiplier = 1.4d + (r.nextInt(1500) / 1000d);
                        luck = 30 + (int) (multiplier * 10) + r.nextInt(20);
                        break;
                    case "half":
                        i = player.getMoney() == 1 ? 1 : player.getMoney() / 2;
                        multiplier = 1.2d + (r.nextInt(1500) / 1000d);
                        luck = 20 + (int) (multiplier * 15) + r.nextInt(20);
                        break;
                    case "quarter":
                        i = player.getMoney() == 1 ? 1 : player.getMoney() / 4;
                        multiplier = 1.1d + (r.nextInt(1100) / 1000d);
                        luck = 25 + (int) (multiplier * 10) + r.nextInt(18);
                        break;
                    default:
                        i = Long.parseLong(content);
                        if (i > player.getMoney() || i < 0)
                            throw new UnsupportedOperationException();
                        multiplier = 1.1d + (i / player.getMoney() * r.nextInt(1300) / 1000d);
                        luck = 15 + (int) (multiplier * 15) + r.nextInt(10);
                        break;
                }
            } catch (NumberFormatException e) {
                event.getChannel().sendMessage(EmoteReference.ERROR2 + "Please type a valid number equal or less than your credits or" + " `all` to gamble all your credits.").queue();
                return;
            } catch (UnsupportedOperationException e) {
                event.getChannel().sendMessage(EmoteReference.ERROR2 + "Please type a value within your credits amount.").queue();
                return;
            }
            User user = event.getAuthor();
            long gains = (long) (i * multiplier);
            gains = Math.round(gains * 0.55);
            final int finalLuck = luck;
            final long finalGains = gains;
            if (i >= Integer.MAX_VALUE / 4) {
                event.getChannel().sendMessage(EmoteReference.WARNING + "You're about to bet **" + i + "** " + "credits (which seems to be a lot). Are you sure? Type **yes** to continue and **no** otherwise.").queue();
                InteractiveOperations.create(event.getChannel(), "Gambling", (int) TimeUnit.SECONDS.toMillis(30), OptionalInt.empty(), new InteractiveOperation() {

                    @Override
                    public boolean run(GuildMessageReceivedEvent e) {
                        if (e.getAuthor().getId().equals(user.getId())) {
                            if (e.getMessage().getContent().equalsIgnoreCase("yes")) {
                                proceedGamble(event, player, finalLuck, random, i, finalGains);
                                return true;
                            } else if (e.getMessage().getContent().equalsIgnoreCase("no")) {
                                e.getChannel().sendMessage(EmoteReference.ZAP + "Cancelled bet.").queue();
                                return true;
                            }
                        }
                        return false;
                    }

                    @Override
                    public void onExpire() {
                        event.getChannel().sendMessage(EmoteReference.ERROR + "Time to complete the operation has ran out.").queue();
                    }
                });
                return;
            }
            proceedGamble(event, player, luck, random, i, gains);
        }

        @Override
        public MessageEmbed help(GuildMessageReceivedEvent event) {
            return helpEmbed(event, "Gamble command").setDescription("Gambles your money").addField("Usage", "~>gamble <all/half/quarter> or ~>gamble <amount>", false).build();
        }
    });
}
Also used : InteractiveOperation(net.kodehawa.mantarobot.core.listeners.operations.InteractiveOperation) Player(net.kodehawa.mantarobot.data.entities.Player) User(net.dv8tion.jda.core.entities.User) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) RateLimiter(net.kodehawa.mantarobot.commands.currency.RateLimiter) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) Command(net.kodehawa.mantarobot.modules.Command)

Aggregations

MessageEmbed (net.dv8tion.jda.core.entities.MessageEmbed)1 User (net.dv8tion.jda.core.entities.User)1 GuildMessageReceivedEvent (net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent)1 RateLimiter (net.kodehawa.mantarobot.commands.currency.RateLimiter)1 InteractiveOperation (net.kodehawa.mantarobot.core.listeners.operations.InteractiveOperation)1 Player (net.kodehawa.mantarobot.data.entities.Player)1 Command (net.kodehawa.mantarobot.modules.Command)1 SimpleCommand (net.kodehawa.mantarobot.modules.commands.SimpleCommand)1