Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
feat(command): Add invest loss differences
Browse files Browse the repository at this point in the history
Invest loss differences based on the time you invest for.

A short term investment will lose between 50%-75% if it fails
A long term investment will lose between 25%-50% if it fails
  • Loading branch information
falconnine9 authored Jul 10, 2021
1 parent ffed692 commit bc318fc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
17 changes: 13 additions & 4 deletions src/Commands/Earning/invest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
"short": [0.6, 0.75],
"long": [0.15, 0.4]
} # Random number in between the 2 in the list
_loss_amounts = {
"short": [0.5, 0.75],
"long": [0.25, 0.5]
} # Random number in between the 2 in the list


async def run(bot, msg, conn):
Expand Down Expand Up @@ -111,22 +115,27 @@ async def run(bot, msg, conn):
random.uniform(_failed_chances[split_data[2]][0], _failed_chances[split_data[2]][1]), 1
)

new_timer = abc.InvestTimer(msg.author.id, random_time, amount, random_multi, failed)
if failed:
loss = round(random.uniform(_loss_amounts[split_data[2]][0], _loss_amounts[split_data[2]][1]), 2)
else:
loss = None

new_timer = abc.InvestTimer(msg.author.id, random_time, amount, random_multi, failed, loss)

user.set_user_attr(msg.author.id, "coins", userinfo.coins - amount, conn, False)
timer.new_invest_timer(new_timer, conn)

await message.response_edit(sent_msg, interaction,
f"You invested {amount} {emojis.coin} for {timer.load_time(random_time)}", title="Invested",
f"You invested {amount:,} {emojis.coin} for {timer.load_time(random_time)}", title="Invested",
from_reaction=userinfo.settings.reaction_confirm
)


async def _reaction_confirm(bot, msg, split_data, amount, emojis):
sent_msg = await message.send_message(msg, f"""Are you sure you want to invest {amount} {emojis.coin}
sent_msg = await message.send_message(msg, f"""Are you sure you want to invest {amount:,} {emojis.coin}
It will take in between {_times[split_data[2]][0]}h to {_times[split_data[2]][1]}h to complete
And you will earn in between {round(_multipliers[split_data[2]][0] * amount)} {emojis.coin} to {round(_multipliers[split_data[2]][1] * amount)} {emojis.coin}""",
And you will earn in between {round(_multipliers[split_data[2]][0] * amount):,} {emojis.coin} to {round(_multipliers[split_data[2]][1] * amount):,} {emojis.coin}""",
title="Investing.."
)

Expand Down
1 change: 1 addition & 0 deletions src/Routine/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"coins": "BIGINT UNSIGNED",
"multiplier": "REAL",
"failed": "TINYINT",
"loss": "REAL",
"CONSTRAINT invest_timers_pk": "PRIMARY KEY (id)"
},
"companies": {
Expand Down
13 changes: 9 additions & 4 deletions src/Routine/loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,18 @@ async def invest_timer(bot, conn):
completed_users = cur.fetchall()

for c_user in completed_users:
timerinfo = abc.InvestTimer(c_user[0], c_user[1], c_user[2], c_user[3], c_user[4])
timerinfo = abc.InvestTimer(c_user[0], c_user[1], c_user[2], c_user[3], c_user[4], c_user[5])
c_userinfo = user.load_user(timerinfo.id, conn)

if c_userinfo is not None:
if timerinfo.failed:
user.set_user_attr(timerinfo.id, "coins", c_userinfo.coins + round(timerinfo.coins / 4), conn, False)
user.set_user_attr(timerinfo.id, "coins",
c_userinfo.coins + (c_userinfo.coins - round(c_userinfo.coins * timerinfo.loss)), conn, False
)
else:
user.set_user_attr(timerinfo.id, "coins", c_userinfo.coins + round(timerinfo.coins * timerinfo.multiplier), conn, False)
user.set_user_attr(timerinfo.id, "coins",
c_userinfo.coins + round(timerinfo.coins * timerinfo.multiplier), conn, False
)
bot.loop.create_task(_invest_timer_alert(bot, timerinfo, c_userinfo, emojis))

cur.execute("DELETE FROM invest_timers WHERE time <= 0")
Expand All @@ -139,7 +143,8 @@ async def _invest_timer_alert(bot, timerinfo, c_userinfo, emojis):
if c_userinfo.settings.notifs["investment"]:
try:
if timerinfo.failed:
await message.send_message(None, f"Your investment failed and you lost 75% of your investment",
await message.send_message(None,
f"Your investment failed and you lost {round(timerinfo.loss * 100)}% of your investment",
title="Investment notification", channel=c_user_obj
)
else:
Expand Down
6 changes: 4 additions & 2 deletions src/Utils/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,22 @@ def cvt_dict(self):


class InvestTimer:
def __init__(self, user_id, time, coins, multiplier, failed):
def __init__(self, user_id, time, coins, multiplier, failed, loss):
self.id = user_id
self.time = time
self.coins = coins
self.multiplier = multiplier
self.failed = failed
self.loss = loss

def cvt_dict(self):
return {
"id": self.id,
"time": self.time,
"coins": self.coins,
"multiplier": self.multiplier,
"failed": self.failed
"failed": self.failed,
"loss": self.loss
}


Expand Down
15 changes: 12 additions & 3 deletions src/Utils/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ def load_invest_timer(user_id, conn):
if timerinfo is None:
return None

return abc.InvestTimer(timerinfo[0], timerinfo[1], timerinfo[2], timerinfo[3], timerinfo[4])
return abc.InvestTimer(
timerinfo[0], timerinfo[1],
timerinfo[2], timerinfo[3],
timerinfo[4], timerinfo[5]
)


def new_invest_timer(timer, conn, write=True):
Expand All @@ -103,8 +107,13 @@ def new_invest_timer(timer, conn, write=True):
2. Commits if the write parameter is true
"""
cur = conn.cursor()
cur.execute("INSERT INTO invest_timers VALUES (%s, %s, %s, %s, %s)", (
timer.id, timer.time, timer.coins, timer.multiplier, timer.failed
cur.execute("INSERT INTO invest_timers VALUES (%s, %s, %s, %s, %s, %s)", (
timer.id,
timer.time,
timer.coins,
timer.multiplier,
timer.failed,
timer.loss
))

if write:
Expand Down

0 comments on commit bc318fc

Please sign in to comment.