From 91d6b33d3a91131cb5bbc1561edc7286e721f407 Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Sun, 19 Feb 2023 13:24:42 +0700 Subject: [PATCH] Got the inventory stuff working, button disables itself when you purchase something --- data.sql | 22 ++++++++++----- mm.py | 78 ++++++++++++++++++++++++++++++++++++++++++------------ tables.sql | 28 ++++++++------------ 3 files changed, 87 insertions(+), 41 deletions(-) diff --git a/data.sql b/data.sql index c8d5a5b..24b19fa 100644 --- a/data.sql +++ b/data.sql @@ -20,16 +20,24 @@ INSERT INTO bank_account(user_id, resource_id, balance) VALUES (1, 4, 25), (1, 5, 10); -INSERT INTO store_item(name, target_resource, price) VALUES - ('Drill 1A', 1, 100), - ('Drill 1B', 2, 100), - ('Drill 1C', 3, 100), - ('Drill 1D', 4, 100), - ('Drill 1E', 5, 100); - +INSERT INTO store_item(name, currency, target_resource, price, claim_amount) VALUES + ('Drill 1A', 1, 1, 100, 500), + ('Drill 1B', 2, 2, 100, 500), + ('Drill 1C', 3, 3, 100, 500), + ('Drill 1D', 4, 4, 100, 500), + ('Drill 1E', 5, 5, 100, 500); SELECT name,init_supply FROM resource_well INNER JOIN resource ON resource.id = resource_well.resource_id 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; diff --git a/mm.py b/mm.py index 5285fa7..6303d81 100644 --- a/mm.py +++ b/mm.py @@ -10,8 +10,6 @@ conn = sql.connect("mm.db", check_same_thread=False) current_user = "Joe" current_user_id = 1 -staking_sources = {} - cursor = conn.cursor() cursor.execute("SELECT id,name FROM resource") @@ -26,16 +24,16 @@ WHERE user_id = ? """, (current_user_id,)) bank = {name: balance for name,balance in cursor.fetchall()} -# cursor.execute(""" -# SELECT store_item.name,price,currency FROM bank_account -# INNER JOIN resource ON resource.id = bank_account.resource_id -# """) -# store = {name: balance for name,price,currency in cursor.fetchall()} +cursor.execute(""" +SELECT store_item.id,store_item.name,resource.name,price,claim_amount FROM store_item +INNER JOIN resource ON resource.id = currency +""") +store = {item[0]:item[1:] for item in cursor.fetchall()} cursor.close() def get_moons(): - staking_sources.clear() + staking_sources = {} cursor = conn.cursor() 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()} staking_sources[sid] = (ts,wells) 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(): cursor = conn.cursor() @@ -84,6 +101,16 @@ def mint(): cursor.close() 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): source_id,well_name = user_data @@ -110,14 +137,25 @@ def destroy(source_id): cursor.close() -get_moons() - -sg.set_options(font=("Fira Code", 15)) - banks = "" for name,amount in bank.items(): 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 = [] for id,source in staking_sources.items(): wells_ui = [] @@ -131,24 +169,30 @@ for id,source in staking_sources.items(): layout = [ [sg.Text(f"User: {current_user}")], [sg.Text(banks, key='-BANKS-')], - [sg.Button("Mint Moon")], + [sg.Button(button_text="Mint Moon", key="-MINT-")], [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: event, values = window.read() if event == sg.WINDOW_CLOSED: break - if event == "Mint Moon": - mint() + elif event.startswith("-BUY-"): + id = event.split("-")[2] + buy_item(id) + window[f"-BUY-{id}-"].update(disabled=True) elif event.startswith("Upgrade-"): print("Upgrade") pass elif event.startswith("Destroy"): destroy(event.split("-")[1]) + elif event == "-MINT-": + window['items'].update([[sg.Text("New Text")]]) elif event.startswith("Mine-"): print("Mine") pass diff --git a/tables.sql b/tables.sql index 99e54c9..ea1877e 100644 --- a/tables.sql +++ b/tables.sql @@ -21,7 +21,7 @@ CREATE TABLE resource_well( id integer primary key autoincrement, source_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) REFERENCES resource(id), CONSTRAINT fk_sid FOREIGN KEY(source_id) @@ -34,6 +34,7 @@ CREATE TABLE staking_event( well_id int not null, source_id int not null, inventory_item_id int not null, + amount int not null, created_at timestamp DEFAULT (current_timestamp), CONSTRAINT fk_sid FOREIGN KEY(source_id) REFERENCES staking_source(id) @@ -43,14 +44,6 @@ CREATE TABLE staking_event( 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( id integer primary key autoincrement, resource_id int not null, @@ -61,19 +54,21 @@ CREATE TABLE claim_event( REFERENCES staking_source(id) ); -CREATE TABLE game_config( +CREATE TABLE upgrade_event( id integer primary key autoincrement, - key text not null, - value text not null, + inventory_item_id int 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( id integer primary key autoincrement, name varchar(128) not null, currency int not null, + price int not null, target_resource int not null, + claim_amount int not null, CONSTRAINT fk_rid FOREIGN KEY(currency) REFERENCES resource(id) CONSTRAINT fk_targetid FOREIGN KEY(target_resource) @@ -83,12 +78,11 @@ CREATE TABLE store_item( CREATE TABLE inventory_item( id integer primary key autoincrement, user_id int not null, - store_id int not null, - name varchar(128) not null, + store_item_id int not null, created_at timestamp DEFAULT (current_timestamp), CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(id) - CONSTRAINT fk_storeid FOREIGN KEY(store_id) + CONSTRAINT fk_sitemid FOREIGN KEY(store_item_id) REFERENCES store_item(id) );