forked from hpi-sam/digital-fuesim-manv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulated-region.ts
358 lines (325 loc) · 12.9 KB
/
simulated-region.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
import { Type } from 'class-transformer';
import { IsString, IsUUID, ValidateNested } from 'class-validator';
import { SimulatedRegion, TransferPoint } from '../../models';
import {
isInSpecificSimulatedRegion,
MapCoordinates,
MapPosition,
SimulatedRegionPosition,
Size,
} from '../../models/utils';
import {
changePosition,
changePositionWithId,
} from '../../models/utils/position/position-helpers-mutable';
import {
ExerciseSimulationBehaviorState,
simulationBehaviorTypeOptions,
VehicleArrivedEvent,
PersonnelAvailableEvent,
NewPatientEvent,
MaterialAvailableEvent,
simulationBehaviorDictionary,
} from '../../simulation';
import { sendSimulationEvent } from '../../simulation/events/utils';
import { cloneDeepMutable, UUID, uuidValidationOptions } from '../../utils';
import { IsLiteralUnion, IsValue } from '../../utils/validators';
import type { Action, ActionReducer } from '../action-reducer';
import { ExpectedReducerError, ReducerError } from '../reducer-error';
import { TransferPointActionReducers } from './transfer-point';
import { isCompletelyLoaded } from './utils/completely-load-vehicle';
import { getElement, getElementByPredicate } from './utils/get-element';
export class AddSimulatedRegionAction implements Action {
@IsValue('[SimulatedRegion] Add simulated region' as const)
readonly type = '[SimulatedRegion] Add simulated region';
@ValidateNested()
@Type(() => SimulatedRegion)
public simulatedRegion!: SimulatedRegion;
@ValidateNested()
@Type(() => TransferPoint)
public transferPoint!: TransferPoint;
}
export class RemoveSimulatedRegionAction implements Action {
@IsValue('[SimulatedRegion] Remove simulated region' as const)
public readonly type = '[SimulatedRegion] Remove simulated region';
@IsUUID(4, uuidValidationOptions)
public readonly simulatedRegionId!: UUID;
}
export class MoveSimulatedRegionAction implements Action {
@IsValue('[SimulatedRegion] Move simulated region' as const)
public readonly type = '[SimulatedRegion] Move simulated region';
@IsUUID(4, uuidValidationOptions)
public readonly simulatedRegionId!: UUID;
@ValidateNested()
@Type(() => MapCoordinates)
public readonly targetPosition!: MapCoordinates;
}
export class ResizeSimulatedRegionAction implements Action {
@IsValue('[SimulatedRegion] Resize simulated region' as const)
public readonly type = '[SimulatedRegion] Resize simulated region';
@IsUUID(4, uuidValidationOptions)
public readonly simulatedRegionId!: UUID;
@ValidateNested()
@Type(() => MapCoordinates)
public readonly targetPosition!: MapCoordinates;
@ValidateNested()
@Type(() => Size)
public readonly newSize!: Size;
}
export class RenameSimulatedRegionAction implements Action {
@IsValue('[SimulatedRegion] Rename simulated region' as const)
public readonly type = '[SimulatedRegion] Rename simulated region';
@IsUUID(4, uuidValidationOptions)
public readonly simulatedRegionId!: UUID;
@IsString()
public readonly newName!: string;
}
export class AddElementToSimulatedRegionAction implements Action {
@IsValue('[SimulatedRegion] Add Element' as const)
public readonly type = '[SimulatedRegion] Add Element';
@IsUUID(4, uuidValidationOptions)
public readonly simulatedRegionId!: UUID;
@IsLiteralUnion({
material: true,
patient: true,
personnel: true,
vehicle: true,
})
public readonly elementToBeAddedType!:
| 'material'
| 'patient'
| 'personnel'
| 'vehicle';
@IsUUID(4, uuidValidationOptions)
public readonly elementToBeAddedId!: UUID;
}
export class AddBehaviorToSimulatedRegionAction implements Action {
@IsValue('[SimulatedRegion] Add Behavior' as const)
public readonly type = '[SimulatedRegion] Add Behavior';
@IsUUID(4, uuidValidationOptions)
public readonly simulatedRegionId!: UUID;
@Type(...simulationBehaviorTypeOptions)
@ValidateNested()
public readonly behaviorState!: ExerciseSimulationBehaviorState;
}
export class RemoveBehaviorFromSimulatedRegionAction implements Action {
@IsValue('[SimulatedRegion] Remove Behavior' as const)
public readonly type = '[SimulatedRegion] Remove Behavior';
@IsUUID(4, uuidValidationOptions)
public readonly behaviorId!: UUID;
@IsUUID(4, uuidValidationOptions)
public readonly simulatedRegionId!: UUID;
}
export namespace SimulatedRegionActionReducers {
export const addSimulatedRegion: ActionReducer<AddSimulatedRegionAction> = {
action: AddSimulatedRegionAction,
reducer: (draftState, { simulatedRegion, transferPoint }) => {
TransferPointActionReducers.addTransferPoint.reducer(draftState, {
type: '[TransferPoint] Add TransferPoint',
transferPoint,
});
draftState.simulatedRegions[simulatedRegion.id] =
cloneDeepMutable(simulatedRegion);
return draftState;
},
rights: 'trainer',
};
export const removeSimulatedRegion: ActionReducer<RemoveSimulatedRegionAction> =
{
action: RemoveSimulatedRegionAction,
reducer: (draftState, { simulatedRegionId }) => {
const simulatedRegion = getElement(
draftState,
'simulatedRegion',
simulatedRegionId
);
const transferPoint = getElementByPredicate(
draftState,
'transferPoint',
(element) =>
isInSpecificSimulatedRegion(element, simulatedRegion.id)
);
TransferPointActionReducers.removeTransferPoint.reducer(
draftState,
{
type: '[TransferPoint] Remove TransferPoint',
transferPointId: transferPoint.id,
}
);
delete draftState.simulatedRegions[simulatedRegionId];
return draftState;
},
rights: 'trainer',
};
export const moveSimulatedRegion: ActionReducer<MoveSimulatedRegionAction> =
{
action: MoveSimulatedRegionAction,
reducer: (draftState, { simulatedRegionId, targetPosition }) => {
changePositionWithId(
simulatedRegionId,
MapPosition.create(targetPosition),
'simulatedRegion',
draftState
);
return draftState;
},
rights: 'trainer',
};
export const resizeSimulatedRegion: ActionReducer<ResizeSimulatedRegionAction> =
{
action: ResizeSimulatedRegionAction,
reducer: (
draftState,
{ simulatedRegionId, targetPosition, newSize }
) => {
const simulatedRegion = getElement(
draftState,
'simulatedRegion',
simulatedRegionId
);
changePosition(
simulatedRegion,
MapPosition.create(targetPosition),
draftState
);
simulatedRegion.size = cloneDeepMutable(newSize);
return draftState;
},
rights: 'trainer',
};
export const renameSimulatedRegion: ActionReducer<RenameSimulatedRegionAction> =
{
action: RenameSimulatedRegionAction,
reducer: (draftState, { simulatedRegionId, newName }) => {
const simulatedRegion = getElement(
draftState,
'simulatedRegion',
simulatedRegionId
);
const transferPoint = getElementByPredicate(
draftState,
'transferPoint',
(element) =>
isInSpecificSimulatedRegion(element, simulatedRegion.id)
);
TransferPointActionReducers.renameTransferPoint.reducer(
draftState,
{
type: '[TransferPoint] Rename TransferPoint',
transferPointId: transferPoint.id,
externalName: `[Simuliert] ${newName}`,
}
);
simulatedRegion.name = newName;
return draftState;
},
rights: 'trainer',
};
export const addElementToSimulatedRegion: ActionReducer<AddElementToSimulatedRegionAction> =
{
action: AddElementToSimulatedRegionAction,
reducer: (
draftState,
{ simulatedRegionId, elementToBeAddedId, elementToBeAddedType }
) => {
const simulatedRegion = getElement(
draftState,
'simulatedRegion',
simulatedRegionId
);
const element = getElement(
draftState,
elementToBeAddedType,
elementToBeAddedId
);
if (
element.type === 'vehicle' &&
!isCompletelyLoaded(draftState, element)
) {
throw new ExpectedReducerError(
'Das Fahrzeug kann nur in die simulierte Region verschoben werden, wenn Personal und Material eingestiegen sind.'
);
}
changePosition(
element,
SimulatedRegionPosition.create(simulatedRegionId),
draftState
);
switch (element.type) {
case 'vehicle':
sendSimulationEvent(
simulatedRegion,
VehicleArrivedEvent.create(
element.id,
draftState.currentTime
)
);
break;
case 'patient':
sendSimulationEvent(
simulatedRegion,
NewPatientEvent.create(element.id)
);
break;
case 'personnel':
sendSimulationEvent(
simulatedRegion,
PersonnelAvailableEvent.create(element.id)
);
break;
case 'material':
sendSimulationEvent(
simulatedRegion,
MaterialAvailableEvent.create(element.id)
);
break;
}
return draftState;
},
rights: 'participant',
};
export const addBehaviorToSimulatedRegion: ActionReducer<AddBehaviorToSimulatedRegionAction> =
{
action: AddBehaviorToSimulatedRegionAction,
reducer: (draftState, { simulatedRegionId, behaviorState }) => {
const simulatedRegion = getElement(
draftState,
'simulatedRegion',
simulatedRegionId
);
simulatedRegion.behaviors.push(cloneDeepMutable(behaviorState));
return draftState;
},
rights: 'participant',
};
export const removeBehaviorFromSimulatedRegion: ActionReducer<RemoveBehaviorFromSimulatedRegionAction> =
{
action: RemoveBehaviorFromSimulatedRegionAction,
reducer: (draftState, { simulatedRegionId, behaviorId }) => {
const simulatedRegion = getElement(
draftState,
'simulatedRegion',
simulatedRegionId
);
const index = simulatedRegion.behaviors.findIndex(
(behavior) => behavior.id === behaviorId
);
if (index === -1) {
throw new ReducerError(
`The simulated region with id ${simulatedRegionId} has no behavior with id ${behaviorId}. Therefore it could not be removed.`
);
}
const behaviorState = simulatedRegion.behaviors[index]!;
if (simulationBehaviorDictionary[behaviorState.type].onRemove) {
simulationBehaviorDictionary[behaviorState.type].onRemove!(
draftState,
simulatedRegion,
behaviorState as any
);
}
simulatedRegion.behaviors.splice(index, 1);
return draftState;
},
rights: 'participant',
};
}