forked from hpi-sam/digital-fuesim-manv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransfer.ts
346 lines (317 loc) · 12.1 KB
/
transfer.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
import { Type } from 'class-transformer';
import { IsInt, IsOptional, IsUUID, ValidateNested } from 'class-validator';
import {
isPositionOnMap,
isInTransfer,
isNotInTransfer,
currentTransferOf,
TransferPosition,
StartPoint,
startPointTypeOptions,
isPositionInSimulatedRegion,
simulatedRegionIdOfPosition,
createVehicleActionTag,
createTransferPointTag,
} from '../../models/utils';
import type { MapPosition } from '../../models/utils';
import {
changePosition,
offsetMapPositionBy,
} from '../../models/utils/position/position-helpers-mutable';
import type { ExerciseState } from '../../state';
import type { Mutable } from '../../utils';
import { cloneDeepMutable, UUID, uuidValidationOptions } from '../../utils';
import type { AllowedValues } from '../../utils/validators';
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 { TransferPoint } from '../../models/transfer-point';
import { PersonnelAvailableEvent } from '../../simulation/events/personnel-available';
import { VehicleArrivedEvent } from '../../simulation/events/vehicle-arrived';
import { imageSizeToPosition } from '../../state-helpers/image-size-to-position';
import type { Vehicle } from '../../models/vehicle';
import { getElement } from './utils';
import {
logElementAddedToTransfer,
logTransferEdited,
logTransferFinished,
logTransferPause,
logVehicle,
} from './utils/log';
export type TransferableElementType = 'personnel' | 'vehicle';
const transferableElementTypeAllowedValues: AllowedValues<TransferableElementType> =
{ personnel: true, vehicle: true };
/**
* Personnel/Vehicle in transfer will arrive immediately at new targetTransferPoint
* Transfer gets deleted afterwards
* @param elementId of an element that is in transfer
*/
export function letElementArrive(
draftState: Mutable<ExerciseState>,
elementType: TransferableElementType,
elementId: UUID
) {
const element = getElement(draftState, elementType, elementId);
// check that element is in transfer, this should be always the case where this function is used
if (isNotInTransfer(element)) {
throw getNotInTransferError(element.id);
}
const targetTransferPoint = getElement(
draftState,
'transferPoint',
currentTransferOf(element).targetTransferPointId
);
const newPosition = cloneDeepMutable(targetTransferPoint.position);
if (isPositionOnMap(newPosition)) {
offsetMapPositionBy(newPosition as Mutable<MapPosition>, {
x: 0,
y: imageSizeToPosition(TransferPoint.image.height / 3),
});
}
if (isPositionInSimulatedRegion(newPosition)) {
const simulatedRegion = getElement(
draftState,
'simulatedRegion',
simulatedRegionIdOfPosition(newPosition)
);
if (elementType === 'personnel') {
sendSimulationEvent(
simulatedRegion,
PersonnelAvailableEvent.create(elementId)
);
}
if (elementType === 'vehicle') {
sendSimulationEvent(
simulatedRegion,
VehicleArrivedEvent.create(elementId, draftState.currentTime)
);
}
}
changePosition(element, newPosition, draftState);
if (elementType === 'vehicle') {
logVehicle(
draftState,
[
createVehicleActionTag(draftState, 'arrived'),
createTransferPointTag(draftState, targetTransferPoint.id),
],
`${(element as Mutable<Vehicle>).name} ist an ${
targetTransferPoint.externalName
} angekommen`,
elementId
);
}
}
export class AddToTransferAction implements Action {
@IsValue('[Transfer] Add to transfer' as const)
public readonly type = '[Transfer] Add to transfer';
@IsLiteralUnion(transferableElementTypeAllowedValues)
elementType!: TransferableElementType;
@IsUUID(4, uuidValidationOptions)
public readonly elementId!: UUID;
@ValidateNested()
@Type(() => Object, startPointTypeOptions)
public readonly startPoint!: StartPoint;
@IsUUID(4, uuidValidationOptions)
public readonly targetTransferPointId!: UUID;
}
export class EditTransferAction implements Action {
@IsValue('[Transfer] Edit transfer' as const)
public readonly type = '[Transfer] Edit transfer';
@IsLiteralUnion(transferableElementTypeAllowedValues)
elementType!: TransferableElementType;
@IsUUID(4, uuidValidationOptions)
public readonly elementId!: UUID;
@IsUUID(4, uuidValidationOptions)
@IsOptional()
public readonly targetTransferPointId?: UUID;
@IsInt()
@IsOptional()
/**
* How much time in ms should be added to the transfer time.
* If it is negative, the transfer time will be decreased.
* If the time is set to a time in the past it will be set to the current time.
*/
public readonly timeToAdd?: number;
}
export class FinishTransferAction implements Action {
@IsValue('[Transfer] Finish transfer' as const)
public readonly type = '[Transfer] Finish transfer';
@IsLiteralUnion(transferableElementTypeAllowedValues)
elementType!: TransferableElementType;
@IsUUID(4, uuidValidationOptions)
public readonly elementId!: UUID;
@IsUUID(4, uuidValidationOptions)
public readonly targetTransferPointId!: UUID;
}
export class TogglePauseTransferAction implements Action {
@IsValue('[Transfer] Toggle pause transfer' as const)
public readonly type = '[Transfer] Toggle pause transfer';
@IsLiteralUnion(transferableElementTypeAllowedValues)
elementType!: TransferableElementType;
@IsUUID(4, uuidValidationOptions)
public readonly elementId!: UUID;
}
export namespace TransferActionReducers {
export const addToTransfer: ActionReducer<AddToTransferAction> = {
action: AddToTransferAction,
reducer: (
draftState,
{ elementType, elementId, startPoint, targetTransferPointId }
) => {
// check if transferPoint exists
getElement(draftState, 'transferPoint', targetTransferPointId);
const element = getElement(draftState, elementType, elementId);
if (isInTransfer(element)) {
throw new ReducerError(
`Element with id ${element.id} is already in transfer`
);
}
// Get the duration
let duration: number;
if (startPoint.type === 'transferStartPoint') {
const transferStartPoint = getElement(
draftState,
'transferPoint',
startPoint.transferPointId
);
const connection =
transferStartPoint.reachableTransferPoints[
targetTransferPointId
];
if (!connection) {
throw new ReducerError(
`TransferPoint with id ${targetTransferPointId} is not reachable from ${transferStartPoint.id}`
);
}
duration = connection.duration;
} else {
duration = startPoint.duration;
}
// Set the element to transfer
changePosition(
element,
TransferPosition.create({
startPoint: cloneDeepMutable(startPoint),
targetTransferPointId,
endTimeStamp: draftState.currentTime + duration,
isPaused: false,
}),
draftState
);
logElementAddedToTransfer(
draftState,
startPoint.type === 'alarmGroupStartPoint'
? startPoint.alarmGroupId
: startPoint.transferPointId,
startPoint.type === 'alarmGroupStartPoint'
? 'alarmGroup'
: 'transferPoint',
elementId,
elementType,
currentTransferOf(element).targetTransferPointId,
'transferPoint',
duration
);
return draftState;
},
rights: 'participant',
};
export const editTransfer: ActionReducer<EditTransferAction> = {
action: EditTransferAction,
reducer: (
draftState,
{ elementType, elementId, targetTransferPointId, timeToAdd }
) => {
const element = getElement(draftState, elementType, elementId);
if (isNotInTransfer(element)) {
throw getNotInTransferError(element.id);
}
const newTransfer = cloneDeepMutable(currentTransferOf(element));
if (targetTransferPointId) {
// check if transferPoint exists
getElement(draftState, 'transferPoint', targetTransferPointId);
newTransfer.targetTransferPointId = targetTransferPointId;
}
if (timeToAdd) {
// The endTimeStamp shouldn't be less then the current time
newTransfer.endTimeStamp = Math.max(
draftState.currentTime,
newTransfer.endTimeStamp + timeToAdd
);
}
logTransferEdited(
draftState,
elementId,
elementType,
currentTransferOf(element).targetTransferPointId,
newTransfer.targetTransferPointId,
currentTransferOf(element).endTimeStamp -
draftState.currentTime,
newTransfer.endTimeStamp - draftState.currentTime
);
changePosition(
element,
TransferPosition.create(newTransfer),
draftState
);
return draftState;
},
rights: 'trainer',
};
export const finishTransfer: ActionReducer<FinishTransferAction> = {
action: FinishTransferAction,
reducer: (
draftState,
{ elementType, elementId, targetTransferPointId }
) => {
// check if transferPoint exists
getElement(draftState, 'transferPoint', targetTransferPointId);
const element = getElement(draftState, elementType, elementId);
if (isNotInTransfer(element)) {
throw getNotInTransferError(element.id);
}
logTransferFinished(
draftState,
elementId,
elementType,
currentTransferOf(element).targetTransferPointId
);
letElementArrive(draftState, elementType, elementId);
return draftState;
},
rights: 'trainer',
};
export const togglePauseTransfer: ActionReducer<TogglePauseTransferAction> =
{
action: TogglePauseTransferAction,
reducer: (draftState, { elementType, elementId }) => {
const element = getElement(draftState, elementType, elementId);
if (isNotInTransfer(element)) {
throw getNotInTransferError(element.id);
}
const newTransfer = cloneDeepMutable(
currentTransferOf(element)
);
newTransfer.isPaused = !newTransfer.isPaused;
logTransferPause(
draftState,
elementId,
elementType,
newTransfer.targetTransferPointId,
newTransfer.isPaused
);
changePosition(
element,
TransferPosition.create(newTransfer),
draftState
);
return draftState;
},
rights: 'trainer',
};
}
function getNotInTransferError(elementId: UUID) {
return new ReducerError(`Element with id ${elementId} is not in transfer`);
}