1
Fork 0
mirror of https://github.com/wlinator/luminara.git synced 2024-10-02 18:03:12 +00:00

Create /shop system

This commit is contained in:
wlinator 2023-07-02 07:30:47 -04:00
parent cb2b56ad74
commit 242804afbf
5 changed files with 208 additions and 19 deletions

72
data/ShopItem.py Normal file
View file

@ -0,0 +1,72 @@
from data.Item import Item
from db import database
class ShopItem:
def __init__(self, item: Item):
self.item = item
self.price = None
self.price_special = None
self.worth = None
self.description = None
self.fetch_or_create_shopitem()
"""
if either price, price_special or worth equal "0",
this will be interpreted as no-buy or no-sell.
"""
def set_price(self, price):
query = "UPDATE ShopItem SET price = ? WHERE item_id = ?"
database.execute_query(query, (price, self.item.id))
self.price = price
def set_price_special(self, price_special):
query = "UPDATE ShopItem SET price_special = ? WHERE item_id = ?"
database.execute_query(query, (price_special, self.item.id))
self.price_special = price_special
def set_worth(self, worth):
query = "UPDATE ShopItem SET worth = ? WHERE item_id = ?"
database.execute_query(query, (worth, self.item.id))
self.worth = worth
def set_description(self, description):
query = "UPDATE ShopItem SET description = ? WHERE item_id = ?"
database.execute_query(query, (description, self.item.id))
self.description = description
def fetch_or_create_shopitem(self):
query = """
SELECT price, price_special, worth, description
FROM ShopItem
WHERE item_id = ?
"""
try:
(price, price_special, worth, description) = database.select_query(query, (self.item.id,))[0]
except (IndexError, TypeError):
query = """
INSERT INTO ShopItem (item_id, price, price_special, worth, description)
VALUES (?, 0, 0, 0, "placeholder_descr")
"""
database.execute_query(query, (self.item.id,))
(price, price_special, worth, description) = 0, 0, 0, "placeholder_descr"
self.price = price
self.price_special = price_special
self.worth = worth
self.description = description
@staticmethod
def get_shop_all():
query = "SELECT item_id FROM ShopItem WHERE price != 0 OR price_special != 0;"
shop_items = database.select_query(query)
shop_items = [item[0] for item in shop_items]
list = []
for item_id in shop_items:
list.append(ShopItem(Item(item_id)))
return list

View file

@ -41,6 +41,16 @@ CREATE TABLE IF NOT EXISTS inventory (
)
"""
shop_item_table = """
CREATE TABLE IF NOT EXISTS ShopItem (
item_id INTEGER PRIMARY KEY,
price INTEGER,
price_special INTEGER,
worth INTEGER,
description TEXT
)
"""
dailies_table = """
CREATE TABLE IF NOT EXISTS dailies (
id INTEGER PRIMARY KEY,
@ -90,6 +100,7 @@ def sync_database():
database.execute_query(currency_table)
database.execute_query(item_table)
database.execute_query(inventory_table)
database.execute_query(shop_item_table)
database.execute_query(dailies_table)
database.execute_query(stats_bj)
database.execute_query(stats_slots)

64
modules/admin.py Normal file
View file

@ -0,0 +1,64 @@
import json
import os
import discord
from discord.ext import commands
from dotenv import load_dotenv
from data.Item import Item
from data.ShopItem import ShopItem
from sb_tools import universal
load_dotenv('.env')
active_blackjack_games = {}
special_balance_name = os.getenv("SPECIAL_BALANCE_NAME")
cash_balance_name = os.getenv("CASH_BALANCE_NAME")
with open("config/economy.json") as file:
json_data = json.load(file)
class AdminCog(commands.Cog):
def __init__(self, sbbot):
self.bot = sbbot
admin = discord.SlashCommandGroup(name="admin", description="Commands only Bot Admins can do.")
shop = admin.create_subgroup(name="shop", description="Shop managament")
@shop.command(
name="insert",
description="Insert a new item into the shop"
)
@commands.check(universal.owner_check)
async def admin_shop_insert(self, ctx, *,
item: discord.Option(choices=Item.get_all_item_names()),
price: discord.Option(int),
price_special: discord.Option(int),
worth: discord.Option(int),
description: discord.Option(str, max_length=60)):
item = Item.get_item_by_display_name(item)
shop_item = ShopItem(item)
shop_item.set_price(abs(price))
shop_item.set_price_special(abs(price_special))
shop_item.set_worth(abs(worth))
shop_item.set_description(description)
price = price if price != 0 else "N/A"
price_special = price_special if price_special != 0 else "N/A"
worth = worth if worth != 0 else "N/A"
embed = discord.Embed(
color=discord.Color.green(),
description=f"Added **{item.name}** to the shop for **${price}** or "
f"**{price_special} {special_balance_name}** "
f"It can be sold by users for **${worth}**"
)
embed.add_field(name="description", value=description)
await ctx.respond(embed=embed)
def setup(sbbot):
sbbot.add_cog(AdminCog(sbbot))

61
modules/shop.py Normal file
View file

@ -0,0 +1,61 @@
import json
import locale
import os
import discord
from discord.ext import commands
from dotenv import load_dotenv
from data.ShopItem import ShopItem
from sb_tools import universal
load_dotenv('.env')
special_balance_name = os.getenv("SPECIAL_BALANCE_NAME")
cash_balance_name = os.getenv("CASH_BALANCE_NAME")
with open("config/economy.json") as file:
json_data = json.load(file)
class ShopCog(commands.Cog):
def __init__(self, sbbot):
self.bot = sbbot
@commands.slash_command(
name="shop",
description="Display the shop.",
guild_only=True
)
@commands.check(universal.beta_check)
async def shop(self, ctx):
shop = ShopItem.get_shop_all()
embed = discord.Embed(
color=discord.Color.embed_background(),
title="Racu Shop",
description="The shop is updated regularly!"
)
embed.set_footer(text="do /buy <item>")
for item in shop:
emoji = self.bot.get_emoji(item.item.emote_id)
locale.setlocale(locale.LC_ALL, '')
if item.price != 0 and item.price_special != 0:
item.price = locale.format_string("%d", item.price, grouping=True)
price = f"${item.price} *or* {item.price_special} {special_balance_name}"
elif item.price == 0:
price = f"{item.price_special} {special_balance_name}"
else:
item.price = locale.format_string("%d", item.price, grouping=True)
price = f"${item.price}"
embed.add_field(name=f"{emoji} {item.item.display_name} - {price}",
value=f"\n*{item.description}*",
inline=False)
await ctx.respond(embed=embed)
def setup(sbbot):
sbbot.add_cog(ShopCog(sbbot))

View file

@ -179,25 +179,6 @@ class DuelChallenge(View):
else:
return True
class LocationOptions(discord.ui.View):
def __init__(self, ctx):
super().__init__(timeout=120)
self.ctx = ctx
self.location = None
async def on_timeout(self):
for child in self.children:
child.disabled = True
await self.message.edit(view=None)
async def interaction_check(self, interaction) -> bool:
if interaction.user != self.ctx.author:
await interaction.response.send_message("You can't use this menu, it's someone else's!", ephemeral=True)
return False
else:
return True
@discord.ui.select(
min_values=1,
max_values=1,