Skip to content

Commit

Permalink
fix 404 problems with transferPoint deletion
Browse files Browse the repository at this point in the history
elements arrive immediately at targetTransferPoint before it gets deleted + fix EOC chosen deleted transferPoint (#420)
  • Loading branch information
anonym-HPI authored May 30, 2022
1 parent 0cf8757 commit 2242a2d
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { OnDestroy } from '@angular/core';
import { Component } from '@angular/core';
import { Store } from '@ngrx/store';
import type { AlarmGroup, UUID } from 'digital-fuesim-manv-shared';
import {
AlarmGroupStartPoint,
createVehicleParameters,
} from 'digital-fuesim-manv-shared';
import { Subject, takeUntil } from 'rxjs';
import { ApiService } from 'src/app/core/api.service';
import { MessageService } from 'src/app/core/messages/message.service';
import type { AppState } from 'src/app/state/app.state';
Expand All @@ -23,7 +25,9 @@ let targetTransferPointId: UUID | undefined;
templateUrl: './send-alarm-group-interface.component.html',
styleUrls: ['./send-alarm-group-interface.component.scss'],
})
export class SendAlarmGroupInterfaceComponent {
export class SendAlarmGroupInterfaceComponent implements OnDestroy {
private readonly destroy$ = new Subject<void>();

public readonly alarmGroups$ = this.store.select(selectAlarmGroups);

public readonly transferPoints$ = this.store.select(selectTransferPoints);
Expand All @@ -32,7 +36,19 @@ export class SendAlarmGroupInterfaceComponent {
private readonly apiService: ApiService,
private readonly store: Store<AppState>,
private readonly messageService: MessageService
) {}
) {
// reset chosen targetTransferPoint if it gets deleted
this.transferPoints$
.pipe(takeUntil(this.destroy$))
.subscribe((transferPoints) => {
if (
targetTransferPointId &&
!transferPoints[targetTransferPointId]
) {
this.targetTransferPointId = undefined;
}
});
}

public get targetTransferPointId() {
return targetTransferPointId;
Expand Down Expand Up @@ -82,4 +98,8 @@ export class SendAlarmGroupInterfaceComponent {
}
});
}

ngOnDestroy(): void {
this.destroy$.next();
}
}
17 changes: 4 additions & 13 deletions shared/src/store/action-reducers/exercise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import {
} from 'class-validator';
import { countBy } from 'lodash-es';
import type { Client, Patient, Vehicle } from '../../models';
import { Personnel, TransferPoint, Viewport } from '../../models';
import { Personnel, Viewport } from '../../models';
import { StatusHistoryEntry } from '../../models/status-history-entry';
import { getStatus, Position } from '../../models/utils';
import { getStatus } from '../../models/utils';
import type { AreaStatistics } from '../../models/utils/area-statistics';
import type { ExerciseState } from '../../state';
import { imageSizeToPosition } from '../../state-helpers';
import type { Mutable } from '../../utils';
import { uuid } from '../../utils';
import { PatientUpdate } from '../../utils/patient-updates';
import type { Action, ActionReducer } from '../action-reducer';
import { letElementArrive } from './transfer';
import { calculateTreatments } from './utils/calculate-treatments';

export class PauseExerciseAction implements Action {
Expand Down Expand Up @@ -151,16 +151,7 @@ function refreshTransfer(
if (element.transfer.endTimeStamp > draftState.currentTime) {
return;
}
// Personnel/Vehicle arrived at new transferPoint
const targetTransferPoint =
draftState.transferPoints[element.transfer.targetTransferPointId];
element.position = Position.create(
targetTransferPoint.position.x,
targetTransferPoint.position.y +
// Position it in the upper half of the transferPoint)
imageSizeToPosition(TransferPoint.image.height / 3)
);
delete element.transfer;
letElementArrive(draftState, key, element.id);
});
}

Expand Down
33 changes: 31 additions & 2 deletions shared/src/store/action-reducers/transfer-point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Position } from '../../models/utils';
import { uuidValidationOptions, UUID } from '../../utils';
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';

