-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
197 lines (158 loc) · 5.84 KB
/
main.py
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
import json
from disnake import Intents
from disnake.ext import commands
import os, traceback
import aiosqlite
from random import randint
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv("BOT_TOKEN")
class MyBot(commands.InteractionBot):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# self.db = None
# async def db_main(self):
# self.db = await aiosqlite.connect("main.sqlite")
# print("Main DataBase Connected")
async def commit(self):
async with aiosqlite.connect("main.sqlite") as db:
await db.commit()
async def execute(self, query, *values):
async with aiosqlite.connect("main.sqlite") as db:
async with db.cursor() as cur:
await cur.execute(query, tuple(values))
await db.commit()
async def executemany(self, query, values):
async with aiosqlite.connect("main.sqlite") as db:
async with db.cursor() as cur:
await cur.executemany(query, values)
await db.commit()
async def fetchval(self, query, *values):
async with aiosqlite.connect("main.sqlite") as db:
async with db.cursor() as cur:
exe = await cur.execute(query, tuple(values))
val = await exe.fetchone()
return val[0] if val else None
async def fetchrow(self, query, *values):
async with aiosqlite.connect("main.sqlite") as db:
async with db.cursor() as cur:
exe = await cur.execute(query, tuple(values))
row = await exe.fetchmany(size=1)
if len(row) > 0:
row = [r for r in row[0]]
else:
row = None
return row
async def fetchmany(self, query, size, *values):
async with aiosqlite.connect("main.sqlite") as db:
async with db.cursor() as cur:
exe = await cur.execute(query, tuple(values))
many = await exe.fetchmany(size)
return many
async def fetch(self, query, *values):
async with aiosqlite.connect("main.sqlite") as db:
async with db.cursor() as cur:
exe = await cur.execute(query, tuple(values))
all = await exe.fetchall()
return all
async def give_money(self, amount, user_id):
self.execute(
"UPDATE users SET money = money + $1 WHERE user_id = $2",
amount, user_id
)
async def remove_money(self, amount, user_id):
user_money = self.fetchval(
"SELECT money FROM users WHERE user_id = $1",
user_id
)
if amount > user_money:
return False
else:
self.execute(
"UPDATE users SET money = money - $1 WHERE user_id = $2",
amount, user_id
)
return True
def determine_stats(self, tank_stats_range: dict, advantage: str):
def decrease_highTQ_chances(ignore_stat: str):
hp_max = tank_stats_range["HP"]["max"]
atk_max = tank_stats_range["ATTACK"]["max"]
def_max = tank_stats_range["DEFENCE"]["max"]
if ignore_stat != "HP":
hp = hp_max - ((30 / 100) * hp_max)
else:
hp = hp_max
if ignore_stat != "ATTACK":
attack = atk_max - ((30 / 100) * atk_max)
else:
attack = atk_max
if ignore_stat != "DEFENCE":
defence = def_max - ((30 / 100) * def_max)
else:
defence = def_max
if randint(0, 9): # If number between 1-9
return (round(hp), round(attack), round(defence))
else: # If number is 0
return (hp_max, atk_max, def_max)
max_ranges = decrease_highTQ_chances(advantage)
hp = randint(tank_stats_range["HP"]["min"], max_ranges[0])
attack = randint(tank_stats_range["ATTACK"]["min"], max_ranges[1])
defence = randint(tank_stats_range["DEFENCE"]["min"], max_ranges[2])
return (hp, attack, defence)
def get_TQ(self, tank_stats_range: dict, hp, defence, attack):
tank_quality = (
(hp + defence + attack)
/ (
tank_stats_range["HP"]["max"]
+ tank_stats_range["ATTACK"]["max"]
+ tank_stats_range["DEFENCE"]["max"]
)
) * 100
return f"{tank_quality:,.2f}"
def get_tank_details(self, name: str):
with open("assets/Tanks/tanks.json", "r") as f:
tanks_details = json.load(f)
return tanks_details[name.upper()]
bot = MyBot(intents=Intents.default())
async def setuptable(bot):
await bot.execute(
"""
CREATE TABLE IF NOT EXISTS users(
user_id INTEGER,
money INTEGER,
battle_tank TEXT,
PRIMARY KEY("user_id")
)
"""
)
await bot.execute(
"""
CREATE TABLE IF NOT EXISTS user_tanks(
user_id INTEGER,
name TEXT,
hp INTEGER,
atk INTEGER,
def INTEGER,
serial INTEGER
)
"""
)
@bot.event
async def on_ready():
print("*********\nBot is Ready.\n*********")
await setuptable(bot)
# @bot.event
# async def on_command_error(ctx,error):
# if isinstance(error, (commands.CommandNotFound, commands.CheckFailure)):
# return
# else:
# print
for file in os.listdir("./cogs"):
if file.endswith(".py") and file != "__init__.py":
try:
bot.load_extension("cogs." + file[:-3])
print(f"{file[:-3]} Loaded successfully.")
except:
print(f"Unable to load {file[:-3]}.")
print(traceback.format_exc())
bot.run(TOKEN)