瀏覽代碼

Update index.html into a working version completing the PoC arc

Sacha Ligthert 2 年之前
父節點
當前提交
ac13371e40
共有 1 個文件被更改,包括 126 次插入64 次删除
  1. 126 64
      index.html

+ 126 - 64
index.html

@@ -2,7 +2,7 @@
 <html>
   <head>
     <meta charset="utf-8" />
-    <title>Honestly stolen from BlackSharkDen</title>
+    <title>DCSW Server Map</title>
     <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
     <script src="https://unpkg.com/maplibre-gl@2.4.0/dist/maplibre-gl.js"></script>
     <link href="https://unpkg.com/maplibre-gl@2.4.0/dist/maplibre-gl.css" rel="stylesheet" />
@@ -28,59 +28,132 @@
     <div id="map"></div>
     <pre id="info"></pre>
     <script>
-      // const sleep = ms => new Promise(r => setTimeout(r, ms));
-      var api_key = "rYNCGwdgATyHCjyDil7t";
-      var serverURL = "http://homeplate.close-air.support:8888/";
-      var map = new maplibregl.Map({
-        center: [42, 42],
-        container: "map",
-        style: "https://api.maptiler.com/maps/topo-v2/style.json?key="+api_key,
-        zoom: 7
-      });
+// Variables
+const api_key = "rYNCGwdgATyHCjyDil7t";
+const serverURL = "http://homeplate.close-air.support:8888/";
+var latest = [];
+var oldest= [];
+var Units;
+
+// Deploy stuff
+var map = new maplibregl.Map({
+  center: [42, 42],
+  container: "map",
+  style: "https://api.maptiler.com/maps/topo-v2/style.json?key="+api_key,
+  zoom: 7
+});
+
+// Update, you know...the things.
+map.on("load", () => { 
+  window.setInterval( () => {
 
-      map.on("load", () => { 
-
-        myData = fetchData(serverURL);
-        for (chip in myData)
-        {
-          //latest.push(myData[chip].UnitName);
-          let UnitImage = myData[chip].UnitName+"-img";
-          let UnitSource = myData[chip].UnitName+"-src";
-          let UnitPoint = myData[chip].UnitName+"-point";
-          let UnitLong = myData[chip].Longitude;
-          let UnitLati = myData[chip].Latitude;
-          let UnitCoalition = myData[chip].Coalition;
-
-          map.loadImage(
-            "/symbols/"+UnitCoalition+"/plane.png",
-            (error, image) => {
-              if (error) throw error;
-              map.addImage(UnitImage, image);
-              map.addSource(UnitSource, { "type": 'geojson', 'data': { 'type': 'FeatureCollection', 'features': [{ 'type': 'Feature', 'geometry': { 'type': 'Point', 'coordinates': [UnitLong, UnitLati] } }] } } );
-              map.addLayer({
-                'id': UnitPoint,
-                'type': 'symbol',
-                'source': UnitSource,
-                'layout': {
-                  'icon-image': UnitImage,
-                  'icon-size': 0.25
-                }
-              }); // map.addLayer()
-            }
-          ); // map.loadImage()
-
-        };// End for() loop
-
-      }); // End map.on()
-
-
-    // function wait(ms){
-    //   var start = new Date().getTime();
-    //   var end = start;
-    //   while(end < start + ms) {
-    //     end = new Date().getTime();
-    //   };
-    // };
+    // Step 1 -- Update contents
+    fetchContent();
+
+    // Step 2 -- Add all the units added
+    units_added = unitsAdded();
+    if (units_added.length != 0) {
+      // units_added
+      // addElements(units_added);
+      for (i in units_added) {
+        unit = fetchUnit(units_added[i])
+        let UnitImage = unit.UnitName+"-img";
+        let UnitSource = unit.UnitName+"-src";
+        let UnitPoint = unit.UnitName+"-point";
+        let UnitLong = unit.Longitude;
+        let UnitLati = unit.Latitude;
+        let UnitCoalition = unit.Coalition;
+        let UnitType = unit.Type;
+
+        map.loadImage(
+          "/symbols/"+UnitCoalition+"/"+UnitType+".png",
+          (error, image) => {
+            if (error) throw error;
+            map.addImage(UnitImage, image);
+            map.addSource(UnitSource, { "type": 'geojson', 'data': { 'type': 'FeatureCollection', 'features': [{ 'type': 'Feature', 'geometry': { 'type': 'Point', 'coordinates': [UnitLong, UnitLati] } }] } } );
+            map.addLayer({ 'id': UnitPoint, 'type': 'symbol', 'source': UnitSource, 'layout': { 'icon-image': UnitImage, 'icon-size': 0.25 } }); // map.addLayer()
+          }
+        ); // map.loadImage()
+      };
+    };
+
+    // Step 3 -- Remove all the units removed
+    units_rmved = unitsRemoved();
+    if (units_rmved != 0) {
+      for (i in units_rmved) {
+        unit = units_rmved[i];
+        let UnitImage = unit+"-img";
+        let UnitSource = unit+"-src";
+        let UnitPoint = unit+"-point";
+
+        if (map.getLayer(UnitPoint)) map.removeLayer(UnitPoint);
+        map.removeSource(UnitSource);
+        if (map.hasImage(UnitImage)) map.removeImage(UnitImage);
+      };
+    };
+
+    // Step 4 -- Update all the Units positions
+    for (i in Units) {
+      let UnitSource = Units[i].UnitName+"-src";
+      let UnitLong = Units[i].Longitude;
+      let UnitLati = Units[i].Latitude;
+
+      const json = {
+          type: 'Feature',
+          geometry: {
+              'type': 'Point',
+              'coordinates': [UnitLong, UnitLati]
+          }
+      };
+      map.getSource(UnitSource).setData(json);
+    };
+
+  }, 5000);
+});
+
+// Functions
+function fetchUnit(unitName) {
+  for (i in Units) {
+    if (unitName == Units[i].UnitName) {
+      return Units[i];
+    };
+  };
+};
+
+function fetchContent() {
+  oldest = latest;
+  Units = fetchData(serverURL);
+  latest = convertJSON(Units);
+};
+
+function convertJSON(jsonFile) {
+  var myArray = [];
+  for (index in jsonFile) { myArray.push(jsonFile[index].UnitName); };
+  return myArray;
+};
+
+function unitsRemoved() {
+  var myArray = [];
+  myArray = oldest.filter(x => !latest.includes(x));
+  return myArray;
+};
+
+function unitsAdded() {
+  var myArray = [];
+  myArray = latest.filter(x => !oldest.includes(x));
+  return myArray;
+};
+
+function fetchData(url)
+{
+  var xhr = new XMLHttpRequest();    
+  var retval
+  
+  xhr.open("GET", url, false);
+  xhr.send();
+  
+  return JSON.parse(xhr.responseText)
+};
 
       function MGRSString (Lat, Long) { 
         //if (Lat < -80) return 'Too far South' ; if (Lat > 84) return 'Too far North' ;
@@ -122,17 +195,6 @@
         var coordinate = e.lngLat.wrap();
         document.getElementById('info').innerHTML = MGRSString(coordinate.lat, coordinate.lng);
       });
-
-      function fetchData(url)
-      {
-          var xhr = new XMLHttpRequest();    
-          var retval
-          
-          xhr.open("GET", url, false);
-          xhr.send();
-          
-          return JSON.parse(xhr.responseText)
-      }
       
     </script>
   </body>