-
Notifications
You must be signed in to change notification settings - Fork 0
/
show_player_teamup.py
64 lines (53 loc) · 2.01 KB
/
show_player_teamup.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import pymysql
import redis
from mysqlcursor import cur, con
# Initialize Redis client
redis_client = redis.Redis(host='localhost', port=6379, db=0)
def option_18():
"This is to show List of Player(s) a player has Team-UP with"
'''Take the following as input
Player_ID\n
'''
try:
row = {}
Player_ID = input("Enter Player ID: ")
cache_key = f"teammates_for_player:{Player_ID}"
# Check if the result is cached in Redis
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 Name, Team.Team_ID
FROM Player
JOIN Team
ON Player.Player_ID = Team.Player_ID1
OR Player.Player_ID = Team.Player_ID2
OR Player.Player_ID = Team.Player_ID3
OR Player.Player_ID = Team.Player_ID4
WHERE Team.Player_ID1 = %s
OR Team.Player_ID2 = %s
OR Team.Player_ID3 = %s
OR Team.Player_ID4 = %s
"""
cur.execute(query, (Player_ID, Player_ID, Player_ID, Player_ID))
teammates = cur.fetchall()
if teammates:
print("Name\t Team_ID\t")
teammates_data = {}
for idx, row in enumerate(teammates):
teammate_str = f"{row['Name']}\t{row['Team_ID']}"
print(teammate_str)
teammates_data[f"teammate_{idx}"] = teammate_str
# Cache the result in Redis
redis_client.hmset(cache_key, teammates_data)
print("Data cached in Redis")
else:
print("No teammates found for the given player")
con.commit()
except Exception as e:
con.rollback()
print("Failed to fetch from database")
print(">>>>>>>>>>>>>", e)