Implement a working job environment

This commit is contained in:
TropiiDev 2025-01-14 17:57:54 -05:00
parent 933eed6627
commit ee4d0f84c5
5 changed files with 337 additions and 23 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
.idea/
.venv/
__pycache__/
__pycache__/
.env

View File

@ -1,8 +1,9 @@
import pymongo
import random
import pymongo
import time
from werkzeug.security import *
client = pymongo.MongoClient("mongodb://192.168.86.221:27017/")
client = pymongo.MongoClient(os.getenv("MONGO_URL"))
db = client.VE
def create_account(name, email, password):
@ -15,7 +16,7 @@ def create_account(name, email, password):
code = random.randint(000000, 999999)
coll.insert_one({"_id": code, "email": email, "password": generate_password_hash(password, "scrypt"), "name": name})
coll.insert_one({"_id": code, "email": email, "password": generate_password_hash(password, "scrypt"), "name": name, "account_status": "default", "job": None, "education": 1})
return code
@ -45,3 +46,56 @@ def find_account(code = None, email = None, password = None):
return code
def get_user_name(code):
coll = db.accounts
user = coll.find_one({"_id": code})
if user is None:
print("The user does not have an account")
return None
return user['name']
def get_account_status(code):
coll = db.accounts
user = coll.find_one({"_id": code})
if user is None:
print("The user does not have an account")
return None
return user['account_status']
def assign_job(code, job_id):
account_coll = db.accounts
job_coll = db.jobs
user = account_coll.find_one({"_id": code})
job = job_coll.find_one({"_id": job_id})
if user is None or job is None:
print("Either that user doesn't exist, or the job doesn't exist")
return None
user_education = user['education']
job_education = job['education']
if user_education < job_education:
print("We are sorry, but you are not qualified for this job."
"Please go to the education center to get a higher education level.")
time.sleep(1)
return False
else:
account_coll.update_one({"_id": code}, {"$set": {"job": job_id}})
return True
def get_user_job(code):
coll = db.accounts
user = coll.find_one({"_id": code})
if user is None:
print("That user could not be found")
return None
return user['job']

View File

@ -1,34 +1,122 @@
import pymongo
import random
from accounts import *
client = pymongo.MongoClient("mongodb://192.168.86.221:27017/")
client = pymongo.MongoClient(os.getenv("MONGO_URL"))
db = client.VE
def create_bank_account(code):
coll = db.bank
user = coll.find_one({"account_id": code})
if user is not None:
print("User already has a bank account")
return None
coll.insert_one({"_id": random.randint(000000, 999999), "account_id": code, "balance": 0, 'active': False})
return True
def get_balance(code):
coll = db.bank
user = coll.find_one({"account_id": code})
if user is None:
create_bank_account(code)
return 0
return user['balance']
def get_cash(code):
coll = db.economy
user = coll.find_one({"account_id": code})
if user is None:
did_create = create_econ_account(code)
if did_create is False:
print("Something went wrong...")
return
print("The user does not have a bank account.")
return None
user = coll.find_one({"account_id": code})
return user['balance']
return user['balance']
return user['cash']
def create_econ_account(code):
coll = db.economy
user = coll.find_one({"account_id": code})
econ_coll = db.economy
bank_coll = db.bank
econ_user = econ_coll.find_one({"account_id": code})
bank_user = bank_coll.find_one({"account_id": code})
if user is not None:
if econ_user is not None or bank_user is not None:
print("User already has an account")
return False
coll.insert_one({"_id": random.randint(000000, 999999), "account_id": code, "balance": 0})
econ_coll.insert_one({"_id": random.randint(000000, 999999), "account_id": code, "cash": 0})
bank_coll.insert_one({"_id": random.randint(000000, 999999), "account_id": code, "balance": 0})
return True
def deposit(code, amount):
bank_coll = db.bank
econ_coll = db.economy
bank_user = bank_coll.find_one({"account_id": code})
econ_user = econ_coll.find_one({"account_id": code})
if bank_user is None or econ_user is None:
print("User does not have an account")
return False
if econ_user['cash'] < amount:
print("You do not have enough money to make this transaction")
return False
econ_coll.update_one({"account_id": code}, {"$inc": {"cash": -amount}})
bank_coll.update_one({"account_id": code}, {"$inc": {"balance": amount}})
return True
def withdraw(code, amount):
bank_coll = db.bank
econ_coll = db.economy
bank_user = bank_coll.find_one({"account_id": code})
econ_user = econ_coll.find_one({"account_id": code})
if bank_user is None or econ_user is None:
print("User does not have an account")
return False
if bank_user['balance'] < amount:
print("You do not have enough money to make this transaction")
return False
econ_coll.update_one({"account_id": code}, {"$inc": {"cash": amount}})
bank_coll.update_one({"account_id": code}, {"$inc": {"balance": -amount}})
return True
# work function
def work(code):
job_coll = db.jobs
econ_coll = db.economy
bank_coll = db.bank
econ_user = econ_coll.find_one({"account_id": code})
if econ_user is None:
print("User could not be found")
return None
job_id = get_user_job(code)
if job_id is None:
print("You do not have a job.")
return None
job = job_coll.find_one({"_id": job_id})
pph = job['pph']
hours_worked = random.randint(1, 10)
total_pay = pph * hours_worked
bank_user = bank_coll.find_one({"account_id": code})
if bank_user is None:
create_bank_account(code)
if bank_user['active'] is True:
bank_coll.update_one({"account_id": code}, {"$inc": {"balance": total_pay}})
return hours_worked
econ_coll.update_one({"account_id": code}, {"$inc": {"cash": total_pay}})
return hours_worked

