-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshop.py
More file actions
399 lines (338 loc) · 10.2 KB
/
shop.py
File metadata and controls
399 lines (338 loc) · 10.2 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
387
388
389
390
391
392
393
394
395
396
397
398
399
"""
Shop system module.
This module handles:
- Shop item listing
- Item purchases (coins and/or XP)
- Item effect application
- Inventory management
"""
import json
from datetime import datetime, timedelta
from typing import Optional
from database import get_db_connection
from db_helpers import (
get_user_balance,
spend_coins,
spend_xp,
)
def get_shop_items(
active_only: bool = True,
conn=None,
) -> list:
"""Get all shop items."""
should_close = conn is None
if conn is None:
conn = get_db_connection()
cursor = conn.cursor()
try:
if active_only:
cursor.execute(
"""
SELECT * FROM shop_items
WHERE active = 1 AND (stock = -1 OR stock > 0)
ORDER BY price_coins, price_xp
"""
)
else:
cursor.execute("SELECT * FROM shop_items ORDER BY price_coins, price_xp")
items = []
for row in cursor.fetchall():
item = dict(row)
# Parse metadata JSON
if item.get("metadata"):
try:
item["metadata"] = json.loads(item["metadata"])
except json.JSONDecodeError:
item["metadata"] = {}
items.append(item)
return items
finally:
if should_close:
conn.close()
def get_shop_item(item_id: int, conn=None) -> Optional[dict]:
"""Get a specific shop item by ID."""
should_close = conn is None
if conn is None:
conn = get_db_connection()
cursor = conn.cursor()
try:
cursor.execute("SELECT * FROM shop_items WHERE id = ?", (item_id,))
row = cursor.fetchone()
if not row:
return None
item = dict(row)
if item.get("metadata"):
try:
item["metadata"] = json.loads(item["metadata"])
except json.JSONDecodeError:
item["metadata"] = {}
return item
finally:
if should_close:
conn.close()
def buy_item(
guild_id: str,
user_id: str,
item_id: int,
quantity: int = 1,
conn=None,
) -> dict:
"""
Purchase an item from the shop.
Args:
guild_id: Guild ID
user_id: User ID
item_id: Shop item ID to purchase
quantity: Number to purchase (default 1)
Returns:
dict with purchase results
"""
if quantity < 1:
raise ValueError("Quantity must be at least 1")
should_close = conn is None
if conn is None:
conn = get_db_connection()
cursor = conn.cursor()
try:
# Get item
item = get_shop_item(item_id, conn)
if not item:
raise ValueError("Item not found")
if not item["active"]:
raise ValueError("Item is not available for purchase")
# Check stock
if item["stock"] != -1 and item["stock"] < quantity:
raise ValueError(
f"Not enough stock: only {item['stock']} available"
)
# Calculate total cost
total_coins = item["price_coins"] * quantity
total_xp = item["price_xp"] * quantity
# Get user balance
balance = get_user_balance(guild_id, user_id, conn)
# Check if user can afford it
if balance["coins"] < total_coins:
raise ValueError(
f"Not enough coins: have {balance['coins']}, need {total_coins}"
)
if total_xp > 0 and balance["xp"] < total_xp:
raise ValueError(
f"Not enough XP: have {balance['xp']}, need {total_xp}"
)
# Deduct costs
if total_coins > 0:
spend_coins(
guild_id,
user_id,
total_coins,
"shop_purchase",
related_id=item_id,
related_type="shop_item",
conn=conn,
)
xp_result = None
if total_xp > 0:
xp_result = spend_xp(
guild_id,
user_id,
total_xp,
"shop_purchase",
related_id=item_id,
related_type="shop_item",
conn=conn,
)
# Update stock if limited
if item["stock"] != -1:
cursor.execute(
"UPDATE shop_items SET stock = stock - ? WHERE id = ?",
(quantity, item_id),
)
# Add to inventory if consumable
if item["consumable"]:
cursor.execute(
"""
INSERT INTO user_inventory (guildId, userId, itemId, quantity)
VALUES (?, ?, ?, ?)
ON CONFLICT(guildId, userId, itemId)
DO UPDATE SET quantity = quantity + excluded.quantity
""",
(str(guild_id), str(user_id), item_id, quantity),
)
conn.commit()
return {
"item_name": item["name"],
"quantity": quantity,
"coins_spent": total_coins,
"xp_spent": total_xp,
"is_consumable": bool(item["consumable"]),
"level_down": xp_result.get("level_down", False) if xp_result else False,
"new_level": xp_result.get("new_level") if xp_result else None,
}
except Exception:
conn.rollback()
raise
finally:
if should_close:
conn.close()
def get_user_inventory(
guild_id: str,
user_id: str,
conn=None,
) -> list:
"""Get user's inventory of consumable items."""
should_close = conn is None
if conn is None:
conn = get_db_connection()
cursor = conn.cursor()
try:
cursor.execute(
"""
SELECT ui.*, si.name, si.description, si.metadata
FROM user_inventory ui
JOIN shop_items si ON ui.itemId = si.id
WHERE ui.guildId = ? AND ui.userId = ? AND ui.quantity > 0
ORDER BY ui.acquired_at DESC
""",
(str(guild_id), str(user_id)),
)
items = []
for row in cursor.fetchall():
item = dict(row)
if item.get("metadata"):
try:
item["metadata"] = json.loads(item["metadata"])
except json.JSONDecodeError:
item["metadata"] = {}
items.append(item)
return items
finally:
if should_close:
conn.close()
def use_item(
guild_id: str,
user_id: str,
item_id: int,
conn=None,
) -> dict:
"""
Use a consumable item from inventory.
Args:
guild_id: Guild ID
user_id: User ID
item_id: Shop item ID to use
Returns:
dict with item effect details
"""
should_close = conn is None
if conn is None:
conn = get_db_connection()
cursor = conn.cursor()
try:
# Check if user has the item
cursor.execute(
"""
SELECT ui.quantity, si.name, si.metadata
FROM user_inventory ui
JOIN shop_items si ON ui.itemId = si.id
WHERE ui.guildId = ? AND ui.userId = ? AND ui.itemId = ?
""",
(str(guild_id), str(user_id), item_id),
)
row = cursor.fetchone()
if not row or row[0] < 1:
raise ValueError("You don't have this item in your inventory")
item_name = row[1]
metadata = {}
if row[2]:
try:
metadata = json.loads(row[2])
except json.JSONDecodeError:
pass
# Decrement quantity
cursor.execute(
"""
UPDATE user_inventory
SET quantity = quantity - 1
WHERE guildId = ? AND userId = ? AND itemId = ?
""",
(str(guild_id), str(user_id), item_id),
)
# Store active effect
effect = metadata.get("effect")
if effect:
duration = metadata.get("duration_minutes", 60)
expires_at = datetime.utcnow() + timedelta(minutes=duration)
cursor.execute(
"""
INSERT INTO user_active_effects (
guildId, userId, effect_type, effect_data, expires_at
) VALUES (?, ?, ?, ?, ?)
ON CONFLICT(guildId, userId, effect_type)
DO UPDATE SET effect_data = excluded.effect_data,
expires_at = excluded.expires_at
""",
(
str(guild_id),
str(user_id),
effect,
json.dumps(metadata),
expires_at.isoformat(),
),
)
conn.commit()
return {
"item_name": item_name,
"effect": effect,
"metadata": metadata,
"message": f"Used {item_name}!",
}
except Exception:
conn.rollback()
raise
finally:
if should_close:
conn.close()
def get_active_effects(
guild_id: str,
user_id: str,
conn=None,
) -> list:
"""Get user's currently active effects."""
should_close = conn is None
if conn is None:
conn = get_db_connection()
cursor = conn.cursor()
try:
now = datetime.utcnow().isoformat()
cursor.execute(
"""
SELECT effect_type, effect_data, expires_at
FROM user_active_effects
WHERE guildId = ? AND userId = ? AND expires_at > ?
""",
(str(guild_id), str(user_id), now),
)
effects = []
for row in cursor.fetchall():
effect = {
"effect_type": row[0],
"effect_data": json.loads(row[1]) if row[1] else {},
"expires_at": row[2],
}
effects.append(effect)
return effects
finally:
if should_close:
conn.close()
def has_active_effect(
guild_id: str,
user_id: str,
effect_type: str,
conn=None,
) -> Optional[dict]:
"""Check if user has a specific active effect."""
effects = get_active_effects(guild_id, user_id, conn)
for effect in effects:
if effect["effect_type"] == effect_type:
return effect
return None