cackerman commited on
Commit
91e6663
·
verified ·
1 Parent(s): 0e308b4

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +68 -36
index.html CHANGED
@@ -3,9 +3,11 @@
3
  <head>
4
  <meta charset="utf-8" />
5
  <title>Room Scenario Game</title>
 
6
  <style>
 
7
  body { font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif; margin: 16px; }
8
- #wrap { max-width: 1000px; margin: 0 auto; }
9
  #log { width: 100%; height: 60vh; border: 1px solid #ccc; padding: 12px; overflow: auto; white-space: pre-wrap; }
10
  #row { margin-top: 12px; display: flex; gap: 8px; }
11
  #cmd { flex: 1; padding: 8px; font-size: 16px; }
@@ -20,65 +22,95 @@
20
  <input id="cmd" type="text" placeholder="[Press Enter to start]" autocomplete="off" />
21
  <button id="primary">Start</button>
22
  </div>
23
- <div id="small">Tip: Enter starts, continues, or submits your move. The button does the same.</div>
24
  </div>
 
25
  <script>
26
  const log = document.getElementById("log");
27
  const cmd = document.getElementById("cmd");
28
  const btn = document.getElementById("primary");
29
 
30
- async function fetchState() {
31
- const r = await fetch("/state");
32
- return r.json();
33
- }
34
  function render(s) {
35
  log.textContent = s.transcript || "";
36
  btn.textContent = s.primary_label || "Start";
37
  cmd.placeholder = s.placeholder || "";
38
- // Show/hide the button: hide during player's action (mode=awaiting_action)
39
- if (s.mode === "awaiting_action") {
40
- btn.style.display = "none";
41
- } else {
42
- btn.style.display = "inline-block";
43
- }
44
- // Keep focus in the input at all times so Enter always works
45
  cmd.focus();
46
- // Scroll to bottom
47
- log.scrollTop = log.scrollHeight;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  }
49
 
50
  async function init() {
51
- render(await fetchState());
 
 
 
 
52
 
53
  btn.addEventListener("click", async () => {
54
- const r = await fetch("/primary", { method: "POST" });
55
- render(await r.json());
56
- cmd.value = "";
57
  });
58
 
59
- // One input for everything: Enter continues or submits based on mode
60
  cmd.addEventListener("keydown", async (e) => {
61
  if (e.key !== "Enter") return;
62
  e.preventDefault();
63
- const s = await fetchState();
64
- if (s.mode === "awaiting_action") {
65
- const text = cmd.value.trim();
66
- const r = await fetch("/action", {
67
- method: "POST",
68
- headers: { "Content-Type": "application/json" },
69
- body: JSON.stringify({ text })
70
- });
71
- render(await r.json());
72
- cmd.value = "";
73
- } else if (s.mode === "awaiting_continue" || s.mode === "awaiting_start") {
74
- const r = await fetch("/primary", { method: "POST" });
75
- render(await r.json());
76
- cmd.value = "";
77
- } else {
78
- // over: do nothing
79
  }
80
  });
 
 
 
 
81
  }
 
82
  init();
83
  </script>
84
  </body>
 
3
  <head>
4
  <meta charset="utf-8" />
5
  <title>Room Scenario Game</title>
6
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
7
  <style>
8
+ :root { --w: 1000px; }
9
  body { font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif; margin: 16px; }
10
+ #wrap { max-width: var(--w); margin: 0 auto; }
11
  #log { width: 100%; height: 60vh; border: 1px solid #ccc; padding: 12px; overflow: auto; white-space: pre-wrap; }
12
  #row { margin-top: 12px; display: flex; gap: 8px; }
13
  #cmd { flex: 1; padding: 8px; font-size: 16px; }
 
22
  <input id="cmd" type="text" placeholder="[Press Enter to start]" autocomplete="off" />
23
  <button id="primary">Start</button>
24
  </div>
25
+ <div id="small">Enter starts, continues, or submits your move. The button does the same.</div>
26
  </div>
27
+
28
  <script>
29
  const log = document.getElementById("log");
30
  const cmd = document.getElementById("cmd");
31
  const btn = document.getElementById("primary");
32
 
33
+ function scrollBottom() { log.scrollTop = log.scrollHeight; }
 
 
 
34
  function render(s) {
35
  log.textContent = s.transcript || "";
36
  btn.textContent = s.primary_label || "Start";
37
  cmd.placeholder = s.placeholder || "";
38
+ // Hide the button when it's the live player's turn; otherwise show it.
39
+ btn.style.display = (s.mode === "awaiting_action") ? "none" : "inline-block";
40
+ // Keep focus where Enter should go
 
 
 
 
41
  cmd.focus();
42
+ scrollBottom();
43
+ }
44
+
45
+ async function fetchState() {
46
+ const r = await fetch("/state", { credentials: "include" });
47
+ if (!r.ok) throw new Error("state fetch failed");
48
+ return r.json();
49
+ }
50
+
51
+ async function callPrimary() {
52
+ btn.disabled = true;
53
+ try {
54
+ const r = await fetch("/primary", { method: "POST", credentials: "include" });
55
+ const s = await r.json();
56
+ render(s);
57
+ cmd.value = "";
58
+ } finally {
59
+ btn.disabled = false;
60
+ }
61
+ }
62
+
63
+ async function callAction(text) {
64
+ btn.disabled = true; // hidden during action anyway; still block double-submit
65
+ try {
66
+ const r = await fetch("/action", {
67
+ method: "POST",
68
+ credentials: "include",
69
+ headers: { "Content-Type": "application/json" },
70
+ body: JSON.stringify({ text })
71
+ });
72
+ const s = await r.json();
73
+ render(s);
74
+ cmd.value = "";
75
+ } finally {
76
+ btn.disabled = false;
77
+ }
78
  }
79
 
80
  async function init() {
81
+ try {
82
+ render(await fetchState());
83
+ } catch (e) {
84
+ log.textContent = "Failed to load initial state.\n" + (e && e.message ? e.message : e);
85
+ }
86
 
87
  btn.addEventListener("click", async () => {
88
+ await callPrimary();
 
 
89
  });
90
 
91
+ // Single input for everything: Enter either submits action or continues
92
  cmd.addEventListener("keydown", async (e) => {
93
  if (e.key !== "Enter") return;
94
  e.preventDefault();
95
+ try {
96
+ const s = await fetchState();
97
+ if (s.mode === "awaiting_action") {
98
+ const text = cmd.value.trim();
99
+ await callAction(text);
100
+ } else if (s.mode === "awaiting_continue" || s.mode === "awaiting_start") {
101
+ await callPrimary();
102
+ } // mode "over": do nothing
103
+ } catch (err) {
104
+ // Keep UI usable even on transient network errors
105
+ console.error(err);
 
 
 
 
 
106
  }
107
  });
108
+
109
+ // Initial focus/scroll
110
+ cmd.focus();
111
+ scrollBottom();
112
  }
113
+
114
  init();
115
  </script>
116
  </body>