Fixed some selling bugs and millify on all money outputs)

This commit is contained in:
DeaDvey 2025-07-08 01:21:41 +01:00
parent 7b36c588e1
commit 0f54e2533b

55
main.py
View File

@ -1,3 +1,4 @@
#TODO comments explaining stuff
import data # Import data.py which contains the businesses and leaderboards import data # Import data.py which contains the businesses and leaderboards
from datetime import datetime from datetime import datetime
import math import math
@ -7,28 +8,25 @@ except:
with open("save.py", "w") as file: with open("save.py", "w") as file:
content = f"businesses = []\nmoney = 100\ndatetime = {datetime.now().strftime('%s')}\nfine_count = 0" content = f"businesses = []\nmoney = 100\ndatetime = {datetime.now().strftime('%s')}\nfine_count = 0"
# Commands # Commands
# TODO help: help's the user # help: help's the user
# status: output's networth, available money, TL, your businesses and earning/hour # status: output's networth, available money, TL, your businesses and earning/hour
# shop/buy: lists available businesses # shop/buy: lists available businesses
# sell: sells that business # sell: sells that business
# leaderboard: shows the wealth leaderboard # leaderboard: shows the wealth leaderboard
# quit: exit's game # quit: exit's game
# TODO upgrade: upgrades some stats about a business for some money
# Additional functionality: # Additional functionality:
# randomly get sent to get fined based on threat level, more than 5 fines means next time is prision and that's 2 years ingame # randomly get sent to get fined based on threat level, more than 5 fines means next time is prision and that's 2 years ingame
# permanent saves # permanent saves
# https://stackoverflow.com/questions/3154460/python-human-readable-large-numbers
def millify(n): def millify(n):
n = float(n) exponent = len(str(round(n))) - 1 # n = 1,000 return 3, n = 300,000 returns 5
millidx = max( millvalue = exponent//3
0, millname = data.millnames[millvalue] # exponent = 3 returns K (thousand), exponent = 7 returns M (million)
min( #print(f"E: {exponent}, M: {millname}, Mv: {millvalue} = ",end="")
len(data.millnames)-1, return f"{round((n/(10**(millvalue*3))),2)}{millname}"
int(math.floor(0 if n == 0 else math.log10(abs(n))/3)) #return '{:.0f}{}'.format(n / 10**(3 * millidx), data.millnames[millidx])
)
)
return '{:.0f}{}'.format(n / 10**(3 * millidx), data.millnames[millidx])
def rand_bool(percent): def rand_bool(percent):
return random.randrange(100) < percent return random.randrange(100) < percent
@ -79,7 +77,7 @@ def main():
game_loop = True game_loop = True
while game_loop: while game_loop:
save.datetime = datetime.now().strftime("%s") save.datetime = datetime.now().strftime("%s")
print(f"{data.currency}{round(save.money,2)} > ", end="") print(f"{data.currency}{millify(save.money)} > ", end="")
user_input = input() user_input = input()
# TODO make this a match case? # TODO make this a match case?
if user_input.lower() == "quit" or user_input.lower() == "exit": if user_input.lower() == "quit" or user_input.lower() == "exit":
@ -93,7 +91,7 @@ def main():
print(" 0: cancel") print(" 0: cancel")
for i in range(len(data.businesses)): for i in range(len(data.businesses)):
if data.businesses[i]['cost'] <= save.money: if data.businesses[i]['cost'] <= save.money:
print(f" {i+1}: {data.businesses[i]['pretty_name']}: {data.currency}{data.businesses[i]['cost']}, Earning: {data.currency}{data.businesses[i]['earning']}/hour, Threat: {data.businesses[i]['threat']}%") print(f" {i+1}: {data.businesses[i]['pretty_name']}: {data.currency}{millify(data.businesses[i]['cost'])}, Earning: {data.currency}{millify(data.businesses[i]['earning'])}/hour, Threat: {data.businesses[i]['threat']}%")
user_input = input() user_input = input()
try: try:
user_input_int = int(user_input) - 1 user_input_int = int(user_input) - 1
@ -112,31 +110,28 @@ def main():
elif user_input.lower() == "sell": elif user_input.lower() == "sell":
print(" 0: cancel") print(" 0: cancel")
for i in range(len(save.businesses)): for i in range(len(save.businesses)):
print(f" {i+1}: {save.businesses[i]['pretty_name']}: {data.currency}{save.businesses[i]['cost']}") print(f" {i+1}: {save.businesses[i]['pretty_name']}: {data.currency}{millify(save.businesses[i]['cost'])}")
user_input = input() user_input = input()
try: user_input_int = int(user_input) - 1
user_input_int = int(user_input) - 1 if user_input_int == -1:
if user_input_int == -1: continue
continue elif user_input_int < len(data.businesses):
elif user_input_int < len(data.businesses): print(f"Selling {save.businesses[user_input_int]['pretty_name']}")
print(f"Selling {data.businesses[user_input_int]['pretty_name']}") save.money += save.businesses[user_input_int]['cost']
save.businesses.pop(user_input_int) save.businesses.pop(user_input_int)
save.money += data.businesses[user_input_int]['cost'] else:
else: print("Invalid number")
print("Invalid number")
except:
print("Didn't input a number")
### Display info relavent to your enterprise ### Display info relavent to your enterprise
elif user_input.lower() == "status": elif user_input.lower() == "status":
print("------------- STATUS -------------") print("------------- STATUS -------------")
print(f"Money Available: {data.currency}{round(save.money,2)}") print(f"Money Available: {data.currency}{millify(save.money)}")
print("Your businesses:") print("Your businesses:")
total_earning = 0 total_earning = 0
networth = save.money networth = save.money
threat = 0 threat = 0
for biz in save.businesses: for biz in save.businesses:
print(f" {biz['pretty_name']}: {data.currency}{biz['earning']}/hour") print(f" {biz['pretty_name']}: {data.currency}{millify(biz['earning'])}/hour")
total_earning += biz['earning'] total_earning += biz['earning']
networth += biz['cost'] networth += biz['cost']
threat += biz['threat'] threat += biz['threat']
@ -145,8 +140,8 @@ def main():
print(f"Threat Level: {threat}%") print(f"Threat Level: {threat}%")
else: else:
print("Threat Level: MIDNIGHT") print("Threat Level: MIDNIGHT")
print(f"Total earning: {data.currency}{total_earning}/hour") print(f"Total earning: {data.currency}{millify(total_earning)}/hour")
print(f"Networth: {data.currency}{round(networth,2)}") print(f"Networth: {data.currency}{millify(networth)}")
print("----------------------------------") print("----------------------------------")
### Display leaderboard ### Display leaderboard