moon-miner/mm.py

122 lines
3.7 KiB
Python

from dateutil.parser import parse
import sqlite3 as sql
import random
import datetime
import dearpygui.demo as demo
import dearpygui.dearpygui as dpg
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}
def get_moons():
staking_sources.clear()
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)
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)
print(rand_hash, " ", user_id)
cursor.execute("""
INSERT INTO staking_source (user_id, address)
VALUES (?, ?)
""", (user_id, f"0x{rand_hash}"))
conn.commit()
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()
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")
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 |")
dpg.add_button(label="Mint Moon", callback=mint)
dpg.add_separator()
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)
dpg.show_viewport()
dpg.set_primary_window("Primary", True)
while dpg.is_dearpygui_running():
# put old render callback code here!
dpg.render_dearpygui_frame()
conn.close()
dpg.destroy_context()