| 1 | import { createBoard, playMove } from "./connect4.js"; |
| 2 | |
| 3 | function initGame(websocket) { |
| 4 | websocket.addEventListener("open", () => { |
| 5 | // Send an "init" event according to who is connecting. |
| 6 | const params = new URLSearchParams(window.location.search); |
| 7 | let event = { type: "init" }; |
| 8 | if (params.has("join")) { |
| 9 | // Second player joins an existing game. |
| 10 | event.join = params.get("join"); |
| 11 | } else if (params.has("watch")) { |
| 12 | // Spectator watches an existing game. |
| 13 | event.watch = params.get("watch"); |
| 14 | } else { |
| 15 | // First player starts a new game. |
| 16 | } |
| 17 | websocket.send(JSON.stringify(event)); |
| 18 | }); |
| 19 | } |
| 20 | |
| 21 | function showMessage(message) { |
| 22 | window.setTimeout(() => window.alert(message), 50); |