-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathindex.jsx
269 lines (257 loc) · 8.04 KB
/
index.jsx
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import Head from "next/head";
import { useEffect, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import "@/i18n/i18n";
import AdminPage from "@/components/AdminPage";
import BuzzerPage from "@/components/BuzzerPage";
import Footer from "@/components/Login/Footer";
import LoginPage from "@/components/LoginPage";
import { ERROR_CODES } from "@/i18n/errorCodes";
import cookieCutter from "cookie-cutter";
export default function Home() {
const { t } = useTranslation();
const [playerName, setPlayerName] = useState("");
const [roomCode, setRoomCode] = useState("");
const [error, setErrorVal] = useState("");
const [registeredRoomCode, setRegisteredRoomCode] = useState(null);
const [host, setHost] = useState(false);
const [game, setGame] = useState(null);
const [team, setTeam] = useState(null);
const [playerID, setPlayerID] = useState(null);
const ws = useRef(null);
function setError(e) {
setErrorVal(e);
setTimeout(() => {
setErrorVal("");
}, 5000);
}
/**
* send quit message to server
* server cleans up data on backend then
* tells client to clean up
*/
function quitGame(host = false) {
ws.current.send(
JSON.stringify({
action: "quit",
host: host,
id: playerID,
room: registeredRoomCode,
})
);
}
function startWsConnection(ws) {
ws.current = new WebSocket(`wss://${window.location.host}/api/ws`);
ws.current.onopen = function () {
console.debug("game connected to server", ws.current);
ws.current.onmessage = function (evt) {
var received_msg = evt.data;
let json = JSON.parse(received_msg);
if (json.action === "host_room") {
console.debug("registering room with host", json.room);
setPlayerID(json.id);
setHost(true);
setRegisteredRoomCode(json.room);
setGame(json.game);
cookieCutter.set("session", `${json.room}:${json.id}`);
} else if (json.action === "join_room") {
console.debug("Joining room : ", json);
setPlayerID(json.id);
setRegisteredRoomCode(json.room);
setGame(json.game);
if (json.team != null) {
setTeam(json.team);
}
} else if (json.action === "quit") {
console.debug("player quit");
setPlayerID(null);
setRegisteredRoomCode(null);
cookieCutter.set("session", "");
setGame({});
setHost(false);
} else if (json.action === "get_back_in") {
console.debug("Getting back into room", json);
if (json.host === true) {
setHost(true);
}
if (Number.isInteger(json.team)) {
setTeam(json.team);
}
setPlayerID(json.id);
setRegisteredRoomCode(json.room);
setGame(json.game);
} else if (json.action === "error") {
console.error(json);
setError(t(json.code, { message: json.message }));
} else if (json.action === "ping") {
console.debug("index.js: ping");
} else {
console.debug("did not expect in index.js: ", json);
}
};
ws.current.onerror = function (e) {
console.error(e);
};
};
}
function waitForSocketConnection(socket, callback, tries = 0) {
setTimeout(function () {
if (socket.readyState === 1) {
if (callback != null) {
callback();
}
} else {
console.debug("wait for connection...");
tries++;
if (tries > 30) {
setError(t(ERROR_CODES.UNABLE_TO_CONNECT));
return;
}
waitForSocketConnection(socket, callback, tries);
}
}, 100); // wait 100 milisecond for the connection...
}
/**
* put initalization logic inside send method
* this is make sure the websocket connection
* doesn't stay idleing while a player is sitting
* on the main page
*/
function send(message) {
console.debug("send", ws);
if (ws.current?.readyState !== 1 || !ws.current) {
console.debug("connecting to server... new connection");
startWsConnection(ws);
waitForSocketConnection(ws.current, function () {
ws.current.send(message);
});
} else {
console.debug("send", message);
ws.current.send(message);
}
}
/**
* on page refresh check for existing session
* if it exists then tell the server to send back
* the game object
*/
useEffect(() => {
let session = cookieCutter.get("session");
console.debug("user session", session);
if (session != "" && session != null) {
send(JSON.stringify({ action: "get_back_in", session: session }));
}
}, []);
function hostRoom() {
send(
JSON.stringify({
action: "host_room",
})
);
}
/**
* tell server to join a game
* do some validation on inputs
*/
function joinRoom() {
console.debug(`ws.current `, ws);
setError("");
let roomcode = document.getElementById("roomCodeInput").value;
if (roomcode.length === 4) {
let playername = document.getElementById("playerNameInput").value;
if (playername.length > 0) {
console.debug(`roomcode: ${roomcode}, playername ${playername}`);
send(
JSON.stringify({
action: "join_room",
room: roomcode.toUpperCase(),
name: playername,
})
);
} else {
setError(t(ERROR_CODES.MISSING_INPUT, { message: t("name") }));
}
} else {
setError(t("room code is not correct length, should be 4 characters"));
}
}
// control what to render based on if the player is hosting
function getPage() {
if (registeredRoomCode !== null && host && game != null) {
return (
<div className="w-full lg:flex lg:flex-row lg:justify-center">
<div className="sm:w-full md:w-full lg:w-3/4">
<AdminPage
ws={ws}
game={game}
id={playerID}
setGame={setGame}
room={registeredRoomCode}
quitGame={quitGame}
/>
</div>
</div>
);
} else if (registeredRoomCode !== null && !host && game != null) {
return (
<div className="flex w-full justify-center">
<div className="flex w-11/12 flex-col space-y-3 pt-5 sm:w-10/12 md:w-3/4 lg:w-1/2">
<BuzzerPage
ws={ws}
game={game}
id={playerID}
setGame={setGame}
room={registeredRoomCode}
quitGame={quitGame}
setTeam={setTeam}
team={team}
/>
</div>
</div>
);
} else {
return (
<div className="relative flex min-h-screen w-full justify-center">
<div className="flex w-full flex-col sm:px-4 md:px-8 lg:w-1/2 lg:px-6">
<LoginPage
setRoomCode={setRoomCode}
roomCode={roomCode}
setPlayerName={setPlayerName}
playerName={playerName}
joinRoom={joinRoom}
hostRoom={hostRoom}
error={error}
/>
</div>
<Footer />
</div>
);
}
}
if (typeof window !== "undefined") {
document.body.className = game?.settings?.theme + " bg-background";
}
return (
<>
<Head>
<title>{t("Friendly Feud")}</title>
<meta name="author" content="Joshua Cold" />
<meta
name="description"
content="Free to play open source friendly feud game. Host your own custom created family feud games with built in online buzzers, timers and admin controls. Visit https://github.com/joshzcold/Cold-Friendly-Feud to check out the source code and contribute."
/>
</Head>
<main>
<div
style={{
width: "100vh",
}}
className={`${game?.settings?.theme} h-screen w-screen`}
>
{/* TODO put in the theme switcher and put setting here */}
{getPage()}
</div>
</main>
</>
);
}