from dateutil.parser import parse import sqlite3 as sql import PySimpleGUI as sg import random import datetime conn = sql.connect("mm.db", check_same_thread=False) current_user = "Joe" current_user_id = 1 staking_sources = {} cursor = conn.cursor() 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") 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) cursor.close() 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}")) 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 cursor = conn.cursor() cursor.execute(""" INSERT INTO staking_event (source_id, well_id) VALUES (?, ?) """, (source_id, resource_to_id[well_name])) 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() sg.set_options(font=("Fira Code", 15)) banks = "" for _,name in resources: banks += f"{name.capitalize()}: 100 | " 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}")]) 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)] ] window = sg.Window("Moon Miner", layout) 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 window.close() conn.close()