moon-miner/mm.py

276 lines
8.1 KiB
Python

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)
conn.execute('PRAGMA foreign_keys = ON')
current_user = "Joe"
current_user_id = 1
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.execute("""
SELECT resource.name,balance FROM bank_account
INNER JOIN resource ON resource.id = bank_account.resource_id
WHERE user_id = ?
""", (current_user_id,))
bank = {name: balance for name,balance in cursor.fetchall()}
cursor.execute("""
SELECT store_item.id,store_item.name,resource.name,price,claim_amount FROM store_item
INNER JOIN resource ON resource.id = currency
""")
store = {item[0]:item[1:] for item in cursor.fetchall()}
cursor.close()
def get_moons():
staking_sources = {}
cursor = conn.cursor()
cursor.execute("SELECT id,created_at FROM staking_source")
for i,(sid,ts) in enumerate(cursor.fetchall()):
cursor.execute("""
SELECT name,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()
return staking_sources
def get_inventory():
inventory = {}
cursor = conn.cursor()
cursor.execute("""
SELECT inventory_item.id,store_item_id, COUNT(upgrade_event.id) as upgrades
FROM inventory_item
LEFT JOIN upgrade_event ON inventory_item.id = upgrade_event.inventory_item_id
WHERE inventory_item.user_id = ?
GROUP BY inventory_item.id;
""", (current_user_id,))
inventory = {item[0]:item[1:] for item in cursor.fetchall()}
cursor.close()
return inventory
def get_stakes(source_id):
stakes = {}
cursor = conn.cursor()
cursor.execute("""
SELECT staking_event.id,well_id,staking_event.source_id,
inventory_item_id,staking_event.created_at,expiration_at
FROM staking_event
INNER JOIN resource_well ON resource_well.id = well_id
INNER JOIN staking_source on staking_event.source_id = staking_source.id
WHERE staking_event.source_id = ? AND staking_source.user_id = ?;
""", (source_id,current_user_id))
fall = cursor.fetchall()
inventory = {item[0]:item[1:] for item in fall}
cursor.close()
return stakes
def mint():
cursor = conn.cursor()
rand_hash = "%010x" % random.randrange(16 ** 16)
cursor.execute('BEGIN')
source_id = -1
try:
cursor.execute("""
INSERT INTO staking_source (user_id, address) VALUES (?, ?)
""", (current_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, supply) VALUES (?, ?, ?)
""", (source_id, id, init_supply))
conn.commit()
except sqlite3.Error as error:
print(error)
conn.rollback()
finally:
cursor.close()
return source_id
def buy_item(item_id):
cursor = conn.cursor()
cursor.execute("""
INSERT INTO inventory_item (user_id, store_item_id)
VALUES (?, ?)
""", (current_user_id, item_id))
item_id = cursor.lastrowid
conn.commit()
cursor.close()
return item_id
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()
def sell(item_id):
cursor = conn.cursor()
cursor.execute("DELETE FROM inventory_item WHERE user_id = ? AND id = ?",
(current_user_id,item_id))
conn.commit()
cursor.close()
def sell_all():
cursor = conn.cursor()
cursor.execute("DELETE FROM inventory_item WHERE user_id = ?", (current_user_id,))
conn.commit()
cursor.close()
bank_txts = [ f"{name.capitalize()}: {amount}" for name,amount in bank.items() ]
banks = " | ".join(bank_txts)
def get_store_ui():
inventory = get_inventory()
store_ui = []
for id,(name,resource,price,claim) in store.items():
owned = False
for (store_item_id,_) in inventory.values():
if id == store_item_id:
owned = True
store_ui.append([sg.Text(f"{name}: Mine {claim} {resource.capitalize()}"),
sg.Button(f"Buy {price} {resource[0:3]}",
key=("-BUY-",id),
disabled=owned)])
return store_ui
def inventory_row(item_id, si_id, tier):
name = store[si_id][0]
row = [sg.Text(f"{name} - Tier {tier+1}"),
sg.Button("Upgrade", key=("-UPGRADE-",item_id)),
sg.Button("Sell", key=("-SELL-",item_id))]
return [sg.pin(sg.Column([row], key=("-IROW-",item_id)))]
def moon_row(id,source):
wbtns = []
for name,(supply,ts) in source[1].items():
col = sg.Col([[sg.Text(name)], [sg.Button("Mine", key=("-MINE-",id,name))]])
wbtns.append(col)
row = [sg.Image("moon.png"),
sg.Column([wbtns])]
return [sg.pin(sg.Column([row, [sg.Button("Destroy", key=("-DESTROY-",id))]], key=("-MROW-",id)))]
def get_inventory_ui():
inventory_ui = []
inventory = get_inventory()
for id,(si_id,tier) in inventory.items():
inventory_ui.append(inventory_row(id,si_id,tier))
return inventory_ui
def get_sources_ui():
staking_sources = get_moons()
sources_ui = []
for id,source in staking_sources.items():
sources_ui.append(moon_row(id,source))
return sources_ui
layout = [
[sg.Text(f"User: {current_user}")],
[sg.Text(banks, key='-BANKS-')],
[sg.HorizontalSeparator()],
[sg.Button("Sell All", key="-SELLALL-")],
[[sg.Column(get_store_ui(), size=(400, 280),),
sg.Column(get_inventory_ui(), key="-ICOL-", vertical_alignment='t')]],
[sg.HorizontalSeparator()],
[sg.Button("Mint Moon", key="-MINT-")],
[sg.Column(layout=get_sources_ui(), key="-MCOL-", size=(1200, 500),
scrollable=True, vertical_scroll_only=True)]
]
window = sg.Window("Moon Miner", layout, font='25')
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
elif type(event) is tuple:
if event[0] == "-UPGRADE-":
sg.Window.Layout([[sg.Text("IT WORKED")]])
elif event[0] == "-SELL-":
id = event[1]
inv = get_inventory()
sell(id)
window[("-BUY-",inv[id][0])].update(disabled=False)
window[("-IROW-",id)].update(visible=False)
elif event[0] == "-BUY-":
id = event[1]
item_id = buy_item(id)
window[("-BUY-",id)].update(disabled=True)
window.extend_layout(window["-ICOL-"], [inventory_row(item_id,id,0)])
elif event[0] == "-DESTROY-":
id = event[1]
destroy(id)
window[("-MROW-",id)].update(visible=False)
else:
if event == "-MINT-":
id = mint()
moons = get_moons()
window.extend_layout(window["-MCOL-"], [moon_row(id,moons[id])])
elif event.startswith("-MINE-"):
print("Mine")
elif event == "-SELLALL-":
inv = get_inventory()
for item in inv:
window[("-IROW-",item)].update(visible=False)
for item in store:
window[("-BUY-",item)].update(disabled=False)
sell_all()
window.close()
conn.close()