def get_top5(self): top5 = None if User.logged: top5 = [] games = read_data_from_games_database() if games['games']: for g in games['games']: if g['name'] == f'{self.name}': top5 = g['top5'] else: top5 = [] return top5
def system_wallet(): games = read_data_from_games_database() total_register = games['games_inf']['total_register'] for gamename in games['games']: print(f"\n{gamename}: {games['games'][gamename]['register']}\n") total_register += games['games'][gamename]['register'] write_data_to_games_database(games) from structures.user_class import User if User.logged.is_admin: print(f"Total number of points in the system:\n{total_register}\n") inp = input("Press Enter to continue.") return MenuNode.current_node()
def check_top_n(self, score, n=5): games = read_data_from_games_database() try: top5: list = games['games'][self.name]['top5'] except KeyError: games['games'][self.name]['top5'] = [] top5 = games['games'][self.name]['top5'] from structures.user_class import User playing_user = User.logged new_top_result = self.get_result_obj(score, playing_user) top5.append(new_top_result) top5.sort(key=lambda x: x['score'], reverse=True) try: top5 = top5[:n] except IndexError: top5 = top5 if top5[-1]['score'] <= score: print('\n\nYour score has reached TOP 5!\n\n') time.sleep(2) write_data_to_games_database(games)
def __init__(self, name): self.name = name games = read_data_from_games_database() if self.name not in games['games'].keys(): games['games'][self.name] = self.__dict__ games['games'][ self.name]['game_id'] = games['games_inf']['game_id'] games['games_inf']['game_id'] += 1 try: self.register = games['games'][self.name]['register'] except KeyError: games['games'][self.name]['register'] = 0 self.register = games['games'][self.name]['register'] try: self.top5 = games['games'][self.name]['top5'] except KeyError: games['games'][self.name]['top5'] = [] self.top5 = games['games'][self.name]['top5'] write_data_to_games_database(games)
def display_top_n(gamename, n=5): os.system('clear') games = read_data_from_games_database() try: top5 = games['games'][gamename]['top5'] except KeyError: games['games'][gamename] = {} games['games'][gamename]['top5'] = [] top5 = games['games'][gamename]['top5'] if len(top5) == 0: print('\nThere are no records yet!\n') else: top5.sort(key=lambda x: x['score'], reverse=True) top5 = top5[:n] print('\n\n\n') for i, result in enumerate(top5): print( f"{i + 1}| {result['username']} | {result['score']} | {result['datetime_details']}\n" ) inp = input('\nPress Enter to continue.\n') return MenuNode.current_node()
def start_game(game): playing_user = game.setting_player() while True: players_choice, drawn_value, players_money_bet = game.game( playing_user) game.evaluating_results(players_choice, drawn_value, players_money_bet, playing_user) print(f"\nSession's score: {game.score}.") print( f"\nCurrent's session results: {game.current_session_results}." ) print(f"\nYour wallet's status: {playing_user.wallet}.") # Updating user's wallet in the database if User.logged: users = read_data_from_users_database() for user in users['users']: if user['username'] == playing_user.username: user['wallet'] += game.score write_data_to_users_database(users) # Updating game's register in the database. games = read_data_from_games_database() games['games'][game.name]['register'] += -game.score write_data_to_games_database(games) rnd = game.round_end(game.score, playing_user) if rnd: break else: continue print("Coming back to the previous menu...") time.sleep(5) return MenuNode.current_node()
class TurtleRace(Game): games = json_data_funcs.read_data_from_games_database() users = json_data_funcs.read_data_from_users_database() racers = 0 # Window setup variables WIDTH, HEIGHT = 700, 700 def __init__(self): super().__init__(name='Turtle Race') self.score = 0 self.current_session_results = [] def how_many_turtles(self): while True: num = input( "Enter number of turtles (2 - 10):\n" "The greater the number, the higher your possible win is.\n" "Your bet is multiplied by the racers number.\n") try: num = int(num) TurtleRace.racers = num except ValueError: print("Enter valid input.") continue if num not in range(2, 11): print("Number not in the range.") continue else: racers = num return racers def screen_setup(self): screen = turtle.Screen() screen.setup(self.WIDTH, self.HEIGHT, startx=0, starty=0) screen.title("Turtles Race!") turtle.TurtleScreen._RUNNING = True def create_turtles(self, colors): turtles = [] spacex = self.WIDTH // (len(colors) + 1) for i, color in enumerate(colors): racer = turtle.Turtle() racer.color(color) racer.shape('turtle') racer.left(90) racer.penup() racer.setpos(-self.WIDTH // 2 + (i + 1) * spacex, -self.HEIGHT // 2 + 25) racer.pendown() turtles.append(racer) return turtles def race(self, colors): turtles = self.create_turtles(colors) while True: for i, racer in enumerate(turtles): distance = random.randrange(1, 20) racer.forward(distance) x, y = racer.pos() if y >= self.HEIGHT // 2 - 10: # Finish line at +190 return colors[i] def evaluating_results(self, players_choice, drawn_value, players_money_bet, playing_user): if players_choice.lower() == drawn_value.lower(): print(f'The winner is {drawn_value}!\n' f'You won!!!\n') self.current_session_results.append([ 'W', f"Money bet: {players_money_bet}", f"Date: {datetime.strftime(datetime.now(), '%d.%m.%Y, %H:%M')}" ]) playing_user.wallet += (TurtleRace.racers * players_money_bet) - players_money_bet self.register -= (TurtleRace.racers * players_money_bet) - players_money_bet self.score += (TurtleRace.racers * players_money_bet) - players_money_bet else: print(f'The winner is {drawn_value}!\n' f'You lost! :( \n') playing_user.wallet -= players_money_bet self.register += players_money_bet self.score -= players_money_bet self.current_session_results.append([ 'L', f"Money bet: {players_money_bet}", f"Date: {datetime.strftime(datetime.now(), '%d.%m.%Y, %H:%M')}" ]) def game(self, playing_user): # while True: self.not_enough_money(playing_user.wallet) # Getting player's points bet players_money_bet = self.getting_players_bet(playing_user) # Ten different colors referring to max number of racers. COLORS = [ 'red', 'blue', 'green', 'yellow', 'black', 'cyan', 'pink', 'purple', 'orange', 'brown' ] random.shuffle(COLORS) # Creating the racers. racers = self.how_many_turtles() colors = COLORS[:int(racers)] chosen_turtle = '' while True: for i, color in enumerate(colors): print(f"\n{i+1}| {color}\n") try: players_choice = input( "\n\nWhich turtle will win the race?\nEnter your type number now!\n\n" ) players_choice = int(players_choice) try: chosen_turtle = colors[players_choice - 1] break except IndexError: print( "\nInvalid input, try typing numbers in the correct range.\n" ) time.sleep(1) continue except ValueError: print("\nInvalid input, try typing numbers.\n") time.sleep(1) continue # turtle.Screen().clear() self.screen_setup() # Race itself. winner = self.race(colors) os.system('clear') # Closing the race window. inp = input("\n\n\t\t\tPress Enter to close the window.\n\n") turtle.Screen().bye() turtle.Screen._RUNNING = True return chosen_turtle, winner, players_money_bet
class Game: games = read_data_from_games_database() current_game = "" # Total system's register status. try: total_register = games['games_inf']['total_register'] except KeyError: games['games_inf']['total_register'] = 0 total_register = games['games_inf']['total_register'] def __init__(self, name): self.name = name games = read_data_from_games_database() if self.name not in games['games'].keys(): games['games'][self.name] = self.__dict__ games['games'][ self.name]['game_id'] = games['games_inf']['game_id'] games['games_inf']['game_id'] += 1 try: self.register = games['games'][self.name]['register'] except KeyError: games['games'][self.name]['register'] = 0 self.register = games['games'][self.name]['register'] try: self.top5 = games['games'][self.name]['top5'] except KeyError: games['games'][self.name]['top5'] = [] self.top5 = games['games'][self.name]['top5'] write_data_to_games_database(games) def __repr__(self): return self.name def __str__(self): return self.name @staticmethod def get_result_obj(score, playing_user): return { 'username': playing_user.username, 'score': score, 'datetime_details': datetime.strftime(datetime.now(), '%d-%m-%Y %H:%M') } def check_top_n(self, score, n=5): games = read_data_from_games_database() try: top5: list = games['games'][self.name]['top5'] except KeyError: games['games'][self.name]['top5'] = [] top5 = games['games'][self.name]['top5'] from structures.user_class import User playing_user = User.logged new_top_result = self.get_result_obj(score, playing_user) top5.append(new_top_result) top5.sort(key=lambda x: x['score'], reverse=True) try: top5 = top5[:n] except IndexError: top5 = top5 if top5[-1]['score'] <= score: print('\n\nYour score has reached TOP 5!\n\n') time.sleep(2) write_data_to_games_database(games) @staticmethod def display_top_n(gamename, n=5): os.system('clear') games = read_data_from_games_database() try: top5 = games['games'][gamename]['top5'] except KeyError: games['games'][gamename] = {} games['games'][gamename]['top5'] = [] top5 = games['games'][gamename]['top5'] if len(top5) == 0: print('\nThere are no records yet!\n') else: top5.sort(key=lambda x: x['score'], reverse=True) top5 = top5[:n] print('\n\n\n') for i, result in enumerate(top5): print( f"{i + 1}| {result['username']} | {result['score']} | {result['datetime_details']}\n" ) inp = input('\nPress Enter to continue.\n') return MenuNode.current_node() @staticmethod def display_personal_best(gamename): users = read_data_from_users_database() playing_user = User.logged for user in users['users']: if user['username'] == playing_user.username: try: game_best = user['my_best'][gamename] except KeyError: user['my_best'][gamename] = {} user['my_best'][gamename]['score'] = 0 game_best = user['my_best'][gamename] write_data_to_users_database(users) print(f"\nYour personal best in {gamename} is:\n" f"\t\t\t{game_best['score']} points.\n") inp = input("\nPress Enter to continue.\n") return MenuNode.current_node() def check_personal_best(self, score): users = read_data_from_users_database() from structures.user_class import User playing_user = User.logged curr_result = self.get_result_obj(score, playing_user) for user in users['users']: if user['username'] == curr_result['username']: try: game_best = user['my_best'][self.name] if curr_result['score'] > game_best['score']: user['my_best'][self.name] = curr_result print("\n\n\nNEW PERSONAL BEST!\n\n\n") except KeyError: user['my_best'][self.name] = curr_result game_best = user['my_best'][self.name] write_data_to_users_database(users) def round_end(self, score, playing_user): # Returns True if we want to exit. next_input = input( "\n\nDo you want to play again? Enter E to exit or any other character to play " "again.\n\n") if next_input.lower() == "e": if score >= 0: print( f'\nYou won {abs(score)}' f' points in this session.\nCurrently in your wallet:\n{playing_user.wallet}' ) else: print( f'\nYou lost {abs(score)} ' f'points in this session.\nCurrently in your wallet:\n{playing_user.wallet}' ) # Checking if the score is high enough to get to top 5 results of the game or to the personal best. if not isinstance(playing_user, GuestUser): self.check_top_n(score) self.check_personal_best(score) return True else: return False @staticmethod def system_wallet(): games = read_data_from_games_database() total_register = games['games_inf']['total_register'] for gamename in games['games']: print(f"\n{gamename}: {games['games'][gamename]['register']}\n") total_register += games['games'][gamename]['register'] write_data_to_games_database(games) from structures.user_class import User if User.logged.is_admin: print(f"Total number of points in the system:\n{total_register}\n") inp = input("Press Enter to continue.") return MenuNode.current_node() # print each game separately @staticmethod def setting_player(): from structures.user_class import User if User.logged: return User.logged else: return GuestUser() @staticmethod def getting_players_bet(player): # Getting player's bet. while True: try: players_money_bet = int( input( f"\nYou currently have {player.wallet} " f"in your wallet. How much do you want to bet? Enter a positive number:\n\n" )) if players_money_bet <= 0: print("\nYou must enter a positive number.") time.sleep(2) continue if players_money_bet > player.wallet: print("\nYou don't have that much money in your wallet!") time.sleep(2) continue return players_money_bet except ValueError: print("Please enter valid number.") continue def get_top5(self): top5 = None if User.logged: top5 = [] games = read_data_from_games_database() if games['games']: for g in games['games']: if g['name'] == f'{self.name}': top5 = g['top5'] else: top5 = [] return top5 @staticmethod def not_enough_money(wallet): if wallet <= 0: print( "You have no money in your wallet. Please refill your wallet or sit and just watch the menu." ) time.sleep(3) return MenuNode.current_node() @staticmethod def start_game(game): playing_user = game.setting_player() while True: players_choice, drawn_value, players_money_bet = game.game( playing_user) game.evaluating_results(players_choice, drawn_value, players_money_bet, playing_user) print(f"\nSession's score: {game.score}.") print( f"\nCurrent's session results: {game.current_session_results}." ) print(f"\nYour wallet's status: {playing_user.wallet}.") # Updating user's wallet in the database if User.logged: users = read_data_from_users_database() for user in users['users']: if user['username'] == playing_user.username: user['wallet'] += game.score write_data_to_users_database(users) # Updating game's register in the database. games = read_data_from_games_database() games['games'][game.name]['register'] += -game.score write_data_to_games_database(games) rnd = game.round_end(game.score, playing_user) if rnd: break else: continue print("Coming back to the previous menu...") time.sleep(5) return MenuNode.current_node()