Make buttons update and got pragma foreign_keys to work
This commit is contained in:
parent
91d6b33d3a
commit
e7857e25e8
3
data.sql
3
data.sql
@ -7,7 +7,7 @@ INSERT INTO resource(name) VALUES
|
|||||||
|
|
||||||
INSERT INTO users(name) VALUES
|
INSERT INTO users(name) VALUES
|
||||||
('Joe'),
|
('Joe'),
|
||||||
('Emile'),
|
('Emil'),
|
||||||
('Niko'),
|
('Niko'),
|
||||||
('Plug'),
|
('Plug'),
|
||||||
('Upgrade');
|
('Upgrade');
|
||||||
@ -28,6 +28,7 @@ INSERT INTO store_item(name, currency, target_resource, price, claim_amount) VAL
|
|||||||
('Drill 1E', 5, 5, 100, 500);
|
('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
|
||||||
|
112
mm.py
112
mm.py
@ -6,6 +6,8 @@ import datetime
|
|||||||
|
|
||||||
|
|
||||||
conn = sql.connect("mm.db", check_same_thread=False)
|
conn = sql.connect("mm.db", check_same_thread=False)
|
||||||
|
conn.execute('PRAGMA foreign_keys = ON')
|
||||||
|
|
||||||
|
|
||||||
current_user = "Joe"
|
current_user = "Joe"
|
||||||
current_user_id = 1
|
current_user_id = 1
|
||||||
@ -40,7 +42,7 @@ def get_moons():
|
|||||||
|
|
||||||
for i,(sid,ts) in enumerate(cursor.fetchall()):
|
for i,(sid,ts) in enumerate(cursor.fetchall()):
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
SELECT name,init_supply,staking_event.created_at
|
SELECT name,supply,staking_event.created_at
|
||||||
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
|
||||||
LEFT JOIN staking_event ON staking_event.well_id = resource_well.id
|
LEFT JOIN staking_event ON staking_event.well_id = resource_well.id
|
||||||
@ -71,26 +73,19 @@ def get_inventory():
|
|||||||
|
|
||||||
def mint():
|
def mint():
|
||||||
cursor = conn.cursor()
|
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)
|
rand_hash = "%010x" % random.randrange(16 ** 16)
|
||||||
|
|
||||||
cursor.execute('BEGIN')
|
cursor.execute('BEGIN')
|
||||||
try:
|
try:
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
INSERT INTO staking_source (user_id, address) VALUES (?, ?)
|
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
|
source_id = cursor.lastrowid
|
||||||
for id,_ in resources:
|
for id,_ in resources:
|
||||||
init_supply = random.randint(50, 200)
|
init_supply = random.randint(50, 200)
|
||||||
cursor.execute("""
|
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))
|
""", (source_id, id, init_supply))
|
||||||
|
|
||||||
conn.commit()
|
conn.commit()
|
||||||
@ -136,10 +131,16 @@ def destroy(source_id):
|
|||||||
conn.commit()
|
conn.commit()
|
||||||
cursor.close()
|
cursor.close()
|
||||||
|
|
||||||
|
def sell_all():
|
||||||
|
cursor = conn.cursor()
|
||||||
|
|
||||||
banks = ""
|
cursor.execute("DELETE FROM inventory_item WHERE user_id = ?", (current_user_id,))
|
||||||
for name,amount in bank.items():
|
|
||||||
banks += f"{name.capitalize()}: {amount} | "
|
conn.commit()
|
||||||
|
cursor.close()
|
||||||
|
|
||||||
|
bank_txts = [ f"{name.capitalize()}: {amount}" for name,amount in bank.items() ]
|
||||||
|
banks = " | ".join(bank_txts)
|
||||||
|
|
||||||
def get_store_ui():
|
def get_store_ui():
|
||||||
inventory = get_inventory()
|
inventory = get_inventory()
|
||||||
@ -150,30 +151,49 @@ def get_store_ui():
|
|||||||
if id == store_item_id:
|
if id == store_item_id:
|
||||||
owned = True
|
owned = True
|
||||||
store_ui.append([sg.Text(f"{name}: Mine {claim} {resource.capitalize()}"),
|
store_ui.append([sg.Text(f"{name}: Mine {claim} {resource.capitalize()}"),
|
||||||
sg.Button(button_text=f"Buy {price} {resource[0:3]}",
|
sg.Button(f"Buy {price} {resource[0:3]}",
|
||||||
key=f"-BUY-{id}-",
|
key=("-BUY-",id),
|
||||||
disabled=owned)])
|
disabled=owned)])
|
||||||
return store_ui
|
return store_ui
|
||||||
|
|
||||||
staking_sources = get_moons()
|
def inventory_row(id, si_id, tier):
|
||||||
sources_ui = []
|
name = store[si_id][0]
|
||||||
for id,source in staking_sources.items():
|
row = [sg.Text(f"{name} - Tier {tier+1}"),
|
||||||
wells_ui = []
|
sg.Button("Upgrade", key=("-UPGRADE-",id)),
|
||||||
for name,(supply,ts) in source[1].items():
|
sg.Button("Sell", key=("-SELL-",id))]
|
||||||
wells_ui.append(sg.Text(name))
|
return [sg.pin(sg.Column([row], key=("-IROW-",id)))]
|
||||||
sources_ui.append([sg.Image("moon.png"),
|
|
||||||
sg.Column(layout=[wells_ui]),
|
def get_inventory_ui():
|
||||||
sg.Button("Upgrade"),
|
inventory_ui = []
|
||||||
sg.Button(f"Destroy-{id}")])
|
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 = [
|
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(button_text="Mint Moon", key="-MINT-")],
|
|
||||||
[sg.HorizontalSeparator()],
|
[sg.HorizontalSeparator()],
|
||||||
[sg.Column(get_store_ui(), vertical_alignment='t')],
|
[sg.Button("Sell All", key="-SELLALL-")],
|
||||||
[sg.HorizontalSeparator()],
|
[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')
|
window = sg.Window("Moon Miner", layout, font='25')
|
||||||
@ -182,20 +202,28 @@ while True:
|
|||||||
event, values = window.read()
|
event, values = window.read()
|
||||||
if event == sg.WINDOW_CLOSED:
|
if event == sg.WINDOW_CLOSED:
|
||||||
break
|
break
|
||||||
elif event.startswith("-BUY-"):
|
elif type(event) is tuple:
|
||||||
id = event.split("-")[2]
|
if event[0] == "-UPGRADE-":
|
||||||
buy_item(id)
|
print("Upgrade")
|
||||||
window[f"-BUY-{id}-"].update(disabled=True)
|
elif event[0] == "-BUY-":
|
||||||
elif event.startswith("Upgrade-"):
|
id = event[1]
|
||||||
print("Upgrade")
|
buy_item(id)
|
||||||
pass
|
window[("-BUY-",id)].update(disabled=True)
|
||||||
elif event.startswith("Destroy"):
|
window.extend_layout(window["-COL-"], [inventory_row(id,id,0)])
|
||||||
destroy(event.split("-")[1])
|
elif event[0] == "-DESTROY-":
|
||||||
elif event == "-MINT-":
|
destroy(event[1])
|
||||||
window['items'].update([[sg.Text("New Text")]])
|
else:
|
||||||
elif event.startswith("Mine-"):
|
if event == "-MINT-":
|
||||||
print("Mine")
|
mint()
|
||||||
pass
|
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()
|
window.close()
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
PRAGMA foreign_keys = ON;
|
||||||
|
|
||||||
CREATE TABLE users (
|
CREATE TABLE users (
|
||||||
id integer primary key autoincrement,
|
id integer primary key autoincrement,
|
||||||
name varchar(32) not null
|
name varchar(32) not null
|
||||||
|
Loading…
x
Reference in New Issue
Block a user