-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
84 lines (66 loc) · 2.33 KB
/
server.js
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
// server.js
const express = require('express');
const path = require('path');
const fs = require('fs');
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware to parse JSON in request bodies
app.use(express.json());
// Serve static files from 'public' folder
app.use(express.static(path.join(__dirname, 'public')));
// ------------- GAME STATE (in-memory) ----------------
let gameState = {
teamAChoice: null, // The name of the person Team A chose
teamBChoice: null, // The name of the person Team B chose
currentTurn: 'A', // Which team’s turn it is - 'A' or 'B'
};
// Helper to shuffle an array in-place (Fisher-Yates)
function shuffleArray(arr) {
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]];
}
}
// ---------------- ROUTES ----------------
// 1) Return a *random* selection of 25 photos
app.get('/api/photos', (req, res) => {
const photosDir = path.join(__dirname, 'public', 'photos');
fs.readdir(photosDir, (err, files) => {
if (err) {
return res.status(500).json({ error: 'Unable to fetch photos' });
}
// Filter out hidden/system files
let photoNames = files.filter(f => !f.startsWith('.'));
// Shuffle the array of filenames
shuffleArray(photoNames);
// Take only the first 25 after shuffling
const selectedPhotos = photoNames.slice(0, 25);
return res.json(selectedPhotos);
});
});
// 2) Set a team’s choice
app.post('/api/choose', (req, res) => {
// Expect { team: 'A' or 'B', choice: 'PersonName.jpg' }
const { team, choice } = req.body;
if (team === 'A') {
gameState.teamAChoice = choice;
} else if (team === 'B') {
gameState.teamBChoice = choice;
} else {
return res.status(400).json({ error: 'Invalid team' });
}
return res.json({ message: 'Choice registered', gameState });
});
// 3) Fetch current game state
app.get('/api/state', (req, res) => {
res.json(gameState);
});
// 4) Switch turns between Team A / Team B
app.post('/api/next-turn', (req, res) => {
gameState.currentTurn = gameState.currentTurn === 'A' ? 'B' : 'A';
res.json({ message: 'Turn switched', currentTurn: gameState.currentTurn });
});
// ------------- START SERVER -------------
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
});