Split client into frontend website and server

This commit is contained in:
Pax1601
2024-02-08 22:04:23 +01:00
parent 55f3bd5adb
commit 5ca6c97cbe
792 changed files with 149898 additions and 13872 deletions

View File

@@ -0,0 +1,73 @@
var express = require('express');
var app = express();
var fs = require('fs');
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false}));
app.use(bodyParser.json());
const allowedTheatres = [
"caucasus",
"falklands",
"marianas",
"nevada",
"normandy",
"persiangulf",
"sinaimap",
"syria",
"thechannel"
];
function getAirbasesData( theatreName ) {
if ( !isValidTheatre( theatreName ) ) {
return false;
}
return JSON.parse( fs.readFileSync( `public/databases/airbases/${theatreName}.json` ) ).airfields
}
function isValidTheatre( theatre ) {
return ( allowedTheatres.indexOf( theatre ) > -1 )
}
function sendInvalidTheatre( res ) {
res.status( 400 ).send( "Missing/invalid theatre name; must be one of:\n\t" + allowedTheatres.join( "\n\t" ) );
}
/**************************************************************************************************************/
// Endpoints
/**************************************************************************************************************/
app.get( "/", ( req, res ) => {
sendInvalidTheatre( res );
});
app.get( "/:theatreName/:airbaseName", ( req, res ) => {
const airbases = getAirbasesData( req.params.theatreName );
if ( !airbases ) {
sendInvalidTheatre( res );
return;
}
const airbaseName = req.params.airbaseName;
if ( !airbases.hasOwnProperty( airbaseName ) ) {
res.status( 404 ).send( `Unknown airbase name "${airbaseName}". Available options are:\n\t` + Object.keys( airbases ).join( "\n\t" ) );
} else {
res.status( 200 ).json( airbases[ airbaseName ] );
}
});
app.get( "/:theatreName", ( req, res ) => {
const theatreName = req.params.theatreName.toLowerCase().replace( /\s*/g, "" );
const airbases = getAirbasesData( theatreName );
if ( !airbases ) {
sendInvalidTheatre( res );
return;
}
res.status( 200 ).json( airbases );
});
module.exports = app;

View File

@@ -0,0 +1,266 @@
var express = require('express');
var app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
function Flight(name, boardId, unitId) {
this.assignedAltitude = 0;
this.assignedSpeed = 0;
this.id = uuidv4();
this.boardId = boardId;
this.name = name;
this.status = "unknown";
this.takeoffTime = -1;
this.unitId = parseInt(unitId);
}
Flight.prototype.getData = function () {
return {
"assignedAltitude": this.assignedAltitude,
"assignedSpeed": this.assignedSpeed,
"id": this.id,
"boardId": this.boardId,
"name": this.name,
"status": this.status,
"takeoffTime": this.takeoffTime,
"unitId": this.unitId
};
}
Flight.prototype.setAssignedAltitude = function (assignedAltitude) {
if (isNaN(assignedAltitude)) {
return "Altitude must be a number"
}
this.assignedAltitude = parseInt(assignedAltitude);
return true;
}
Flight.prototype.setAssignedSpeed = function (assignedSpeed) {
if (isNaN(assignedSpeed)) {
return "Speed must be a number"
}
this.assignedSpeed = parseInt(assignedSpeed);
return true;
}
Flight.prototype.setOrder = function (order) {
this.order = order;
return true;
}
Flight.prototype.setStatus = function (status) {
if (["unknown", "checkedin", "readytotaxi", "clearedtotaxi", "halted", "terminated"].indexOf(status) < 0) {
return "Invalid status";
}
this.status = status;
return true;
}
Flight.prototype.setTakeoffTime = function (takeoffTime) {
if (takeoffTime === "" || takeoffTime === -1) {
this.takeoffTime = -1;
}
if (isNaN(takeoffTime)) {
return "Invalid takeoff time"
}
this.takeoffTime = parseInt(takeoffTime);
return true;
}
function ATCDataHandler(data) {
this.data = data;
}
ATCDataHandler.prototype.addFlight = function (flight) {
if (flight instanceof Flight === false) {
throw new Error("Given flight is not an instance of Flight");
}
this.data.flights[flight.id] = flight;
}
ATCDataHandler.prototype.deleteFlight = function (flightId) {
delete this.data.flights[flightId];
}
ATCDataHandler.prototype.getFlight = function (flightId) {
return this.data.flights[flightId] || false;
}
ATCDataHandler.prototype.getFlights = function () {
return this.data.flights;
}
const dataHandler = new ATCDataHandler({
"flights": {}
});
/**************************************************************************************************************/
// Endpoints
/**************************************************************************************************************/
app.get("/flight", (req, res) => {
let flights = Object.values(dataHandler.getFlights());
if (flights && req.query.boardId) {
flights = flights.reduce((acc, flight) => {
if (flight.boardId === req.query.boardId) {
acc[flight.id] = flight;
}
return acc;
}, {});
}
res.json(flights);
});
app.patch("/flight/:flightId", (req, res) => {
const flightId = req.params.flightId;
const flight = dataHandler.getFlight(flightId);
if (!flight) {
res.status(400).send(`Unrecognised flight ID (given: "${req.params.flightId}")`);
}
if (req.body.hasOwnProperty("assignedAltitude")) {
const altitudeChangeSuccess = flight.setAssignedAltitude(req.body.assignedAltitude);
if (altitudeChangeSuccess !== true) {
res.status(400).send(altitudeChangeSuccess);
}
}
if (req.body.hasOwnProperty("assignedSpeed")) {
const speedChangeSuccess = flight.setAssignedSpeed(req.body.assignedSpeed);
if (speedChangeSuccess !== true) {
res.status(400).send(speedChangeSuccess);
}
}
if (req.body.status) {
const statusChangeSuccess = flight.setStatus(req.body.status);
if (statusChangeSuccess !== true) {
res.status(400).send(statusChangeSuccess);
}
}
if (req.body.hasOwnProperty("takeoffTime")) {
const takeoffChangeSuccess = flight.setTakeoffTime(req.body.takeoffTime);
if (takeoffChangeSuccess !== true) {
res.status(400).send(takeoffChangeSuccess);
}
}
res.json(flight.getData());
});
app.post("/flight/order", (req, res) => {
if (!req.body.boardId) {
res.status(400).send("Invalid/missing boardId");
}
if (!req.body.order || !Array.isArray(req.body.order)) {
res.status(400).send("Invalid/missing boardId");
}
req.body.order.forEach((flightId, i) => {
dataHandler.getFlight(flightId).setOrder(i);
});
res.send("");
});
app.post("/flight", (req, res) => {
if (!req.body.boardId) {
res.status(400).send("Invalid/missing boardId");
}
if (!req.body.name) {
res.status(400).send("Invalid/missing flight name");
}
if (!req.body.unitId || isNaN(req.body.unitId)) {
res.status(400).send("Invalid/missing unitId");
}
const flight = new Flight(req.body.name, req.body.boardId, req.body.unitId);
dataHandler.addFlight(flight);
res.status(201);
res.json(flight.getData());
});
app.delete("/flight/:flightId", (req, res) => {
const flight = dataHandler.getFlight(req.params.flightId);
if (!flight) {
res.status(400).send(`Unrecognised flight ID (given: "${req.params.flightId}")`);
}
dataHandler.deleteFlight(req.params.flightId);
res.status(204).send("");
});
module.exports = app;

