Got the inventory stuff working, button disables itself when you purchase something

This commit is contained in:
Joseph Ferano 2023-02-19 13:24:42 +07:00
parent 286be600f5
commit 91d6b33d3a
3 changed files with 87 additions and 41 deletions

View File

@ -20,16 +20,24 @@ INSERT INTO bank_account(user_id, resource_id, balance) VALUES
(1, 4, 25), (1, 4, 25),
(1, 5, 10); (1, 5, 10);
INSERT INTO store_item(name, target_resource, price) VALUES INSERT INTO store_item(name, currency, target_resource, price, claim_amount) VALUES
('Drill 1A', 1, 100), ('Drill 1A', 1, 1, 100, 500),
('Drill 1B', 2, 100), ('Drill 1B', 2, 2, 100, 500),
('Drill 1C', 3, 100), ('Drill 1C', 3, 3, 100, 500),
('Drill 1D', 4, 100), ('Drill 1D', 4, 4, 100, 500),
('Drill 1E', 5, 100); ('Drill 1E', 5, 5, 100, 500);
SELECT name,init_supply SELECT name,init_supply
FROM resource_well FROM resource_well
INNER JOIN resource ON resource.id = resource_well.resource_id INNER JOIN resource ON resource.id = resource_well.resource_id
WHERE source_id = 1; WHERE source_id = 1;
SELECT inventory_item.id,store_item_id, COUNT(upgrade_event.id) as upgrades
FROM inventory_item
LEFT JOIN upgrade_event ON inventory_item.id = upgrade_event.inventory_item_id
WHERE inventory_item.user_id = 1
GROUP BY inventory_item.id;
SELECT inventory_item.id,store_item_id
FROM inventory_item;

78
mm.py
View File

@ -10,8 +10,6 @@ conn = sql.connect("mm.db", check_same_thread=False)
current_user = "Joe" current_user = "Joe"
current_user_id = 1 current_user_id = 1
staking_sources = {}
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute("SELECT id,name FROM resource") cursor.execute("SELECT id,name FROM resource")
@ -26,16 +24,16 @@ WHERE user_id = ?
""", (current_user_id,)) """, (current_user_id,))
bank = {name: balance for name,balance in cursor.fetchall()} bank = {name: balance for name,balance in cursor.fetchall()}
# cursor.execute(""" cursor.execute("""
# SELECT store_item.name,price,currency FROM bank_account SELECT store_item.id,store_item.name,resource.name,price,claim_amount FROM store_item
# INNER JOIN resource ON resource.id = bank_account.resource_id INNER JOIN resource ON resource.id = currency
# """) """)
# store = {name: balance for name,price,currency in cursor.fetchall()} store = {item[0]:item[1:] for item in cursor.fetchall()}
cursor.close() cursor.close()
def get_moons(): def get_moons():
staking_sources.clear() staking_sources = {}
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute("SELECT id,created_at FROM staking_source") cursor.execute("SELECT id,created_at FROM staking_source")
@ -51,6 +49,25 @@ def get_moons():
wells = {name: (supply,timestamp) for name,supply,timestamp in cursor.fetchall()} wells = {name: (supply,timestamp) for name,supply,timestamp in cursor.fetchall()}
staking_sources[sid] = (ts,wells) staking_sources[sid] = (ts,wells)
cursor.close() cursor.close()
return staking_sources
def get_inventory():
inventory = {}
cursor = conn.cursor()
cursor.execute("""
SELECT inventory_item.id,store_item_id, COUNT(upgrade_event.id) as upgrades
FROM inventory_item
LEFT JOIN upgrade_event ON inventory_item.id = upgrade_event.inventory_item_id
WHERE inventory_item.user_id = ?
GROUP BY inventory_item.id;
""", (current_user_id,))
fall = cursor.fetchall()
inventory = {item[0]:item[1:] for item in fall}
cursor.close()
return inventory
def mint(): def mint():
cursor = conn.cursor() cursor = conn.cursor()
@ -84,6 +101,16 @@ def mint():
cursor.close() cursor.close()
get_moons() get_moons()
def buy_item(item_id):
cursor = conn.cursor()
cursor.execute("""
INSERT INTO inventory_item (user_id, store_item_id)
VALUES (?, ?)
""", (current_user_id, item_id))
conn.commit()
cursor.close()
def mine(a,b,user_data): def mine(a,b,user_data):
source_id,well_name = user_data source_id,well_name = user_data
@ -110,14 +137,25 @@ def destroy(source_id):
cursor.close() cursor.close()
get_moons()
sg.set_options(font=("Fira Code", 15))
banks = "" banks = ""
for name,amount in bank.items(): for name,amount in bank.items():
banks += f"{name.capitalize()}: {amount} | " banks += f"{name.capitalize()}: {amount} | "
def get_store_ui():
inventory = get_inventory()
store_ui = []
for id,(name,resource,price,claim) in store.items():
owned = False
for (store_item_id,_) in inventory.values():
if id == store_item_id:
owned = True
store_ui.append([sg.Text(f"{name}: Mine {claim} {resource.capitalize()}"),
sg.Button(button_text=f"Buy {price} {resource[0:3]}",
key=f"-BUY-{id}-",
disabled=owned)])
return store_ui
staking_sources = get_moons()
sources_ui = [] sources_ui = []
for id,source in staking_sources.items(): for id,source in staking_sources.items():
wells_ui = [] wells_ui = []
@ -131,24 +169,30 @@ for id,source in staking_sources.items():
layout = [ layout = [
[sg.Text(f"User: {current_user}")], [sg.Text(f"User: {current_user}")],
[sg.Text(banks, key='-BANKS-')], [sg.Text(banks, key='-BANKS-')],
[sg.Button("Mint Moon")], [sg.Button(button_text="Mint Moon", key="-MINT-")],
[sg.HorizontalSeparator()], [sg.HorizontalSeparator()],
[sg.Column(layout=sources_ui, size=(1200, 800), scrollable=True, vertical_scroll_only=True)] [sg.Column(get_store_ui(), vertical_alignment='t')],
[sg.HorizontalSeparator()],
[sg.Column(layout=sources_ui, size=(1200, 500), scrollable=True, vertical_scroll_only=True)]
] ]
window = sg.Window("Moon Miner", layout) window = sg.Window("Moon Miner", layout, font='25')
while True: while True:
event, values = window.read() event, values = window.read()
if event == sg.WINDOW_CLOSED: if event == sg.WINDOW_CLOSED:
break break
if event == "Mint Moon": elif event.startswith("-BUY-"):
mint() id = event.split("-")[2]
buy_item(id)
window[f"-BUY-{id}-"].update(disabled=True)
elif event.startswith("Upgrade-"): elif event.startswith("Upgrade-"):
print("Upgrade") print("Upgrade")
pass pass
elif event.startswith("Destroy"): elif event.startswith("Destroy"):
destroy(event.split("-")[1]) destroy(event.split("-")[1])
elif event == "-MINT-":
window['items'].update([[sg.Text("New Text")]])
elif event.startswith("Mine-"): elif event.startswith("Mine-"):
print("Mine") print("Mine")
pass pass

