From 6c3f2c9d2246af64eecbba923c73d01bd48d64ac Mon Sep 17 00:00:00 2001 From: dpassoni Date: Tue, 7 Mar 2023 12:59:24 +0100 Subject: [PATCH] Added demo data generator --- client/app.js | 12 +++++ client/demo.js | 93 ++++++++++++++++++++++++++++++++ client/src/server/server.ts | 66 ++--------------------- client/src/units/unitsmanager.ts | 3 +- 4 files changed, 110 insertions(+), 64 deletions(-) create mode 100644 client/demo.js diff --git a/client/app.js b/client/app.js index ca21f2bf..210711e8 100644 --- a/client/app.js +++ b/client/app.js @@ -22,3 +22,15 @@ app.use('/uikit', uikitRouter); app.set('view engine', 'ejs'); module.exports = app; + +const DemoDataGenerator = require('./demo.js'); + +var demoDataGenerator = new DemoDataGenerator(10); + +app.get('/demo/units/refresh', (req, res) => demoDataGenerator.unitsRefresh(req, res)); +app.get('/demo/units/update', (req, res) => demoDataGenerator.unitsUpdate(req, res)); +app.get('/demo/logs', (req, res) => demoDataGenerator.logs(req, res)); +app.get('/demo/bullseyes', (req, res) => demoDataGenerator.bullseyes(req, res)); +app.get('/demo/airbases', (req, res) => demoDataGenerator.airbases(req, res)); + + diff --git a/client/demo.js b/client/demo.js new file mode 100644 index 00000000..ae0cbd04 --- /dev/null +++ b/client/demo.js @@ -0,0 +1,93 @@ + +const DEMO_UNIT_DATA = { + AI: true, + name: "F-5E", + unitName: "Olympus 1-1", + groupName: "Group 1", + alive: true, + category: "Aircraft", + flightData: { + latitude: 37.2, + longitude: -115.8, + altitude: 2000, + heading: 0.5, + speed: 300 + }, + missionData: { + fuel: 0.5, + flags: {human: false}, + ammo: [], + targets: [], + hasTask: true, + coalition: "blue" + }, + formationData: { + formation: "Echelon", + isLeader: false, + isWingman: false, + leaderID: null, + wingmen: [], + wingmenIDs: [] + }, + taskData: { + currentTask: "Example task", + activePath: undefined, + targetSpeed: 400, + targetAltitude: 3000 + }, + optionsData: { + ROE: "None", + reactionToThreat: "None", + } +} + +class DemoDataGenerator { + constructor(unitsNumber) + { + this.demoUnits = this.generateRandomUnitsDemoData(unitsNumber); + } + + unitsRefresh(req, res) { + var ret = this.demoUnits; + res.send(JSON.stringify(ret)); + } + + unitsUpdate(req, res){ + Object.keys(this.demoUnits.units).forEach((ID) => { + this.demoUnits.units[ID].flightData.heading += 0.05; + this.demoUnits.units[ID].flightData.latitude += 0.001 * Math.cos(this.demoUnits.units[ID].flightData.heading); + this.demoUnits.units[ID].flightData.longitude += 0.001 * Math.sin(this.demoUnits.units[ID].flightData.heading); + }); + var ret = this.demoUnits; + res.send(JSON.stringify(ret)); + }; + + logs(req, res){ + var ret = {logs: {}}; + res.send(JSON.stringify(ret)); + }; + + airbases(req, res){ + var ret = {airbases: {}}; + res.send(JSON.stringify(ret)); + }; + + bullseyes(req, res){ + var ret = {bullseyes: {}}; + res.send(JSON.stringify(ret)); + }; + + generateRandomUnitsDemoData(unitsNumber) + { + var units = {}; + for (let i = 0; i < unitsNumber; i++) + { + units[String(i)] = JSON.parse(JSON.stringify(DEMO_UNIT_DATA)); + units[String(i)].flightData.latitude += (Math.random() - 0.5) * 0.3; + units[String(i)].flightData.longitude += (Math.random() - 0.5) * 0.3; + } + return {"units": units}; + } +} + +module.exports = DemoDataGenerator; \ No newline at end of file diff --git a/client/src/server/server.ts b/client/src/server/server.ts index 1456c127..7a257021 100644 --- a/client/src/server/server.ts +++ b/client/src/server/server.ts @@ -1,16 +1,14 @@ import * as L from 'leaflet' import { setConnected } from '..'; -const DEMO = false; - /* Edit here to change server address */ -const REST_ADDRESS = "http://localhost:30000/olympus"; +const REST_ADDRESS = "http://localhost:3000/demo"; const UNITS_URI = "units"; const REFRESH_URI = "refresh"; const UPDATE_URI = "update"; const LOGS_URI = "logs"; const AIRBASES_URI = "airbases"; -const BULLSEYE_URI = "bullseye"; +const BULLSEYE_URI = "bullseyes"; export function GET(callback: CallableFunction, uri: string){ var xmlHttp = new XMLHttpRequest(); @@ -50,10 +48,7 @@ export function getLogs(callback: CallableFunction) { } export function getUnits(callback: CallableFunction, refresh: boolean = false) { - if (!DEMO) - GET(callback, `${UNITS_URI}/${refresh? REFRESH_URI: UPDATE_URI}`); - else - callback(refresh? generateRandomUnitsDemoData(100): {units:{}}); + GET(callback, `${UNITS_URI}/${refresh? REFRESH_URI: UPDATE_URI}`); } export function addDestination(ID: number, path: any) { @@ -145,58 +140,3 @@ export function setReactionToThreat(ID: number, reactionToThreat: string) { var data = {"setReactionToThreat": command} POST(data, () => { }); } - - -function generateRandomUnitsDemoData(unitsNumber: number) -{ - var units: any = {}; - for (let i = 0; i < unitsNumber; i++) - { - units[String(i)] = structuredClone(DEMO_UNIT_DATA); - units[String(i)].flightData.latitude += (Math.random() - 0.5) * 0.3; - units[String(i)].flightData.longitude += (Math.random() - 0.5) * 0.3; - } - return {"units": units}; -} - -const DEMO_UNIT_DATA = { - AI: true, - name: "F-5E", - unitName: "Olympus 1-1", - groupName: "Group 1", - alive: true, - category: "Aircraft", - flightData: { - latitude: 37.2, - longitude: -115.8, - altitude: 2000, - heading: 0.5, - speed: 300 - }, - missionData: { - fuel: 0.5, - flags: {human: false}, - ammo: [], - targets: [], - hasTask: true, - coalition: "blue" - }, - formationData: { - formation: "Echelon", - isLeader: false, - isWingman: false, - leaderID: null, - wingmen: [], - wingmenIDs: [] - }, - taskData: { - currentTask: "Example task", - activePath: undefined, - targetSpeed: 400, - targetAltitude: 3000 - }, - optionsData: { - ROE: "None", - reactionToThreat: "None", - } -} \ No newline at end of file diff --git a/client/src/units/unitsmanager.ts b/client/src/units/unitsmanager.ts index 62004856..3ddd6af5 100644 --- a/client/src/units/unitsmanager.ts +++ b/client/src/units/unitsmanager.ts @@ -48,7 +48,8 @@ export class UnitsManager { .filter((ID: string) => !(ID in this.#units)) .reduce((timeout: number, ID: string) => { setTimeout(() => { - this.addUnit(parseInt(ID), data.units[ID]); + if (!(ID in this.#units)) + this.addUnit(parseInt(ID), data.units[ID]); this.#units[parseInt(ID)]?.setData(data.units[ID]); }, timeout); return timeout + 10;