View File

@@ -0,0 +1,61 @@
module.exports = function (databasesLocation) {
const express = require('express');
const router = express.Router();
const fs = require("fs");
const path = require("path");
router.get('/:type/:name', function (req, res) {
var contents = fs.readFileSync(path.join(databasesLocation, req.params.type, req.params.name + ".json"));
res.status(200).send(contents);
});
router.put('/save/:type/:name', function (req, res) {
var dir = path.join(databasesLocation, req.params.type, "old");
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
var filepath = path.join(databasesLocation, req.params.type, req.params.name + ".json");
if (fs.existsSync(filepath)) {
var newFilepath = path.join(databasesLocation, req.params.type, "old", req.params.name + ".json");
fs.copyFileSync(filepath, newFilepath);
if (fs.existsSync(newFilepath)) {
try {
var json = JSON.stringify(req.body.blueprints, null, "\t");
fs.writeFileSync(filepath, json, 'utf8');
res.send("OK");
} catch {
res.status(422).send("Error");
}
} else {
res.status(422).send("Error");
}
} else {
res.status(404).send('Not found');
}
});
router.put('/reset/:type/:name', function (req, res) {
var filepath = path.join(databasesLocation, req.params.type, "default", req.params.name + ".json");
if (fs.existsSync(filepath)) {
var newFilepath = path.join(databasesLocation, req.params.type, req.params.name + ".json");
fs.copyFileSync(filepath, newFilepath);
res.send("OK");
} else {
res.status(404).send('Not found');
}
});
router.put('/restore/:type/:name', function (req, res) {
var filepath = path.join(databasesLocation, req.params.type, "old", req.params.name + ".json");
if (fs.existsSync(filepath)) {
var newFilepath = path.join(databasesLocation, req.params.type, req.params.name + ".json");
fs.copyFileSync(filepath, newFilepath);
res.send("OK");
} else {
res.status(404).send('Not found');
}
});
return router;
}

View File

@@ -0,0 +1,28 @@
const express = require('express');
var fs = require('fs');
const router = express.Router();
const TileSet = require('srtm-elevation').TileSet;
const SRTMElevationDownloader = require('srtm-elevation').SRTMElevationDownloader;
let rawdata = fs.readFileSync('../../olympus.json');
let config = JSON.parse(rawdata);
var tileset = null;
if (config["client"] === undefined || config["client"]["elevationProvider"] === undefined)
tileset = new TileSet('./hgt');
else
tileset = new TileSet('./hgt', {downloader: new SRTMElevationDownloader('./hgt', config["client"]["elevationProvider"])});
router.get( "/:lat/:lng", ( req, res ) => {
tileset.getElevation([req.params.lat, req.params.lng], function(err, elevation) {
if (err) {
console.log('getElevation failed: ' + err.message);
res.send("n/a");
} else {
res.send(String(elevation));
}
});
});
module.exports = router;