billionaire/main.py

193 lines
8.5 KiB
Python

#TODO comments explaining stuff
import data # Import data.py which contains the businesses and leaderboards
from datetime import datetime
import math
try:
import save # TODO saving functionality
except:
with open("save.py", "w") as file:
content = f"businesses = []\nmoney = 100\ndatetime = {datetime.now().strftime('%s')}\nfine_count = 0"
# Commands
# help: help's the user
# status: output's networth, available money, TL, your businesses and earning/hour
# shop/buy: lists available businesses
# sell: sells that business
# leaderboard: shows the wealth leaderboard
# quit: exit's game
# TODO upgrade (?): upgrades some stats about a business for some money
# 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
# permanent saves
# This function accepts a number and returns the value millified
# For example 1000 becomes 1K
# 135220 becomes 135.22K
# 25012345 becomes 25.01M etc
# millnames can be redefined in data.py
def millify(n):
exponent = len(str(round(n))) - 1 # n = 1,000 return 3, n = 300,000 returns 5
millvalue = exponent//3 # millvalue = how many sets of 3 zero's there are after the mill, 1000 -> 999999 all equals 3, 1million -> 999 million all equal 6, etc
millname = data.millnames[millvalue] # exponent = 3 returns K (thousand), exponent = 7 returns M (million)
return f"{round((n/(10**(millvalue*3))),2)}{millname}" # Returns the formatted value
# Returns a random bool based on a percentage, 100 or more always returns True
def rand_bool(percent):
return random.randrange(100) < percent
# Every day there is a chance that you will be fined based on the threat level
# This function checks if the day has changed, if it has then it uses your threat level to
# randomly fine you or not, eg a threat level of 100% or more (threat level MIDNIGHT) will always
# result in an arrest and a threat level of 5% will result in a fine 1/20 of the time
# Every time you get a fine the value increases based on the fine values in data.fines
# If all fines have been received then each fine results in the highest fine in data.fines
def try_fine():
current_day = datetime.now().strftime("%d")
current_month = datetime.now().strftime("%m")
current_year = datetime.now().strftime("%Y")
previous_date = datetime.fromtimestamp(int(save.datetime))
# Checks if the day, month or year has changed
if previous_date.strftime("%d") != current_day or previous_date.strftime("%m") != current_month or previous_date.strftime("%Y") != current_year:
# Work out the current threat level
for biz in save.businesses:
threat += biz['cost']
# Randomly decide to fine or not based on threat level
if rand_bool(threat):
# If the user has been fined more times than there are fines in data.fines, fine them the last fine in that list
if save.fine_count >= len(data.fines):
save.money -= data.fines[len(data.fines)-1]
print(f"You've been fined {data.currency}{data.fines[len(data.fines)-1]}")
# Else just fine them the amount at the current fine index
else:
save.money -= data.fines[save.fine_count]
print(f"You've been fined {data.currency}{data.fines[save.fine_count]}")
save.fine_count += 1 # Increase the amount of times you've been fined
save_game()
def add_new_money():
current_time = datetime.now().strftime("%s")
time_difference = int(current_time) - int(save.datetime)
time_passed = time_difference * data.time_rate
for biz in save.businesses:
earning = biz['earning']
save.money += (earning / 3600) * time_passed
def display_leaderboard():
leaderboard = data.leaderboard[:]
networth = save.money
for biz in save.businesses:
networth += biz['cost']
leaderboard.append(["Yourself", round(networth,2)])
leaderboard = sorted(leaderboard, key=lambda leaderboard: leaderboard[1], reverse=True)
for index in range(len(leaderboard)):
print(f"#{index+1}: {leaderboard[index][0]} {data.currency}{millify(leaderboard[index][1])}")
def save_game():
with open("save.py", "w") as file:
content = f'''businesses = {save.businesses}\nmoney = {save.money}\ndatetime = {save.datetime}\nfine_count = {save.fine_count}'''
file.write(content)
def main():
add_new_money()
print("Welcome, entrepeneur")
game_loop = True
while game_loop:
save.datetime = datetime.now().strftime("%s")
print(f"{data.currency}{millify(save.money)} > ", end="")
user_input = input()
# TODO make this a match case?
if user_input.lower() == "quit" or user_input.lower() == "exit":
save.datetime = datetime.now().strftime("%s")
add_new_money()
save_game()
game_loop = False
### Buy a new business
elif user_input.lower() == "buy" or user_input.lower() == "shop":
print(" 0: cancel")
for i in range(len(data.businesses)):
if data.businesses[i]['cost'] <= save.money:
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()
try:
user_input_int = int(user_input) - 1
if user_input_int == -1:
continue
elif user_input_int < len(data.businesses) and save.money >= data.businesses[user_input_int]['cost']:
print(f"Buying {data.businesses[user_input_int]['pretty_name']}")
save.businesses.append(data.businesses[user_input_int])
save.money -= data.businesses[user_input_int]['cost']
else:
print("Invalid number")
except:
print("Didn't input a number")
### Sell an owned business
elif user_input.lower() == "sell":
print(" 0: cancel")
for i in range(len(save.businesses)):
print(f" {i+1}: {save.businesses[i]['pretty_name']}: {data.currency}{millify(save.businesses[i]['cost'])}")
user_input = input()
user_input_int = int(user_input) - 1
if user_input_int == -1:
continue
elif user_input_int < len(data.businesses):
print(f"Selling {save.businesses[user_input_int]['pretty_name']}")
save.money += save.businesses[user_input_int]['cost']
save.businesses.pop(user_input_int)
else:
print("Invalid number")
### Display info relavent to your enterprise
elif user_input.lower() == "status":
print("------------- STATUS -------------")
print(f"Money Available: {data.currency}{millify(save.money)}")
print("Your businesses:")
total_earning = 0
networth = save.money
threat = 0
for biz in save.businesses:
print(f" {biz['pretty_name']}: {data.currency}{millify(biz['earning'])}/hour")
total_earning += biz['earning']
networth += biz['cost']
threat += biz['threat']
if threat < 100:
print(f"Threat Level: {threat}%")
else:
print("Threat Level: MIDNIGHT")
print(f"Total earning: {data.currency}{millify(total_earning)}/hour")
print(f"Networth: {data.currency}{millify(networth)}")
print("----------------------------------")
### Display leaderboard
elif user_input.lower() == "leaderboard":
display_leaderboard()
elif user_input.lower() == "help":
print('''
Commands
help: help's the user
status: output's networth, available money, threat level, your businesses and earning/hour
shop/buy: lists available businesses
sell: sells that business
leaderboard: shows the wealth leaderboard
quit: exit's game
Additional functionality:
randomly get fined based on threat level
permanent saves
''')
elif user_input == "":
print("",end="")
else:
print("Error: Invalid command")
### End of loop
add_new_money()
try_fine()
save_game()
main()