:spakles: Database and OOP
This commit is contained in:
@ -1,131 +1,137 @@
|
||||
import json
|
||||
class Invest():
|
||||
def __init__(self,bot):
|
||||
self.bot = bot
|
||||
|
||||
from funcs import getName
|
||||
from .money import checkBalance, addMoney
|
||||
def getPrice(self, symbol : str):
|
||||
res = self.bot.finnhubClient.quote(symbol.upper())
|
||||
if res == {}:
|
||||
return 0
|
||||
else:
|
||||
return int(res["c"] * 100)
|
||||
|
||||
def getPrice(symbol : str,finnhubClient):
|
||||
res = finnhubClient.quote(symbol.upper())
|
||||
if res == {}:
|
||||
return 0
|
||||
else:
|
||||
return int(res["c"] * 100)
|
||||
def getPortfolio(self, user : str):
|
||||
userInvestments = self.bot.database["investments"].find_one({"_id":user})
|
||||
|
||||
def getPortfolio(user : str,finnhubClient):
|
||||
with open("resources/games/investments.json") as f:
|
||||
data = json.load(f)
|
||||
if userInvestments in [None,{}]:
|
||||
return f"{self.bot.funcs.getName(user)} does not have a stock portfolio."
|
||||
else:
|
||||
portfolio = f"**Stock portfolio for {self.bot.funcs.getName(user)}**"
|
||||
|
||||
if user not in data or data[user] == {}:
|
||||
return f"{getName(user)} does not have a stock portfolio."
|
||||
else:
|
||||
portfolio = f"**Stock portfolio for {getName(user)}**"
|
||||
for key, value in list(userInvestments["investments"].items()):
|
||||
purchaseValue = value["purchased for"]
|
||||
currentValue = int((self.getPrice(key) / value["value at purchase"]) * value["purchased"])
|
||||
if purchaseValue == "?":
|
||||
portfolio += f"\n**{key}**: ___{str(currentValue)} GwendoBucks___"
|
||||
else:
|
||||
portfolio += f"\n**{key}**: ___{str(currentValue)} GwendoBucks___ (purchased for {str(purchaseValue)})"
|
||||
|
||||
for key, value in list(data[user].items()):
|
||||
purchaseValue = value["purchased for"]
|
||||
currentValue = int((getPrice(key,finnhubClient) / value["value at purchase"]) * value["purchased"])
|
||||
if purchaseValue == "?":
|
||||
portfolio += f"\n**{key}**: ___{str(currentValue)} GwendoBucks___"
|
||||
else:
|
||||
portfolio += f"\n**{key}**: ___{str(currentValue)} GwendoBucks___ (purchased for {str(purchaseValue)})"
|
||||
return portfolio
|
||||
|
||||
return portfolio
|
||||
def buyStock(self, user : str, stock : str, buyAmount : int):
|
||||
if buyAmount >= 100:
|
||||
if self.bot.money.checkBalance(user) >= buyAmount:
|
||||
stockPrice = self.getPrice(stock)
|
||||
if stockPrice > 0:
|
||||
userInvestments = self.bot.database["investments"].find_one({"_id":user})
|
||||
|
||||
def buyStock(user : str, stock : str, buyAmount : int,finnhubClient):
|
||||
if buyAmount >= 100:
|
||||
if checkBalance(user) >= buyAmount:
|
||||
stockPrice = getPrice(stock,finnhubClient)
|
||||
if stockPrice > 0:
|
||||
with open("resources/games/investments.json", "r") as f:
|
||||
data = json.load(f)
|
||||
self.bot.money.addMoney(user,-1*buyAmount)
|
||||
stock = stock.upper()
|
||||
|
||||
addMoney(user,-1*buyAmount)
|
||||
stock = stock.upper()
|
||||
if userInvestments != None:
|
||||
userInvestments = userInvestments["investments"]
|
||||
if stock in userInvestments:
|
||||
value = userInvestments[stock]
|
||||
newAmount = int((stockPrice / value["value at purchase"]) * value["purchased"]) + buyAmount
|
||||
|
||||
if user in data:
|
||||
if stock in data[user]:
|
||||
value = data[user][stock]
|
||||
newAmount = int((stockPrice / value["value at purchase"]) * value["purchased"]) + buyAmount
|
||||
self.bot.database["investments"].update_one({"_id":user},
|
||||
{"$set":{"investments."+stock+".value at purchase" : stockPrice}})
|
||||
|
||||
data[user][stock]["value at purchase"] = stockPrice
|
||||
data[user][stock]["purchased"] = newAmount
|
||||
if value["purchased for"] != "?":
|
||||
data[user][stock]["purchased for"] += buyAmount
|
||||
self.bot.database["investments"].update_one({"_id":user},
|
||||
{"$set":{"investments."+stock+".purchased" : newAmount}})
|
||||
|
||||
if value["purchased for"] != "?":
|
||||
self.bot.database["investments"].update_one({"_id":user},
|
||||
{"$set":{"investments."+stock+".purchased for" : buyAmount}})
|
||||
else:
|
||||
self.bot.database["investments"].update_one({"_id":user},
|
||||
{"$set":{"investments."+stock : {"purchased" : buyAmount, "value at purchase" : stockPrice,
|
||||
"purchased for" : buyAmount}}})
|
||||
else:
|
||||
data[user][stock] = {"purchased" : buyAmount, "value at purchase" : stockPrice, "purchased for" : buyAmount}
|
||||
newUser = {"_id":user,"investments":{stock : {"purchased" : buyAmount, "value at purchase" : stockPrice, "purchased for" : buyAmount}}}
|
||||
self.bot.database["investments"].insert_one(newUser)
|
||||
|
||||
return f"{self.bot.funcs.getName(user)} bought {buyAmount} GwendoBucks worth of {stock} stock"
|
||||
else:
|
||||
data[user] = {stock : {"purchased" : buyAmount, "value at purchase" : stockPrice, "purchased for" : buyAmount}}
|
||||
|
||||
with open("resources/games/investments.json", "w") as f:
|
||||
json.dump(data,f,indent=4)
|
||||
|
||||
return f"{getName(user)} bought {buyAmount} GwendoBucks worth of {stock} stock"
|
||||
return f"{stock} is not traded on the american market."
|
||||
else:
|
||||
return f"{stock} is not traded on the american market."
|
||||
return "You don't have enough money for that"
|
||||
else:
|
||||
return "You don't have enough money for that"
|
||||
else:
|
||||
return "You cannot buy stocks for less than 100 GwendoBucks"
|
||||
return "You cannot buy stocks for less than 100 GwendoBucks"
|
||||
|
||||
def sellStock(user : str, stock : str, sellAmount : int,finnhubClient):
|
||||
if sellAmount > 0:
|
||||
with open("resources/games/investments.json", "r") as f:
|
||||
data = json.load(f)
|
||||
def sellStock(self, user : str, stock : str, sellAmount : int):
|
||||
if sellAmount > 0:
|
||||
|
||||
stock = stock.upper()
|
||||
userInvestments = self.bot.database["investments"].find_one({"_id":user})["investments"]
|
||||
|
||||
if user in data and stock in data[user]:
|
||||
value = data[user][stock]
|
||||
stockPrice = getPrice(stock,finnhubClient)
|
||||
data[user][stock]["purchased"] = int((stockPrice / value["value at purchase"]) * value["purchased"])
|
||||
data[user][stock]["value at purchase"] = stockPrice
|
||||
if value["purchased"] >= sellAmount:
|
||||
stock = stock.upper()
|
||||
|
||||
addMoney(user,sellAmount)
|
||||
if sellAmount < value["purchased"]:
|
||||
data[user][stock]["purchased"] -= sellAmount
|
||||
data[user][stock]["purchased for"] = "?"
|
||||
if userInvestments != None and stock in userInvestments:
|
||||
value = userInvestments[stock]
|
||||
stockPrice = self.getPrice(stock)
|
||||
self.bot.database["investments"].update_one({"_id":user},
|
||||
{"$set":{"investments."+stock+".purchased" :
|
||||
int((stockPrice / value["value at purchase"]) * value["purchased"])}})
|
||||
self.bot.database["investments"].update_one({"_id":user},
|
||||
{"$set":{"investments."+stock+".value at purchase" : stockPrice}})
|
||||
if value["purchased"] >= sellAmount:
|
||||
self.bot.money.addMoney(user,sellAmount)
|
||||
if sellAmount < value["purchased"]:
|
||||
self.bot.database["investments"].update_one({"_id":user},
|
||||
{"$inc":{"investments."+stock+".purchased" : -sellAmount}})
|
||||
|
||||
self.bot.database["investments"].update_one({"_id":user},
|
||||
{"$set":{"investments."+stock+".purchased for" : "?"}})
|
||||
else:
|
||||
self.bot.database["investments"].update_one({"_id":user},
|
||||
{"$unset":{"investments."+stock:""}})
|
||||
|
||||
return f"{self.bot.funcs.getName(user)} sold {sellAmount} GwendoBucks worth of {stock} stock"
|
||||
else:
|
||||
del data[user][stock]
|
||||
|
||||
with open("resources/games/investments.json", "w") as f:
|
||||
json.dump(data,f,indent=4)
|
||||
|
||||
return f"{getName(user)} sold {sellAmount} GwendoBucks worth of {stock} stock"
|
||||
return f"You don't have enough {stock} stocks to do that"
|
||||
else:
|
||||
return f"You don't have enough {stock} stocks to do that"
|
||||
return f"You don't have any {stock} stock"
|
||||
else:
|
||||
return f"You don't have any {stock} stock"
|
||||
else:
|
||||
return "no"
|
||||
return "no"
|
||||
|
||||
def parseInvest(content: str, user : str,finnhubClient):
|
||||
if content.startswith("check"):
|
||||
commands = content.split(" ")
|
||||
if len(commands) == 1:
|
||||
return getPortfolio(user,finnhubClient)
|
||||
else:
|
||||
price = getPrice(commands[1],finnhubClient)
|
||||
if price == 0:
|
||||
return f"{commands[1].upper()} is not traded on the american market."
|
||||
def parseInvest(self, content: str, user : str):
|
||||
if content.startswith("check"):
|
||||
commands = content.split(" ")
|
||||
if len(commands) == 1:
|
||||
return self.getPortfolio(user)
|
||||
else:
|
||||
price = f"{price:,}".replace(",",".")
|
||||
return f"The current {commands[1].upper()} stock is valued at **{price}** GwendoBucks"
|
||||
price = self.getPrice(commands[1])
|
||||
if price == 0:
|
||||
return f"{commands[1].upper()} is not traded on the american market."
|
||||
else:
|
||||
price = f"{price:,}".replace(",",".")
|
||||
return f"The current {commands[1].upper()} stock is valued at **{price}** GwendoBucks"
|
||||
|
||||
elif content.startswith("buy"):
|
||||
commands = content.split(" ")
|
||||
if len(commands) == 3:
|
||||
try:
|
||||
return buyStock(user,commands[1],int(commands[2]),finnhubClient)
|
||||
except:
|
||||
return "The command must be given as \"!invest buy [stock] [amount of GwendoBucks to purchase with]\""
|
||||
else:
|
||||
return "You must give both a stock name and an amount of gwendobucks you wish to spend."
|
||||
elif content.startswith("buy"):
|
||||
commands = content.split(" ")
|
||||
if len(commands) == 3:
|
||||
#try:
|
||||
return self.buyStock(user,commands[1],int(commands[2]))
|
||||
#except:
|
||||
# return "The command must be given as \"!invest buy [stock] [amount of GwendoBucks to purchase with]\""
|
||||
else:
|
||||
return "You must give both a stock name and an amount of gwendobucks you wish to spend."
|
||||
|
||||
elif content.startswith("sell"):
|
||||
commands = content.split(" ")
|
||||
if len(commands) == 3:
|
||||
try:
|
||||
return sellStock(user,commands[1],int(commands[2]),finnhubClient)
|
||||
except:
|
||||
return "The command must be given as \"!invest sell [stock] [amount of GwendoBucks to sell stocks for]\""
|
||||
else:
|
||||
return "You must give both a stock name and an amount of GwendoBucks you wish to sell stocks for."
|
||||
elif content.startswith("sell"):
|
||||
commands = content.split(" ")
|
||||
if len(commands) == 3:
|
||||
try:
|
||||
return self.sellStock(user,commands[1],int(commands[2]))
|
||||
except:
|
||||
return "The command must be given as \"!invest sell [stock] [amount of GwendoBucks to sell stocks for]\""
|
||||
else:
|
||||
return "You must give both a stock name and an amount of GwendoBucks you wish to sell stocks for."
|
||||
|
Reference in New Issue
Block a user