-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
53 lines (45 loc) · 1.59 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
const express = require('express');
const path = require('path');
const fs = require('fs');
const app = express();
const PORT = 3000;
app.use(express.static(path.join(__dirname)));
app.use(express.json());
app.post('/log-movement', (req, res) => {
const { key } = req.body;
fs.appendFile(path.join(__dirname, 'movement.txt'), key + '\n', (err) => {
if (err) {
console.error('Failed to write movement command to file:', err);
res.status(500).send('Failed to write movement command');
} else {
res.send('Movement command updated');
}
});
});
app.post('/log-action', (req, res) => {
const { action } = req.body;
const parts = action.split('_');
const actionType = parts[0];
const x = parts[1];
const y = parts[2];
const id = parts.length > 3 ? parts[3] : null;
let filePath;
if (actionType === 'break') {
filePath = path.join(__dirname, 'break.txt');
} else if (actionType === 'place') {
filePath = path.join(__dirname, 'place.txt');
} else {
return res.status(400).send('Invalid action type');
}
const line = id ? `${actionType}_${x}_${y}_${id}\n` : `${actionType}_${x}_${y}\n`;
fs.appendFile(filePath, line, (err) => {
if (err) {
console.error('Failed to write action:', err);
return res.status(500).send('Failed to write action');
}
res.sendStatus(200);
});
});
app.listen(PORT, '0.0.0.0', () => {
console.log(`Server is running on http://localhost:${PORT}`);
});