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'), ('Plug'),
('Upgrade'); ('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 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

113
mm.py
View File

@ -1,9 +1,9 @@
from dateutil.parser import parse from dateutil.parser import parse
import sqlite3 as sql import sqlite3 as sql
import PySimpleGUI as sg
import random import random
import datetime import datetime
import dearpygui.demo as demo
import dearpygui.dearpygui as dpg
conn = sql.connect("mm.db", check_same_thread=False) conn = sql.connect("mm.db", check_same_thread=False)
@ -18,9 +18,11 @@ cursor.execute("SELECT id,name FROM resource")
resources = cursor.fetchall() resources = cursor.fetchall()
id_to_resource = {id: name for id, name in resources} id_to_resource = {id: name for id, name in resources}
resource_to_id = {name: id for id, name in resources} resource_to_id = {name: id for id, name in resources}
cursor.close()
def get_moons(): def get_moons():
staking_sources.clear() staking_sources.clear()
cursor = conn.cursor()
cursor.execute("SELECT id,created_at FROM staking_source") cursor.execute("SELECT id,created_at FROM staking_source")
@ -34,6 +36,7 @@ def get_moons():
""", (sid,)) """, (sid,))
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()
def mint(): def mint():
cursor = conn.cursor() cursor = conn.cursor()
@ -45,15 +48,29 @@ def mint():
raise Exception("User not found: " + user_name) raise Exception("User not found: " + user_name)
rand_hash = "%010x" % random.randrange(16 ** 16) rand_hash = "%010x" % random.randrange(16 ** 16)
print(rand_hash, " ", user_id)
cursor.execute('BEGIN')
try:
cursor.execute(""" cursor.execute("""
INSERT INTO staking_source (user_id, address) INSERT INTO staking_source (user_id, address) VALUES (?, ?)
VALUES (?, ?)
""", (user_id, f"0x{rand_hash}")) """, (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 (?, ?, ?)
""", (source_id, id, init_supply))
conn.commit() conn.commit()
except sqlite3.Error as error:
print(error)
conn.rollback()
finally:
cursor.close()
get_moons() get_moons()
def mine(a,b,user_data): def mine(a,b,user_data):
source_id,well_name = user_data source_id,well_name = user_data
cursor = conn.cursor() cursor = conn.cursor()
@ -65,57 +82,63 @@ def mine(a,b,user_data):
conn.commit() conn.commit()
get_moons() get_moons()
cursor.close()
def upgrade(a,b,user_data): 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() get_moons()
dpg.create_context() sg.set_options(font=("Fira Code", 15))
dpg.create_viewport(title='Custom Title', width=1200, height=800)
dpg.setup_dearpygui()
# dpg.font("fira.ttf", 50) banks = ""
with dpg.font_registry(): for _,name in resources:
# first argument ids the path to the .ttf or .otf file banks += f"{name.capitalize()}: 100 | "
default_font = dpg.add_font("fira.ttf", 20)
dpg.bind_font(default_font)
width, height, channels, data = dpg.load_image("moon-small.jpg") sources_ui = []
with dpg.texture_registry(show=False): for id,source in staking_sources.items():
dpg.add_static_texture(width=width, height=height, default_value=data, tag="moon") 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): layout = [
dpg.add_text("User: Joe") [sg.Text(f"User: {current_user}")],
with dpg.group(horizontal=True): [sg.Text(banks, key='-BANKS-')],
for res in resources: [sg.Button("Mint Moon")],
dpg.add_text(f"{res[1].capitalize()}: 0 |") [sg.HorizontalSeparator()],
[sg.Column(layout=sources_ui, size=(1200, 800), scrollable=True, vertical_scroll_only=True)]
]
dpg.add_button(label="Mint Moon", callback=mint) window = sg.Window("Moon Miner", layout)
dpg.add_separator()
for sid,source in staking_sources.items(): while True:
dpg.add_image("moon") event, values = window.read()
with dpg.group(horizontal=True): if event == sg.WINDOW_CLOSED:
for name,(amount,timestamp) in source[1].items(): break
if timestamp: if event == "Mint Moon":
with dpg.group(horizontal=False): mint()
dpg.add_text(name.capitalize()) elif event.startswith("Upgrade-"):
elapsed = str(datetime.datetime.utcnow() - parse(timestamp)).split(".")[0] print("Upgrade")
dpg.add_text(f"Mining for {elapsed}") pass
data = (sid,name) elif event.startswith("Destroy"):
dpg.add_button(label="Upgrade", callback=upgrade, user_data=data) destroy(event.split("-")[1])
else: elif event.startswith("Mine-"):
lbl = f"Mine {name.capitalize()} ({amount})" print("Mine")
data = (sid,name) pass
dpg.add_button(label=lbl, callback=mine, user_data=data)
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() 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), REFERENCES resource(id),
CONSTRAINT fk_sid FOREIGN KEY(source_id) CONSTRAINT fk_sid FOREIGN KEY(source_id)
REFERENCES staking_source(id) REFERENCES staking_source(id)
ON DELETE CASCADE
); );
CREATE TABLE staking_event( CREATE TABLE staking_event(
@ -49,19 +50,35 @@ CREATE TABLE upgrade_event(
CREATE TABLE claim_event( CREATE TABLE claim_event(
id integer primary key autoincrement, 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_type text CHECK ( claim_type IN ('BASE','BONUS') ),
claim_amount int not null,
created_at timestamp DEFAULT (current_timestamp), created_at timestamp DEFAULT (current_timestamp),
CONSTRAINT fk_ures FOREIGN KEY(staking_source_id) CONSTRAINT fk_ures FOREIGN KEY(staking_source_id)
REFERENCES 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( CREATE TABLE bank_account(
id integer primary key autoincrement, id integer primary key autoincrement,
user_id int not null, user_id int not null,
resource_id int not null, resource_id int not null,
balance int not null default 0 CHECK (balance >= 0), 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) CONSTRAINT fk_user FOREIGN KEY(user_id)
REFERENCES users(id), REFERENCES users(id),
CONSTRAINT fk_resource FOREIGN KEY(resource_id) CONSTRAINT fk_resource FOREIGN KEY(resource_id)