-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_weapon.py
57 lines (48 loc) · 1.84 KB
/
add_weapon.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
import pymysql
import redis
from mysqlcursor import cur, con
# Initialize Redis client
redis_client = redis.Redis(host='localhost', port=6379, db=0)
def option_3():
"This is to insert weapons information into the database"
'''take the following as input
Weapon_ID\n
Weapon_Name\n
Ammo\n
Fire_Rate\n
Damage\n
Extension_ID\n
'''
try:
Weapon_ID = input("Enter Weapon ID: ")
Weapon_Name = input("Enter Weapon Name: ")
Ammo = input("Enter Ammo: ")
Fire_Rate = input("Enter Fire Rate: ")
Damage = input("Enter Damage: ")
Extension_ID = input("Enter Extension ID: ")
# Check if weapon data exists in Redis cache
weapon_key = f"weapon:{Weapon_ID}"
if redis_client.exists(weapon_key):
print("Weapon data found in cache:")
weapon_data = redis_client.hgetall(weapon_key)
print({k.decode('utf-8'): v.decode('utf-8') for k, v in weapon_data.items()})
else:
query = "INSERT INTO Weapons(Weapon_ID, Weapon_Name, Ammo, Fire_Rate, Damage, Extension_ID) VALUES (%s, %s, %s, %s, %s, %s)"
cur.execute(query, (Weapon_ID, Weapon_Name, Ammo, Fire_Rate, Damage, Extension_ID))
print("Inserted into Weapons table")
# Cache weapon data in Redis
weapon_data = {
'Weapon_Name': Weapon_Name,
'Ammo': Ammo,
'Fire_Rate': Fire_Rate,
'Damage': Damage,
'Extension_ID': Extension_ID
}
redis_client.hmset(weapon_key, weapon_data)
print("Weapon data cached in Redis")
con.commit()
print("Inserted into database")
except Exception as e:
con.rollback()
print("Failed to insert into database")
print(">>>>>>>>>>>>>", e)