forked from hpi-sam/digital-fuesim-manv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvehicle.ts
611 lines (546 loc) · 21.4 KB
/
vehicle.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
import { Type } from 'class-transformer';
import { IsArray, IsString, IsUUID, ValidateNested } from 'class-validator';
import { Material } from '../../models/material';
import {
changeOccupation,
currentCoordinatesOf,
currentSimulatedRegionIdOf,
currentSimulatedRegionOf,
ExerciseOccupation,
isInSimulatedRegion,
isInSpecificSimulatedRegion,
isInTransfer,
isInVehicle,
isOnMap,
MapCoordinates,
MapPosition,
occupationTypeOptions,
SimulatedRegionPosition,
VehiclePosition,
} from '../../models/utils';
import {
changePosition,
changePositionWithId,
} from '../../models/utils/position/position-helpers-mutable';
import type { ExerciseState } from '../../state';
import { imageSizeToPosition } from '../../state-helpers/image-size-to-position';
import type { Mutable } from '../../utils';
import {
cloneDeepMutable,
StrictObject,
UUID,
uuidValidationOptions,
} from '../../utils';
import { IsLiteralUnion, IsValue } from '../../utils/validators';
import type { Action, ActionReducer } from '../action-reducer';
import { ReducerError } from '../reducer-error';
import { sendSimulationEvent } from '../../simulation/events/utils';
import {
MaterialAvailableEvent,
MaterialRemovedEvent,
NewPatientEvent,
PersonnelAvailableEvent,
PersonnelRemovedEvent,
VehicleRemovedEvent,
} from '../../simulation/events';
import { Vehicle } from '../../models/vehicle';
import { Personnel } from '../../models/personnel';
import { deletePatient } from './patient';
import { completelyLoadVehicle as completelyLoadVehicleHelper } from './utils/completely-load-vehicle';
import { getElement } from './utils/get-element';
import { removeElementPosition } from './utils/spatial-elements';
import { logVehicleAdded, logVehicleRemoved } from './utils/log';
/**
* Performs all necessary actions to remove a vehicle from the state.
* This includes removing the material, personnel and (if there are some) patients and sending removed events for those elements if the vehicle is in a simulated region.
* @param vehicleId The ID of the vehicle to be deleted
*/
export function deleteVehicle(
draftState: Mutable<ExerciseState>,
vehicleId: UUID
) {
logVehicleRemoved(draftState, vehicleId);
const vehicle = getElement(draftState, 'vehicle', vehicleId);
// Delete related material and personnel
Object.keys(vehicle.materialIds).forEach((materialId) => {
const material = getElement(draftState, 'material', materialId);
if (isInSimulatedRegion(material)) {
const simulatedRegion = currentSimulatedRegionOf(
draftState,
material
);
sendSimulationEvent(
simulatedRegion,
MaterialRemovedEvent.create(materialId)
);
}
removeElementPosition(draftState, 'material', materialId);
delete draftState.materials[materialId];
});
Object.keys(vehicle.personnelIds).forEach((personnelId) => {
const personnel = getElement(draftState, 'personnel', personnelId);
if (isInSimulatedRegion(personnel)) {
const simulatedRegion = currentSimulatedRegionOf(
draftState,
personnel
);
sendSimulationEvent(
simulatedRegion,
PersonnelRemovedEvent.create(personnelId)
);
}
removeElementPosition(draftState, 'personnel', personnelId);
delete draftState.personnel[personnelId];
});
Object.keys(vehicle.patientIds).forEach((patientId) => {
// The PatientRemovedEvent will be sent by the function below, so we don't have to do it here
deletePatient(draftState, patientId);
});
if (isInSimulatedRegion(vehicle)) {
const simulatedRegion = currentSimulatedRegionOf(draftState, vehicle);
sendSimulationEvent(
simulatedRegion,
VehicleRemovedEvent.create(vehicleId)
);
}
// Delete the vehicle
delete draftState.vehicles[vehicleId];
}
export class AddVehicleAction implements Action {
@IsValue('[Vehicle] Add vehicle' as const)
public readonly type = '[Vehicle] Add vehicle';
@ValidateNested()
@Type(() => Vehicle)
public readonly vehicle!: Vehicle;
@IsArray()
@ValidateNested()
@Type(() => Material)
public readonly materials!: readonly Material[];
@IsArray()
@ValidateNested()
@Type(() => Personnel)
public readonly personnel!: readonly Personnel[];
}
export class RenameVehicleAction implements Action {
@IsValue('[Vehicle] Rename vehicle' as const)
public readonly type = '[Vehicle] Rename vehicle';
@IsUUID(4, uuidValidationOptions)
public readonly vehicleId!: UUID;
@IsString()
public readonly name!: string;
}
export class MoveVehicleAction implements Action {
@IsValue('[Vehicle] Move vehicle' as const)
public readonly type = '[Vehicle] Move vehicle';
@IsUUID(4, uuidValidationOptions)
public readonly vehicleId!: UUID;
@ValidateNested()
@Type(() => MapCoordinates)
public readonly targetPosition!: MapCoordinates;
}
export class RemoveVehicleAction implements Action {
@IsValue('[Vehicle] Remove vehicle' as const)
public readonly type = '[Vehicle] Remove vehicle';
@IsUUID(4, uuidValidationOptions)
public readonly vehicleId!: UUID;
}
export class UnloadVehicleAction implements Action {
@IsValue('[Vehicle] Unload vehicle' as const)
public readonly type = '[Vehicle] Unload vehicle';
@IsUUID(4, uuidValidationOptions)
public readonly vehicleId!: UUID;
}
export class LoadVehicleAction implements Action {
@IsValue('[Vehicle] Load vehicle' as const)
public readonly type = '[Vehicle] Load vehicle';
@IsUUID(4, uuidValidationOptions)
public readonly vehicleId!: UUID;
@IsLiteralUnion({
material: true,
patient: true,
personnel: true,
})
public readonly elementToBeLoadedType!:
| 'material'
| 'patient'
| 'personnel';
@IsUUID(4, uuidValidationOptions)
public readonly elementToBeLoadedId!: UUID;
}
export class CompletelyLoadVehicleAction implements Action {
@IsValue('[Vehicle] Completely load vehicle' as const)
public readonly type = '[Vehicle] Completely load vehicle';
@IsUUID(4, uuidValidationOptions)
public readonly vehicleId!: UUID;
}
export class RemoveVehicleFromSimulatedRegionAction implements Action {
@IsValue('[Vehicle] Remove from simulated region' as const)
public readonly type = '[Vehicle] Remove from simulated region';
@IsUUID(4, uuidValidationOptions)
public readonly vehicleId!: UUID;
@IsUUID(4, uuidValidationOptions)
public readonly simulatedRegionId!: UUID;
}
export class SetVehicleOccupationAction implements Action {
@IsValue('[Vehicle] Set occupation' as const)
public readonly type = '[Vehicle] Set occupation';
@IsUUID(4, uuidValidationOptions)
public readonly vehicleId!: UUID;
@Type(...occupationTypeOptions)
@ValidateNested()
public readonly occupation!: ExerciseOccupation;
}
export namespace VehicleActionReducers {
export const addVehicle: ActionReducer<AddVehicleAction> = {
action: AddVehicleAction,
reducer: (draftState, { vehicle, materials, personnel }) => {
if (
materials.some(
(material) =>
material.vehicleId !== vehicle.id ||
vehicle.materialIds[material.id] === undefined
) ||
StrictObject.keys(vehicle.materialIds).length !==
materials.length
) {
throw new ReducerError(
'Vehicle material ids do not match material ids'
);
}
if (
personnel.some(
(currentPersonnel) =>
currentPersonnel.vehicleId !== vehicle.id ||
vehicle.personnelIds[currentPersonnel.id] === undefined
) ||
StrictObject.keys(vehicle.personnelIds).length !==
personnel.length
) {
throw new ReducerError(
'Vehicle personnel ids do not match personnel ids'
);
}
draftState.vehicles[vehicle.id] = cloneDeepMutable(vehicle);
for (const material of cloneDeepMutable(materials)) {
changePosition(
material,
VehiclePosition.create(vehicle.id),
draftState
);
draftState.materials[material.id] = material;
}
for (const person of cloneDeepMutable(personnel)) {
changePosition(
person,
VehiclePosition.create(vehicle.id),
draftState
);
draftState.personnel[person.id] = person;
}
logVehicleAdded(draftState, vehicle.id);
return draftState;
},
rights: 'trainer',
};
export const moveVehicle: ActionReducer<MoveVehicleAction> = {
action: MoveVehicleAction,
reducer: (draftState, { vehicleId, targetPosition }) => {
changePositionWithId(
vehicleId,
MapPosition.create(targetPosition),
'vehicle',
draftState
);
return draftState;
},
rights: 'participant',
};
export const renameVehicle: ActionReducer<RenameVehicleAction> = {
action: RenameVehicleAction,
reducer: (draftState, { vehicleId, name }) => {
const vehicle = getElement(draftState, 'vehicle', vehicleId);
vehicle.name = name;
for (const personnelId of Object.keys(vehicle.personnelIds)) {
draftState.personnel[personnelId]!.vehicleName = name;
}
for (const materialId of Object.keys(vehicle.materialIds)) {
draftState.materials[materialId]!.vehicleName = name;
}
return draftState;
},
rights: 'trainer',
};
export const removeVehicle: ActionReducer<RemoveVehicleAction> = {
action: RemoveVehicleAction,
reducer: (draftState, { vehicleId }) => {
deleteVehicle(draftState, vehicleId);
return draftState;
},
rights: 'trainer',
};
export const unloadVehicle: ActionReducer<UnloadVehicleAction> = {
action: UnloadVehicleAction,
reducer: (draftState, { vehicleId }) => {
const vehicle = getElement(draftState, 'vehicle', vehicleId);
if (!isOnMap(vehicle) && !isInSimulatedRegion(vehicle)) {
throw new ReducerError(
`Vehicle with id ${vehicleId} is currently not on the map or in a simulated region`
);
}
const materialIds = Object.keys(vehicle.materialIds);
const personnelIds = Object.keys(vehicle.personnelIds);
const patientIds = Object.keys(vehicle.patientIds);
if (isOnMap(vehicle)) {
const unloadPosition = currentCoordinatesOf(vehicle);
const vehicleWidthInPosition = imageSizeToPosition(
vehicle.image.aspectRatio * vehicle.image.height
);
const space =
vehicleWidthInPosition /
(personnelIds.length +
materialIds.length +
patientIds.length +
1);
let x = unloadPosition.x - vehicleWidthInPosition / 2;
// Unload all patients, personnel and material and put them on the vehicle
for (const patientId of patientIds) {
x += space;
changePositionWithId(
patientId,
MapPosition.create(
MapCoordinates.create(x, unloadPosition.y)
),
'patient',
draftState
);
delete vehicle.patientIds[patientId];
}
for (const personnelId of personnelIds) {
x += space;
const personnel = getElement(
draftState,
'personnel',
personnelId
);
if (isInVehicle(personnel)) {
changePositionWithId(
personnelId,
MapPosition.create(
MapCoordinates.create(x, unloadPosition.y)
),
'personnel',
draftState
);
}
}
for (const materialId of materialIds) {
x += space;
const material = getElement(
draftState,
'material',
materialId
);
if (isInVehicle(material)) {
changePosition(
material,
MapPosition.create(
MapCoordinates.create(x, unloadPosition.y)
),
draftState
);
}
}
} else {
const simulatedRegionId = currentSimulatedRegionIdOf(vehicle);
const simulatedRegion = getElement(
draftState,
'simulatedRegion',
simulatedRegionId
);
for (const patientId of patientIds) {
changePositionWithId(
patientId,
SimulatedRegionPosition.create(simulatedRegionId),
'patient',
draftState
);
sendSimulationEvent(
simulatedRegion,
NewPatientEvent.create(patientId)
);
delete vehicle.patientIds[patientId];
}
for (const personnelId of personnelIds) {
const personnel = getElement(
draftState,
'personnel',
personnelId
);
if (isInVehicle(personnel)) {
changePositionWithId(
personnelId,
SimulatedRegionPosition.create(simulatedRegionId),
'personnel',
draftState
);
sendSimulationEvent(
simulatedRegion,
PersonnelAvailableEvent.create(personnelId)
);
}
}
for (const materialId of materialIds) {
const material = getElement(
draftState,
'material',
materialId
);
if (isInVehicle(material)) {
changePosition(
material,
SimulatedRegionPosition.create(simulatedRegionId),
draftState
);
sendSimulationEvent(
simulatedRegion,
MaterialAvailableEvent.create(materialId)
);
}
}
}
return draftState;
},
rights: 'participant',
};
export const loadVehicle: ActionReducer<LoadVehicleAction> = {
action: LoadVehicleAction,
reducer: (
draftState,
{ vehicleId, elementToBeLoadedId, elementToBeLoadedType }
) => {
const vehicle = getElement(draftState, 'vehicle', vehicleId);
switch (elementToBeLoadedType) {
case 'material': {
const material = getElement(
draftState,
'material',
elementToBeLoadedId
);
if (!vehicle.materialIds[elementToBeLoadedId]) {
throw new ReducerError(
`Material with id ${material.id} is not assignable to the vehicle with id ${vehicle.id}`
);
}
changePosition(
material,
VehiclePosition.create(vehicleId),
draftState
);
break;
}
case 'personnel': {
const personnel = getElement(
draftState,
'personnel',
elementToBeLoadedId
);
if (isInTransfer(personnel)) {
throw new ReducerError(
`Personnel with id ${elementToBeLoadedId} is currently in transfer`
);
}
if (!vehicle.personnelIds[elementToBeLoadedId]) {
throw new ReducerError(
`Personnel with id ${personnel.id} is not assignable to the vehicle with id ${vehicle.id}`
);
}
changePosition(
personnel,
VehiclePosition.create(vehicleId),
draftState
);
break;
}
case 'patient': {
const patient = getElement(
draftState,
'patient',
elementToBeLoadedId
);
if (
Object.keys(vehicle.patientIds).length >=
vehicle.patientCapacity
) {
throw new ReducerError(
`Vehicle with id ${vehicle.id} is already full`
);
}
vehicle.patientIds[elementToBeLoadedId] = true;
changePosition(
patient,
VehiclePosition.create(vehicleId),
draftState
);
completelyLoadVehicleHelper(draftState, vehicle);
}
}
return draftState;
},
rights: 'participant',
};
export const completelyLoadVehicle: ActionReducer<CompletelyLoadVehicleAction> =
{
action: CompletelyLoadVehicleAction,
reducer: (draftState, { vehicleId }) => {
const vehicle = getElement(draftState, 'vehicle', vehicleId);
completelyLoadVehicleHelper(draftState, vehicle);
return draftState;
},
rights: 'trainer',
};
export const removeVehicleFromSimulatedRegion: ActionReducer<RemoveVehicleFromSimulatedRegionAction> =
{
action: RemoveVehicleFromSimulatedRegionAction,
reducer: (draftState, { vehicleId, simulatedRegionId }) => {
const vehicle = getElement(draftState, 'vehicle', vehicleId);
if (!isInSpecificSimulatedRegion(vehicle, simulatedRegionId)) {
throw new ReducerError(
`Vehicle with id ${vehicleId} has to be in simulated region with id ${simulatedRegionId} to be removed from there.`
);
}
completelyLoadVehicleHelper(draftState, vehicle);
const simulatedRegion = currentSimulatedRegionOf(
draftState,
vehicle
);
sendSimulationEvent(
simulatedRegion,
VehicleRemovedEvent.create(vehicleId)
);
const coordinates = cloneDeepMutable(
currentCoordinatesOf(simulatedRegion)
);
// place the vehicle on the right hand side of the simulated region
coordinates.y -= 0.5 * simulatedRegion.size.height;
coordinates.x += 5 + Math.max(simulatedRegion.size.width, 0);
changePositionWithId(
vehicleId,
MapPosition.create(coordinates),
'vehicle',
draftState
);
return draftState;
},
rights: 'trainer',
};
export const setVehicleOccupation: ActionReducer<SetVehicleOccupationAction> =
{
action: SetVehicleOccupationAction,
reducer: (draftState, { vehicleId, occupation }) => {
const vehicle = getElement(draftState, 'vehicle', vehicleId);
changeOccupation(draftState, vehicle, occupation);
return draftState;
},
rights: 'trainer',
};
}