diff --git a/data.sql b/data.sql index 4b879fe..097119e 100644 --- a/data.sql +++ b/data.sql @@ -15,13 +15,18 @@ INSERT INTO users(name) VALUES INSERT INTO staking_source(user_id, address) VALUES (1, '12345'); -INSERT INTO resource_well(staking_source_id, resource_id, init_supply) VALUES -(1, 1, 100); -INSERT INTO resource_well(staking_source_id, resource_id, init_supply) VALUES +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(staking_source_id, resource_id, init_supply) VALUES -(1, 3, 100); -INSERT INTO resource_well(staking_source_id, resource_id, init_supply) VALUES -(1, 4, 100); -INSERT INTO resource_well(staking_source_id, resource_id, init_supply) VALUES -(1, 5, 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 +WHERE source_id = 1; diff --git a/mm.py b/mm.py index 0e9b87d..c3422ae 100644 --- a/mm.py +++ b/mm.py @@ -5,50 +5,35 @@ import datetime import dearpygui.demo as demo import dearpygui.dearpygui as dpg -dpg.create_context() -dpg.create_viewport(title='Custom Title', width=1200, height=800) -dpg.setup_dearpygui() - -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) - conn = sql.connect("mm.db", check_same_thread=False) current_user = "Joe" current_user_id = 1 -current_source = 1 + +staking_sources = {} cursor = conn.cursor() -cursor.execute("SELECT created_at FROM staking_source WHERE user_id = ?", (current_user_id,)) -staked_timestamp = parse(cursor.fetchone()[0]) -cursor.execute("SELECT name FROM resource") -resources = [i[0].capitalize() for i in cursor.fetchall()] +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.execute("SELECT id,created_at FROM staking_source") -staking_sources = [(i[0], i[1].capitalize()) for i in cursor.fetchall()] +def get_moons(): + staking_sources.clear() -for i,(sid,ts) in enumerate(staking_sources): - cursor.execute(""" - SELECT resource.id,name,init_supply - FROM resource_well - INNER JOIN resource ON resource.id = resource_well.resource_id - WHERE staking_source_id = ?; - """, (sid,)) - wells = [(i[0],i[1].capitalize(),i[2]) for i in cursor.fetchall()] - staking_sources[i] = (sid,ts,wells) + cursor.execute("SELECT id,created_at FROM staking_source") -for i,(sid,ts,wells) in enumerate(staking_sources): - cursor.execute(""" - SELECT resoure_id, - FROM resource_well - INNER JOIN resource ON resource.id = resource_well.resource_id - WHERE staking_source_id = ?; - """, (sid,)) - wells = [(i[0],i[1].capitalize(),i[2]) for i in cursor.fetchall()] - staking_sources[i] = (sid,ts,wells) + for i,(sid,ts) in enumerate(cursor.fetchall()): + cursor.execute(""" + SELECT name,init_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 + WHERE resource_well.source_id = ?; + """, (sid,)) + wells = {name: (supply,timestamp) for name,supply,timestamp in cursor.fetchall()} + staking_sources[sid] = (ts,wells) def mint(): cursor = conn.cursor() @@ -67,21 +52,36 @@ def mint(): """, (user_id, f"0x{rand_hash}")) conn.commit() + get_moons() def mine(a,b,user_data): - print(user_data) - source_id,well_id = user_data + source_id,well_name = user_data cursor = conn.cursor() - + cursor.execute(""" INSERT INTO staking_event (source_id, well_id) VALUES (?, ?) - """, (source_id, well_id)) + """, (source_id, resource_to_id[well_name])) conn.commit() - -width, height, channels, data = dpg.load_image("moon-small.jpg") + get_moons() +def upgrade(a,b,user_data): + () + +get_moons() + +dpg.create_context() +dpg.create_viewport(title='Custom Title', width=1200, height=800) +dpg.setup_dearpygui() + +# 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) + +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") @@ -89,45 +89,33 @@ 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}: 0 |") - + dpg.add_text(f"{res[1].capitalize()}: 0 |") + dpg.add_button(label="Mint Moon", callback=mint) dpg.add_separator() - for source in staking_sources: + for sid,source in staking_sources.items(): dpg.add_image("moon") with dpg.group(horizontal=True): - dpg.add_button(label="Upgrade") - dpg.add_button(label="Claim") - with dpg.group(horizontal=True): - for well in source[2]: - lbl = f"Mine {well[1]} ({well[2]})" - data = (source[0],well[0]) - dpg.add_button(label=lbl, callback=mine, user_data=data) + 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) dpg.show_viewport() dpg.set_primary_window("Primary", True) -dpg.start_dearpygui() - - - # if gui_button(Rectangle(10,10,150,50), "#154#Mint"): - # mint() - # if current_source: - # now = datetime.datetime.utcnow() - # timespan = str(now - staked_timestamp).split('.')[0] - # draw_text(f"Mining start time: {timespan}", 200, 20, 30, text_color) - # btn_pos = 100 - # spacing = 145 - # for i,res in enumerate(resources): - # draw_text(f"{res}", 20, i * spacing + btn_pos, 27, text_color) - # draw_text(f"Amount", 20, i * spacing + btn_pos + 40, 20, text_color) - # if gui_button(Rectangle(20,i * spacing + btn_pos + 70,110,40), "#78#Upgrade"): - # mint() - # if gui_button(Rectangle(160,i * spacing + btn_pos + 70,110,40), "#78#Claim"): - # mint() - - # end_drawing() +while dpg.is_dearpygui_running(): + # put old render callback code here! + dpg.render_dearpygui_frame() conn.close() dpg.destroy_context() diff --git a/tables.sql b/tables.sql index b6a4565..785e7bc 100644 --- a/tables.sql +++ b/tables.sql @@ -3,23 +3,9 @@ CREATE TABLE users ( name varchar(32) not null ); --- DROP TABLE users; - -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), - CONSTRAINT fk_user FOREIGN KEY(user_id) - REFERENCES users(id), - CONSTRAINT fk_resource FOREIGN KEY(resource_id) - REFERENCES resource(id) -); - CREATE TABLE resource( id integer primary key autoincrement, - name varchar(32) not null + name varchar(32) not null unique ); CREATE TABLE staking_source( @@ -33,12 +19,12 @@ CREATE TABLE staking_source( CREATE TABLE resource_well( id integer primary key autoincrement, - staking_source_id int not null, + source_id int not null, resource_id int not null, init_supply int not null, - CONSTRAINT fk_resource FOREIGN KEY(resource_id) + CONSTRAINT fk_rid FOREIGN KEY(resource_id) REFERENCES resource(id), - CONSTRAINT fk_ssource FOREIGN KEY(staking_source_id) + CONSTRAINT fk_sid FOREIGN KEY(source_id) REFERENCES staking_source(id) ); @@ -69,3 +55,15 @@ CREATE TABLE claim_event( CONSTRAINT fk_ures FOREIGN KEY(staking_source_id) REFERENCES staking_source(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), + CONSTRAINT fk_user FOREIGN KEY(user_id) + REFERENCES users(id), + CONSTRAINT fk_resource FOREIGN KEY(resource_id) + REFERENCES resource(id) +);