-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
314 lines (275 loc) · 12 KB
/
database.py
File metadata and controls
314 lines (275 loc) · 12 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# ═══════════════════════════════════════════════════════════
# DATABASE MANAGER - FIXED & COMPLETE
# ═══════════════════════════════════════════════════════════
import sqlite3
import datetime
import threading
from contextlib import contextmanager
DB_FILE = 'flowx_ultimate.db'
db_lock = threading.Lock()
@contextmanager
def get_db():
"""Thread-safe database connection"""
with db_lock:
conn = sqlite3.connect(DB_FILE, check_same_thread=False)
conn.row_factory = sqlite3.Row
try:
yield conn
conn.commit()
except Exception:
conn.rollback()
raise
finally:
conn.close()
def init_database():
"""Initialize all tables"""
with get_db() as conn:
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
telegram_id INTEGER UNIQUE NOT NULL,
username TEXT,
first_name TEXT NOT NULL,
last_name TEXT,
referral_code TEXT UNIQUE NOT NULL,
referred_by INTEGER,
points INTEGER DEFAULT 0,
total_earned INTEGER DEFAULT 0,
total_spent INTEGER DEFAULT 0,
vip_until TEXT,
vip_streak INTEGER DEFAULT 0,
last_spin TEXT,
last_bonus TEXT,
streak INTEGER DEFAULT 0,
login_streak INTEGER DEFAULT 0,
total_spins INTEGER DEFAULT 0,
total_refs INTEGER DEFAULT 0,
total_games INTEGER DEFAULT 0,
total_tools INTEGER DEFAULT 0,
games_won INTEGER DEFAULT 0,
banned INTEGER DEFAULT 0,
warned INTEGER DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (referred_by) REFERENCES users(telegram_id)
)
''')
c.execute('''
CREATE TABLE IF NOT EXISTS transactions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
type TEXT NOT NULL,
amount INTEGER NOT NULL,
balance_after INTEGER,
description TEXT,
metadata TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(telegram_id)
)
''')
c.execute('''
CREATE TABLE IF NOT EXISTS withdrawals (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
points INTEGER NOT NULL,
usd_amount REAL NOT NULL,
fee REAL DEFAULT 0,
net_amount REAL NOT NULL,
method TEXT NOT NULL,
payment_details TEXT,
transaction_id TEXT,
status TEXT DEFAULT 'pending',
status_reason TEXT,
requested_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
processed_at TEXT,
completed_at TEXT,
processed_by INTEGER,
notes TEXT,
FOREIGN KEY (user_id) REFERENCES users(telegram_id)
)
''')
c.execute('''
CREATE TABLE IF NOT EXISTS spin_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
points_won INTEGER NOT NULL,
multiplier REAL DEFAULT 1.0,
is_jackpot INTEGER DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(telegram_id)
)
''')
c.execute('''
CREATE TABLE IF NOT EXISTS referral_earnings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
referrer_id INTEGER NOT NULL,
referred_id INTEGER NOT NULL,
level INTEGER DEFAULT 1,
points_earned INTEGER DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (referrer_id) REFERENCES users(telegram_id),
FOREIGN KEY (referred_id) REFERENCES users(telegram_id),
UNIQUE(referrer_id, referred_id)
)
''')
c.execute('''
CREATE TABLE IF NOT EXISTS game_sessions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
game_type TEXT NOT NULL,
bet_amount INTEGER DEFAULT 0,
win_amount INTEGER DEFAULT 0,
result TEXT,
details TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(telegram_id)
)
''')
c.execute('''
CREATE TABLE IF NOT EXISTS notes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
title TEXT,
content TEXT NOT NULL,
category TEXT DEFAULT 'general',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(telegram_id)
)
''')
c.execute('''
CREATE TABLE IF NOT EXISTS ad_views (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
ad_id INTEGER NOT NULL,
points_earned INTEGER NOT NULL,
verified INTEGER DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(telegram_id)
)
''')
c.execute('''
CREATE TABLE IF NOT EXISTS admin_logs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
admin_id INTEGER NOT NULL,
action TEXT NOT NULL,
target_user INTEGER,
details TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
# Indexes
c.execute('CREATE INDEX IF NOT EXISTS idx_users_telegram ON users(telegram_id)')
c.execute('CREATE INDEX IF NOT EXISTS idx_users_referral ON users(referral_code)')
c.execute('CREATE INDEX IF NOT EXISTS idx_transactions_user ON transactions(user_id)')
c.execute('CREATE INDEX IF NOT EXISTS idx_withdrawals_status ON withdrawals(status)')
c.execute('CREATE INDEX IF NOT EXISTS idx_spin_history_user ON spin_history(user_id)')
print("✅ Database initialized!")
def create_user(telegram_id, username, first_name, last_name=None, referred_by=None):
"""Create new user with referral tracking. Returns (referral_code, status_msg)"""
import random, string
code = ''.join(random.choices(string.ascii_uppercase + string.digits, k=8))
with get_db() as conn:
c = conn.cursor()
existing = c.execute(
"SELECT telegram_id FROM users WHERE telegram_id = ?", (telegram_id,)
).fetchone()
if existing:
return None, "exists"
try:
c.execute('''
INSERT INTO users (telegram_id, username, first_name, last_name,
referral_code, referred_by, points, total_earned)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
''', (telegram_id, username, first_name, last_name, code, referred_by,
10 if referred_by else 0,
10 if referred_by else 0))
c.execute('''
INSERT INTO transactions (user_id, type, amount, description)
VALUES (?, ?, ?, ?)
''', (telegram_id, 'welcome_bonus', 10 if referred_by else 0,
'Welcome bonus' + (' (referral)' if referred_by else '')))
if referred_by:
c.execute('''
UPDATE users
SET points = points + 50,
total_earned = total_earned + 50,
total_refs = total_refs + 1
WHERE telegram_id = ?
''', (referred_by,))
c.execute('''
INSERT INTO transactions (user_id, type, amount, description)
VALUES (?, ?, ?, ?)
''', (referred_by, 'referral_bonus', 50, f'Referral: {first_name}'))
c.execute('''
INSERT OR IGNORE INTO referral_earnings (referrer_id, referred_id, points_earned)
VALUES (?, ?, ?)
''', (referred_by, telegram_id, 50))
return code, "success"
except Exception as e:
return None, str(e)
def get_user(telegram_id):
"""Get complete user data as dict"""
with get_db() as conn:
row = conn.execute(
"SELECT * FROM users WHERE telegram_id = ?", (telegram_id,)
).fetchone()
return dict(row) if row else None
def update_points(telegram_id, amount, reason, transaction_type='adjustment'):
"""Update user points. Returns (success: bool, new_balance: int)"""
with get_db() as conn:
c = conn.cursor()
current = c.execute(
"SELECT points FROM users WHERE telegram_id = ?", (telegram_id,)
).fetchone()
if not current:
return False, 0
new_balance = current['points'] + amount
if new_balance < 0:
return False, current['points']
if amount > 0:
c.execute('''
UPDATE users
SET points = ?, total_earned = total_earned + ?
WHERE telegram_id = ?
''', (new_balance, amount, telegram_id))
else:
c.execute('''
UPDATE users
SET points = ?, total_spent = total_spent + ?
WHERE telegram_id = ?
''', (new_balance, abs(amount), telegram_id))
c.execute('''
INSERT INTO transactions (user_id, type, amount, balance_after, description)
VALUES (?, ?, ?, ?, ?)
''', (telegram_id, transaction_type, amount, new_balance, reason))
return True, new_balance
def get_leaderboard(limit=10):
"""Get top users by points"""
with get_db() as conn:
rows = conn.execute('''
SELECT telegram_id, first_name, username, points, total_earned, total_refs
FROM users WHERE banned = 0
ORDER BY points DESC
LIMIT ?
''', (limit,)).fetchall()
return [dict(r) for r in rows]
def get_stats():
"""Get global bot statistics"""
with get_db() as conn:
c = conn.cursor()
pending = c.execute(
"SELECT COUNT(*), COALESCE(SUM(points), 0) FROM withdrawals WHERE status = 'pending'"
).fetchone()
return {
'total_users': c.execute("SELECT COUNT(*) FROM users").fetchone()[0],
'today_users': c.execute("SELECT COUNT(*) FROM users WHERE date(created_at) = date('now')").fetchone()[0],
'total_points': c.execute("SELECT COALESCE(SUM(points), 0) FROM users").fetchone()[0],
'total_earned': c.execute("SELECT COALESCE(SUM(total_earned), 0) FROM users").fetchone()[0],
'active_today': c.execute("SELECT COUNT(*) FROM users WHERE date(updated_at) = date('now')").fetchone()[0],
'pending_count': pending[0],
'pending_points': pending[1],
'vip_users': c.execute("SELECT COUNT(*) FROM users WHERE vip_until > datetime('now')").fetchone()[0],
'spins_today': c.execute("SELECT COUNT(*) FROM spin_history WHERE date(created_at) = date('now')").fetchone()[0],
}