Spaces:
Building
Building
Update petroll.py
Browse files- petroll.py +35 -6
petroll.py
CHANGED
@@ -3,6 +3,7 @@ from discord import app_commands
|
|
3 |
import aiohttp
|
4 |
import random
|
5 |
import time
|
|
|
6 |
|
7 |
from cash import user_cash
|
8 |
|
@@ -12,7 +13,18 @@ luck_opportunities = {}
|
|
12 |
used_luck_opportunities = set()
|
13 |
first_luck_claimed = set()
|
14 |
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
async def fetch_data(url):
|
17 |
async with aiohttp.ClientSession() as session:
|
18 |
async with session.get(url) as response:
|
@@ -20,8 +32,10 @@ async def perform_roll(interaction: discord.Interaction):
|
|
20 |
return await response.json()
|
21 |
return None
|
22 |
|
23 |
-
rap_data = await
|
24 |
-
|
|
|
|
|
25 |
|
26 |
if not rap_data or not collection_data:
|
27 |
return None
|
@@ -31,15 +45,30 @@ async def perform_roll(interaction: discord.Interaction):
|
|
31 |
if not pets:
|
32 |
return None
|
33 |
|
34 |
-
user_id = interaction.user.id
|
35 |
-
luck_multiplier = luck_multipliers.get(user_id, 1)
|
36 |
-
|
37 |
# Sort pets by difficulty (rarity)
|
38 |
sorted_pets = sorted(pets, key=lambda x: x['configData']['difficulty'])
|
39 |
|
40 |
# Divide pets into tiers
|
41 |
tier_size = len(sorted_pets) // 5
|
42 |
tiers = [sorted_pets[i:i+tier_size] for i in range(0, len(sorted_pets), tier_size)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
# Adjust probabilities based on luck
|
45 |
tier_probabilities = [0.5, 0.25, 0.15, 0.07, 0.03]
|
|
|
3 |
import aiohttp
|
4 |
import random
|
5 |
import time
|
6 |
+
import asyncio
|
7 |
|
8 |
from cash import user_cash
|
9 |
|
|
|
13 |
used_luck_opportunities = set()
|
14 |
first_luck_claimed = set()
|
15 |
|
16 |
+
# Cache for pet data
|
17 |
+
pet_cache = None
|
18 |
+
pet_cache_expiry = 0
|
19 |
+
CACHE_DURATION = 300 # 5 minutes
|
20 |
+
|
21 |
+
async def fetch_and_process_pets():
|
22 |
+
global pet_cache, pet_cache_expiry
|
23 |
+
|
24 |
+
current_time = time.time()
|
25 |
+
if pet_cache and current_time < pet_cache_expiry:
|
26 |
+
return pet_cache
|
27 |
+
|
28 |
async def fetch_data(url):
|
29 |
async with aiohttp.ClientSession() as session:
|
30 |
async with session.get(url) as response:
|
|
|
32 |
return await response.json()
|
33 |
return None
|
34 |
|
35 |
+
rap_data, collection_data = await asyncio.gather(
|
36 |
+
fetch_data("https://petsgo.biggamesapi.io/api/Rap"),
|
37 |
+
fetch_data("https://petsgo.biggamesapi.io/api/collection/Pets")
|
38 |
+
)
|
39 |
|
40 |
if not rap_data or not collection_data:
|
41 |
return None
|
|
|
45 |
if not pets:
|
46 |
return None
|
47 |
|
|
|
|
|
|
|
48 |
# Sort pets by difficulty (rarity)
|
49 |
sorted_pets = sorted(pets, key=lambda x: x['configData']['difficulty'])
|
50 |
|
51 |
# Divide pets into tiers
|
52 |
tier_size = len(sorted_pets) // 5
|
53 |
tiers = [sorted_pets[i:i+tier_size] for i in range(0, len(sorted_pets), tier_size)]
|
54 |
+
|
55 |
+
pet_cache = {
|
56 |
+
'tiers': tiers,
|
57 |
+
'rap_data': rap_data
|
58 |
+
}
|
59 |
+
pet_cache_expiry = current_time + CACHE_DURATION
|
60 |
+
return pet_cache
|
61 |
+
|
62 |
+
async def perform_roll(interaction: discord.Interaction):
|
63 |
+
pet_data = await fetch_and_process_pets()
|
64 |
+
if not pet_data:
|
65 |
+
return None
|
66 |
+
|
67 |
+
tiers = pet_data['tiers']
|
68 |
+
rap_data = pet_data['rap_data']
|
69 |
+
|
70 |
+
user_id = interaction.user.id
|
71 |
+
luck_multiplier = luck_multipliers.get(user_id, 1)
|
72 |
|
73 |
# Adjust probabilities based on luck
|
74 |
tier_probabilities = [0.5, 0.25, 0.15, 0.07, 0.03]
|