def create_wallet(cls, user_id: str) -> bool: "A method to initialize a users wallet" if not user_id in cls._wallets: cls._wallets[user_id] = Decimal('0') Reports.log_event(f"wallet for `{user_id}` created and set to 0") return True return False
def register_user(cls, new_user: dict[str, str]) -> str: "register a user" if not new_user["user_name"] in cls._users: # generate really complicated unique user_id. # Using the existing user_name as the id for simplicity user_id = new_user["user_name"] cls._users[user_id] = new_user Reports.log_event(f"new user `{user_id}` created") # create a wallet for the new user Wallets().create_wallet(user_id) # give the user a sign up bonus Reports.log_event(f"Give new user `{user_id}` sign up bonus of 10") Wallets().adjust_balance(user_id, Decimal(10)) return user_id return ""
def submit_entry(cls, user_id: str, entry: Decimal) -> bool: "Submit a new entry for the user in this game" now = int(time.time()) time_remaining = cls._start_time - now + cls._clock if time_remaining > 0: if Wallets.get_balance(user_id) > Decimal('1'): if Wallets.adjust_balance(user_id, Decimal('-1')): cls._entries.append((user_id, entry)) Reports.log_event( f"New entry `{entry}` submitted by `{user_id}`") return True Reports.log_event( f"Problem adjusting balance for `{user_id}`") return False Reports.log_event(f"User Balance for `{user_id}` to low") return False Reports.log_event("Game Closed") return False
def adjust_balance(cls, user_id: str, amount: Decimal) -> Decimal: "A method to adjust a user balance up or down" cls._wallets[user_id] = cls._wallets[user_id] + Decimal(amount) Reports.log_event(f"Balance adjustment for `{user_id}`. " f"New balance = {cls._wallets[user_id]}") return cls._wallets[user_id]
def get_balance(cls, user_id: str) -> Decimal: "A method to check a users balance" Reports.log_event( f"Balance check for `{user_id}` = {cls._wallets[user_id]}") return cls._wallets[user_id]