from pyodide.ffi import create_proxy
import random
from js import document
def create_round(players, sit_out_count, previous_rounds, num_courts):
players_per_round = num_courts * 4
sit_out_num = len(players) - players_per_round
players_sorted = sorted(players, key=lambda p: sit_out_count[p])
sit_out = random.sample(players_sorted[:sit_out_num + 2], sit_out_num)
remaining_players = [p for p in players if p not in sit_out]
random.shuffle(remaining_players)
courts = []
used_pairs = set()
for i in range(0, len(remaining_players), 4):
court = tuple(sorted((remaining_players[i], remaining_players[i+1], remaining_players[i+2], remaining_players[i+3])))
if court not in previous_rounds:
courts.append(court)
used_pairs.add(court)
for p in sit_out:
sit_out_count[p] += 1
previous_rounds.update(used_pairs)
return courts, sit_out
def generate_rounds(event):
players_input = document.getElementById('players').value
players = [player.strip() for player in players_input.split(',')]
try:
num_courts = int(document.getElementById('courts').value)
num_rounds = int(document.getElementById('rounds').value)
except ValueError:
result_text = "Please enter valid numbers for courts and rounds."
document.getElementById('results').innerHTML = result_text
return
sit_out_count = {player: 0 for player in players}
previous_rounds = set()
rounds = []
for _ in range(num_rounds):
courts, sit_out = create_round(players, sit_out_count, previous_rounds, num_courts)
rounds.append((courts, sit_out))
result_text = ""
for i, (courts, sit_out) in enumerate(rounds, 1):
result_text += f"Round {i}:
"
for j, court in enumerate(courts, 1):
result_text += f" Court {j}: {court[0]} & {court[1]} vs {court[2]} & {court[3]}
"
result_text += f" Sitting out: {', '.join(sit_out)}
"
document.getElementById('results').innerHTML = result_text
generate_button = document.getElementById('generateButton')
if generate_button:
generate_button.addEventListener('click', create_proxy(generate_rounds))
else:
print("Button not found")