import pygame
import sys
import time
import json
import os
pygame.init()
# Screen setup
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("WebSim AI - Advanced Custom Timer with JSON Storage")
# Colors
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
WHITE = (255, 255, 255)
# Fonts
font = pygame.font.Font(None, 32)
large_font = pygame.font.Font(None, 48)
# Timer variables
custom_timers = {}
active_timer = None
timer_start_time = 0
current_phase = ""
# Input box
input_box = pygame.Rect(50, HEIGHT - 70, WIDTH - 100, 40)
user_input = ""
input_active = False
# JSON file for storing timers
TIMERS_FILE = "custom_timers.json"
def save_timers():
with open(TIMERS_FILE, 'w') as f:
json.dump(custom_timers, f)
def load_timers():
global custom_timers
if os.path.exists(TIMERS_FILE):
with open(TIMERS_FILE, 'r') as f:
custom_timers = json.load(f)
def draw_text(text, font, color, x, y):
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect()
text_rect.center = (x, y)
screen.blit(text_surface, text_rect)
def process_user_input(input_text):
global active_timer, timer_start_time, current_phase, custom_timers
input_text = input_text.lower().strip()
if input_text.startswith("set timer"):
parts = input_text[9:].split("|")
if len(parts) == 7:
keyphrase, start_response, start_seconds, middle_response, middle_seconds, end_response, end_seconds = [p.strip() for p in parts]
custom_timers[keyphrase] = {
"keyphrase": keyphrase,
"start": {"response": start_response, "duration": int(start_seconds)},
"middle": {"response": middle_response, "duration": int(middle_seconds)},
"end": {"response": end_response, "duration": int(end_seconds)}
}
save_timers()
return f"Custom timer '{keyphrase}' set successfully and saved to JSON file."
else:
return "Invalid timer format. Use: set timer keyphrase | start response | start seconds | middle response | middle seconds | end response | end seconds"
elif input_text in custom_timers:
active_timer = custom_timers[input_text]
timer_start_time = time.time()
current_phase = "start"
return f"Starting timer: {input_text}"
elif input_text == "list timers":
return "Available timers: " + ", ".join(custom_timers.keys())
else:
return "Command not recognized. Try setting a timer, starting an existing one, or use 'list timers' to see available timers."
def update_timer():
global active_timer, current_phase
if active_timer:
elapsed_time = time.time() - timer_start_time
total_duration = (active_timer["start"]["duration"] +
active_timer["middle"]["duration"] +
active_timer["end"]["duration"])
if current_phase == "start" and elapsed_time >= active_timer["start"]["duration"]:
current_phase = "middle"
elif current_phase == "middle" and elapsed_time >= active_timer["start"]["duration"] + active_timer["middle"]["duration"]:
current_phase = "end"
elif elapsed_time >= total_duration:
active_timer = None
current_phase = ""
return "Timer completed!"
remaining_time = max(0, total_duration - elapsed_time)
return f"{active_timer['keyphrase']}: {int(remaining_time)} seconds remaining"
return ""
# Load existing timers from JSON file
load_timers()
running = True
clock = pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
if input_box.collidepoint(event.pos):
input_active = True
else:
input_active = False
if event.type == pygame.KEYDOWN:
if input_active:
if event.key == pygame.K_RETURN:
response = process_user_input(user_input)
user_input = ""
elif event.key == pygame.K_BACKSPACE:
user_input = user_input[:-1]
else:
user_input += event.unicode
screen.fill(BLACK)
# Draw input box
pygame.draw.rect(screen, GREEN, input_box, 2)
text_surface = font.render(user_input, True, GREEN)
screen.blit(text_surface, (input_box.x + 5, input_box.y + 5))
# Draw timer status
timer_status = update_timer()
if timer_status:
draw_text(timer_status, font, GREEN, WIDTH // 2, 50)
# Draw current phase message
if active_timer and current_phase:
phase_message = active_timer[current_phase]["response"]
draw_text(phase_message, large_font, GREEN, WIDTH // 2, HEIGHT // 2)
pygame.display.flip()
clock.tick(60)
pygame.quit()
sys.exit()
set timer [keyphrase] | [start response] | [start seconds] | [middle response] | [middle seconds] | [end response] | [end seconds]
Example:
set timer pomodoro | Start working! | 1500 | Take a short break | 300 | Session complete | 5
This sets a Pomodoro timer with a 25-minute work session, 5-minute break, and 5-second completion notice.
Simply type the keyphrase of the timer you want to start. For example:
pomodoro
To see all available timers, type:
list timers
The timers you set will be automatically saved to "custom_timers.json" in the same directory as the script, ensuring they're available in future sessions.
Make sure to run this script in a directory where you have write permissions, as it needs to create and modify the "custom_timers.json" file to store your timers.