Add express handlers for managing save games
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
Manuel Thalmann 2022-12-14 11:23:04 +01:00
parent f2a8b1e58e
commit 7a28a7e314
No known key found for this signature in database
GPG key ID: 5FD9AD3CCDDBD27B
3 changed files with 131 additions and 2 deletions

31
package-lock.json generated
View file

@ -2660,6 +2660,14 @@
"node": ">=6.0.0" "node": ">=6.0.0"
} }
}, },
"node_modules/drange": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/drange/-/drange-1.1.1.tgz",
"integrity": "sha512-pYxfDYpued//QpnLIm4Avk7rsNtAtQkUES2cwAYSvD/wd2pKD71gN2Ebj3e7klzXwjocvE8c5vx/1fxwpqmSxA==",
"engines": {
"node": ">=4"
}
},
"node_modules/duplexify": { "node_modules/duplexify": {
"version": "3.7.1", "version": "3.7.1",
"resolved": "https://registry.npmjs.org/duplexify/-/duplexify-3.7.1.tgz", "resolved": "https://registry.npmjs.org/duplexify/-/duplexify-3.7.1.tgz",
@ -7238,6 +7246,26 @@
} }
] ]
}, },
"node_modules/randexp": {
"version": "0.5.3",
"resolved": "https://registry.npmjs.org/randexp/-/randexp-0.5.3.tgz",
"integrity": "sha512-U+5l2KrcMNOUPYvazA3h5ekF80FHTUG+87SEAmHZmolh1M+i/WyTCxVzmi+tidIa1tM4BSe8g2Y/D3loWDjj+w==",
"dependencies": {
"drange": "^1.0.2",
"ret": "^0.2.0"
},
"engines": {
"node": ">=4"
}
},
"node_modules/randexp/node_modules/ret": {
"version": "0.2.2",
"resolved": "https://registry.npmjs.org/ret/-/ret-0.2.2.tgz",
"integrity": "sha512-M0b3YWQs7R3Z917WRQy1HHA7Ba7D8hvZg6UE5mLykJxQVE2ju0IXbGlaHPPlkY+WN7wFP+wUMXmBFA0aV6vYGQ==",
"engines": {
"node": ">=4"
}
},
"node_modules/range-parser": { "node_modules/range-parser": {
"version": "1.2.1", "version": "1.2.1",
"resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz",
@ -9692,7 +9720,8 @@
"name": "connect-force-server", "name": "connect-force-server",
"version": "0.0.0", "version": "0.0.0",
"dependencies": { "dependencies": {
"express": "^4.18.2" "express": "^4.18.2",
"randexp": "^0.5.3"
}, },
"devDependencies": { "devDependencies": {
"@types/express": "^4.17.15", "@types/express": "^4.17.15",

View file

@ -8,7 +8,8 @@
"start": "noderel -e ./src/main.js" "start": "noderel -e ./src/main.js"
}, },
"dependencies": { "dependencies": {
"express": "^4.18.2" "express": "^4.18.2",
"randexp": "^0.5.3"
}, },
"devDependencies": { "devDependencies": {
"@types/express": "^4.17.15", "@types/express": "^4.17.15",

View file

@ -1,8 +1,11 @@
import { join } from "path"; import { join } from "path";
import { fileURLToPath } from "url"; import { fileURLToPath } from "url";
import express from "express"; import express from "express";
import RandExp from "randexp";
import { HTTPError } from "./HTTPError.js"; import { HTTPError } from "./HTTPError.js";
const { randexp } = RandExp;
const dirname = fileURLToPath(new URL(".", import.meta.url)); const dirname = fileURLToPath(new URL(".", import.meta.url));
const app = express(); const app = express();
@ -10,6 +13,27 @@ const apiKeys = [
"c4game" "c4game"
]; ];
const dataPath = "/api/data";
const parametrizedDataPath = "/api/data/:id";
/**
* The data provided by the api.
*
* @type {Record<string, unknown>}
*/
let data = {};
/**
* Creates a new guid.
*
* @returns {string}
* The newly created guid.
*/
function createGuid()
{
return randexp(/[0-9a-f]{8}(-[0-9a-f]){4}[0-9a-f]{8}/);
}
app.use(express.static(join(dirname, "..", "..", "game", "lib", "static"))); app.use(express.static(join(dirname, "..", "..", "game", "lib", "static")));
app.use("/api", express.json()); app.use("/api", express.json());
@ -38,6 +62,66 @@ app.use(
} }
}); });
app.get(
parametrizedDataPath,
(request, response, next) =>
{
let id = request.params.id;
console.log(`Data ID \`${id}\` requested`);
if (id in data)
{
response.send(data[id]);
}
else
{
next();
}
});
app.post(
dataPath,
(request, response, next) =>
{
let id = createGuid();
data[id] = request.body;
response.send({ id });
});
app.put(
parametrizedDataPath,
(request, response, next) =>
{
let id = request.params.id;
if (id in data)
{
data[id] = request.body;
}
else
{
next();
}
});
app.delete(
parametrizedDataPath,
(request, response, next) =>
{
let id = request.params.id;
if (id in data)
{
delete data[id];
response.send();
response.status(204);
}
else
{
next();
}
});
app.use( app.use(
[ [
(error, request, response, next) => (error, request, response, next) =>
@ -47,4 +131,19 @@ app.use(
} }
]); ]);
app.use(
"/api",
(request, response) =>
{
response.send({ error: "Not Found" });
response.status(404);
});
app.use(
(request, response) =>
{
response.send("Not Found");
response.status(404);
});
app.listen(1337); app.listen(1337);