2023-07-11 16:07:05 +00:00
|
|
|
import logging
|
|
|
|
|
2023-06-22 18:11:48 +00:00
|
|
|
from db import database
|
2024-03-29 17:17:51 +00:00
|
|
|
from services import Item
|
2023-06-22 18:11:48 +00:00
|
|
|
|
2024-02-28 13:01:20 +00:00
|
|
|
logs = logging.getLogger('Racu.Core')
|
2023-07-11 16:07:05 +00:00
|
|
|
|
2023-06-22 18:11:48 +00:00
|
|
|
|
|
|
|
class Inventory:
|
|
|
|
def __init__(self, user_id):
|
|
|
|
self.user_id = user_id
|
|
|
|
|
|
|
|
def add_item(self, item: Item.Item, quantity=1):
|
|
|
|
"""
|
|
|
|
Adds an item with a specific count (default 1) to the database, if there are
|
|
|
|
no records of this user having that item yet, it will just add a record with quantity=quantity.
|
|
|
|
:param item:
|
|
|
|
:param quantity:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
|
|
|
query = """
|
2023-10-24 17:04:54 +00:00
|
|
|
INSERT INTO inventory (user_id, item_id, quantity)
|
|
|
|
VALUES (%s, %s, %s)
|
|
|
|
ON DUPLICATE KEY UPDATE quantity = quantity + %s;
|
2023-06-22 18:11:48 +00:00
|
|
|
"""
|
|
|
|
|
2023-10-24 17:04:54 +00:00
|
|
|
database.execute_query(query, (self.user_id, item.id, abs(quantity), abs(quantity)))
|
2023-06-22 18:11:48 +00:00
|
|
|
|
2023-07-11 16:49:20 +00:00
|
|
|
def take_item(self, item: Item.Item, quantity=1):
|
2023-10-24 17:04:54 +00:00
|
|
|
|
2023-07-11 16:49:20 +00:00
|
|
|
query = """
|
2023-10-24 17:04:54 +00:00
|
|
|
INSERT INTO inventory (user_id, item_id, quantity)
|
|
|
|
VALUES (%s, %s, 0)
|
|
|
|
ON DUPLICATE KEY UPDATE quantity = CASE
|
|
|
|
WHEN quantity - %s < 0 THEN 0
|
|
|
|
ELSE quantity - %s
|
|
|
|
END;
|
|
|
|
"""
|
2023-07-11 16:49:20 +00:00
|
|
|
|
|
|
|
database.execute_query(query, (self.user_id, item.id, self.user_id, item.id, abs(quantity)))
|
|
|
|
|
2023-06-22 18:11:48 +00:00
|
|
|
def get_inventory(self):
|
2023-10-23 12:49:46 +00:00
|
|
|
query = "SELECT item_id, quantity FROM inventory WHERE user_id = %s AND quantity > 0"
|
2023-06-22 18:11:48 +00:00
|
|
|
results = database.select_query(query, (self.user_id,))
|
|
|
|
|
|
|
|
items_dict = {}
|
|
|
|
for row in results:
|
|
|
|
item_id, quantity = row
|
|
|
|
item = Item.Item(item_id)
|
|
|
|
items_dict[item] = quantity
|
|
|
|
|
|
|
|
return items_dict
|
|
|
|
|
|
|
|
def get_item_quantity(self, item: Item.Item):
|
2023-10-23 12:49:46 +00:00
|
|
|
query = "SELECT COALESCE(quantity, 0) FROM inventory WHERE user_id = %s AND item_id = %s"
|
2023-06-28 13:28:02 +00:00
|
|
|
result = database.select_query_one(query, (self.user_id, item.id))
|
|
|
|
return result
|
2023-07-11 16:07:05 +00:00
|
|
|
|
|
|
|
def get_sell_data(self):
|
|
|
|
query = """
|
|
|
|
SELECT item.display_name
|
|
|
|
FROM inventory
|
|
|
|
JOIN ShopItem ON inventory.item_id = ShopItem.item_id
|
|
|
|
JOIN item ON inventory.item_id = item.id
|
2023-10-23 12:49:46 +00:00
|
|
|
WHERE inventory.user_id = %s AND inventory.quantity > 0 AND ShopItem.worth > 0
|
2023-07-11 16:07:05 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
item_names = []
|
|
|
|
results = database.select_query(query, (self.user_id,))
|
|
|
|
|
|
|
|
for item in results:
|
|
|
|
item_names.append(item[0])
|
|
|
|
|
|
|
|
return item_names
|
|
|
|
|
|
|
|
except Exception as e:
|
2024-02-28 13:01:20 +00:00
|
|
|
logs.error(e)
|
2023-07-11 16:07:05 +00:00
|
|
|
return []
|