summaryrefslogtreecommitdiff
path: root/commands/basic_games.py
blob: bad02c64c51e42682173f923fef06e775d6425be (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
from discord.ext.commands import Bot, Cog
from discord_slash import cog_ext, SlashContext
from discord_slash.utils.manage_commands import create_option, create_choice
from base import config
from models.user import User
import random
import yaml


class BasicGames(Cog):
    def __init__(self, bot: Bot):
        self.bot = bot
        with open("config.yaml", "r") as yamlfile:
            config = yaml.load(yamlfile, Loader=yaml.CLoader)
            print("Read config successful")
            self.config = config

    @cog_ext.cog_slash(
        name="coin_flip",
        description="Flip a coin",
        guild_ids=[config["discord_server_id"]],
        options=[
            create_option(
                name="num_coins",
                description="coins to bet",
                option_type=4,
                required=True,
            ),
            create_option(
                name="choice",
                description="heads or tails?",
                option_type=3,
                required=True,
                choices=[
                    create_choice(name="Heads", value="Heads"),
                    create_choice(name="Tails", value="Tails"),
                ],
            ),
        ],
    )
    async def coin_flip(self, ctx: SlashContext, num_coins: int, choice: int):
        user = User.get(User.discord_id == ctx.author_id)

        await ctx.send(
            content=f"{user.username} is betting {num_coins} {config['currency']} on {choice}"
        )
        choices = ["Heads", "Tails"]
        result = random.choice(choices)
        await ctx.send(content=f"Coin flip lands on {result}")
        if choice == result:
            await ctx.send(content=f"You win {num_coins * 2} {config['currency']}!")
            user.add_coins(num_coins * 2)
        else:
            await ctx.send(content=f"You lose {num_coins} {config['currency']} :(")
            user.remove_coins(num_coins)

        user.save()

    @cog_ext.cog_slash(
        name="dice",
        description="Roll some dice.",
        guild_ids=[config["discord_server_id"]],
        options=[
            create_option(
                name="num_coins",
                description="coins to bet",
                option_type=4,
                required=True,
            ),
        ],
    )
    async def dice(self, ctx: SlashContext, num_coins: int):
        user = User.get(User.discord_id == ctx.author_id)
        if user.currency < num_coins:
            await ctx.send(
                content=f"{user.display_name}, you don't have enough {config['currency']} for that bet"
            )
            return False

        results = [
            random.randint(1, 6),
            random.randint(1, 6),
            random.randint(1, 6),
            random.randint(1, 6),
        ]
        await ctx.send(content=f"{user.display_name}, you roll a {results[0]} and a {results[1]}")

        user_total = results[0] + results[1]
        bot_total = results[2] + results[3]
        if user_total == 12:
            winnings = num_coins * 2
            await ctx.channel.send(content=f"Double 6! You win {winnings} {config['currency']}!")
            user.add_coins(winnings)
        elif results[0] == results[1]:
            winnings = num_coins * 2
            await ctx.channel.send(
                content=f"You roll a double! You win {winnings} {config['currency']}!"
            )
            user.add_coins(winnings)
        else:
            await ctx.channel.send(
                content=f"{user.display_name}, I rolled a {results[2]} and a {results[3]}"
            )
            if user_total > bot_total:
                await ctx.channel.send(content=f"You win {num_coins} {config['currency']}!")
                user.add_coins(num_coins)
            elif user_total == bot_total:
                await ctx.channel.send(content="It's a draw! Have your money back...")
            else:
                await ctx.channel.send(content=f"You lose {num_coins} {config['currency']} :(")
                user.remove_coins(num_coins)
        user.save()

    @cog_ext.cog_slash(
        name="slots",
        description="Pull the slot machine.",
        guild_ids=[config["discord_server_id"]],
    )
    async def slots(self, ctx: SlashContext):
        user = User.get(User.discord_id == ctx.author_id)
        cost = 100
        if user.currency < cost:
            await ctx.send(
                content=f"{user.display_name}, you don't have enough {config['currency']}, it costs 100 {config['currency']} to play"
            )
            return False

        await ctx.send(
            content=f"{user.display_name}, you put {cost} {config['currency']} in the machine and pull the handle..."
        )
        user.remove_coins(cost)

        results = [
            random.randint(0, 5),
            random.randint(0, 5),
            random.randint(0, 5),
        ]
        results_top = [
            random.randint(0, 5),
            random.randint(0, 5),
            random.randint(0, 5),
        ]
        results_bottom = [
            random.randint(0, 5),
            random.randint(0, 5),
            random.randint(0, 5),
        ]

        result_text = ""
        e = list(config["slots"].values())
        winnings = 0

        for r in results_top:
            result_text += e[r]["dark"]
        result_text += "\n"

        for r in results:
            result_text += e[r]["emoji"]
            winnings += e[r]["reward_single"]
        result_text += "\n"

        double = False
        triple = False
        if results[0] == results[1] or results[0] == results[2]:
            double = True
            winnings += e[results[0]]["reward_double"]
        elif results[1] == results[2]:
            double = True
            winnings += e[results[1]]["reward_double"]
        elif results[0] == results[1] and results[0] == results[2]:
            triple = True
            winnings += e[results[0]]["reward_triple"]

        for r in results_bottom:
            result_text += e[r]["dark"]
        result_text += "\n"

        await ctx.channel.send(content=result_text)

        if winnings == 0:
            await ctx.channel.send(content=f"No match :( You lose {cost} {config['currency']}")
            user.remove_coins(cost)
        else:
            if triple:
                await ctx.channel.send(content=f"JACKPOT! You win {winnings} {config['currency']}")
            elif double:
                await ctx.channel.send(
                    content=f"Ding ding! You win {winnings} {config['currency']}"
                )
            else:
                await ctx.channel.send(content=f"You win {winnings} {config['currency']}")
            user.add_coins(winnings)

        user.save()


def setup(bot):
    bot.add_cog(BasicGames(bot))

    print("Loaded BasicGames")
    print(config["discord_server_id"])