Multiple bug fixes, improving claiming, and got upgrading working

This commit is contained in:
Joseph Ferano 2023-02-24 14:12:59 +07:00
parent 31440a9776
commit d8ade16b96
3 changed files with 92 additions and 69 deletions

141
mm.py
View File

@ -29,23 +29,68 @@ def get_store_items():
cursor.close() cursor.close()
return items return items
def get_moons(): # An active stake is basically defined as one that has yet to be claimed
def get_active_stakes(source_id):
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute("SELECT id,created_at FROM staking_source")
staking_sources = {}
for i,(sid,ts) in enumerate(cursor.fetchall()):
cursor.execute(""" cursor.execute("""
SELECT name,supply,staking_event.created_at SELECT staking_event.id, well_id, inventory_item_id, duration_in_mins,
staking_event.created_at
FROM staking_event
LEFT JOIN claim_event ON staking_event.id = claim_event.staking_event_id
INNER JOIN resource_well ON resource_well.id = staking_event.well_id
WHERE resource_well.source_id = ? AND claim_event.staking_event_id IS NULL;
""", (source_id,))
active_stakes = {item[0]:item[1:] for item in cursor.fetchall()}
cursor.close()
return active_stakes
def get_wells(source_id):
cursor = conn.cursor()
cursor.execute("""
SELECT resource_well.id,name,supply,staking_event.created_at
FROM resource_well FROM resource_well
INNER JOIN resource ON resource.id = resource_well.resource_id INNER JOIN resource ON resource.id = resource_well.resource_id
LEFT JOIN staking_event ON staking_event.well_id = resource_well.id LEFT JOIN staking_event ON staking_event.well_id = resource_well.id
WHERE resource_well.source_id = ?; WHERE resource_well.source_id = ?;
""", (sid,)) """, (source_id,))
wells = {name: (supply,timestamp) for name,supply,timestamp in cursor.fetchall()}
stakes = get_active_stakes(sid) wells = {id: (name,supply,timestamp) for id,name,supply,timestamp in cursor.fetchall()}
staking_sources[sid] = (ts,wells,stakes)
cursor.close()
return wells
def get_stakes():
cursor = conn.cursor()
cursor.execute("""
SELECT staking_event.id,well_id,inventory_item_id,duration_in_mins,
staking_event.created_at,
CASE WHEN claim_event.staking_event_id IS NULL THEN 1 ELSE 0 END AS is_active
FROM staking_event
INNER JOIN resource_well ON resource_well.id = staking_event.well_id
INNER JOIN staking_source ON staking_source.id = resource_well.source_id
LEFT JOIN claim_event ON claim_event.staking_event_id = staking_event.id
WHERE staking_source.user_id = ?;
""", (world.current_user_id,))
stakes = {item[0]:(item[1],item[2],item[3],item[4],bool(item[-1])) for item in cursor.fetchall()}
cursor.close()
return stakes
def get_moons():
cursor = conn.cursor()
cursor.execute("SELECT id,created_at FROM staking_source WHERE user_id = ?",
(world.current_user_id,))
staking_sources = {}
for sid,ts in cursor.fetchall():
staking_sources[sid] = (ts, get_wells(sid), get_active_stakes(sid))
cursor.close() cursor.close()
return staking_sources return staking_sources
@ -66,41 +111,6 @@ def get_inventory():
cursor.close() cursor.close()
return inventory return inventory
# An active stake is basically defined as one that has yet to be claimed
def get_active_stakes(source_id):
cursor = conn.cursor()
cursor.execute("""
SELECT staking_event.id, source_id, well_id, inventory_item_id,
duration_in_mins, staking_event.created_at
FROM staking_event
LEFT JOIN claim_event ON staking_event.id = claim_event.staking_event_id
WHERE staking_event.source_id = ? AND claim_event.staking_event_id IS NULL;
""", (source_id,))
active_stakes = {item[0]:item[1:] for item in cursor.fetchall()}
cursor.close()
return active_stakes
def get_stakes(source_id):
cursor = conn.cursor()
cursor.execute("""
SELECT staking_event.id,well_id,staking_event.source_id,
inventory_item_id,duration_in_mins,staking_event.created_at
FROM staking_event
INNER JOIN staking_source on staking_event.source_id = staking_source.id
WHERE staking_event.source_id = ? AND staking_source.user_id = ?;
""", (source_id, world.current_user_id))
# (id, ( wellId, sourceId, invId, duration, created ))
stakes = {item[0]:item[1:] for item in cursor.fetchall()}
cursor.close()
return stakes
def mint(): def mint():
cursor = conn.cursor() cursor = conn.cursor()
rand_hash = "%010x" % random.randrange(16 ** 16) rand_hash = "%010x" % random.randrange(16 ** 16)
@ -143,33 +153,39 @@ def buy_item(item_id):
world.inventory = get_inventory() world.inventory = get_inventory()
return item_id return item_id
def mine(source_id, resource, item_id): def mine(well_id, item_id):
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute(""" cursor.execute("""
INSERT INTO staking_event (source_id, well_id, inventory_item_id, duration_in_mins) INSERT INTO staking_event (well_id, inventory_item_id, duration_in_mins)
VALUES (?, (SELECT id FROM resource_well WHERE resource_id = ? AND source_id = ?), ?, ?) VALUES (?, ?, ?)
""", (source_id, world.resource_to_id[resource], source_id, item_id, 3)) """, (well_id, item_id, 3))
conn.commit() conn.commit()
world.staking_sources = get_moons() world.staking_sources = get_moons()
world.stakes = get_stakes()
cursor.close() cursor.close()
def claim(staking_event_id): def claim(staking_event_id):
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute(""" cursor.execute("INSERT INTO claim_event (claim_amount, staking_event_id) VALUES (?, ?)",
INSERT INTO claim_event (claim_amount, staking_event_id) (10, staking_event_id))
VALUES (?, ?)
""", (10, staking_event_id))
conn.commit() conn.commit()
world.staking_sources = get_moons() world.staking_sources = get_moons()
world.stakes = get_stakes()
cursor.close() cursor.close()
def upgrade(a,b,user_data): def upgrade(item_id):
() cursor = conn.cursor()
cursor.execute("INSERT INTO upgrade_event (inventory_item_id) VALUES (?)", (item_id,))
conn.commit()
world.inventory = get_inventory()
cursor.close()
def destroy(source_id): def destroy(source_id):
cursor = conn.cursor() cursor = conn.cursor()
@ -229,7 +245,7 @@ def draw_inventory():
im.text(f"{id} - {world.store[sid][0]} - Tier {tier+1}") im.text(f"{id} - {world.store[sid][0]} - Tier {tier+1}")
im.push_id(f"Upgrade{id}") im.push_id(f"Upgrade{id}")
if im.button("Upgrade"): if im.button("Upgrade"):
print("Upgrade") upgrade(id)
im.pop_id() im.pop_id()
im.same_line() im.same_line()
im.push_id(f"Sell{id}") im.push_id(f"Sell{id}")
@ -265,25 +281,25 @@ def draw_moons():
im.next_column() im.next_column()
for name,(supply,ts) in wells.items(): for well_id,(name,supply,ts) in wells.items():
im.text(f"{name.capitalize()}: {supply}") im.text(f"{name.capitalize()}: {supply}")
for item_id,(store_id,_) in world.inventory.items(): for item_id,(store_id,_) in world.inventory.items():
item_name = world.store[store_id][0] item_name = world.store[store_id][0]
skip = False skip = False
for ss in world.staking_sources[source_id][2].values(): for ss in world.stakes.values():
if ss[2] == item_id: if ss[1] == item_id and ss[4] == True:
skip = True skip = True
if skip == True: if skip == True:
continue continue
im.push_id(f"Mine{id}{name}") im.push_id(f"Mine{well_id}")
if im.button(item_name): if im.button(item_name):
mine(source_id, name, item_id) mine(well_id, item_id)
im.pop_id() im.pop_id()
im.same_line() im.same_line()
im.spacing() im.spacing()
im.next_column() im.next_column()
for stake_id,(source_id,wid,invId,dur,start) in stakes.items(): for stake_id,(_,invId,dur,start) in stakes.items():
start_time = parse(start) start_time = parse(start)
now = datetime.datetime.utcnow().replace(microsecond=0) now = datetime.datetime.utcnow().replace(microsecond=0)
delta = now - start_time delta = now - start_time
@ -390,6 +406,7 @@ world.bank = get_bank()
world.store = get_store_items() world.store = get_store_items()
world.inventory = get_inventory() world.inventory = get_inventory()
world.staking_sources = get_moons() world.staking_sources = get_moons()
world.stakes = get_stakes()
def imgui_init(): def imgui_init():
world.moon_img_tex_id = texture_id = load_texture("moon.png") world.moon_img_tex_id = texture_id = load_texture("moon.png")

