11import { createHash } from "node:crypto" ;
2- import { rdsGetMultiple , rdsGetSingle } from "@sr24/db/redis" ;
2+ import { rdsGetMultiple , rdsGetSingle , rdsSetMultiple } from "@sr24/db/redis" ;
33import type { StaticAirport } from "@sr24/types/db" ;
44import type { PilotDelta , PilotFlightPlan , PilotLong , PilotShort , PilotTimes } from "@sr24/types/interface" ;
55import type { VatsimData , VatsimPilot , VatsimPilotFlightPlan , VatsimPrefile } from "@sr24/types/vatsim" ;
@@ -108,21 +108,27 @@ export async function mapPilots(vatsimData: VatsimData): Promise<PilotLong[]> {
108108 if ( cachedPilot ) {
109109 pilotLong = { ...cachedPilot , ...updatedFields } ;
110110 } else {
111- pilotLong = {
112- id : id ,
113- cid : String ( pilot . cid ) ,
114- callsign : pilot . callsign ,
115- aircraft : pilot . flight_plan ?. aircraft_short || "A320" ,
116- name : pilot . name ,
117- server : pilot . server ,
118- pilot_rating : PILOT_RATINGS . find ( ( r ) => r . id === pilot . pilot_rating ) ?. short_name || "NEW" ,
119- military_rating : MILITARY_RATINGS . find ( ( r ) => r . id === pilot . military_rating ) ?. short_name || "M0" ,
120- flight_plan : await mapPilotFlightPlan ( pilot . flight_plan ) ,
121- logon_time : new Date ( pilot . logon_time ) ,
122- times : null ,
123- live : "live" ,
124- ...updatedFields ,
125- } ;
111+ const existing = ( await rdsGetSingle ( `pilot:${ id } ` ) ) as PilotLong | undefined ;
112+
113+ if ( existing ) {
114+ pilotLong = { ...existing , ...updatedFields } ;
115+ } else {
116+ pilotLong = {
117+ id : id ,
118+ cid : String ( pilot . cid ) ,
119+ callsign : pilot . callsign ,
120+ aircraft : pilot . flight_plan ?. aircraft_short || "A320" ,
121+ name : pilot . name ,
122+ server : pilot . server ,
123+ pilot_rating : PILOT_RATINGS . find ( ( r ) => r . id === pilot . pilot_rating ) ?. short_name || "NEW" ,
124+ military_rating : MILITARY_RATINGS . find ( ( r ) => r . id === pilot . military_rating ) ?. short_name || "M0" ,
125+ flight_plan : await mapPilotFlightPlan ( pilot . flight_plan ) ,
126+ logon_time : new Date ( pilot . logon_time ) ,
127+ times : null ,
128+ live : "live" ,
129+ ...updatedFields ,
130+ } ;
131+ }
126132 }
127133
128134 pilotLong . vertical_speed = calculateVerticalSpeed ( pilotLong , cachedPilot ) ;
@@ -194,6 +200,8 @@ export async function mapPilots(vatsimData: VatsimData): Promise<PilotLong[]> {
194200 }
195201 }
196202
203+ await rdsSetMultiple ( newPilotsLong , "pilot" , ( p ) => p . id , 12 * 60 * 60 ) ;
204+
197205 cached = newCached ;
198206 return newPilotsLong ;
199207}
@@ -208,7 +216,7 @@ function getPilotId(pilot: VatsimPilot | VatsimPrefile): string {
208216 . update ( `${ base } ${ plan ? `_${ variable } ` : "" } ` )
209217 . digest ( ) ;
210218 const b64url = digest . toString ( "base64" ) . replace ( / \+ / g, "-" ) . replace ( / \/ / g, "_" ) . replace ( / = + $ / , "" ) ;
211- return b64url . slice ( 0 , 10 ) ;
219+ return b64url . slice ( 0 , 16 ) ;
212220}
213221
214222export function getPilotDelta ( ) : PilotDelta {
@@ -353,7 +361,7 @@ function mapPilotTimes(
353361 touch_down : new Date ( sched_off_block . getTime ( ) + TAXI_TIME_MS + enrouteTimeMs ) ,
354362 sched_on_block : roundDateTo5Min ( sched_on_block ) ,
355363 on_block : sched_on_block ,
356- state : estimateInitState ( current ) ,
364+ state : prefile ? "Boarding" : estimateInitState ( current ) ,
357365 stop_counter : 0 ,
358366 } ;
359367 }
0 commit comments