summaryrefslogtreecommitdiff
path: root/server.js
diff options
context:
space:
mode:
authoralex <[email protected]>2026-11-10 20:54:44 +0100
committeralex <[email protected]>2026-11-10 20:54:44 +0100
commit5bdac896f505b850142682a12e3f5961f775efb7 (patch)
tree766ded4a9dd6b1e84339432cfcaa3862c13c4984 /server.js
downloadwheel-of-names-clone.git-5bdac896f505b850142682a12e3f5961f775efb7.tar.xz
wheel-of-names-clone.git-5bdac896f505b850142682a12e3f5961f775efb7.zip
init
Diffstat (limited to 'server.js')
-rw-r--r--server.js114
1 files changed, 114 insertions, 0 deletions
diff --git a/server.js b/server.js
new file mode 100644
index 0000000..1f5db18
--- /dev/null
+++ b/server.js
@@ -0,0 +1,114 @@
+const express = require("express");
+const http = require("http");
+const path = require("path");
+const { Server } = require("socket.io");
+
+const app = express();
+const server = http.createServer(app);
+const io = new Server(server);
+
+app.use(express.json());
+app.use(express.static(path.join(__dirname, "public")));
+
+const state = {
+ entries: ["Alice", "Bob", "Charlie", "Diana", "Eve", "Frank"],
+ selectedWinner: null,
+ lastSpin: null,
+};
+
+const normalizeEntries = (entries) => {
+ if (!Array.isArray(entries) || entries.length < 2) {
+ return null;
+ }
+
+ const cleaned = entries
+ .map((entry) => String(entry).trim())
+ .filter((entry) => entry.length > 0);
+
+ if (cleaned.length < 2) {
+ return null;
+ }
+
+ return cleaned;
+};
+
+const emitState = () => {
+ io.emit("state", {
+ entries: state.entries,
+ selectedWinner: state.selectedWinner,
+ lastSpin: state.lastSpin,
+ });
+};
+
+io.on("connection", (socket) => {
+ socket.emit("state", {
+ entries: state.entries,
+ selectedWinner: state.selectedWinner,
+ lastSpin: state.lastSpin,
+ });
+});
+
+app.get("/api/state", (_req, res) => {
+ res.json({
+ entries: state.entries,
+ selectedWinner: state.selectedWinner,
+ lastSpin: state.lastSpin,
+ });
+});
+
+app.post("/api/entries", (req, res) => {
+ const entries = normalizeEntries(req.body?.entries);
+ if (!entries) {
+ return res.status(400).json({
+ error: "entries must be an array with at least 2 non-empty names",
+ });
+ }
+
+ state.entries = entries;
+
+ if (state.selectedWinner && !state.entries.includes(state.selectedWinner)) {
+ state.selectedWinner = null;
+ }
+
+ emitState();
+
+ return res.json({
+ ok: true,
+ entries: state.entries,
+ });
+});
+
+app.post("/api/spin", (req, res) => {
+ const winner = String(req.body?.winner || "").trim();
+
+ if (!winner) {
+ return res.status(400).json({ error: "winner is required" });
+ }
+
+ if (!state.entries.includes(winner)) {
+ return res.status(400).json({
+ error: "winner must be one of the configured entries",
+ entries: state.entries,
+ });
+ }
+
+ state.selectedWinner = winner;
+ state.lastSpin = {
+ winner,
+ requestedAt: new Date().toISOString(),
+ };
+
+ io.emit("spin", state.lastSpin);
+ emitState();
+
+ return res.json({
+ ok: true,
+ spin: state.lastSpin,
+ });
+});
+
+const port = Number(process.env.PORT || 3000);
+
+server.listen(port, () => {
+ console.log(`Wheel server running on http://localhost:${port}`);
+});