-
Notifications
You must be signed in to change notification settings - Fork 0
/
Loop.js
158 lines (129 loc) · 4.73 KB
/
Loop.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
var allSettled = require('promise.allsettled');
var SocketSetup = require('./SocketSetup')
var DataTypes = require('./DataTypes');
var LandedPlaneCount
module.exports = async function (io, Planes, Airports) {
let promiseArray = []
for (let i = 0; i < Planes.length; i++) {
const plane = Planes[i];
if (plane.type == undefined || plane.type == "Not Live" || plane.type == "Landed" ) {
console.log("special Case")
} else if (plane.promise != undefined) {
promiseArray.push(plane.promise(plane.hexcode, plane.tailNumber, plane));
}
}
Planes.forEach(plane => {
console.log(plane.type)
});
await allSettled(promiseArray).then((results) => {
// Do something with each result from the promiseArray results array
for (let i = 0; i < results.length; i++) {
const result = results[i]
if (result.status == "rejected") {
let plane = result.reason;
if(plane.reason == "Can't"){
// Flight XML
plane = result.reason.plane
if(findPlaneIndex(Planes, plane) != false){
emitUpdate(plane,io, Planes)
}
continue;
}
if (plane.type == "openSky") {
if (plane.openSkyFails <= 3) {
plane.openSkyFails++;
} else {
plane.type = "FlightXML"
}
} else if (plane.type == "FlightXML") {
plane.posData = undefined;
plane.type = "Not Live";
}
emitUpdate(plane, io, Planes)
} else {
let plane = result.value;
if (result.value.reason == "Can't") {
if(findPlaneIndex(Planes, result.value.plane) != false){
emitUpdate(result.value.plane, io, Planes)
}
} else if (plane.type == "Landed") {
let airport = Airports.findAirport(plane);
Airports.addAirplaneToAirport(airport,plane);
io.emit("AddPlaneToAirport",{
Plane: plane,
airport: airport,
Airports: Airports
})
setTimeout(movePlaneToPlanesFromAirports, 4.32e7, plane, io, Planes, Airports)
Planes.splice(findPlaneIndex(Planes,plane), 1);
} else {
console.log(plane.posData);
if (plane.type == "openSky") {
plane.openSkyFails = 0;
}
// plane.posData = result.value;
if (plane == undefined) {
}
// if(findPlaneIndex(Planes, plane)!==false){
emitUpdate(plane, io, Planes)
}
}
}
if(Airports.list.Planes!= undefined){
console.log(Airports.list[0].Planes.length)
}
console.log("Planes Length: " + Planes.length)
})
console.log("Finished")
return Promise.resolve();
}
function findPlaneIndex(Planes, plane) {
for (let i = 0; i < Planes.length; i++) {
const element = Planes[i];
if(element.ID === plane.ID){
return i;
}
}
return false;
}
function movePlaneToPlanesFromAirports(plane, io, Planes,Airports) {
let port = Airports.findAirport(plane)
let myPlane = port.findPlane(plane, false)
port.RemovePlane(plane)
setTimeout(() => {
myPlane.type = "openSky"
}, 6000);
Planes.push(myPlane);
io.emit("AirplaneDelete", {
Plane: plane,
airport: port
})
}
function GetPlaneCounts(Planes) {
let NotLiveCountPlanes =0
let OpenSkyCountPlanes =0
// let Landed = 0;
let flightXMLCountPlanes =0;
Planes.forEach(plane => {
if(plane.type =="openSky"){
OpenSkyCountPlanes++
}if(plane.type =="FlightXML"){
flightXMLCountPlanes++
}if(plane.type =="Not Live"){
NotLiveCountPlanes++
}
});
return {
NotLiveCountPlanes,
OpenSkyCountPlanes,
flightXMLCountPlanes
}
}
function emitUpdate(plane,io, Planes) {
//emit update to all connected clients with Socket.Js
io.emit("update", {
plane: plane,
count: GetPlaneCounts(Planes),
FlightXMLCount: DataTypes.flightXMLCount
})
}