View File

@ -32,4 +32,13 @@ FROM staking_event
LEFT JOIN claim_event ON staking_event.id = claim_event.staking_event_id LEFT JOIN claim_event ON staking_event.id = claim_event.staking_event_id
WHERE staking_event.source_id = 4 AND claim_event.staking_event_id IS NULL; WHERE staking_event.source_id = 4 AND claim_event.staking_event_id IS NULL;
UPDATE staking_event SET created_at = '2023-02-21 12:58:02' WHERE id = 1; UPDATE staking_event SET created_at = '2023-02-21 12:58:02' WHERE id = 5;
SELECT staking_event.id,well_id,inventory_item_id,duration_in_mins,
staking_event.created_at,
CASE WHEN claim_event.staking_event_id IS NULL THEN 1 ELSE 0 END AS is_active
FROM staking_event
INNER JOIN resource_well ON resource_well.id = staking_event.well_id
INNER JOIN staking_source ON staking_source.id = resource_well.source_id
LEFT JOIN claim_event ON claim_event.staking_event_id = staking_event.id
WHERE staking_source.user_id = 1;

View File

@ -34,12 +34,9 @@ CREATE TABLE resource_well(
CREATE TABLE staking_event( CREATE TABLE staking_event(
id integer primary key autoincrement, id integer primary key autoincrement,
well_id int not null, well_id int not null,
source_id int not null,
inventory_item_id int not null, inventory_item_id int not null,
duration_in_mins int not null, duration_in_mins int not null,
created_at timestamp DEFAULT (current_timestamp), created_at timestamp DEFAULT (current_timestamp),
CONSTRAINT fk_sid FOREIGN KEY(source_id)
REFERENCES staking_source(id)
CONSTRAINT fk_wid FOREIGN KEY(well_id) CONSTRAINT fk_wid FOREIGN KEY(well_id)
REFERENCES resource_well(id) REFERENCES resource_well(id)
CONSTRAINT fk_iiid FOREIGN KEY(inventory_item_id) CONSTRAINT fk_iiid FOREIGN KEY(inventory_item_id)
@ -48,8 +45,8 @@ CREATE TABLE staking_event(
CREATE TABLE claim_event( CREATE TABLE claim_event(
id integer primary key autoincrement, id integer primary key autoincrement,
claim_amount int not null,
staking_event_id int not null, staking_event_id int not null,
claim_amount int not null,
created_at timestamp DEFAULT (current_timestamp), created_at timestamp DEFAULT (current_timestamp),
CONSTRAINT fk_se_id FOREIGN KEY(staking_event_id) CONSTRAINT fk_se_id FOREIGN KEY(staking_event_id)
REFERENCES staking_event(id) REFERENCES staking_event(id)