<!doctype html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <script src="https://cdn.tailwindcss.com"></script>
  <script src="https://cdn.socket.io/4.7.5/socket.io.min.js"></script>
</head>
<body class="bg-slate-900 text-white">
  <div class="max-w-3xl mx-auto p-6">
    <h1 class="text-3xl font-bold mb-4">Tapdown Showdown – Room <span id="room"></span></h1>
    <div class="mb-4 flex gap-2">
      <button id="startBtn" class="px-4 py-2 bg-emerald-500 rounded">Start Match</button>
    </div>
    <div class="relative h-40 rounded bg-slate-800 overflow-hidden">
      <div class="absolute inset-y-0 left-1/2 w-0.5 bg-slate-600"></div>
      <div id="puck" class="absolute top-1/2 -translate-y-1/2 w-6 h-6 rounded-full bg-white shadow"></div>
      <div class="absolute inset-y-0 left-0 w-1 bg-red-500"></div>
      <div class="absolute inset-y-0 right-0 w-1 bg-blue-500"></div>
    </div>
    <p id="status" class="mt-4 text-lg"></p>
  </div>

<script>
const room = "{{ room }}";
document.getElementById("room").textContent = room;

const socket = io("/game");
socket.emit("join_room", { room, player_id: "host", side: null });

document.getElementById("startBtn").onclick = () => {
  socket.emit("start", { room });
  document.getElementById("status").textContent = "Fight!";
};

socket.on("state", ({ puck }) => {
  const box = document.querySelector(".relative.h-40");
  const puckEl = document.getElementById("puck");
  const w = box.clientWidth;
  const x = (puck / 100) * (w/2 - 10); // map -100..100 to pixels
  puckEl.style.left = `calc(50% + ${x}px)`;
});

socket.on("match_end", ({ winner }) => {
  document.getElementById("status").textContent = winner.toUpperCase() + " WINS!";
});
</script>
</body>
</html>

