From dd42a9880e58d195a49c15577b1ab20507ef117b Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Sat, 25 Feb 2023 13:35:31 +0700 Subject: [PATCH] Claim adds to resource_account and deducts from resource_well --- data.sql | 12 +++--- mm.py | 107 ++++++++++++++++++++++++++++++++++------------------- tables.sql | 4 +- 3 files changed, 77 insertions(+), 46 deletions(-) diff --git a/data.sql b/data.sql index d8e25d1..0a13aa7 100644 --- a/data.sql +++ b/data.sql @@ -21,9 +21,9 @@ INSERT INTO resource_account(user_id, resource_id, balance) VALUES INSERT INTO bank_account(user_id, balance) VALUES (1, 500); -INSERT INTO store_item(name, price, claim_amount) VALUES - ('Drill 1A', 25, 50), - ('Drill 1B', 25, 50), - ('Drill 1C', 25, 50), - ('Drill 1D', 25, 50), - ('Drill 1E', 25, 50); +INSERT INTO store_item(name, price, claim_amount, completion_time_mins) VALUES + ('Drill A', 25, 50, 2), + ('Drill B', 25, 50, 2), + ('Drill C', 25, 50, 2), + ('Drill D', 25, 50, 2), + ('Drill E', 25, 50, 2); diff --git a/mm.py b/mm.py index 6cdbb2d..7b168c8 100644 --- a/mm.py +++ b/mm.py @@ -24,17 +24,39 @@ def get_bank(): def get_store_items(): cursor = conn.cursor() - cursor.execute("SELECT store_item.id,store_item.name,price,claim_amount FROM store_item") + cursor.execute(""" + SELECT store_item.id, store_item.name, price, claim_amount, completion_time_mins + FROM store_item + """) items = {item[0]:item[1:] for item in cursor.fetchall()} cursor.close() return items +def get_stakes(): + cursor = conn.cursor() + + cursor.execute(""" + SELECT staking_event.id, well_id, inventory_item_id, stake_amount, 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 + # 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, + SELECT staking_event.id, well_id, inventory_item_id, stake_amount, duration_in_mins, staking_event.created_at FROM staking_event LEFT JOIN claim_event ON staking_event.id = claim_event.staking_event_id @@ -63,25 +85,6 @@ def get_wells(source_id): 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() @@ -188,10 +191,11 @@ def sell_item(item_id): def mine(well_id, item_id): cursor = conn.cursor() + store_item = world.store[world.inventory[item_id][0]] cursor.execute(""" - INSERT INTO staking_event (well_id, inventory_item_id, duration_in_mins) - VALUES (?, ?, ?) - """, (well_id, item_id, 3)) + INSERT INTO staking_event (well_id, inventory_item_id, stake_amount, duration_in_mins) + VALUES (?, ?, ?, ?) + """, (well_id, item_id, store_item[2], store_item[3])) conn.commit() world.staking_sources = get_moons() @@ -201,14 +205,37 @@ def mine(well_id, item_id): def claim(staking_event_id): cursor = conn.cursor() - cursor.execute("INSERT INTO claim_event (claim_amount, staking_event_id) VALUES (?, ?)", - (10, staking_event_id)) + claim_amount = world.stakes[staking_event_id][2] + well_id = world.stakes[staking_event_id][0] + try: + cursor.execute("SELECT supply FROM resource_well WHERE id = ?", + (well_id,)) + supply = cursor.fetchone()[0] + claim_amount = claim_amount if supply >= claim_amount else supply + print(claim_amount) + cursor.execute(""" + UPDATE resource_well SET supply = supply - ? WHERE resource_well.id = ? + """, (claim_amount, well_id)) - conn.commit() - world.staking_sources = get_moons() - world.stakes = get_stakes() - cursor.close() + cursor.execute("INSERT INTO claim_event (claim_amount, staking_event_id) VALUES (?, ?)", + (claim_amount, staking_event_id)) + cursor.execute(""" + WITH w_res_id AS (SELECT resource_id FROM resource_well WHERE id = ?) + UPDATE resource_account SET balance = balance + ? + FROM w_res_id + WHERE resource_account.user_id = ? + AND resource_account.resource_id = w_res_id.resource_id + """, (well_id, claim_amount, world.current_user_id)) + except sql.Error as error: + print(error) + conn.rollback() + finally: + conn.commit() + world.stakes = get_stakes() + world.staking_sources = get_moons() + world.bank = get_bank() + cursor.close() def upgrade(item_id): cursor = conn.cursor() @@ -248,12 +275,12 @@ def draw_dashboard(): im.text(f"{name.capitalize()}: {balance}") def draw_store(): - for id,(name,price,claim) in world.store.items(): + for id,(name,price,claim,duration) in world.store.items(): owned = False for (store_item_id,_) in world.inventory.values(): if id == store_item_id: owned = True - im.text(f"{name}: Mine {claim}") + im.text(f"{name}: Mine {claim} in {duration} mins") if owned: im.text_disabled(f"Buy {price}") @@ -312,7 +339,7 @@ def draw_moons(): item_name = world.store[store_id][0] skip = False for ss in world.stakes.values(): - if ss[1] == item_id and ss[4] == True: + if ss[1] == item_id and ss[4] == True or supply == 0: skip = True if skip == True: continue @@ -324,18 +351,20 @@ def draw_moons(): im.spacing() im.next_column() - for stake_id,(_,invId,dur,start) in stakes.items(): + for stake_id,(_,invId,amount,dur,start) in stakes.items(): start_time = parse(start) now = datetime.datetime.utcnow().replace(microsecond=0) - delta = now - start_time - im.push_id(f"Claim{stake_id}") - if delta > datetime.timedelta(minutes=dur): + elapsed = now - start_time + remaining = start_time + datetime.timedelta(minutes=dur) - now + if elapsed > datetime.timedelta(minutes=dur): + im.push_id(f"Claim{stake_id}") if im.button("Claim"): claim(stake_id) + im.pop_id() + im.text(f"Finished {elapsed} ago") else: im.text_disabled("Claim") - im.pop_id() - im.text(f"{world.store[world.inventory[invId][0]][0]}: {delta}") + im.text(f"{remaining} left") im.next_column() diff --git a/tables.sql b/tables.sql index 23c465d..ce451b8 100644 --- a/tables.sql +++ b/tables.sql @@ -36,6 +36,7 @@ CREATE TABLE staking_event( well_id int not null, inventory_item_id int not null, duration_in_mins int not null, + stake_amount int not null, created_at timestamp DEFAULT (current_timestamp), CONSTRAINT fk_wid FOREIGN KEY(well_id) REFERENCES resource_well(id) @@ -64,7 +65,8 @@ CREATE TABLE store_item( id integer primary key autoincrement, name varchar(128) not null, price int not null, - claim_amount int not null + claim_amount int not null, + completion_time_mins int not null ); CREATE TABLE inventory_item(