summaryrefslogtreecommitdiff
path: root/src/main/java/net/tylermurphy/ken/command/fun/Slots.java
blob: f03d50c92233a22474eb7752a92dcd2c4c01a3b6 (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
package net.tylermurphy.ken.command.fun;

import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.tylermurphy.ken.Ken;
import net.tylermurphy.ken.command.Response;
import net.tylermurphy.ken.command.annotation.Command;
import net.tylermurphy.ken.command.annotation.Option;
import org.json.JSONObject;

import java.util.List;


public class Slots {

    record Slot(int weight, float full_reward, float half_reward, String emoji){}

    private final Slot[] CHOICES = new Slot[]{
            new Slot(25, 0.75f, 0.50f, ":melon:"),
            new Slot(15, 1.00f, 0.75f, ":green_apple:"),
            new Slot(15, 1.25f, 1.00f, ":apple:"),
            new Slot(15, 2.00f, 1.25f, ":tangerine:"),
            new Slot(10, 2.50f, 1.50f, ":mango:"),
            new Slot(10, 3.00f, 1.75f, ":lemon:"),
            new Slot(10, 5.00f, 2.00f, ":cherries:"),
    };

    @Command(name="slots", description="Bet your money on a slot machine")
    @Option(name="amount", description="Amount to bet", type=OptionType.INTEGER, required=true)
    public Response execute(Member sender, Guild guild, List<Object> args){
        String data = Ken.getInstance().getDatabase().getEconomyData().getData(guild.getIdLong(), sender.getUser().getIdLong());
        JSONObject json = Ken.getInstance().getDatabase().getEconomyData().updateData(guild.getIdLong(), sender.getUser().getIdLong(), data);
        int money = json.getInt("money");
        int bet = (int)args.get(0);
        if(bet < 5){
            return Response.error("You cannot bet anything less than 5 moneys");
        } else if(bet > money){
            return Response.error("You don't have enough moneys to bet this much");
        }
        Slot slot1 = getSlot(0);
        Slot slot2 = getSlot(slot1.weight);
        Slot slot3 = getSlot(slot2.weight);
        EmbedBuilder builder = Ken.getInstance().getDefaultEmbed()
                .setTitle("Slot Machine")
                .appendDescription("`Balance:` "+money+"\n")
                .appendDescription("`Bet:` "+bet+"\n")
                .appendDescription(":black_large_square:".repeat(5)+"\n")
                .appendDescription(":black_large_square:" + slot1.emoji + slot2.emoji + slot3.emoji +":black_large_square:\n")
                .appendDescription(":black_large_square:".repeat(5)+"\n");
        if(slot1.emoji.equals(slot2.emoji) && slot1.emoji.equals(slot3.emoji)){
            // 3 of a kind
            money += bet * slot2.full_reward;
            builder.setFooter("3 of a kind! ("+slot2.full_reward+"x)");
        } else if(slot1.emoji.equals(slot2.emoji) || slot2.emoji.equals(slot3.emoji) || slot1.emoji.equals(slot3.emoji)){
            // 2 of a kind
            Slot slot = (slot1.emoji.equals(slot2.emoji) ? slot1 : slot3);
            money += bet * slot.half_reward;
            builder.setFooter("2 of a kind! ("+slot.half_reward+"x)");
        } else {
            // Fail
            money -= bet;
            builder.setFooter("None of a kind, loose bet.");
        }
        builder.appendDescription("`New Balance:` "+money);
        json.put("money",money);
        Ken.getInstance().getDatabase().getEconomyData().setData(guild.getIdLong(), sender.getUser().getIdLong(), json.toString());
        return Response.success(builder.build());
    }

    private Slot getSlot(int pull){
        double rand = (int)(Math.random()*100)+1;
        double diff = pull - rand;
        if(pull != 0){
            rand = diff > 0 ? rand + diff*Math.random() : rand - diff*Math.random();
        }
        int walk = 0;
        for(Slot slot : CHOICES){
            if(slot.weight + walk > rand) return slot;
            else walk += slot.weight;
        }
        return CHOICES[CHOICES.length-1];
    }

}