Replace dearpygui with PySimpleGui. Get several queries working

This commit is contained in:
Joseph Ferano 2023-02-15 14:24:54 +07:00
parent 83010fa171
commit ab21de5ac3
5 changed files with 90 additions and 64 deletions

View File

@ -12,20 +12,6 @@ INSERT INTO users(name) VALUES
('Plug'),
('Upgrade');
INSERT INTO staking_source(user_id, address) VALUES
(1, '12345');
INSERT INTO resource_well(source_id, resource_id, init_supply) VALUES
(1, 1, 200);
INSERT INTO resource_well(source_id, resource_id, init_supply) VALUES
(1, 2, 100);
INSERT INTO resource_well(source_id, resource_id, init_supply) VALUES
(1, 3, 50);
INSERT INTO resource_well(source_id, resource_id, init_supply) VALUES
(1, 4, 125);
INSERT INTO resource_well(source_id, resource_id, init_supply) VALUES
(1, 5, 150);
SELECT name,init_supply
FROM resource_well
INNER JOIN resource ON resource.id = resource_well.resource_id

119
mm.py
View File

@ -1,9 +1,9 @@
from dateutil.parser import parse
import sqlite3 as sql
import PySimpleGUI as sg
import random
import datetime
import dearpygui.demo as demo
import dearpygui.dearpygui as dpg
conn = sql.connect("mm.db", check_same_thread=False)
@ -18,9 +18,11 @@ cursor.execute("SELECT id,name FROM resource")
resources = cursor.fetchall()
id_to_resource = {id: name for id, name in resources}
resource_to_id = {name: id for id, name in resources}
cursor.close()
def get_moons():
staking_sources.clear()
cursor = conn.cursor()
cursor.execute("SELECT id,created_at FROM staking_source")
@ -34,6 +36,7 @@ def get_moons():
""", (sid,))
wells = {name: (supply,timestamp) for name,supply,timestamp in cursor.fetchall()}
staking_sources[sid] = (ts,wells)
cursor.close()
def mint():
cursor = conn.cursor()
@ -45,14 +48,28 @@ def mint():
raise Exception("User not found: " + user_name)
rand_hash = "%010x" % random.randrange(16 ** 16)
print(rand_hash, " ", user_id)
cursor.execute("""
INSERT INTO staking_source (user_id, address)
VALUES (?, ?)
cursor.execute('BEGIN')
try:
cursor.execute("""
INSERT INTO staking_source (user_id, address) VALUES (?, ?)
""", (user_id, f"0x{rand_hash}"))
conn.commit()
get_moons()
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 (?, ?, ?)
""", (source_id, id, init_supply))
conn.commit()
except sqlite3.Error as error:
print(error)
conn.rollback()
finally:
cursor.close()
get_moons()
def mine(a,b,user_data):
source_id,well_name = user_data
@ -65,57 +82,63 @@ def mine(a,b,user_data):
conn.commit()
get_moons()
cursor.close()
def upgrade(a,b,user_data):
()
def destroy(source_id):
cursor = conn.cursor()
cursor.execute("DELETE FROM staking_source WHERE id = ?", (source_id,))
conn.commit()
cursor.close()
get_moons()
dpg.create_context()
dpg.create_viewport(title='Custom Title', width=1200, height=800)
dpg.setup_dearpygui()
sg.set_options(font=("Fira Code", 15))
# dpg.font("fira.ttf", 50)
with dpg.font_registry():
# first argument ids the path to the .ttf or .otf file
default_font = dpg.add_font("fira.ttf", 20)
dpg.bind_font(default_font)
banks = ""
for _,name in resources:
banks += f"{name.capitalize()}: 100 | "
width, height, channels, data = dpg.load_image("moon-small.jpg")
with dpg.texture_registry(show=False):
dpg.add_static_texture(width=width, height=height, default_value=data, tag="moon")
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}")])
with dpg.window(label="Example Window", tag="Primary", width=1200, height=800):
dpg.add_text("User: Joe")
with dpg.group(horizontal=True):
for res in resources:
dpg.add_text(f"{res[1].capitalize()}: 0 |")
layout = [
[sg.Text(f"User: {current_user}")],
[sg.Text(banks, key='-BANKS-')],
[sg.Button("Mint Moon")],
[sg.HorizontalSeparator()],
[sg.Column(layout=sources_ui, size=(1200, 800), scrollable=True, vertical_scroll_only=True)]
]
dpg.add_button(label="Mint Moon", callback=mint)
dpg.add_separator()
window = sg.Window("Moon Miner", layout)
for sid,source in staking_sources.items():
dpg.add_image("moon")
with dpg.group(horizontal=True):
for name,(amount,timestamp) in source[1].items():
if timestamp:
with dpg.group(horizontal=False):
dpg.add_text(name.capitalize())
elapsed = str(datetime.datetime.utcnow() - parse(timestamp)).split(".")[0]
dpg.add_text(f"Mining for {elapsed}")
data = (sid,name)
dpg.add_button(label="Upgrade", callback=upgrade, user_data=data)
else:
lbl = f"Mine {name.capitalize()} ({amount})"
data = (sid,name)
dpg.add_button(label=lbl, callback=mine, user_data=data)
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
if event == "Mint Moon":
mint()
elif event.startswith("Upgrade-"):
print("Upgrade")
pass
elif event.startswith("Destroy"):
destroy(event.split("-")[1])
elif event.startswith("Mine-"):
print("Mine")
pass
dpg.show_viewport()
dpg.set_primary_window("Primary", True)
while dpg.is_dearpygui_running():
# put old render callback code here!
dpg.render_dearpygui_frame()
window.close()
conn.close()
dpg.destroy_context()

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.9 KiB

BIN
moon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

View File

@ -26,6 +26,7 @@ CREATE TABLE resource_well(
REFERENCES resource(id),
CONSTRAINT fk_sid FOREIGN KEY(source_id)
REFERENCES staking_source(id)
ON DELETE CASCADE
);
CREATE TABLE staking_event(
@ -49,19 +50,35 @@ CREATE TABLE upgrade_event(
CREATE TABLE claim_event(
id integer primary key autoincrement,
staking_source_id int not null,
resource_id int not null,
claim_type text CHECK ( claim_type IN ('BASE','BONUS') ),
claim_amount int not null,
created_at timestamp DEFAULT (current_timestamp),
CONSTRAINT fk_ures FOREIGN KEY(staking_source_id)
REFERENCES staking_source(id)
);
CREATE TABLE store_item(
id integer primary key autoincrement,
name varchar(128) not null,
price int not null
);
CREATE TABLE inventory_item(
id integer primary key autoincrement,
user_id int not null,
name varchar(128) not null,
created_at timestamp DEFAULT (current_timestamp),
CONSTRAINT fk_user FOREIGN KEY(user_id)
REFERENCES users(id)
);
CREATE TABLE bank_account(
id integer primary key autoincrement,
user_id int not null,
resource_id int not null,
balance int not null default 0 CHECK (balance >= 0),
created_at timestamp DEFAULT (current_timestamp),
updated_at timestamp DEFAULT (current_timestamp),
CONSTRAINT fk_user FOREIGN KEY(user_id)
REFERENCES users(id),
CONSTRAINT fk_resource FOREIGN KEY(resource_id)