62
jobs.py Normal file
View File

@ -0,0 +1,62 @@
import pymongo
import os
client = pymongo.MongoClient(os.getenv("MONGO_URL"))
db = client.VE
class Job:
def __init__(self, title, education, pph):
self.title = title
self.education = education
self.pph = pph
def create_new_job(title, education, pph):
coll = db.jobs
job = coll.find_one({"title": title})
### Check if there is a job with that title, if so ask the admin if they want to update that job with the new education requirement and pph
if job is not None:
do_update = input("A job with that title has already been created. Do you want to update it? (y/n): ")
if do_update == "y":
coll.update_one({"title": title}, {"$set": {"education": education, "pph": pph}})
return True
else:
return False
### Check if there is any job with the id 0, if not then add it
first_job = coll.find_one({"_id": 0})
if first_job is None:
coll.insert_one({"_id": 0, "title": title, "education": education, "pph": pph})
return True
### Get the all the IDs of the jobs in the db
all_jobs = coll.find({})
jobs = []
for job in all_jobs:
jobs.append(job["_id"])
### Insert the job into the db, but get the last ID in the array and add 1 to it.
coll.insert_one({"_id": (jobs[-1] + 1), "title": title, "education": education, "pph": pph})
return True
def get_job_details(job_id):
coll = db.jobs
job = coll.find_one({"_id": job_id})
return Job(job['title'], job['education'], job['pph'])
def get_all_jobs():
coll = db.jobs
jobs = coll.find({})
for job in jobs:
print(f"""
Job ID: {job['_id']}
Job Title: {job['title']}
Education Requirements: {job['education']}
Pay Per Hour: {job['pph']}
""")
return

115
main.py
View File

@ -1,11 +1,16 @@
# imports
import time
from dotenv import load_dotenv
load_dotenv()
from accounts import *
from economy import *
from jobs import *
# pre vars
code = None
# login user
does_user_have_account = input("Do you have an account? (y/n): ")
if does_user_have_account == "y":
@ -19,10 +24,114 @@ elif does_user_have_account == "n":
name = input("Please enter your name: ")
code = create_account(name, email, password)
create_econ_account(code)
else:
print("That is not a valid answer")
# start function
def start(option):
# start jobs
if option == "jobs":
user_job = get_user_job(code)
if user_job is None:
get_all_jobs()
job_id = int(input("Please enter the ID of the job you would like: "))
did_job_assign = assign_job(code, job_id)
if did_job_assign is True:
print("Congratulations! Welcome to your new job. Work to get some money")
time.sleep(1)
return
else:
print("Whoops. Sorry. You did not get accepted into this job.")
else:
job_details = get_job_details(get_user_job(code))
update_job = input(f"You already have a job.\n\nTitle: {job_details.title}\nEducation Requirement: {job_details.education}\nPay Per Hour: {job_details.pph}"
f"Do you want to change your job? (y/n): ")
if update_job == "y":
get_all_jobs()
job_id = int(input("Please enter the ID of the job you would like: "))
did_job_assign = assign_job(code, job_id)
if did_job_assign is True:
print("Congratulations! Welcome to your new job. Work to get some money")
time.sleep(1)
return
else:
print("Whoops. Sorry. You did not get accepted into this job.")
else:
return
# end jobs
# begin add_job
elif option == "add_job":
account_status = get_account_status(code)
if account_status == "admin":
title = input("Name for the job: ")
education_level = int(input("What is the education requirement? (1 - 5): "))
pay = int(input("What is the pph (pay-per-hour)? "))
new_job = create_new_job(title, education_level, pay)
if new_job:
print("The new job has been created successfully!")
time.sleep(1)
return
else:
print("The new job has not been created. Something went wrong.")
time.sleep(1)
return
# end add_job
# begin work
elif option == "work":
hours_worked = work(code)
job_id = get_user_job(code)
job = get_job_details(job_id)
if hours_worked is None:
print("Something went wrong.. ")
time.sleep(1)
return
print(f"""
You worked for a total of: {hours_worked} hours.
You made ${hours_worked * job.pph}!
Great job today!
""")
time.sleep(2)
return
# end work
# begin balance
elif option == "balance":
balance = get_balance(code)
cash = get_cash(code)
print(f"The balance in your bank is: ${balance}. The balance of cash you have is: ${cash}")
time.sleep(3)
return
# end balance
# TODO: Add setup, store, inventory, deposit, and withdraw
# main function
while code is not None:
balance = get_balance(code)
print(f"Welcome to your bank account.\nBalance: {balance}")
break
print(f"""
Welcome {str(get_user_name(code)).capitalize()}.
Your bank balance is {get_balance(code)}
In cash, you have {get_cash(code)}
This is not a real bank account. This is all fiction.
What would you like to do?
Options:
work - work for some money
setup - enter the user configuration menu
balance - view your balance
jobs - view and apply for jobs
store - spend your hard earned cash
inventory - view your inventory
deposit - deposit some of your cash into your bank account
withdraw - take some money out of your bank account
leave - exit your virtual economy
""")
options = input("")
if options == "leave":
break
start(options)