diff --git a/data.sql b/data.sql index 24b19fa..d979ed9 100644 --- a/data.sql +++ b/data.sql @@ -7,7 +7,7 @@ INSERT INTO resource(name) VALUES INSERT INTO users(name) VALUES ('Joe'), - ('Emile'), + ('Emil'), ('Niko'), ('Plug'), ('Upgrade'); @@ -28,6 +28,7 @@ INSERT INTO store_item(name, currency, target_resource, price, claim_amount) VAL ('Drill 1E', 5, 5, 100, 500); + SELECT name,init_supply FROM resource_well INNER JOIN resource ON resource.id = resource_well.resource_id diff --git a/mm.py b/mm.py index 6303d81..fac078d 100644 --- a/mm.py +++ b/mm.py @@ -6,6 +6,8 @@ import datetime conn = sql.connect("mm.db", check_same_thread=False) +conn.execute('PRAGMA foreign_keys = ON') + current_user = "Joe" current_user_id = 1 @@ -40,7 +42,7 @@ def get_moons(): for i,(sid,ts) in enumerate(cursor.fetchall()): cursor.execute(""" - SELECT name,init_supply,staking_event.created_at + SELECT name,supply,staking_event.created_at FROM resource_well INNER JOIN resource ON resource.id = resource_well.resource_id LEFT JOIN staking_event ON staking_event.well_id = resource_well.id @@ -71,26 +73,19 @@ def get_inventory(): def mint(): cursor = conn.cursor() - cursor.execute("SELECT id FROM users WHERE name = ?", (current_user,)) - result = cursor.fetchone() - if result and result[0]: - user_id = result[0] - else: - raise Exception("User not found: " + user_name) - rand_hash = "%010x" % random.randrange(16 ** 16) cursor.execute('BEGIN') try: cursor.execute(""" INSERT INTO staking_source (user_id, address) VALUES (?, ?) - """, (user_id, f"0x{rand_hash}")) + """, (current_user_id, f"0x{rand_hash}")) source_id = cursor.lastrowid for id,_ in resources: init_supply = random.randint(50, 200) cursor.execute(""" - INSERT INTO resource_well (source_id, resource_id, init_supply) VALUES (?, ?, ?) + INSERT INTO resource_well (source_id, resource_id, supply) VALUES (?, ?, ?) """, (source_id, id, init_supply)) conn.commit() @@ -136,10 +131,16 @@ def destroy(source_id): conn.commit() cursor.close() +def sell_all(): + cursor = conn.cursor() -banks = "" -for name,amount in bank.items(): - banks += f"{name.capitalize()}: {amount} | " + cursor.execute("DELETE FROM inventory_item WHERE user_id = ?", (current_user_id,)) + + conn.commit() + cursor.close() + +bank_txts = [ f"{name.capitalize()}: {amount}" for name,amount in bank.items() ] +banks = " | ".join(bank_txts) def get_store_ui(): inventory = get_inventory() @@ -150,30 +151,49 @@ def get_store_ui(): 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}-", + sg.Button(f"Buy {price} {resource[0:3]}", + key=("-BUY-",id), disabled=owned)]) return store_ui -staking_sources = get_moons() -sources_ui = [] -for id,source in staking_sources.items(): - wells_ui = [] - for name,(supply,ts) in source[1].items(): - wells_ui.append(sg.Text(name)) - sources_ui.append([sg.Image("moon.png"), - sg.Column(layout=[wells_ui]), - sg.Button("Upgrade"), - sg.Button(f"Destroy-{id}")]) +def inventory_row(id, si_id, tier): + name = store[si_id][0] + row = [sg.Text(f"{name} - Tier {tier+1}"), + sg.Button("Upgrade", key=("-UPGRADE-",id)), + sg.Button("Sell", key=("-SELL-",id))] + return [sg.pin(sg.Column([row], key=("-IROW-",id)))] + +def get_inventory_ui(): + inventory_ui = [] + inventory = get_inventory() + for id,(si_id,tier) in inventory.items(): + inventory_ui.append(inventory_row(id,si_id,tier)) + return inventory_ui + +def get_sources_ui(): + staking_sources = get_moons() + sources_ui = [] + for id,source in staking_sources.items(): + wells_ui = [] + for name,(supply,ts) in source[1].items(): + wells_ui.append(sg.Text(name)) + sources_ui.append([sg.Image("moon.png"), + sg.Column(layout=[wells_ui]), + sg.Button("Destroy", key=("-DESTROY-",id))]) + return sources_ui layout = [ [sg.Text(f"User: {current_user}")], [sg.Text(banks, key='-BANKS-')], - [sg.Button(button_text="Mint Moon", key="-MINT-")], [sg.HorizontalSeparator()], - [sg.Column(get_store_ui(), vertical_alignment='t')], + [sg.Button("Sell All", key="-SELLALL-")], [sg.HorizontalSeparator()], - [sg.Column(layout=sources_ui, size=(1200, 500), scrollable=True, vertical_scroll_only=True)] + [[sg.Column(get_store_ui(), size=(400, 280),), + sg.Column(get_inventory_ui(), key="-COL-", vertical_alignment='t')]], + [sg.HorizontalSeparator()], + [sg.Button("Mint Moon", key="-MINT-")], + [sg.Column(layout=get_sources_ui(), size=(1200, 500), + scrollable=True, vertical_scroll_only=True)] ] window = sg.Window("Moon Miner", layout, font='25') @@ -182,20 +202,28 @@ while True: event, values = window.read() if event == sg.WINDOW_CLOSED: break - 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 + elif type(event) is tuple: + if event[0] == "-UPGRADE-": + print("Upgrade") + elif event[0] == "-BUY-": + id = event[1] + buy_item(id) + window[("-BUY-",id)].update(disabled=True) + window.extend_layout(window["-COL-"], [inventory_row(id,id,0)]) + elif event[0] == "-DESTROY-": + destroy(event[1]) + else: + if event == "-MINT-": + mint() + elif event.startswith("Mine-"): + print("Mine") + elif event == "-SELLALL-": + inv = get_inventory() + for item in inv: + window[("-IROW-",item)].update(visible=False) + for item in store: + window[("-BUY-",item)].update(disabled=False) + sell_all() window.close() diff --git a/tables.sql b/tables.sql index ea1877e..7d0d6ef 100644 --- a/tables.sql +++ b/tables.sql @@ -1,3 +1,5 @@ +PRAGMA foreign_keys = ON; + CREATE TABLE users ( id integer primary key autoincrement, name varchar(32) not null