📄 GameScreen.svelte
<script lang="ts">
  import { PLAYER_COLORS } from "$lib/types";
  import { onMount } from "svelte";

  interface Props {
    playerCount: number;
    onTap: (playerId: number) => void;
  }

  let { playerCount, onTap }: Props = $props();

  let currentPlayer: number | null = $state(null);
  let timeout: NodeJS.Timeout | null = null;

  onMount(() => {
    nextTimeout();

    return () => {
      if (timeout) {
        clearTimeout(timeout);
      }
    };
  });

  function nextTimeout() {
    const minInterval = 800;
    const maxInterval = 6000;
    const interval = Math.random() * (maxInterval - minInterval) + minInterval;

    timeout = setTimeout(() => {
      // Choose a random player
      currentPlayer = Math.floor(Math.random() * playerCount);

      // Clear current player after a short delay
      const minInterval = 800;
      const maxInterval = 2500;
      const interval = Math.random() * (maxInterval - minInterval) + minInterval;
      timeout = setTimeout(() => {
        currentPlayer = null;
        nextTimeout();
      }, interval);
    }, interval);
  }

  function handleClick() {
    if (currentPlayer != null) {
      onTap(currentPlayer);
      if (timeout) {
        clearTimeout(timeout);
      }
      currentPlayer = null;
      nextTimeout();
    }
  }

  function handleKeydown(event: KeyboardEvent) {
    if (event.key === "Enter" || event.key === " ") {
      handleClick();
    }
  }
</script>

<div
  class="game-screen"
  style="background-color: {currentPlayer != null ? PLAYER_COLORS[currentPlayer] : '#000000'}"
  onclick={handleClick}
  onkeydown={handleKeydown}
  role="button"
  tabindex="0"
>
  {#if currentPlayer != null}
    <div class="tap-indicator">TAP!</div>
  {/if}
</div>

<style>
  .game-screen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
  }

  .tap-indicator {
    font-size: 4rem;
    font-weight: bold;
    color: rgba(255, 255, 255, 0.9);
    text-shadow:
      0 0 10px rgba(0, 0, 0, 0.5),
      0 0 20px rgba(0, 0, 0, 0.3);
    animation: pulse 0.5s ease-in-out infinite alternate;
  }

  @keyframes pulse {
    from {
      transform: scale(1);
      opacity: 0.8;
    }
    to {
      transform: scale(1.1);
      opacity: 1;
    }
  }
</style>