@@ -3,28 +3,30 @@ import type { FIRFeature, SimAwareTraconFeature } from "@sr24/types/db";
33import type { ControllerDelta , ControllerLong , ControllerMerged , ControllerShort , PilotLong , VatsimData } from "@sr24/types/vatsim" ;
44import { haversineDistance } from "./utils/helpers.js" ;
55
6- let cachedMerged : ControllerMerged [ ] = [ ] ;
7- let deleted : string [ ] = [ ] ;
6+ let cached : ControllerMerged [ ] = [ ] ;
87let updated : ControllerMerged [ ] = [ ] ;
98let added : ControllerMerged [ ] = [ ] ;
109
1110export async function mapControllers ( vatsimData : VatsimData , pilotsLong : PilotLong [ ] ) : Promise < [ ControllerLong [ ] , ControllerMerged [ ] ] > {
12- const controllersLong : ControllerLong [ ] = vatsimData . controllers . map ( ( controller ) => {
13- return {
14- callsign : controller . callsign ,
15- frequency : parseFrequencyToKHz ( controller . frequency ) ,
16- facility : controller . facility ,
17- atis : controller . text_atis ,
18- connections : 0 ,
19- cid : controller . cid ,
20- name : controller . name ,
21- rating : controller . rating ,
22- server : controller . server ,
23- visual_range : controller . visual_range ,
24- logon_time : new Date ( controller . logon_time ) ,
25- timestamp : new Date ( controller . last_updated ) ,
26- } ;
27- } ) ;
11+ const controllersLong : ControllerLong [ ] = vatsimData . controllers
12+ . map ( ( controller ) => {
13+ if ( controller . facility === 0 && ! controller . callsign . includes ( "OBS" ) ) return null ;
14+ return {
15+ callsign : controller . callsign ,
16+ frequency : parseFrequencyToKHz ( controller . frequency ) ,
17+ facility : controller . facility ,
18+ atis : controller . text_atis ,
19+ connections : 0 ,
20+ cid : controller . cid ,
21+ name : controller . name ,
22+ rating : controller . rating ,
23+ server : controller . server ,
24+ visual_range : controller . visual_range ,
25+ logon_time : new Date ( controller . logon_time ) ,
26+ timestamp : new Date ( controller . last_updated ) ,
27+ } ;
28+ } )
29+ . filter ( ( c ) => c !== null ) ;
2830
2931 getConnectionsCount ( vatsimData , controllersLong , pilotsLong ) ;
3032
@@ -46,19 +48,63 @@ export async function mapControllers(vatsimData: VatsimData, pilotsLong: PilotLo
4648 } ) ;
4749
4850 const merged = await mergeControllers ( controllersLong ) ;
51+ setControllerDelta ( merged ) ;
4952
50- const deletedMerged = cachedMerged . filter ( ( a ) => ! merged . some ( ( b ) => b . id === a . id ) ) ;
51- deleted = deletedMerged . map ( ( c ) => c . id ) ;
52- added = merged . filter ( ( a ) => ! cachedMerged . some ( ( b ) => b . id === a . id ) ) ;
53- updated = merged . filter ( ( a ) => cachedMerged . some ( ( b ) => b . id === a . id ) ) ;
54-
55- cachedMerged = merged ;
5653 return [ controllersLong , merged ] ;
5754}
5855
56+ function setControllerDelta ( merged : ControllerMerged [ ] ) : void {
57+ added = [ ] ;
58+ updated = [ ] ;
59+
60+ for ( const m of merged ) {
61+ const cachedMerged = cached . find ( ( c ) => c . id === m . id ) ;
62+ if ( ! cachedMerged ) {
63+ added . push ( m ) ;
64+ } else {
65+ const updatedControllers : ControllerShort [ ] = [ ] ;
66+
67+ for ( const controller of m . controllers ) {
68+ const cachedController = cachedMerged . controllers . find ( ( c ) => c . callsign === controller . callsign ) ;
69+ const controllerShort = getControllerShort ( controller , cachedController ) ;
70+ updatedControllers . push ( controllerShort ) ;
71+ }
72+
73+ if ( updatedControllers . length > 0 ) {
74+ updated . push ( {
75+ id : m . id ,
76+ facility : m . facility ,
77+ controllers : updatedControllers ,
78+ } ) ;
79+ }
80+ }
81+ }
82+
83+ cached = merged ;
84+ }
85+
86+ function getControllerShort ( controller : ControllerShort , cachedController ?: ControllerShort ) : ControllerShort {
87+ if ( ! cachedController ) {
88+ return {
89+ callsign : controller . callsign ,
90+ frequency : controller . frequency ,
91+ facility : controller . facility ,
92+ atis : controller . atis ,
93+ connections : controller . connections ,
94+ } ;
95+ } else {
96+ const controllerShort : ControllerShort = { callsign : controller . callsign , facility : controller . facility } ;
97+
98+ if ( controller . frequency !== cachedController . frequency ) controllerShort . frequency = controller . frequency ;
99+ if ( JSON . stringify ( controller . atis ) !== JSON . stringify ( cachedController . atis ) ) controllerShort . atis = controller . atis ;
100+ if ( controller . connections !== cachedController . connections ) controllerShort . connections = controller . connections ;
101+
102+ return controllerShort ;
103+ }
104+ }
105+
59106export function getControllerDelta ( ) : ControllerDelta {
60107 return {
61- deleted,
62108 added,
63109 updated,
64110 } ;
0 commit comments