Expand Down Expand Up @@ -219,8 +220,36 @@ export namespace TransferPointActionReducers {
{
action: RemoveTransferPointAction,
reducer: (draftState, { transferPointId }) => {
// check if transferPoint exists
getElement(draftState, 'transferPoints', transferPointId);
delete draftState.transferPoints[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,
'vehicles',
vehicleId
);
if (
vehicle.transfer?.targetTransferPointId ===
transferPointId
) {
letElementArrive(draftState, 'vehicles', vehicleId);
}
}
for (const personnelId of Object.keys(draftState.personnel)) {
const personnel = getElement(
draftState,
'personnel',
personnelId
);
if (
personnel.transfer?.targetTransferPointId ===
transferPointId
) {
letElementArrive(draftState, 'personnel', 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 _transferPointId of Object.keys(
Expand All @@ -238,7 +267,7 @@ export namespace TransferPointActionReducers {
];
}
}
// TODO: Remove the vehicles and personnel in transit
delete draftState.transferPoints[transferPointId];
return draftState;
},
rights: 'trainer',
Expand Down
70 changes: 69 additions & 1 deletion shared/src/store/action-reducers/transfer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,42 @@
import { IsInt, IsObject, IsOptional, IsString, IsUUID } from 'class-validator';
import type { ExerciseState } from '../..';
import { imageSizeToPosition, Position, TransferPoint } from '../..';
import { StartPoint } from '../../models/utils/start-points';
import type { Mutable } from '../../utils';
import { UUID, uuidValidationOptions } from '../../utils';
import type { Action, ActionReducer } from '../action-reducer';
import { ReducerError } from '../reducer-error';
import { getElement } from './utils/get-element';

/**
* 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: 'personnel' | 'vehicles',
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 (!element.transfer) {
throw getNotInTransferError(element.id);
}
const targetTransferPoint = getElement(
draftState,
'transferPoints',
element.transfer.targetTransferPointId
);
element.position = Position.create(
targetTransferPoint.position.x,
targetTransferPoint.position.y +
// Position it in the upper half of the transferPoint)
imageSizeToPosition(TransferPoint.image.height / 3)
);
delete element.transfer;
}

export class AddToTransferAction implements Action {
@IsString()
public readonly type = '[Transfer] Add to transfer';
Expand Down Expand Up @@ -41,11 +73,25 @@ export class EditTransferAction implements Action {
/**
* 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 set the the end of the transfer time to the past, it sets it to the current time.
* 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 DeleteTransferAction implements Action {
@IsString()
public readonly type = '[Transfer] delete transfer';

@IsString()
elementType!: 'personnel' | 'vehicles';

@IsUUID(4, uuidValidationOptions)
public readonly elementId!: UUID;

@IsUUID(4, uuidValidationOptions)
public readonly targetTransferPointId!: UUID;
}

export class TogglePauseTransferAction implements Action {
@IsString()
public readonly type = '[Transfer] Toggle pause transfer';
Expand All @@ -64,6 +110,8 @@ export namespace TransferActionReducers {
draftState,
{ elementType, elementId, startPoint, targetTransferPointId }
) => {
// check if transferPoint exists
getElement(draftState, 'transferPoints', targetTransferPointId);
const element = getElement(draftState, elementType, elementId);
if (element.transfer) {
throw new ReducerError(
Expand Down Expand Up @@ -117,6 +165,8 @@ export namespace TransferActionReducers {
throw getNotInTransferError(element.id);
}
if (targetTransferPointId) {
// check if transferPoint exists
getElement(draftState, 'transferPoints', targetTransferPointId);
element.transfer.targetTransferPointId = targetTransferPointId;
}
if (timeToAdd) {
Expand All @@ -131,6 +181,24 @@ export namespace TransferActionReducers {
rights: 'trainer',
};

export const deleteTransfer: ActionReducer<DeleteTransferAction> = {
action: DeleteTransferAction,
reducer: (
draftState,
{ elementType, elementId, targetTransferPointId }
) => {
// check if transferPoint exists
getElement(draftState, 'transferPoints', targetTransferPointId);
const element = getElement(draftState, elementType, elementId);
if (!element.transfer) {
throw getNotInTransferError(element.id);
}
letElementArrive(draftState, elementType, elementId);
return draftState;
},
rights: 'trainer',
};

export const togglePauseTransfer: ActionReducer<TogglePauseTransferAction> =
{
action: TogglePauseTransferAction,
Expand Down

0 comments on commit 2242a2d

Please sign in to comment.