-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
116 lines (96 loc) · 2.87 KB
/
server.js
File metadata and controls
116 lines (96 loc) · 2.87 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
const express = require('express');
const fs = require('fs');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(express.json());
app.use(express.static('public'));
// File paths
const DATA_DIR = path.join(__dirname, 'data');
const SCORES_FILE = path.join(DATA_DIR, 'scores.json');
// Initialize scores file
function initializeScoresFile() {
try {
if (!fs.existsSync(DATA_DIR)) {
fs.mkdirSync(DATA_DIR, { recursive: true });
}
if (!fs.existsSync(SCORES_FILE)) {
const defaultData = { score: 0, date: null };
fs.writeFileSync(SCORES_FILE, JSON.stringify(defaultData, null, 2));
console.log('Created scores.json with default values');
}
} catch (error) {
console.error('Error initializing scores file:', error);
}
}
// Read scores from file
function readScores() {
try {
const data = fs.readFileSync(SCORES_FILE, 'utf8');
return JSON.parse(data);
} catch (error) {
console.error('Error reading scores:', error);
return { score: 0, date: null };
}
}
// Write scores to file
function writeScores(scoreData) {
try {
fs.writeFileSync(SCORES_FILE, JSON.stringify(scoreData, null, 2));
return true;
} catch (error) {
console.error('Error writing scores:', error);
return false;
}
}
// GET /api/highscore - Retrieve current high score
app.get('/api/highscore', (req, res) => {
try {
const scores = readScores();
res.json(scores);
} catch (error) {
res.status(500).json({ error: 'Failed to read high score' });
}
});
// POST /api/score - Submit new score
app.post('/api/score', (req, res) => {
try {
const { score } = req.body;
// Validate input
if (typeof score !== 'number' || score < 0 || !Number.isFinite(score)) {
return res.status(400).json({ error: 'Invalid score value' });
}
// Read current high score
const currentScores = readScores();
const currentHighScore = currentScores.score || 0;
// Check if new high score
let isNewHighScore = false;
let highScore = currentHighScore;
if (score > currentHighScore) {
isNewHighScore = true;
highScore = score;
const newScoreData = {
score: score,
date: new Date().toISOString()
};
const writeSuccess = writeScores(newScoreData);
if (!writeSuccess) {
return res.status(500).json({ error: 'Failed to update score' });
}
}
res.json({
isNewHighScore: isNewHighScore,
highScore: highScore,
submittedScore: score
});
} catch (error) {
console.error('Error processing score:', error);
res.status(500).json({ error: 'Failed to process score' });
}
});
// Initialize and start server
initializeScoresFile();
app.listen(PORT, () => {
console.log(`RapidClick server running on http://localhost:${PORT}`);
});