View File

@ -21,7 +21,7 @@ CREATE TABLE resource_well(
id integer primary key autoincrement, id integer primary key autoincrement,
source_id int not null, source_id int not null,
resource_id int not null, resource_id int not null,
init_supply int not null, supply int not null,
CONSTRAINT fk_rid FOREIGN KEY(resource_id) CONSTRAINT fk_rid FOREIGN KEY(resource_id)
REFERENCES resource(id), REFERENCES resource(id),
CONSTRAINT fk_sid FOREIGN KEY(source_id) CONSTRAINT fk_sid FOREIGN KEY(source_id)
@ -34,6 +34,7 @@ CREATE TABLE staking_event(
well_id int not null, well_id int not null,
source_id int not null, source_id int not null,
inventory_item_id int not null, inventory_item_id int not null,
amount int not null,
created_at timestamp DEFAULT (current_timestamp), created_at timestamp DEFAULT (current_timestamp),
CONSTRAINT fk_sid FOREIGN KEY(source_id) CONSTRAINT fk_sid FOREIGN KEY(source_id)
REFERENCES staking_source(id) REFERENCES staking_source(id)
@ -43,14 +44,6 @@ CREATE TABLE staking_event(
REFERENCES inventory_item(id) REFERENCES inventory_item(id)
); );
CREATE TABLE upgrade_event(
id integer primary key autoincrement,
staking_event_id int not null,
created_at timestamp DEFAULT (current_timestamp),
CONSTRAINT fk_mevent FOREIGN KEY(staking_event_id)
REFERENCES staking_event(id)
);
CREATE TABLE claim_event( CREATE TABLE claim_event(
id integer primary key autoincrement, id integer primary key autoincrement,
resource_id int not null, resource_id int not null,
@ -61,19 +54,21 @@ CREATE TABLE claim_event(
REFERENCES staking_source(id) REFERENCES staking_source(id)
); );
CREATE TABLE game_config( CREATE TABLE upgrade_event(
id integer primary key autoincrement, id integer primary key autoincrement,
key text not null, inventory_item_id int not null,
value text not null, created_at timestamp DEFAULT (current_timestamp),
CONSTRAINT fk_iid FOREIGN KEY(inventory_item_id)
REFERENCES inventory_item(id)
); );
"Another Drill Price": 250 Sollux
CREATE TABLE store_item( CREATE TABLE store_item(
id integer primary key autoincrement, id integer primary key autoincrement,
name varchar(128) not null, name varchar(128) not null,
currency int not null, currency int not null,
price int not null,
target_resource int not null, target_resource int not null,
claim_amount int not null,
CONSTRAINT fk_rid FOREIGN KEY(currency) CONSTRAINT fk_rid FOREIGN KEY(currency)
REFERENCES resource(id) REFERENCES resource(id)
CONSTRAINT fk_targetid FOREIGN KEY(target_resource) CONSTRAINT fk_targetid FOREIGN KEY(target_resource)
@ -83,12 +78,11 @@ CREATE TABLE store_item(
CREATE TABLE inventory_item( CREATE TABLE inventory_item(
id integer primary key autoincrement, id integer primary key autoincrement,
user_id int not null, user_id int not null,
store_id int not null, store_item_id int not null,
name varchar(128) not null,
created_at timestamp DEFAULT (current_timestamp), created_at timestamp DEFAULT (current_timestamp),
CONSTRAINT fk_user FOREIGN KEY(user_id) CONSTRAINT fk_user FOREIGN KEY(user_id)
REFERENCES users(id) REFERENCES users(id)
CONSTRAINT fk_storeid FOREIGN KEY(store_id) CONSTRAINT fk_sitemid FOREIGN KEY(store_item_id)
REFERENCES store_item(id) REFERENCES store_item(id)
); );