-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflight.ts
295 lines (253 loc) · 8.95 KB
/
flight.ts
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
import { SettingsComponent } from '../components/control-tower/settings/settings.component';
import { ReplyCode, Transmission } from '../models/transmission';
import { Animation, IAnimationStory } from '../shared/animation';
import { CONTROL_TOWER, LEGS } from '../shared/constants';
import { INotifyPropertyChanged, PropertyChangedEventArgs } from '../shared/events';
import Utils from '../shared/utils';
import { FlightStatus, IArrivingFlight, IDepartingFlight, IFlight } from './IFlight';
import { Leg } from './Leg';
export abstract class Flight implements IFlight, INotifyPropertyChanged {
private readonly _propertyChanged = new Subject<PropertyChangedEventArgs>();
private _isPropertyChangedEnabled: boolean = true;
private readonly _animation: Animation = new Animation();
private _animationTimeline: anime.AnimeTimelineInstance;
private _isPaused: boolean = false;
get propertyChanged(): Observable<PropertyChangedEventArgs> {
return this._propertyChanged.asObservable();
}
constructor(
public airline: string,
public flightCode: string,
status: FlightStatus = FlightStatus.OnTime,
isEmergency: boolean = false,
legId: number = null,
createdAt: Date = new Date(),
updatedAt: Date = new Date()
) {
this._status = status;
this._isEmergency = isEmergency;
this._legId = legId;
this._createdAt = createdAt;
this._updatedAt = updatedAt;
}
//#region Properties
private _status: FlightStatus;
get status(): FlightStatus {
return this._status;
}
set status(value: FlightStatus) {
if (this._status !== value) {
// let oldValue = this._status;
this._status = value;
this.onPropertyChanged('status'/* , oldValue */);
}
}
private _isEmergency: boolean;
get isEmergency(): boolean {
return this._isEmergency;
}
set isEmergency(value: boolean) {
if (this._isEmergency !== value) {
// let oldValue = this._isEmergency;
this._isEmergency = value;
this.onPropertyChanged('isEmergency'/* , oldValue */);
}
}
private _legId: number;
get legId(): number {
return this._legId;
}
set legId(value: number) {
if (this._legId !== value) {
let oldValue = this._legId;
this._legId = value;
this.onPropertyChanged('legId', oldValue);
}
}
private _createdAt: Date;
get createdAt(): Date {
return this._createdAt;
}
private _updatedAt: Date;
get updatedAt(): Date {
return this._updatedAt;
}
//#endregion Properties
// 1. Moves to input leg and sets legId upon completion
// 2. Returns confirmation
async moveTo(leg: Leg, animationStory: IAnimationStory): Promise<Transmission> {
this.setStoryElements(animationStory);
if (leg.isClosed)
setTimeout(() => this.pause(), animationStory.duration / 2);
this._animationTimeline = this._animation.animate(animationStory, !this._isPaused);
// Promise.all([this._animationTimeline.finished, Utils.delay(animationStory.duration)])
this._animationTimeline.finished
.then(() => this.legId = leg._id)
.catch(err => console.error(err));
await Utils.delay(Math.min(SettingsComponent.radioResponseTime, animationStory.duration / 2));
return Transmission.createConfirmation(this.flightCode, CONTROL_TOWER);
}
// Moves outside of legs area
// Returns end response
async endInteraction(animationStory: IAnimationStory): Promise<Transmission> {
this.setStoryElements(animationStory);
this._animationTimeline = this._animation.animate(animationStory, !this._isPaused);
// Promise.all([this._animationTimeline.finished, Utils.delay(animationStory.duration)])
this._animationTimeline.finished
.then(() => {
this.status = this.legId === LEGS.Exit ? FlightStatus.Departed : FlightStatus.Landed;
this.legId = null;
})
.catch(err => console.error(err));
await Utils.delay(Math.min(SettingsComponent.radioResponseTime, animationStory.duration / 2));
let response = this.legId === LEGS.Exit ? 'Thank you, Goodbye :)' : 'Thank you! :)';
return new Transmission(this.flightCode, CONTROL_TOWER, response, ReplyCode.Out);
}
pause(): void {
if (!this._animationTimeline.completed) {
this._isPaused = true;
this._animationTimeline.pause();
}
}
continue(): void {
this._isPaused = false;
// if (!this._animationTimeline.completed)
this._animationTimeline.play();
}
// Returns true if flight has greater priority than other flight, otherwise returns false
compareByPriority(other: Flight): boolean {
if (!other)
return true;
if (this.isEmergency && !other.isEmergency)
return true;
if (!this.isEmergency && other.isEmergency)
return false;
// if (this.updatedAt.valueOf() < other.updatedAt.valueOf())
if (this.createdAt.valueOf() < other.createdAt.valueOf())
return true;
// if (this.updatedAt.valueOf() === other.updatedAt.valueOf()) {
if (this.createdAt.valueOf() === other.createdAt.valueOf()) {
if (this._legId === LEGS.Landing) return true;
if (other._legId === LEGS.Landing) return false;
if (Flight.isDepartingFlight(this)) return true;
if (Flight.isDepartingFlight(other)) return false;
return true;
}
return false;
}
getObject(): IFlight {
return {
airline: this.airline,
flightCode: this.flightCode,
status: this._status,
isEmergency: this._isEmergency,
legId: this._legId,
createdAt: this._createdAt,
updatedAt: this._updatedAt
};
}
disablePropertyChanged(): void {
this._isPropertyChangedEnabled = false;
}
enablePropertyChanged(): void {
this._isPropertyChangedEnabled = true;
}
private onPropertyChanged(propertyName: string, oldValue?: any): void {
this._updatedAt = new Date();
// if (this._isPropertyChangedEnabled)
this._propertyChanged.next(new PropertyChangedEventArgs(propertyName, oldValue));
}
private setStoryElements(animationStory: IAnimationStory): void {
this.setFlightElement(animationStory.startElement);
animationStory.frames.forEach(frame => {
if (frame.element)
this.setFlightElement(frame.element);
});
}
private setFlightElement(element: HTMLElement): void {
let pathId = element.id.replace('flight', '');
element.querySelector(`#flightCode${pathId}`).innerHTML = this.flightCode;
element.querySelector(`#airline${pathId}`).innerHTML = this.airline;
element.querySelector(`#location${pathId}`).innerHTML = Flight.isArrivingFlight(this) ?
this.comingFrom : Flight.isDepartingFlight(this) ? this.departingTo : '';
$(element).find(`#warning${pathId}`)[0].style.display = this.isEmergency ? '' : 'none';
}
//#region static utilities
static isValidFlight(obj: any): obj is IArrivingFlight | IDepartingFlight {
return (obj && obj.airline && obj.flightCode)
&& (Flight.isArrivingFlight(obj as IFlight) || Flight.isDepartingFlight(obj as IFlight));
}
static isArrivingFlight(flight: IFlight): flight is IArrivingFlight {
return (<IArrivingFlight>flight).comingFrom !== undefined;
}
static isDepartingFlight(flight: IFlight): flight is IDepartingFlight {
return (<IDepartingFlight>flight).departingTo !== undefined;
}
static createNew(flight: IFlight): Flight {
if (Flight.isArrivingFlight(flight))
return ArrivingFlight.createNew(flight as IArrivingFlight);
else
return DepartingFlight.createNew(flight as IDepartingFlight);
}
//#endregion static utilities
}
export class ArrivingFlight extends Flight implements IArrivingFlight {
constructor(
airline: string,
flightCode: string,
public comingFrom: string,
status: FlightStatus = FlightStatus.OnTime,
isEmergency: boolean = false,
legId: number = null,
createdAt: Date = new Date(),
updatedAt: Date = new Date()
) {
super(airline, flightCode, status, isEmergency, legId, createdAt, updatedAt);
}
static createNew(obj: IArrivingFlight): ArrivingFlight {
return new ArrivingFlight(
obj.airline,
obj.flightCode,
obj.comingFrom,
obj.status,
obj.isEmergency,
obj.legId,
obj.createdAt,
obj.updatedAt
);
}
getObject(): IArrivingFlight {
return Object.assign(super.getObject(), { comingFrom: this.comingFrom });
}
}
export class DepartingFlight extends Flight implements IDepartingFlight {
constructor(
airline: string,
flightCode: string,
public departingTo: string,
status: FlightStatus = FlightStatus.OnTime,
isEmergency: boolean = false,
legId: number = null,
createdAt: Date = new Date(),
updatedAt: Date = new Date()
) {
super(airline, flightCode, status, isEmergency, legId, createdAt, updatedAt);
}
static createNew(obj: IDepartingFlight): DepartingFlight {
return new DepartingFlight(
obj.airline,
obj.flightCode,
obj.departingTo,
obj.status,
obj.isEmergency,
obj.legId,
obj.createdAt,
obj.updatedAt
);
}
getObject(): IDepartingFlight {
return Object.assign(super.getObject(), { departingTo: this.departingTo });
}
}