-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclientexample.html
More file actions
205 lines (175 loc) · 7.22 KB
/
Copy pathclientexample.html
File metadata and controls
205 lines (175 loc) · 7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>COE Client Example</title>
</head>
<body>
<button id="connectBtn">Connect</button>
<button id="createSessionBtn">Create Session</button>
<button id="joinSessionBtn">Join Session</button>
<button id="submitSnippetBtn">Submit Snippet</button>
<button id="voteP1">Vote Player 1</button>
<button id="voteP2">Vote Player 2</button>
<button id="voteP3">Vote Player 3</button>
<button id="voteP4">Vote Player 4</button>
<div id="logs"></div>
<script>
let socket = null;
let sessionId = null;
let playerId = null;
let otherPlayersIds = [];
let otherPlayersNames = [];
let voteBuffer = [];
const logsDiv = document.getElementById('logs');
const logMessage = (msg) => {
logsDiv.innerHTML += `<p>${msg}</p>`;
};
document.getElementById('connectBtn').onclick = () => {
// Connect to your FastAPI WebSocket server
socket = new WebSocket("ws://172.30.187.106:8000/ws");
socket.onopen = () => {
logMessage("WebSocket connected!");
};
socket.onmessage = (event) => {
const data = JSON.parse(event.data);
logMessage("Server says: " + JSON.stringify(data, null, 2));
switch (data.event) {
case "session_created":
sessionId = data.session_id;
playerId = data.player_id;
break;
case "session_joined":
sessionId = data.session_id;
playerId = data.player_id;
if (data.existing_players) otherPlayersIds = data.existing_players.map(p => p.player_id);
console.log(otherPlayersIds);
break;
case "player_joined":
otherPlayersIds.push(data.player_id);
otherPlayersNames.push(data.player_name);
break;
case "game_started":
logMessage("Game started. Turn order: " + data.turn_order);
break;
case "snippet_submitted":
logMessage("Player " + data.player_id + " submitted: " + data.snippet_text);
break;
case "vote_received":
logMessage("Player " + data.voter_id + " voted: " + data.vote_value);
break;
case "votes_finalized":
logMessage("Votes are in! Tally: " + JSON.stringify(data.tally));
break;
case "turn_updated":
logMessage("Now it's " + data.current_turn_player + "'s turn.");
break;
// ... handle other events ...
case "error":
logMessage("Error: " + data.error);
break;
default:
break;
}
};
socket.onclose = () => {
logMessage("WebSocket closed");
};
};
document.getElementById('createSessionBtn').onclick = () => {
if (!socket) return;
socket.send(JSON.stringify({
action: "create_session",
player_name: "Alice"
}));
};
document.getElementById('joinSessionBtn').onclick = () => {
if (!socket) return;
const sId = prompt("Enter session ID:");
const pName = prompt("Enter your player name:");
socket.send(JSON.stringify({
action: "join_session",
session_id: sId,
player_name: pName
}));
};
document.getElementById('submitSnippetBtn').onclick = () => {
if (!socket) return;
const snippet = prompt("Enter your snippet (5 words, for example)");
socket.send(JSON.stringify({
action: "submit_snippet",
snippet
}));
};
document.getElementById('voteP1').onclick = () => {
// If player has already been voted for, do nothing
if (voteBuffer.includes(otherPlayersIds[0])) return;
// Otherwise add to vote buffer
voteBuffer.push(otherPlayersIds[0]);
if (!socket) return;
console.log(voteBuffer.length, otherPlayersIds.length);
// If all three votes cast, send to server
if (voteBuffer.length === otherPlayersIds.length) {
socket.send(JSON.stringify({
action: "submit_votes",
voter_id: playerId,
votes: voteBuffer
}));
voteBuffer = [];
}
};
document.getElementById('voteP2').onclick = () => {
// If player has already been voted for, do nothing
if (voteBuffer.includes(otherPlayersIds[1])) return;
console.log(otherPlayersIds[1]);
// Otherwise add to vote buffer
voteBuffer.push(otherPlayersIds[1]);
if (!socket) return;
console.log(voteBuffer.length, otherPlayersIds.length);
// If all three votes cast, send to server
if (voteBuffer.length === otherPlayersIds.length) {
socket.send(JSON.stringify({
action: "submit_votes",
voter_id: playerId,
votes: voteBuffer
}));
voteBuffer = [];
}
};
document.getElementById('voteP3').onclick = () => {
// If player has already been voted for, do nothing
if (voteBuffer.includes(otherPlayersIds[2])) return;
// Otherwise add to vote buffer
voteBuffer.push(otherPlayersIds[2]);
if (!socket) return;
// If all three votes cast, send to server
console.log(voteBuffer.length, otherPlayersIds.length);
if (voteBuffer.length === otherPlayersIds.length) {
socket.send(JSON.stringify({
action: "submit_votes",
voter_id: playerId,
votes: voteBuffer
}));
voteBuffer = [];
}
};
document.getElementById('voteP4').onclick = () => {
// If player has already been voted for, do nothing
if (voteBuffer.includes(otherPlayersIds[3])) return;
// Otherwise add to vote buffer
voteBuffer.push(otherPlayersIds[3]);
if (!socket) return;
// If all three votes cast, send to server
console.log(voteBuffer.length, otherPlayersIds.length);
if (voteBuffer.length === otherPlayersIds.length) {
socket.send(JSON.stringify({
action: "submit_votes",
voter_id: playerId,
votes: voteBuffer
}));
voteBuffer = [];
}
};
</script>
</body>
</html>