From d8ade16b96098ea9b4bff2a724b9f5b893733fab Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Fri, 24 Feb 2023 14:12:59 +0700 Subject: [PATCH] Multiple bug fixes, improving claiming, and got upgrading working --- mm.py | 145 +++++++++++++++++++++++++++++----------------------- queries.sql | 11 +++- tables.sql | 5 +- 3 files changed, 92 insertions(+), 69 deletions(-) diff --git a/mm.py b/mm.py index c34f6e0..fb486fc 100644 --- a/mm.py +++ b/mm.py @@ -29,23 +29,68 @@ def get_store_items(): cursor.close() return items +# 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, 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 + 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 = ?; + """, (source_id,)) + + wells = {id: (name,supply,timestamp) for id,name,supply,timestamp in cursor.fetchall()} + + 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") + cursor.execute("SELECT id,created_at FROM staking_source WHERE user_id = ?", + (world.current_user_id,)) staking_sources = {} - 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()} - stakes = get_active_stakes(sid) - staking_sources[sid] = (ts,wells,stakes) + for sid,ts in cursor.fetchall(): + staking_sources[sid] = (ts, get_wells(sid), get_active_stakes(sid)) cursor.close() return staking_sources @@ -66,41 +111,6 @@ def get_inventory(): cursor.close() 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(): cursor = conn.cursor() rand_hash = "%010x" % random.randrange(16 ** 16) @@ -143,33 +153,39 @@ def buy_item(item_id): world.inventory = get_inventory() return item_id -def mine(source_id, resource, item_id): +def mine(well_id, item_id): cursor = conn.cursor() cursor.execute(""" - INSERT INTO staking_event (source_id, well_id, inventory_item_id, duration_in_mins) - VALUES (?, (SELECT id FROM resource_well WHERE resource_id = ? AND source_id = ?), ?, ?) - """, (source_id, world.resource_to_id[resource], source_id, item_id, 3)) + INSERT INTO staking_event (well_id, inventory_item_id, duration_in_mins) + VALUES (?, ?, ?) + """, (well_id, item_id, 3)) conn.commit() world.staking_sources = get_moons() + world.stakes = get_stakes() cursor.close() def claim(staking_event_id): cursor = conn.cursor() - cursor.execute(""" - INSERT INTO claim_event (claim_amount, staking_event_id) - VALUES (?, ?) - """, (10, staking_event_id)) + cursor.execute("INSERT INTO claim_event (claim_amount, staking_event_id) VALUES (?, ?)", + (10, staking_event_id)) conn.commit() world.staking_sources = get_moons() + world.stakes = get_stakes() 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): cursor = conn.cursor() @@ -229,7 +245,7 @@ def draw_inventory(): im.text(f"{id} - {world.store[sid][0]} - Tier {tier+1}") im.push_id(f"Upgrade{id}") if im.button("Upgrade"): - print("Upgrade") + upgrade(id) im.pop_id() im.same_line() im.push_id(f"Sell{id}") @@ -265,25 +281,25 @@ def draw_moons(): 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}") for item_id,(store_id,_) in world.inventory.items(): item_name = world.store[store_id][0] skip = False - for ss in world.staking_sources[source_id][2].values(): - if ss[2] == item_id: + for ss in world.stakes.values(): + if ss[1] == item_id and ss[4] == True: skip = True if skip == True: continue - im.push_id(f"Mine{id}{name}") + im.push_id(f"Mine{well_id}") if im.button(item_name): - mine(source_id, name, item_id) + mine(well_id, item_id) im.pop_id() im.same_line() im.spacing() 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) now = datetime.datetime.utcnow().replace(microsecond=0) delta = now - start_time @@ -390,6 +406,7 @@ world.bank = get_bank() world.store = get_store_items() world.inventory = get_inventory() world.staking_sources = get_moons() +world.stakes = get_stakes() def imgui_init(): world.moon_img_tex_id = texture_id = load_texture("moon.png") diff --git a/queries.sql b/queries.sql index 298a7f9..495650e 100644 --- a/queries.sql +++ b/queries.sql @@ -32,4 +32,13 @@ FROM staking_event 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; -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; diff --git a/tables.sql b/tables.sql index f65172e..072e859 100644 --- a/tables.sql +++ b/tables.sql @@ -34,12 +34,9 @@ CREATE TABLE resource_well( CREATE TABLE staking_event( id integer primary key autoincrement, well_id int not null, - source_id int not null, inventory_item_id int not null, duration_in_mins int not null, created_at timestamp DEFAULT (current_timestamp), - CONSTRAINT fk_sid FOREIGN KEY(source_id) - REFERENCES staking_source(id) CONSTRAINT fk_wid FOREIGN KEY(well_id) REFERENCES resource_well(id) CONSTRAINT fk_iiid FOREIGN KEY(inventory_item_id) @@ -48,8 +45,8 @@ CREATE TABLE staking_event( CREATE TABLE claim_event( id integer primary key autoincrement, - claim_amount int not null, staking_event_id int not null, + claim_amount int not null, created_at timestamp DEFAULT (current_timestamp), CONSTRAINT fk_se_id FOREIGN KEY(staking_event_id) REFERENCES staking_event(id)