play.astro 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. ---
  2. import Layout from "@layouts/layout.astro";
  3. import Nav from "@components/Nav.astro";
  4. import { init, getUser } from "@utils/db";
  5. import { getUserFromRequest } from "@utils/auth";
  6. await init();
  7. const currentUserName = getUserFromRequest(Astro.request);
  8. if (!currentUserName) {
  9. return Astro.redirect("/login?redirect=/play");
  10. }
  11. const currentUser = await getUser(currentUserName);
  12. ---
  13. <Layout
  14. title="Play"
  15. currentUser={currentUserName}
  16. currentRating={currentUser?.rating ?? null}
  17. >
  18. <Nav
  19. currentUser={currentUserName}
  20. currentRating={currentUser?.rating ?? null}
  21. activePage="play"
  22. />
  23. <main class="container play-page">
  24. <h1 class="page-title" style="margin: 32px 0 24px">Start a Game</h1>
  25. <!-- Mode selector -->
  26. <div class="mode-tabs">
  27. <button
  28. class="mode-tab active"
  29. id="tab-inperson"
  30. data-mode="inperson"
  31. >
  32. <span class="mode-icon">🏓</span>
  33. <span class="mode-label">In Person</span>
  34. <span class="mode-desc">Track score for a physical game</span>
  35. </button>
  36. <button class="mode-tab" id="tab-virtual" data-mode="virtual">
  37. <span class="mode-icon">🌐</span>
  38. <span class="mode-label">Virtual</span>
  39. <span class="mode-desc">Play online with PeerJS</span>
  40. </button>
  41. </div>
  42. <div class="play-grid">
  43. <!-- Create Game -->
  44. <div class="card">
  45. <h2 class="card-title">Create Game</h2>
  46. <p class="card-desc" id="create-desc">
  47. Generate a link and share it with your opponent.
  48. </p>
  49. <button
  50. id="create-btn"
  51. class="btn"
  52. style="width:100%; padding:12px; margin-top:8px"
  53. >
  54. Create Game Room
  55. </button>
  56. <div id="created-result" style="display:none; margin-top:24px">
  57. <div class="created-link-box">
  58. <p class="section-label">Game Link</p>
  59. <div class="link-row">
  60. <input type="text" id="game-link" readonly />
  61. <button
  62. id="copy-btn"
  63. class="btn btn-secondary"
  64. style="flex-shrink:0; width:auto; padding:10px 14px"
  65. >Copy</button
  66. >
  67. </div>
  68. </div>
  69. <div class="qr-section">
  70. <p class="section-label">QR Code</p>
  71. <div id="qr-container" class="qr-box">
  72. <img id="qr-img" src="" alt="QR code" />
  73. </div>
  74. </div>
  75. <a
  76. id="open-game-btn"
  77. href="#"
  78. class="btn"
  79. style="width:100%; padding:12px; display:block; text-align:center; margin-top:16px"
  80. >
  81. Open Game Room →
  82. </a>
  83. </div>
  84. </div>
  85. <!-- Join Game -->
  86. <div class="card">
  87. <h2 class="card-title">Join Game</h2>
  88. <p class="card-desc">
  89. Have a game ID or link? Enter it below to join.
  90. </p>
  91. <form id="join-form" style="margin-top:8px">
  92. <div class="form-group">
  93. <label for="join-input">Game ID or Link</label>
  94. <input
  95. type="text"
  96. id="join-input"
  97. placeholder="e.g. abc123 or full URL"
  98. autocomplete="off"
  99. />
  100. </div>
  101. <div id="join-error" class="error-msg" style="display:none">
  102. </div>
  103. <button
  104. type="submit"
  105. class="btn"
  106. style="width:100%; padding:12px"
  107. >
  108. Join Game →
  109. </button>
  110. </form>
  111. </div>
  112. </div>
  113. </main>
  114. </Layout>
  115. <script>
  116. let selectedMode = "inperson";
  117. const tabs = document.querySelectorAll(".mode-tab");
  118. tabs.forEach((tab) => {
  119. tab.addEventListener("click", () => {
  120. tabs.forEach((t) => t.classList.remove("active"));
  121. tab.classList.add("active");
  122. selectedMode = (tab as HTMLElement).dataset.mode!;
  123. // Hide any previous result when switching modes
  124. const result = document.getElementById(
  125. "created-result",
  126. ) as HTMLElement;
  127. result.style.display = "none";
  128. });
  129. });
  130. const createBtn = document.getElementById(
  131. "create-btn",
  132. ) as HTMLButtonElement;
  133. const createdResult = document.getElementById(
  134. "created-result",
  135. ) as HTMLElement;
  136. const gameLink = document.getElementById("game-link") as HTMLInputElement;
  137. const copyBtn = document.getElementById("copy-btn") as HTMLButtonElement;
  138. const qrImg = document.getElementById("qr-img") as HTMLImageElement;
  139. const openGameBtn = document.getElementById(
  140. "open-game-btn",
  141. ) as HTMLAnchorElement;
  142. createBtn.addEventListener("click", async () => {
  143. createBtn.disabled = true;
  144. createBtn.textContent = "Creating...";
  145. const res = await fetch("/api/game", {
  146. method: "POST",
  147. headers: { "Content-Type": "application/json" },
  148. body: JSON.stringify({ mode: selectedMode }),
  149. });
  150. if (!res.ok) {
  151. createBtn.disabled = false;
  152. createBtn.textContent = "Create Game Room";
  153. alert("Failed to create game. Are you signed in?");
  154. return;
  155. }
  156. const data = await res.json();
  157. const url = `${location.origin}/game/${data.id}`;
  158. gameLink.value = url;
  159. openGameBtn.href = `/game/${data.id}`;
  160. qrImg.src = `/api/qr/${data.id}`;
  161. createdResult.style.display = "block";
  162. createBtn.textContent = "Create Another";
  163. createBtn.disabled = false;
  164. });
  165. copyBtn.addEventListener("click", () => {
  166. navigator.clipboard.writeText(gameLink.value);
  167. copyBtn.textContent = "Copied!";
  168. setTimeout(() => {
  169. copyBtn.textContent = "Copy";
  170. }, 2000);
  171. });
  172. const joinForm = document.getElementById("join-form") as HTMLFormElement;
  173. const joinInput = document.getElementById("join-input") as HTMLInputElement;
  174. const joinError = document.getElementById("join-error") as HTMLElement;
  175. joinForm.addEventListener("submit", (e) => {
  176. e.preventDefault();
  177. joinError.style.display = "none";
  178. let val = joinInput.value.trim();
  179. if (!val) return;
  180. let id = val;
  181. try {
  182. const url = new URL(val);
  183. const parts = url.pathname.split("/").filter(Boolean);
  184. if (parts.length >= 2 && parts[0] === "game") id = parts[1];
  185. } catch {}
  186. if (!id) {
  187. joinError.textContent = "Invalid game ID";
  188. joinError.style.display = "block";
  189. return;
  190. }
  191. location.href = `/game/${id}`;
  192. });
  193. </script>
  194. <style>
  195. .play-page {
  196. padding-bottom: 48px;
  197. }
  198. .mode-tabs {
  199. display: flex;
  200. gap: 12px;
  201. margin-bottom: 24px;
  202. }
  203. .mode-tab {
  204. flex: 1;
  205. background: var(--card);
  206. border: 2px solid var(--border);
  207. border-radius: 4px;
  208. padding: 16px;
  209. cursor: pointer;
  210. text-align: left;
  211. display: flex;
  212. flex-direction: column;
  213. gap: 4px;
  214. transition:
  215. border-color 0.15s,
  216. background 0.15s;
  217. font-family: "DM Mono", monospace;
  218. }
  219. .mode-tab:hover {
  220. border-color: var(--border-light);
  221. }
  222. .mode-tab.active {
  223. border-color: var(--green);
  224. background: rgba(129, 182, 76, 0.06);
  225. }
  226. .mode-icon {
  227. font-size: 20px;
  228. }
  229. .mode-label {
  230. font-family: "Bebas Neue", sans-serif;
  231. font-size: 1.3rem;
  232. letter-spacing: 0.05em;
  233. color: var(--text);
  234. }
  235. .mode-desc {
  236. font-size: 11px;
  237. color: var(--text-muted);
  238. }
  239. .play-grid {
  240. display: grid;
  241. grid-template-columns: 1fr 1fr;
  242. gap: 24px;
  243. }
  244. .card-title {
  245. font-family: "Bebas Neue", sans-serif;
  246. font-size: 1.5rem;
  247. letter-spacing: 0.05em;
  248. margin-bottom: 8px;
  249. }
  250. .card-desc {
  251. color: var(--text-muted);
  252. font-size: 13px;
  253. margin-bottom: 0;
  254. line-height: 1.6;
  255. }
  256. .created-link-box {
  257. margin-bottom: 20px;
  258. }
  259. .link-row {
  260. display: flex;
  261. gap: 8px;
  262. }
  263. .link-row input {
  264. flex: 1;
  265. font-size: 12px;
  266. }
  267. .qr-section {
  268. margin-bottom: 8px;
  269. }
  270. .qr-box {
  271. background: white;
  272. border-radius: 4px;
  273. display: inline-block;
  274. padding: 12px;
  275. }
  276. .qr-box img {
  277. display: block;
  278. width: 160px;
  279. height: 160px;
  280. }
  281. .error-msg {
  282. color: var(--red);
  283. font-size: 12px;
  284. margin-bottom: 12px;
  285. padding: 8px 12px;
  286. background: rgba(224, 110, 110, 0.1);
  287. border-radius: 3px;
  288. border: 1px solid rgba(224, 110, 110, 0.2);
  289. }
  290. @media (max-width: 640px) {
  291. .play-grid {
  292. grid-template-columns: 1fr;
  293. }
  294. .mode-tabs {
  295. flex-direction: column;
  296. }
  297. }
  298. </style>