-
Notifications
You must be signed in to change notification settings - Fork 0
/
show_weapon_kills.py
50 lines (43 loc) · 1.56 KB
/
show_weapon_kills.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
import pymysql
import redis
from mysqlcursor import cur, con
# Initialize Redis client
redis_client = redis.Redis(host='localhost', port=6379, db=0)
def option_19():
"This is to output the Weapon with Most Kills"
try:
# Check if the result is cached in Redis
cache_key = "weapon_with_most_kills"
if redis_client.exists(cache_key):
print("Data found in cache:")
cached_data = redis_client.hgetall(cache_key)
for key, value in cached_data.items():
print(value.decode('utf-8'))
else:
query = """
SELECT *
FROM Weapons
WHERE Weapon_ID IN (
SELECT Weapon_ID
FROM Kills
GROUP BY Weapon_ID
ORDER BY COUNT(Weapon_ID) DESC
LIMIT 1
)
"""
cur.execute(query)
print("Weapon_ID\tWeapon_Name\tAmmo\tFire_Rate\tDamage\tExtension_ID")
for row in cur:
weapon_info = f"{row['Weapon_ID']}\t{row['Weapon_Name']}\t{row['Ammo']}\t{row['Fire_Rate']}\t{row['Damage']}\t{row['Extension_ID']}"
print(weapon_info)
# Cache the result in Redis
if row:
redis_client.hmset(cache_key, {"result": weapon_info})
print("Data cached in Redis")
else:
print("No weapons found")
con.commit()
except Exception as e:
con.rollback()
print("Failed to fetch from database")
print(">>>>>>>>>>>>>", e)