forked from hpi-sam/digital-fuesim-manv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transfer-point.ts
396 lines (356 loc) · 14.3 KB
/
transfer-point.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
import { Type } from 'class-transformer';
import {
IsNumber,
IsOptional,
IsString,
IsUUID,
ValidateNested,
} from 'class-validator';
import { TransferPoint } from '../../models';
import {
currentTransferOf,
isInTransfer,
MapCoordinates,
MapPosition,
nestedCoordinatesOf,
} from '../../models/utils';
import { changePositionWithId } from '../../models/utils/position/position-helpers-mutable';
import { cloneDeepMutable, UUID, uuidValidationOptions } from '../../utils';
import { IsValue } from '../../utils/validators';
import type { Action, ActionReducer } from '../action-reducer';
import { ReducerError } from '../reducer-error';
import { letElementArrive } from './transfer';
import { calculateDistance } from './utils/calculate-distance';
import { getElement } from './utils/get-element';
import {
logTransferPointConnection,
logTransferPointConnectionRemoved,
} from './utils/log';
// TODO check: type "TransferPoint" the T is big, in other files, the second word starts with a small letter
export class AddTransferPointAction implements Action {
@IsValue('[TransferPoint] Add TransferPoint' as const)
public readonly type = `[TransferPoint] Add TransferPoint`;
@ValidateNested()
@Type(() => TransferPoint)
public readonly transferPoint!: TransferPoint;
}
export class MoveTransferPointAction implements Action {
@IsValue('[TransferPoint] Move TransferPoint' as const)
public readonly type = '[TransferPoint] Move TransferPoint';
@IsUUID(4, uuidValidationOptions)
public readonly transferPointId!: UUID;
@ValidateNested()
@Type(() => MapCoordinates)
public readonly targetPosition!: MapCoordinates;
}
export class RenameTransferPointAction implements Action {
@IsValue('[TransferPoint] Rename TransferPoint' as const)
public readonly type = '[TransferPoint] Rename TransferPoint';
@IsUUID(4, uuidValidationOptions)
public readonly transferPointId!: UUID;
@IsOptional()
@IsString()
public readonly internalName?: string;
@IsOptional()
@IsString()
public readonly externalName?: string;
}
export class RemoveTransferPointAction implements Action {
@IsValue('[TransferPoint] Remove TransferPoint' as const)
public readonly type = '[TransferPoint] Remove TransferPoint';
@IsUUID(4, uuidValidationOptions)
public readonly transferPointId!: UUID;
}
export class ConnectTransferPointsAction implements Action {
@IsValue('[TransferPoint] Connect TransferPoints' as const)
public readonly type = '[TransferPoint] Connect TransferPoints';
@IsUUID(4, uuidValidationOptions)
public readonly transferPointId1!: UUID;
@IsUUID(4, uuidValidationOptions)
public readonly transferPointId2!: UUID;
@IsOptional()
@IsNumber()
public readonly duration?: number;
}
export class DisconnectTransferPointsAction implements Action {
@IsValue('[TransferPoint] Disconnect TransferPoints' as const)
public readonly type = '[TransferPoint] Disconnect TransferPoints';
@IsUUID(4, uuidValidationOptions)
public readonly transferPointId1!: UUID;
@IsUUID(4, uuidValidationOptions)
public readonly transferPointId2!: UUID;
}
export class ConnectHospitalAction implements Action {
@IsValue('[TransferPoint] Connect hospital' as const)
public readonly type = '[TransferPoint] Connect hospital';
@IsUUID(4, uuidValidationOptions)
public readonly hospitalId!: UUID;
@IsUUID(4, uuidValidationOptions)
public readonly transferPointId!: UUID;
}
export class DisconnectHospitalAction implements Action {
@IsValue('[TransferPoint] Disconnect hospital' as const)
public readonly type = '[TransferPoint] Disconnect hospital';
@IsUUID(4, uuidValidationOptions)
public readonly hospitalId!: UUID;
@IsUUID(4, uuidValidationOptions)
public readonly transferPointId!: UUID;
}
export namespace TransferPointActionReducers {
export const addTransferPoint: ActionReducer<AddTransferPointAction> = {
action: AddTransferPointAction,
reducer: (draftState, { transferPoint }) => {
draftState.transferPoints[transferPoint.id] =
cloneDeepMutable(transferPoint);
return draftState;
},
rights: 'trainer',
};
export const moveTransferPoint: ActionReducer<MoveTransferPointAction> = {
action: MoveTransferPointAction,
reducer: (draftState, { transferPointId, targetPosition }) => {
changePositionWithId(
transferPointId,
MapPosition.create(targetPosition),
'transferPoint',
draftState
);
return draftState;
},
rights: 'trainer',
};
export const renameTransferPoint: ActionReducer<RenameTransferPointAction> =
{
action: RenameTransferPointAction,
reducer: (
draftState,
{ transferPointId, internalName, externalName }
) => {
const transferPoint = getElement(
draftState,
'transferPoint',
transferPointId
);
// Empty strings are ignored
if (internalName) {
transferPoint.internalName = internalName;
}
if (externalName) {
transferPoint.externalName = externalName;
}
return draftState;
},
rights: 'trainer',
};
export const connectTransferPoints: ActionReducer<ConnectTransferPointsAction> =
{
action: ConnectTransferPointsAction,
reducer: (
draftState,
{ transferPointId1, transferPointId2, duration }
) => {
// If the transferPoints are already connected, we only update the duration
// TODO: We currently only support bidirectional connections between different transfer points.
if (transferPointId1 === transferPointId2) {
throw new ReducerError(
`TransferPoint with id ${transferPointId1} cannot connect to itself`
);
}
const transferPoint1 = getElement(
draftState,
'transferPoint',
transferPointId1
);
const transferPoint2 = getElement(
draftState,
'transferPoint',
transferPointId2
);
const _duration =
duration ??
estimateDuration(
nestedCoordinatesOf(transferPoint1, draftState),
nestedCoordinatesOf(transferPoint2, draftState)
);
transferPoint1.reachableTransferPoints[transferPointId2] = {
duration: _duration,
};
transferPoint2.reachableTransferPoints[transferPointId1] = {
duration: _duration,
};
logTransferPointConnection(
draftState,
transferPointId1,
transferPointId2,
'transferPoint'
);
return draftState;
},
rights: 'trainer',
};
export const disconnectTransferPoints: ActionReducer<DisconnectTransferPointsAction> =
{
action: DisconnectTransferPointsAction,
reducer: (draftState, { transferPointId1, transferPointId2 }) => {
// We remove the connection from both directions
if (transferPointId1 === transferPointId2) {
throw new ReducerError(
`TransferPoint with id ${transferPointId1} cannot disconnect from itself`
);
}
const transferPoint1 = getElement(
draftState,
'transferPoint',
transferPointId1
);
const transferPoint2 = getElement(
draftState,
'transferPoint',
transferPointId2
);
delete transferPoint1.reachableTransferPoints[transferPointId2];
delete transferPoint2.reachableTransferPoints[transferPointId1];
logTransferPointConnectionRemoved(
draftState,
transferPointId1,
transferPointId2,
'transferPoint'
);
return draftState;
},
rights: 'trainer',
};
export const removeTransferPoint: ActionReducer<RemoveTransferPointAction> =
{
action: RemoveTransferPointAction,
reducer: (draftState, { transferPointId }) => {
// check if transferPoint exists
getElement(draftState, 'transferPoint', transferPointId);
// TODO: make it dynamic (if at any time something else is able to transfer this part needs to be changed accordingly)
// Let all vehicles and personnel arrive that are on transfer to this transferPoint before deleting it
for (const vehicleId of Object.keys(draftState.vehicles)) {
const vehicle = getElement(
draftState,
'vehicle',
vehicleId
);
if (
isInTransfer(vehicle) &&
currentTransferOf(vehicle).targetTransferPointId ===
transferPointId
) {
letElementArrive(draftState, vehicle.type, vehicleId);
}
}
for (const personnelId of Object.keys(draftState.personnel)) {
const personnel = getElement(
draftState,
'personnel',
personnelId
);
if (
isInTransfer(personnel) &&
currentTransferOf(personnel).targetTransferPointId ===
transferPointId
) {
letElementArrive(
draftState,
personnel.type,
personnelId
);
}
}
// TODO: If we can assume that the transfer points are always connected to each other,
// we could just iterate over draftState.transferPoints[transferPointId].reachableTransferPoints
for (const transferPoint of Object.values(
draftState.transferPoints
)) {
for (const connectedTransferPointId of Object.keys(
transferPoint.reachableTransferPoints
)) {
const connectedTransferPoint =
draftState.transferPoints[
connectedTransferPointId
]!;
delete connectedTransferPoint.reachableTransferPoints[
transferPointId
];
}
}
delete draftState.transferPoints[transferPointId];
return draftState;
},
rights: 'trainer',
};
export const connectHospital: ActionReducer<ConnectHospitalAction> = {
action: ConnectHospitalAction,
reducer: (draftState, { transferPointId, hospitalId }) => {
// Check if hospital with this Id exists
getElement(draftState, 'hospital', hospitalId);
const transferPoint = getElement(
draftState,
'transferPoint',
transferPointId
);
transferPoint.reachableHospitals[hospitalId] = true;
logTransferPointConnection(
draftState,
transferPointId,
hospitalId,
'hospital'
);
return draftState;
},
rights: 'trainer',
};
export const disconnectHospital: ActionReducer<DisconnectHospitalAction> = {
action: DisconnectHospitalAction,
reducer: (draftState, { hospitalId, transferPointId }) => {
// Check if hospital with this Id exists
getElement(draftState, 'hospital', hospitalId);
const transferPoint = getElement(
draftState,
'transferPoint',
transferPointId
);
delete transferPoint.reachableHospitals[hospitalId];
logTransferPointConnectionRemoved(
draftState,
transferPointId,
hospitalId,
'hospital'
);
return draftState;
},
rights: 'trainer',
};
}
// Helpers
/**
*
* @returns an estimated duration in ms to drive between the the two given positions
* The resulting value is a multiple of 0.1 minutes.
*/
function estimateDuration(
startPosition: MapCoordinates,
targetPosition: MapCoordinates
) {
// TODO: tweak these values more
// How long in ms it takes to start + stop moving
const overheadSummand = 10 * 1000;
// In meters per second
// On average an RTW drives 11.5 m/s (41.3 km/h https://www.leitstelle-lausitz.de/leitstelle/haeufig-gestellte-fragen/#:~:text=Wie%20viel%20schneller%20ist%20ein,30%2C4%20km%2Fh.)
// Be aware that this could be significantly off for longer distances due to, e.g., the use of the Autobahn.
const averageSpeed = 11.5;
// How many times longer is the actual driving distance in contrast to the distance as the crow flies?
// A good heuristic is 1.3 (https://forum.openstreetmap.org/viewtopic.php?id=3941)
const distanceFactor = 1.3;
const estimateTime =
overheadSummand +
((distanceFactor * calculateDistance(startPosition, targetPosition)) /
averageSpeed) *
// Convert to milliseconds
1000;
const multipleOf = 1000 * 60 * 0.1;
return Math.round(estimateTime / multipleOf) * multipleOf;
}