-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
386 lines (333 loc) · 15.4 KB
/
Copy pathdatabase.py
File metadata and controls
386 lines (333 loc) · 15.4 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import sqlite3
import logging
from datetime import datetime, timedelta
import json
from config import Config
logger = logging.getLogger(__name__)
class Database:
def __init__(self, db_path="roleplay_bot.db"):
self.db_path = db_path
def get_connection(self):
"""Obtiene una conexión a la base de datos"""
return sqlite3.connect(self.db_path)
def initialize(self):
"""Inicializa las tablas de la base de datos"""
with self.get_connection() as conn:
cursor = conn.cursor()
# Tabla de usuarios
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
user_id INTEGER PRIMARY KEY,
username TEXT NOT NULL,
joined_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
dni TEXT UNIQUE,
dni_issued_at TIMESTAMP,
nombre TEXT,
apellido TEXT,
edad INTEGER,
fecha_nacimiento TEXT,
sexo TEXT,
nacionalidad TEXT
)
''')
# Tabla de cuentas bancarias
cursor.execute('''
CREATE TABLE IF NOT EXISTS bank_accounts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
bank_name TEXT NOT NULL,
balance REAL DEFAULT 0.0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users (user_id),
UNIQUE(user_id, bank_name)
)
''')
# Tabla de transacciones
cursor.execute('''
CREATE TABLE IF NOT EXISTS transactions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
type TEXT NOT NULL,
amount REAL NOT NULL,
description TEXT,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users (user_id)
)
''')
# Tabla de multas
cursor.execute('''
CREATE TABLE IF NOT EXISTS fines (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
officer_id INTEGER NOT NULL,
amount REAL NOT NULL,
reason TEXT NOT NULL,
issued_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
paid BOOLEAN DEFAULT FALSE,
paid_at TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users (user_id),
FOREIGN KEY (officer_id) REFERENCES users (user_id)
)
''')
# Tabla de préstamos
cursor.execute('''
CREATE TABLE IF NOT EXISTS loans (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
amount REAL NOT NULL,
interest_rate REAL DEFAULT 0.05,
due_date TIMESTAMP NOT NULL,
paid BOOLEAN DEFAULT FALSE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users (user_id)
)
''')
# Tabla de inventario
cursor.execute('''
CREATE TABLE IF NOT EXISTS inventory (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
item_name TEXT NOT NULL,
quantity INTEGER DEFAULT 1,
value REAL DEFAULT 0.0,
acquired_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users (user_id)
)
''')
# Tabla de licencias
cursor.execute('''
CREATE TABLE IF NOT EXISTS licenses (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
license_type TEXT NOT NULL,
issued_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
expires_at TIMESTAMP,
valid BOOLEAN DEFAULT TRUE,
FOREIGN KEY (user_id) REFERENCES users (user_id),
UNIQUE(user_id, license_type)
)
''')
# Tabla de sanciones
cursor.execute('''
CREATE TABLE IF NOT EXISTS sanctions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
staff_id INTEGER NOT NULL,
reason TEXT NOT NULL,
duration_hours INTEGER,
issued_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
expires_at TIMESTAMP,
active BOOLEAN DEFAULT TRUE,
FOREIGN KEY (user_id) REFERENCES users (user_id),
FOREIGN KEY (staff_id) REFERENCES users (user_id)
)
''')
# Tabla de arrestos
cursor.execute('''
CREATE TABLE IF NOT EXISTS arrests (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
officer_id INTEGER NOT NULL,
reason TEXT NOT NULL,
arrested_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
released_at TIMESTAMP,
active BOOLEAN DEFAULT TRUE,
FOREIGN KEY (user_id) REFERENCES users (user_id),
FOREIGN KEY (officer_id) REFERENCES users (user_id)
)
''')
# Tabla de tienda
cursor.execute('''
CREATE TABLE IF NOT EXISTS shop_items (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,
price REAL NOT NULL,
description TEXT,
category TEXT DEFAULT 'general',
available BOOLEAN DEFAULT TRUE
)
''')
# Tabla de historias de usuario
cursor.execute('''
CREATE TABLE IF NOT EXISTS user_stories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
story_text TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users (user_id)
)
''')
# Tabla de incautaciones
cursor.execute('''
CREATE TABLE IF NOT EXISTS seizures (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
officer_id INTEGER NOT NULL,
model TEXT NOT NULL,
license_plate TEXT NOT NULL,
image_url TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users (user_id),
FOREIGN KEY (officer_id) REFERENCES users (user_id)
)
''')
conn.commit()
logger.info("Base de datos inicializada correctamente")
# Insertar elementos de tienda por defecto
self._populate_default_shop_items()
def _populate_default_shop_items(self):
"""Puebla la tienda con elementos por defecto"""
default_items = [
("Seguro de vehiculo", 300.0, "Seguro para protección vehicular", "seguros"),
("Beretta M9", 1750.0, "Pistola Beretta M9 para portación", "armas"),
("Dron", 2000.0, "Dron para vigilancia y uso recreativo", "tecnologia"),
("Bate", 15.0, "Bate de béisbol", "herramientas"),
("Navaja", 30.0, "Navaja plegable", "herramientas"),
("Spray", 45.0, "Spray de defensa personal", "herramientas"),
("Casa pequeña", 15000.0, "Casa residencial pequeña", "propiedades"),
("Establecimiento empresarial", 20000.0, "Local comercial para negocio", "propiedades"),
("Casa de campo", 25000.0, "Casa en zona rural", "propiedades"),
("Casa grande", 35000.0, "Casa residencial grande", "propiedades")
]
with self.get_connection() as conn:
cursor = conn.cursor()
for item in default_items:
cursor.execute('''
INSERT OR IGNORE INTO shop_items (name, price, description, category)
VALUES (?, ?, ?, ?)
''', item)
conn.commit()
def register_user(self, user_id: int, username: str):
"""Registra un nuevo usuario"""
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute('''
INSERT OR IGNORE INTO users (user_id, username)
VALUES (?, ?)
''', (user_id, username))
conn.commit()
def get_user_balance(self, user_id: int, bank_name: str = "Banco Central"):
"""Obtiene el balance de un usuario en un banco específico"""
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute('''
SELECT balance FROM bank_accounts
WHERE user_id = ? AND bank_name = ?
''', (user_id, bank_name))
result = cursor.fetchone()
return result[0] if result else 0.0
def get_user_total_balance(self, user_id: int):
"""Obtiene el balance total de un usuario en todos los bancos"""
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute('''
SELECT SUM(balance) FROM bank_accounts
WHERE user_id = ?
''', (user_id,))
result = cursor.fetchone()
return result[0] if result and result[0] is not None else 0.0
def deduct_payment(self, user_id: int, amount: float):
"""Descuenta dinero del usuario, priorizando el banco con más fondos"""
with self.get_connection() as conn:
cursor = conn.cursor()
# Obtener cuentas ordenadas por balance (mayor a menor)
cursor.execute('''
SELECT bank_name, balance
FROM bank_accounts
WHERE user_id = ? AND balance > 0
ORDER BY balance DESC
''', (user_id,))
accounts = cursor.fetchall()
remaining_amount = amount
for bank_name, balance in accounts:
if remaining_amount <= 0:
break
deduct = min(remaining_amount, balance)
cursor.execute('''
UPDATE bank_accounts
SET balance = balance - ?
WHERE user_id = ? AND bank_name = ?
''', (deduct, user_id, bank_name))
remaining_amount -= deduct
conn.commit()
return remaining_amount == 0 # True si se pudo descontar todo
def create_bank_account(self, user_id: int, bank_name: str):
"""Crea una cuenta bancaria para un usuario"""
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute('''
INSERT OR IGNORE INTO bank_accounts (user_id, bank_name, balance)
VALUES (?, ?, ?)
''', (user_id, bank_name, Config.INITIAL_BALANCE))
conn.commit()
return cursor.rowcount > 0
def update_balance(self, user_id: int, amount: float, bank_name: str = "Banco Central"):
"""Actualiza el balance de un usuario"""
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute('''
UPDATE bank_accounts
SET balance = balance + ?
WHERE user_id = ? AND bank_name = ?
''', (amount, user_id, bank_name))
conn.commit()
return cursor.rowcount > 0
def add_transaction(self, user_id: int, transaction_type: str, amount: float, description: str = None):
"""Añade una transacción al historial"""
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute('''
INSERT INTO transactions (user_id, type, amount, description)
VALUES (?, ?, ?, ?)
''', (user_id, transaction_type, amount, description))
conn.commit()
def create_user_story(self, user_id: int, story_text: str):
"""Crea una historia de usuario"""
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute('''
INSERT OR REPLACE INTO user_stories (user_id, story_text)
VALUES (?, ?)
''', (user_id, story_text))
conn.commit()
def get_user_story(self, user_id: int):
"""Obtiene la historia de un usuario"""
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute('''
SELECT story_text, created_at FROM user_stories
WHERE user_id = ?
ORDER BY created_at DESC LIMIT 1
''', (user_id,))
return cursor.fetchone()
def create_seizure(self, user_id: int, officer_id: int, model: str, license_plate: str, image_url: str = None):
"""Crea un registro de incautación"""
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute('''
INSERT INTO seizures (user_id, officer_id, model, license_plate, image_url)
VALUES (?, ?, ?, ?, ?)
''', (user_id, officer_id, model, license_plate, image_url))
seizure_id = cursor.lastrowid
conn.commit()
return seizure_id
def update_user_dni_info(self, user_id: int, nombre: str, apellido: str, edad: int, fecha_nacimiento: str, sexo: str, nacionalidad: str):
"""Actualiza la información personal del DNI de un usuario"""
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute('''
UPDATE users
SET nombre = ?, apellido = ?, edad = ?, fecha_nacimiento = ?, sexo = ?, nacionalidad = ?
WHERE user_id = ?
''', (nombre, apellido, edad, fecha_nacimiento, sexo, nacionalidad, user_id))
conn.commit()
def revoke_license(self, user_id: int, license_type: str):
"""Revoca una licencia de usuario"""
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute('''
UPDATE licenses
SET valid = FALSE
WHERE user_id = ? AND license_type = ?
''', (user_id, license_type))
conn.commit()