From f6417c78ddecbb98b62024200805fa1b467010cd Mon Sep 17 00:00:00 2001 From: alexpolo1 Date: Sat, 18 Jan 2025 21:07:14 +0100 Subject: [PATCH] new version --- assets/level.txt | 2 +- assets/session.log | 241 ++++++++++++++++++++++++++++++++++++++++++ assets/total_time.txt | 2 +- assets/xp.txt | 2 +- main.py | 187 ++++++++++++++++++-------------- 5 files changed, 354 insertions(+), 80 deletions(-) diff --git a/assets/level.txt b/assets/level.txt index d8263ee..e440e5c 100644 --- a/assets/level.txt +++ b/assets/level.txt @@ -1 +1 @@ -2 \ No newline at end of file +3 \ No newline at end of file diff --git a/assets/session.log b/assets/session.log index 4dba26e..7e3bab8 100644 --- a/assets/session.log +++ b/assets/session.log @@ -65,3 +65,244 @@ Session end: 2025-01-08 20:11:28 Session start: 2025-01-08 20:55:07 XP and level updated: 25 XP, Level 2 Session end: 2025-01-08 20:55:36 +Session start: 2025-01-08 21:08:29 +Session start: 2025-01-08 21:08:29 +Session start: 2025-01-08 21:08:29 +Session start: 2025-01-08 21:08:30 +Session start: 2025-01-08 21:08:30 +Session start: 2025-01-08 21:19:58 +Session start: 2025-01-08 21:19:58 +Session start: 2025-01-08 21:19:58 +Session start: 2025-01-08 21:19:58 +Session start: 2025-01-08 21:19:59 +Session start: 2025-01-08 23:03:13 +Session start: 2025-01-08 23:03:14 +Session start: 2025-01-08 23:03:14 +Session start: 2025-01-08 23:03:15 +Session start: 2025-01-08 23:03:15 +Session start: 2025-01-09 19:32:07 +Session start: 2025-01-09 19:32:08 +Session start: 2025-01-09 19:32:08 +Session start: 2025-01-09 19:32:23 +Session start: 2025-01-09 19:32:23 +Session start: 2025-01-09 19:32:23 +Session start: 2025-01-09 19:32:24 +Session start: 2025-01-09 19:32:24 +Session start: 2025-01-09 19:42:58 +XP and level updated: 35 XP, Level 2 +Session end: 2025-01-09 20:03:12 +Session start: 2025-01-10 12:54:39 +XP and level updated: 45 XP, Level 2 +Session end: 2025-01-10 12:56:40 +Session start: 2025-01-10 12:56:44 +XP and level updated: 55 XP, Level 2 +Session end: 2025-01-10 12:57:08 +Session start: 2025-01-10 12:58:33 +XP and level updated: 65 XP, Level 2 +Session end: 2025-01-10 12:58:47 +Session start: 2025-01-10 12:58:58 +XP and level updated: 75 XP, Level 2 +Session end: 2025-01-10 12:59:20 +Session start: 2025-01-10 12:59:22 +XP and level updated: 85 XP, Level 2 +Session end: 2025-01-10 12:59:44 +Session start: 2025-01-10 13:05:23 +XP and level updated: 95 XP, Level 2 +Session end: 2025-01-10 13:05:37 +Session start: 2025-01-10 13:06:37 +XP and level updated: 105 XP, Level 2 +Session end: 2025-01-10 13:06:52 +Session start: 2025-01-10 13:16:09 +Session start: 2025-01-10 13:16:09 +Session start: 2025-01-10 13:16:09 +Session start: 2025-01-10 13:16:09 +Session start: 2025-01-10 13:16:10 +Session start: 2025-01-14 18:59:04 +XP and level updated: 100 XP, Level 2 +Session end: 2025-01-14 19:23:41 +Session start: 2025-01-15 07:47:49 +Session start: 2025-01-15 07:47:49 +Session start: 2025-01-15 07:47:50 +Session start: 2025-01-15 07:47:50 +Session start: 2025-01-15 07:47:51 +Session start: 2025-01-15 07:55:49 +XP and level updated: 110 XP, Level 2 +Session end: 2025-01-15 08:17:28 +Session start: 2025-01-15 14:59:38 +Session start: 2025-01-15 14:59:38 +Session start: 2025-01-15 14:59:38 +Session start: 2025-01-15 14:59:39 +Session start: 2025-01-15 14:59:46 +Session start: 2025-01-15 14:59:46 +Session start: 2025-01-15 14:59:46 +Session start: 2025-01-15 14:59:47 +Session start: 2025-01-15 14:59:47 +Session start: 2025-01-15 21:39:12 +Session start: 2025-01-15 21:39:13 +Session start: 2025-01-15 21:39:13 +Session start: 2025-01-15 21:39:13 +Session start: 2025-01-15 21:39:18 +Session start: 2025-01-15 21:39:18 +Session start: 2025-01-15 21:39:19 +Session start: 2025-01-15 21:39:19 +Session start: 2025-01-15 21:39:20 +Session start: 2025-01-17 15:21:48 +Session start: 2025-01-17 15:21:48 +Session start: 2025-01-17 15:21:48 +Session start: 2025-01-17 15:21:49 +Session start: 2025-01-17 15:21:52 +Session start: 2025-01-17 15:21:53 +Session start: 2025-01-17 15:21:53 +Session start: 2025-01-17 15:21:53 +Session start: 2025-01-17 15:21:53 +Session start: 2025-01-17 19:15:35 +Session start: 2025-01-17 19:15:36 +Session start: 2025-01-17 19:15:36 +Session start: 2025-01-17 19:15:36 +Session start: 2025-01-17 19:15:42 +Session start: 2025-01-17 19:15:42 +Session start: 2025-01-17 19:15:42 +Session start: 2025-01-17 19:15:42 +Session start: 2025-01-17 19:15:42 +Session start: 2025-01-17 19:20:19 +Session end: 2025-01-17 19:20:25 +Session start: 2025-01-17 19:20:35 +Session start: 2025-01-17 19:24:39 +Session start: 2025-01-17 19:24:49 +XP and level updated: 120 XP, Level 2 +Session end: 2025-01-17 19:46:00 +Session start: 2025-01-17 19:46:11 +XP and level updated: 130 XP, Level 2 +Session end: 2025-01-17 19:46:24 +Session start: 2025-01-17 19:46:34 +Session end: 2025-01-17 19:46:38 +Session start: 2025-01-17 19:46:48 +XP and level updated: 125 XP, Level 2 +Session start: 2025-01-18 20:21:53 +Session start: 2025-01-18 20:22:03 +XP and level updated: 135 XP, Level 2 +Session start: 2025-01-18 20:26:51 +Session start: 2025-01-18 20:27:18 +Session start: 2025-01-18 20:28:48 +Session start: 2025-01-18 20:31:24 +Session start: 2025-01-18 20:33:13 +XP and level updated: 145 XP, Level 2 +XP and level updated: 155 XP, Level 2 +Session end: 2025-01-18 20:33:50 +Session start: 2025-01-18 20:35:58 +XP and level updated: 165 XP, Level 2 +Session end: 2025-01-18 20:36:18 +Session start: 2025-01-18 20:36:20 +XP and level updated: 175 XP, Level 2 +Session end: 2025-01-18 20:36:35 +Session start: 2025-01-18 20:38:28 +XP and level updated: 170 XP, Level 2 +Session end: 2025-01-18 20:38:47 +Session start: 2025-01-18 20:41:32 +XP and level updated: 180 XP, Level 2 +Session end: 2025-01-18 20:42:35 +Session start: 2025-01-18 20:42:41 +Session end: 2025-01-18 20:42:49 +Session start: 2025-01-18 20:44:43 +XP and level updated: 175 XP, Level 2 +Session end: 2025-01-18 20:45:05 +Session start: 2025-01-18 20:45:09 +XP and level updated: 185 XP, Level 2 +Session end: 2025-01-18 20:45:22 +Session start: 2025-01-18 20:45:58 +XP and level updated: 180 XP, Level 2 +Session end: 2025-01-18 20:46:12 +Session start: 2025-01-18 20:46:15 +XP and level updated: 190 XP, Level 2 +Session end: 2025-01-18 20:46:28 +Session end: 2025-01-18 20:47:18 +Session start: 2025-01-18 20:47:29 +XP and level updated: 0 XP, Level 3 +Session end: 2025-01-18 20:47:46 +Session start: 2025-01-18 20:47:52 +Session start: 2025-01-18 20:47:56 +Session end: 2025-01-18 20:48:06 +Session end: 2025-01-18 20:48:09 +Session start: 2025-01-18 20:48:19 +Session end: 2025-01-18 20:48:25 +Session start: 2025-01-18 20:48:35 +Session end: 2025-01-18 20:48:51 +Session start: 2025-01-18 20:49:01 +XP and level updated: 10 XP, Level 3 +Session end: 2025-01-18 20:49:13 +Session start: 2025-01-18 20:49:23 +XP and level updated: 20 XP, Level 3 +Session end: 2025-01-18 20:49:30 +Session start: 2025-01-18 20:49:37 +Session start: 2025-01-18 20:49:40 +XP and level updated: 15 XP, Level 3 +Session end: 2025-01-18 20:49:53 +XP and level updated: 25 XP, Level 3 +Session end: 2025-01-18 20:50:03 +Session start: 2025-01-18 20:50:03 +Session end: 2025-01-18 20:50:07 +Session start: 2025-01-18 20:50:17 +XP and level updated: 35 XP, Level 3 +Session end: 2025-01-18 20:51:43 +Session start: 2025-01-18 20:51:53 +XP and level updated: 45 XP, Level 3 +Session end: 2025-01-18 20:52:04 +Session start: 2025-01-18 20:52:14 +XP and level updated: 55 XP, Level 3 +Session end: 2025-01-18 20:52:23 +Session start: 2025-01-18 20:52:33 +Session end: 2025-01-18 20:52:37 +Session start: 2025-01-18 20:52:47 +Session start: 2025-01-18 20:53:03 +XP and level updated: 50 XP, Level 3 +Session end: 2025-01-18 20:53:17 +XP and level updated: 60 XP, Level 3 +Session end: 2025-01-18 20:53:39 +Session start: 2025-01-18 20:53:49 +Session end: 2025-01-18 20:53:55 +Session start: 2025-01-18 20:54:05 +Session end: 2025-01-18 20:54:43 +Session start: 2025-01-18 20:54:53 +Session end: 2025-01-18 20:55:04 +Session start: 2025-01-18 20:55:15 +Session end: 2025-01-18 20:55:27 +Session start: 2025-01-18 20:55:37 +Session end: 2025-01-18 20:56:02 +Session start: 2025-01-18 20:56:13 +Session duration: 0 minutes and 3 seconds +Session end: 2025-01-18 20:56:16 +Session start: 2025-01-18 20:56:17 +Session start: 2025-01-18 20:56:26 +Session duration: 0 minutes and 3 seconds +Session end: 2025-01-18 20:56:30 +XP and level updated: 70 XP, Level 3 +Session start: 2025-01-18 20:56:40 +Session duration: 0 minutes and 3 seconds +Session end: 2025-01-18 20:56:44 +Session duration: 0 minutes and 26 seconds +Session end: 2025-01-18 20:56:44 +Session start: 2025-01-18 20:56:54 +XP and level updated: 80 XP, Level 3 +Session duration: 0 minutes and 10 seconds +Session end: 2025-01-18 20:57:04 +Session start: 2025-01-18 20:57:14 +Session start: 2025-01-18 21:00:19 +XP and level updated: 90 XP, Level 3 +Session duration: 0 minutes and 13 seconds +Session end: 2025-01-18 21:00:33 +Session start: 2025-01-18 21:01:07 +Session duration: 0 minutes and 4 seconds +Session end: 2025-01-18 21:01:12 +Session start: 2025-01-18 21:01:22 +XP and level updated: 100 XP, Level 3 +Session duration: 0 minutes and 10 seconds +Session end: 2025-01-18 21:01:33 +Session start: 2025-01-18 21:01:43 +XP and level updated: 95 XP, Level 3 +Session duration: 0 minutes and 11 seconds +Session end: 2025-01-18 21:01:54 +Session start: 2025-01-18 21:02:04 +Session start: 2025-01-18 21:04:01 +XP and level updated: 105 XP, Level 3 +Session duration: 0 minutes and 12 seconds +Session end: 2025-01-18 21:04:14 diff --git a/assets/total_time.txt b/assets/total_time.txt index c227083..6d58c4e 100644 --- a/assets/total_time.txt +++ b/assets/total_time.txt @@ -1 +1 @@ -0 \ No newline at end of file +109 \ No newline at end of file diff --git a/assets/xp.txt b/assets/xp.txt index 410b14d..ffda4e7 100644 --- a/assets/xp.txt +++ b/assets/xp.txt @@ -1 +1 @@ -25 \ No newline at end of file +105 \ No newline at end of file diff --git a/main.py b/main.py index c544eac..e37a1b5 100644 --- a/main.py +++ b/main.py @@ -4,93 +4,135 @@ import psutil import tkinter as tk from tkinter import messagebox import random -import sys import subprocess -# Utility functions -def initialize_assets(assets_dir, xp_file, level_file, session_log, total_time_file, questions_file): - if not os.path.exists(assets_dir): - os.makedirs(assets_dir) - if not os.path.exists(xp_file): - with open(xp_file, 'w') as f: - f.write('0') - if not os.path.exists(level_file): - with open(level_file, 'w') as f: - f.write('1') - if not os.path.exists(session_log): - open(session_log, 'w').close() - if not os.path.exists(total_time_file): - with open(total_time_file, 'w') as f: - f.write('0') - if not os.path.exists(questions_file): - raise FileNotFoundError(f"Questions file not found: {questions_file}") +# Constants +SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) +ASSETS_DIR = os.path.join(SCRIPT_DIR, 'assets') +XP_FILE = os.path.join(ASSETS_DIR, 'xp.txt') +LEVEL_FILE = os.path.join(ASSETS_DIR, 'level.txt') +SESSION_LOG = os.path.join(ASSETS_DIR, 'session.log') +QUESTIONS_FILE = os.path.join(ASSETS_DIR, 'questions.txt') +TOTAL_TIME_FILE = os.path.join(ASSETS_DIR, 'total_time.txt') +URL = "https://rol.redhat.com" +TIMER_MINUTES = 20 -def log_session(session_log, status): - with open(session_log, 'a') as f: +# Utility Functions +def initialize_assets(): + """Ensure all necessary files and directories exist.""" + os.makedirs(ASSETS_DIR, exist_ok=True) + for file_path, default_content in [ + (XP_FILE, '0'), + (LEVEL_FILE, '1'), + (SESSION_LOG, ''), + (TOTAL_TIME_FILE, '0'), + ]: + if not os.path.exists(file_path): + with open(file_path, 'w') as f: + f.write(default_content) + if not os.path.exists(QUESTIONS_FILE): + raise FileNotFoundError(f"Questions file not found: {QUESTIONS_FILE}") + +def log_session(status): + """Log session start or end with a timestamp.""" + with open(SESSION_LOG, 'a') as f: f.write(f"Session {status}: {time.strftime('%Y-%m-%d %H:%M:%S')}\n") -def display_summary(total_time_file, xp_file, level_file): - with open(total_time_file, 'r') as f: - total_time = int(f.read().strip()) - with open(xp_file, 'r') as f: - xp = int(f.read().strip()) - with open(level_file, 'r') as f: - level = int(f.read().strip()) - hours, minutes = divmod(total_time, 60) +def display_summary(): + """Display a summary of total time, XP, and level.""" + try: + with open(TOTAL_TIME_FILE, 'r') as f: + total_time = int(f.read().strip() or 0) # Handle empty file case + except FileNotFoundError: + total_time = 0 + + try: + with open(XP_FILE, 'r') as f: + xp = int(f.read().strip() or 0) + except FileNotFoundError: + xp = 0 + + try: + with open(LEVEL_FILE, 'r') as f: + level = int(f.read().strip() or 1) + except FileNotFoundError: + level = 1 + + hours, remainder = divmod(total_time, 3600) + minutes, seconds = divmod(remainder, 60) + print(f"Welcome to the RHEL Learning Script!") + print(f"Total session time: {hours} hours, {minutes} minutes, and {seconds} seconds.") # Ensure correct output + print(f"Current Level: {level}") + print(f"Current XP: {xp}") + print() + return total_time, xp, level def kill_steam(): - for proc in psutil.process_iter(): - try: - if 'steam' in proc.name().lower(): - proc.kill() - except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): - pass + """Kill Steam only before the session starts.""" + for proc in psutil.process_iter(['name']): + if proc.info['name'] and "steam" in proc.info['name'].lower(): + print("Stopping Steam...") + proc.kill() + +def update_xp(correct): + """Update XP and level based on whether the question was answered correctly.""" + with open(XP_FILE, 'r') as f: + xp = int(f.read().strip() or 0) + with open(LEVEL_FILE, 'r') as f: + level = int(f.read().strip() or 1) -def update_xp(xp_file, level_file, session_log, correct): - with open(xp_file, 'r') as f: - xp = int(f.read().strip()) - with open(level_file, 'r') as f: - level = int(f.read().strip()) if correct: - new_xp = xp + 10 + xp += 10 else: - new_xp = max(0, xp - 5) # Ensure XP doesn't go below 0 - if new_xp >= level * 100: + xp = max(0, xp - 5) # Ensure XP doesn't go below 0 + + if xp >= level * 100: level += 1 - new_xp -= (level - 1) * 100 + xp -= (level - 1) * 100 messagebox.showinfo("LEVEL UP!", f"LEVEL UP! You are now Level {level}!") - with open(xp_file, 'w') as f: - f.write(str(new_xp)) - with open(level_file, 'w') as f: + + with open(XP_FILE, 'w') as f: + f.write(str(xp)) + with open(LEVEL_FILE, 'w') as f: f.write(str(level)) - with open(session_log, 'a') as f: - f.write(f"XP and level updated: {new_xp} XP, Level {level}\n") - return new_xp, level + + with open(SESSION_LOG, 'a') as f: + f.write(f"XP and level updated: {xp} XP, Level {level}\n") + + return xp, level def show_timer(minutes, questions_file, session_log, total_time, xp, level): def update_timer(): - nonlocal seconds_left + nonlocal seconds_left, elapsed_time if seconds_left > 0: seconds_left -= 1 + elapsed_time += 1 hours, remainder = divmod(seconds_left, 3600) minutes, seconds = divmod(remainder, 60) timer_label.config(text=f"Time left: {hours:02}:{minutes:02}:{seconds:02}") root.after(1000, update_timer) else: - messagebox.showinfo("Time's up!", "The session has ended.") - with open(session_log, 'a') as f: - f.write(f"Session duration: {minutes} minutes\n") - root.destroy() + end_session() + + def end_session(): + with open(session_log, 'a') as f: + f.write(f"Session duration: {elapsed_time // 60} minutes and {elapsed_time % 60} seconds\n") + with open(TOTAL_TIME_FILE, 'w') as f: + f.write(str(total_time + elapsed_time)) + root.destroy() + + def on_closing(): + end_session() def check_answer(): selected_answer = answer_var.get() if selected_answer == correct_answer: messagebox.showinfo("Correct!", "You selected the correct answer.") - new_xp, new_level = update_xp(XP_FILE, LEVEL_FILE, SESSION_LOG, correct=True) + new_xp, new_level = update_xp(True) else: messagebox.showinfo("Incorrect", "You selected the wrong answer.") - new_xp, new_level = update_xp(XP_FILE, LEVEL_FILE, SESSION_LOG, correct=False) + new_xp, new_level = update_xp(False) for button in radio_buttons: button.config(state=tk.DISABLED) xp_label.config(text=f"XP: {new_xp}") @@ -102,8 +144,10 @@ def show_timer(minutes, questions_file, session_log, total_time, xp, level): root = tk.Tk() root.title("RHEL Learning Timer") + root.protocol("WM_DELETE_WINDOW", on_closing) seconds_left = minutes * 60 + elapsed_time = 0 timer_label = tk.Label(root, text=f"Time left: {minutes:02}:00:00", font=("Helvetica", 16)) timer_label.pack(pady=20) @@ -114,7 +158,7 @@ def show_timer(minutes, questions_file, session_log, total_time, xp, level): level_label = tk.Label(root, text=f"Level: {level}", font=("Helvetica", 14)) level_label.pack(pady=5) - total_time_label = tk.Label(root, text=f"Total session time: {total_time // 60} hours and {total_time % 60} minutes", font=("Helvetica", 14)) + total_time_label = tk.Label(root, text=f"Total session time: {total_time // 3600} hours, {(total_time % 3600) // 60} minutes, and {total_time % 60} seconds", font=("Helvetica", 14)) total_time_label.pack(pady=5) with open(questions_file, 'r') as f: @@ -154,38 +198,27 @@ def open_firefox(url): else: print("No graphical environment detected. Skipping browser opening.") -# Constants -SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) -ASSETS_DIR = os.path.join(SCRIPT_DIR, 'assets') -XP_FILE = os.path.join(ASSETS_DIR, 'xp.txt') -LEVEL_FILE = os.path.join(ASSETS_DIR, 'level.txt') -SESSION_LOG = os.path.join(ASSETS_DIR, 'session.log') -QUESTIONS_FILE = os.path.join(ASSETS_DIR, 'questions.txt') -TOTAL_TIME_FILE = os.path.join(ASSETS_DIR, 'total_time.txt') -URL = "https://rol.redhat.com" -TIMER_MINUTES = 20 - def main(): # Initialize assets - initialize_assets(ASSETS_DIR, XP_FILE, LEVEL_FILE, SESSION_LOG, TOTAL_TIME_FILE, QUESTIONS_FILE) + initialize_assets() # Log session start - log_session(SESSION_LOG, "start") + log_session("start") # Display session summary - total_time, xp, level = display_summary(TOTAL_TIME_FILE, XP_FILE, LEVEL_FILE) + total_time, xp, level = display_summary() - # Open Firefox to the specified URL + # Stop Steam before session + kill_steam() + + # Open Firefox with RHEL Learning open_firefox(URL) # Start session show_timer(TIMER_MINUTES, QUESTIONS_FILE, SESSION_LOG, total_time, xp, level) # Log session end - log_session(SESSION_LOG, "end") + log_session("end") if __name__ == "__main__": - if len(sys.argv) > 1 and sys.argv[1] == "kill_steam": - kill_steam() - else: - main() + main()