cackerman commited on
Commit
5e45c59
·
verified ·
1 Parent(s): e6334a3

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +27 -50
index.html CHANGED
@@ -1,5 +1,5 @@
1
  <!doctype html>
2
- <html>
3
  <head>
4
  <meta charset="utf-8" />
5
  <title>Room Scenario Game</title>
@@ -30,87 +30,64 @@
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>
 
1
  <!doctype html>
2
+ <html>
3
  <head>
4
  <meta charset="utf-8" />
5
  <title>Room Scenario Game</title>
 
30
  const cmd = document.getElementById("cmd");
31
  const btn = document.getElementById("primary");
32
 
33
+ let SID = ""; // explicit session id (no cookies needed)
34
+
35
+ function scrollBottom(){ log.scrollTop = log.scrollHeight; }
36
+ function render(s){
37
+ if (s.sid) SID = s.sid;
38
  log.textContent = s.transcript || "";
39
  btn.textContent = s.primary_label || "Start";
40
  cmd.placeholder = s.placeholder || "";
 
41
  btn.style.display = (s.mode === "awaiting_action") ? "none" : "inline-block";
 
42
  cmd.focus();
43
  scrollBottom();
44
  }
45
 
46
+ async function fetchState(){
47
+ const r = await fetch(SID ? `/state?sid=${SID}` : "/state");
48
  if (!r.ok) throw new Error("state fetch failed");
49
  return r.json();
50
  }
51
+ async function callPrimary(){
 
52
  btn.disabled = true;
53
+ try{
54
+ const r = await fetch(SID ? `/primary?sid=${SID}` : "/primary", { method: "POST" });
55
+ render(await r.json());
 
56
  cmd.value = "";
57
+ } finally { btn.disabled = false; }
 
 
58
  }
59
+ async function callAction(text){
60
+ btn.disabled = true;
61
+ try{
62
+ const r = await fetch(SID ? `/action?sid=${SID}` : "/action", {
 
63
  method: "POST",
 
64
  headers: { "Content-Type": "application/json" },
65
  body: JSON.stringify({ text })
66
  });
67
+ render(await r.json());
 
68
  cmd.value = "";
69
+ } finally { btn.disabled = false; }
 
 
70
  }
71
 
72
+ async function init(){
73
+ render(await fetchState());
 
 
 
 
74
 
75
+ btn.addEventListener("click", async () => { await callPrimary(); });
 
 
76
 
 
77
  cmd.addEventListener("keydown", async (e) => {
78
  if (e.key !== "Enter") return;
79
  e.preventDefault();
80
+ const s = await fetchState();
81
+ if (s.mode === "awaiting_action"){
82
+ await callAction(cmd.value.trim());
83
+ } else if (s.mode === "awaiting_continue" || s.mode === "awaiting_start"){
84
+ await callPrimary();
 
 
 
 
 
 
85
  }
86
  });
87
 
 
88
  cmd.focus();
89
  scrollBottom();
90
  }
 
91
  init();
92
  </script>
93
  </body>