moon-miner/mm.py

134 lines
4.2 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
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
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,created_at FROM staking_source")
staking_sources = [(i[0], i[1].capitalize()) for i in cursor.fetchall()]
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)
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)
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()
def mine(a,b,user_data):
print(user_data)
source_id,well_id = user_data
cursor = conn.cursor()
cursor.execute("""
INSERT INTO staking_event (source_id, well_id)
VALUES (?, ?)
""", (source_id, well_id))
conn.commit()
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}: 0 |")
dpg.add_button(label="Mint Moon", callback=mint)
dpg.add_separator()
for source in staking_sources:
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)
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()
conn.close()
dpg.destroy_context()