diff --git a/client/app.js b/client/app.js index a10aaf2f..85d5c31d 100644 --- a/client/app.js +++ b/client/app.js @@ -5,10 +5,11 @@ var logger = require('morgan'); var fs = require('fs'); var basicAuth = require('express-basic-auth') -var atcRouter = require('./routes/api/atc'); -var indexRouter = require('./routes/index'); -var uikitRouter = require('./routes/uikit'); -var usersRouter = require('./routes/users'); +var atcRouter = require('./routes/api/atc'); +var airbasesRouter = require('./routes/api/airbases'); +var indexRouter = require('./routes/index'); +var uikitRouter = require('./routes/uikit'); +var usersRouter = require('./routes/users'); var resourcesRouter = require('./routes/resources'); var app = express(); @@ -21,6 +22,7 @@ app.use(express.static(path.join(__dirname, 'public'))); app.use('/', indexRouter); app.use('/api/atc', atcRouter); +app.use('/api/airbases', airbasesRouter); app.use('/users', usersRouter); app.use('/uikit', uikitRouter); app.use('/resources', resourcesRouter); diff --git a/client/routes/api/airbases.js b/client/routes/api/airbases.js new file mode 100644 index 00000000..a080c794 --- /dev/null +++ b/client/routes/api/airbases.js @@ -0,0 +1,93 @@ +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", + "marianas", + "nevada", + "persiangulf", + "syria" +]; + + +function getAirbasesData( theatreName ) { + + if ( !isValidTheatre( theatreName ) ) { + return false; + } + + return JSON.parse( fs.readFileSync( `src/airfields/${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 airbases = getAirbasesData( req.params.theatreName ); + + if ( !airbases ) { + sendInvalidTheatre( res ); + return; + } + + res.status( 200 ).json( airbases ); + +}); + + + +module.exports = app; \ No newline at end of file diff --git a/client/src/airfields/caucasus.json b/client/src/airfields/caucasus.json index 6d75819f..7c060874 100644 --- a/client/src/airfields/caucasus.json +++ b/client/src/airfields/caucasus.json @@ -1,6 +1,6 @@ { "airfields": { - "Anapa": { + "Anapa-Vityazevo": { "ICAO": "URKA", "Elevation": "141", "TACAN": "", @@ -85,7 +85,7 @@ } } }, - "KOBULETI": { + "Kobuleti": { "ICAO": "UG5X", "Elevation": "69", "TACAN": "67X", @@ -102,7 +102,7 @@ } } }, - "Krasnodar": { + "Krasnodar-Center": { "ICAO": "URKL", "Elevation": "98", "TACAN": "", @@ -119,7 +119,7 @@ } } }, - "Krasnodar-P": { + "Krasnodar-Pashkovsky": { "ICAO": "URKK", "Elevation": "112", "TACAN": "", @@ -153,7 +153,7 @@ } } }, - "KUTAISI": { + "Kutaisi": { "ICAO": "UGKO", "Elevation": "148", "TACAN": "44X", @@ -170,7 +170,7 @@ } } }, - "Maykop": { + "Maykop-Khanskaya": { "ICAO": "URKH", "Elevation": "591", "TACAN": "", @@ -187,7 +187,7 @@ } } }, - "MinVody": { + "Mineralnye Vody": { "ICAO": "URMM", "Elevation": "1050", "TACAN": "", @@ -255,7 +255,7 @@ } } }, - "Senaki": { + "Senaki-Kolkhi": { "ICAO": "UGKS", "Elevation": "43", "TACAN": "31X", @@ -272,7 +272,7 @@ } } }, - "Sochi": { + "Sochi-Adler": { "ICAO": "URSS", "Elevation": "98", "TACAN": "", @@ -289,7 +289,24 @@ } } }, - "TbilisiMilitary": { + "Tbilisi-Lochini": { + "ICAO": "UGTB", + "Elevation": "1574", + "TACAN": "25X", + "Runways": { + "13": { + "Mag Hdg": "121", + "Length": "9300", + "ILS": "110.30" + }, + "31": { + "Mag Hdg": "301", + "Length": "9300", + "ILS": "108.90" + } + } + }, + "Soganlug": { "ICAO": "UG24", "Elevation": "1500", "TACAN": "25X", @@ -306,7 +323,7 @@ } } }, - "Sukhumi": { + "Sukhumi-Babushara": { "ICAO": "UGSS", "Elevation": "43", "TACAN": "", @@ -323,23 +340,6 @@ } } }, - "Lochini": { - "ICAO": "UGTB", - "Elevation": "1574", - "TACAN": "25X", - "Runways": { - "13": { - "Mag Hdg": "121", - "Length": "9300", - "ILS": "110.30" - }, - "31": { - "Mag Hdg": "301", - "Length": "9300", - "ILS": "108.90" - } - } - }, "Vaziani": { "ICAO": "UG27", "Elevation": "1524", diff --git a/client/src/missionhandler/missionhandler.ts b/client/src/missionhandler/missionhandler.ts index 93b13192..253283c2 100644 --- a/client/src/missionhandler/missionhandler.ts +++ b/client/src/missionhandler/missionhandler.ts @@ -9,6 +9,8 @@ export class MissionHandler #airbases : {[name: string]: Airbase} = {}; #theatre : string = ""; + #airbaseData : { [name: string]: object } = {}; + constructor() { @@ -32,8 +34,29 @@ export class MissionHandler } } + + if ("mission" in data) + { + if (data.mission != null && data.mission.theatre != this.#theatre) + { + this.#theatre = data.mission.theatre; + getMap().setTheatre(this.#theatre); + + getInfoPopup().setText("Map set to " + this.#theatre); + } + } + + if ("airbases" in data) { +/* + console.log( Object.values( data.airbases ).sort( ( a:any, b:any ) => { + const aVal = a.callsign.toLowerCase(); + const bVal = b.callsign.toLowerCase(); + + return aVal > bVal ? 1 : -1; + }) ); +//*/ for (let idx in data.airbases) { var airbase = data.airbases[idx] @@ -54,17 +77,6 @@ export class MissionHandler //this.#airbases[idx].setParkings(["2x big", "5x small"]); } } - - if ("mission" in data) - { - if (data.mission != null && data.mission.theatre != this.#theatre) - { - this.#theatre = data.mission.theatre; - getMap().setTheatre(this.#theatre); - - getInfoPopup().setText("Map set to " + this.#theatre); - } - } } getBullseyes()