Claim adds to resource_account and deducts from resource_well

This commit is contained in:
Joseph Ferano 2023-02-25 13:35:31 +07:00
parent 5d05ee372a
commit dd42a9880e
3 changed files with 77 additions and 46 deletions

View File

@ -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 bank_account(user_id, balance) VALUES (1, 500);
INSERT INTO store_item(name, price, claim_amount) VALUES INSERT INTO store_item(name, price, claim_amount, completion_time_mins) VALUES
('Drill 1A', 25, 50), ('Drill A', 25, 50, 2),
('Drill 1B', 25, 50), ('Drill B', 25, 50, 2),
('Drill 1C', 25, 50), ('Drill C', 25, 50, 2),
('Drill 1D', 25, 50), ('Drill D', 25, 50, 2),
('Drill 1E', 25, 50); ('Drill E', 25, 50, 2);

107
mm.py
View File

@ -24,17 +24,39 @@ def get_bank():
def get_store_items(): def get_store_items():
cursor = conn.cursor() 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()} items = {item[0]:item[1:] for item in cursor.fetchall()}
cursor.close() cursor.close()
return items 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 # An active stake is basically defined as one that has yet to be claimed
def get_active_stakes(source_id): def get_active_stakes(source_id):
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute(""" 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 staking_event.created_at
FROM staking_event 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
@ -63,25 +85,6 @@ def get_wells(source_id):
cursor.close() cursor.close()
return wells 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(): def get_moons():
cursor = conn.cursor() cursor = conn.cursor()
@ -188,10 +191,11 @@ def sell_item(item_id):
def mine(well_id, item_id): def mine(well_id, item_id):
cursor = conn.cursor() cursor = conn.cursor()
store_item = world.store[world.inventory[item_id][0]]
cursor.execute(""" cursor.execute("""
INSERT INTO staking_event (well_id, inventory_item_id, duration_in_mins) INSERT INTO staking_event (well_id, inventory_item_id, stake_amount, duration_in_mins)
VALUES (?, ?, ?) VALUES (?, ?, ?, ?)
""", (well_id, item_id, 3)) """, (well_id, item_id, store_item[2], store_item[3]))
conn.commit() conn.commit()
world.staking_sources = get_moons() world.staking_sources = get_moons()
@ -201,14 +205,37 @@ def mine(well_id, item_id):
def claim(staking_event_id): def claim(staking_event_id):
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute("INSERT INTO claim_event (claim_amount, staking_event_id) VALUES (?, ?)", claim_amount = world.stakes[staking_event_id][2]
(10, staking_event_id)) 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() cursor.execute("INSERT INTO claim_event (claim_amount, staking_event_id) VALUES (?, ?)",
world.staking_sources = get_moons() (claim_amount, staking_event_id))
world.stakes = get_stakes()
cursor.close()
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): def upgrade(item_id):
cursor = conn.cursor() cursor = conn.cursor()
@ -248,12 +275,12 @@ def draw_dashboard():
im.text(f"{name.capitalize()}: {balance}") im.text(f"{name.capitalize()}: {balance}")
def draw_store(): def draw_store():
for id,(name,price,claim) in world.store.items(): for id,(name,price,claim,duration) in world.store.items():
owned = False owned = False
for (store_item_id,_) in world.inventory.values(): for (store_item_id,_) in world.inventory.values():
if id == store_item_id: if id == store_item_id:
owned = True owned = True
im.text(f"{name}: Mine {claim}") im.text(f"{name}: Mine {claim} in {duration} mins")
if owned: if owned:
im.text_disabled(f"Buy {price}") im.text_disabled(f"Buy {price}")
@ -312,7 +339,7 @@ def draw_moons():
item_name = world.store[store_id][0] item_name = world.store[store_id][0]
skip = False skip = False
for ss in world.stakes.values(): 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 skip = True
if skip == True: if skip == True:
continue continue
@ -324,18 +351,20 @@ def draw_moons():
im.spacing() im.spacing()
im.next_column() 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) start_time = parse(start)
now = datetime.datetime.utcnow().replace(microsecond=0) now = datetime.datetime.utcnow().replace(microsecond=0)
delta = now - start_time elapsed = now - start_time
im.push_id(f"Claim{stake_id}") remaining = start_time + datetime.timedelta(minutes=dur) - now
if delta > datetime.timedelta(minutes=dur): if elapsed > datetime.timedelta(minutes=dur):
im.push_id(f"Claim{stake_id}")
if im.button("Claim"): if im.button("Claim"):
claim(stake_id) claim(stake_id)
im.pop_id()
im.text(f"Finished {elapsed} ago")
else: else:
im.text_disabled("Claim") im.text_disabled("Claim")
im.pop_id() im.text(f"{remaining} left")
im.text(f"{world.store[world.inventory[invId][0]][0]}: {delta}")
im.next_column() im.next_column()

View File

@ -36,6 +36,7 @@ CREATE TABLE staking_event(
well_id int not null, well_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,
stake_amount int not null,
created_at timestamp DEFAULT (current_timestamp), created_at timestamp DEFAULT (current_timestamp),
CONSTRAINT fk_wid FOREIGN KEY(well_id) CONSTRAINT fk_wid FOREIGN KEY(well_id)
REFERENCES resource_well(id) REFERENCES resource_well(id)
@ -64,7 +65,8 @@ CREATE TABLE store_item(
id integer primary key autoincrement, id integer primary key autoincrement,
name varchar(128) not null, name varchar(128) not null,
price int 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( CREATE TABLE inventory_item(