Skip to content

Commit

Permalink
Revert "Fix deletion of simulated region now cascades (#740)"
Browse files Browse the repository at this point in the history
This reverts commit 41e9cb3.
  • Loading branch information
Nils1729 committed Mar 11, 2023
1 parent 4167831 commit 7b91763
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 139 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ and this project does **not** adhere to [Semantic Versioning](https://semver.org
### Changed

- Images are now stored in git and not git lfs anymore
- Patients, vehicles, personnel and material inside a simulated region are now deleted, when the simulated region is deleted. For vehicles, personnel, and material, they will only be deleted if all that belong together are in the same simulated region.

### Fixed

Expand Down
112 changes: 20 additions & 92 deletions shared/src/store/action-reducers/simulated-region.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { IsString, IsUUID, ValidateNested } from 'class-validator';
import { SimulatedRegion, TransferPoint } from '../../models';
import {
isInSpecificSimulatedRegion,
isInSpecificVehicle,
MapCoordinates,
MapPosition,
SimulatedRegionPosition,
Expand All @@ -22,103 +21,14 @@ import {
MaterialAvailableEvent,
} from '../../simulation';
import { sendSimulationEvent } from '../../simulation/events/utils';
import type { ExerciseState } from '../../state';
import type { Mutable } from '../../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 {
deleteTransferPoint,
TransferPointActionReducers,
} from './transfer-point';
import { TransferPointActionReducers } from './transfer-point';
import { isCompletelyLoaded } from './utils/completely-load-vehicle';
import { getElement, getElementByPredicate } from './utils/get-element';

/**
* This function assumes that every SimulatedRegion holds **ONLY** material,vehicles,patients and personnel
* (in any amount) and **ONE** TransferPoint.
* It only deletes material,vehicles and personnel if they are **ALL** in the SimulatedRegion to be deleted
* If one were to add more things to a SimulatedRegion one would have to change this function accordingly.
* @param draftState: The Draft State from which the SimulatedRegion should be deleted.
* @param simulatedRegionId: The Id of the SimulatedRegion that should be deleted.
*/

export function deleteSimulatedRegion(
draftState: Mutable<ExerciseState>,
simulatedRegionId: UUID
) {
// Delete the TransferPoint

const simulatedRegion = getElement(
draftState,
'simulatedRegion',
simulatedRegionId
);

const transferPointId = getElementByPredicate(
draftState,
'transferPoint',
(element) => isInSpecificSimulatedRegion(element, simulatedRegion.id)
).id;

deleteTransferPoint(draftState, transferPointId);

// Find related vehicles

const relatedVehicles = Object.values(draftState.vehicles).filter(
(vehicle) =>
isInSpecificSimulatedRegion(vehicle, simulatedRegionId) &&
Object.keys(vehicle.materialIds).every((materialId) => {
const material = getElement(draftState, 'material', materialId);
return (
isInSpecificSimulatedRegion(material, simulatedRegionId) ||
isInSpecificVehicle(material, vehicle.id)
);
}) &&
Object.keys(vehicle.personnelIds).every((personnelId) => {
const personnel = getElement(
draftState,
'personnel',
personnelId
);
return (
isInSpecificSimulatedRegion(personnel, simulatedRegionId) ||
isInSpecificVehicle(personnel, vehicle.id)
);
})
);

// Find and delete related materials and personnel

relatedVehicles.forEach((vehicle) => {
Object.keys(vehicle.materialIds).forEach(
(vehicleId) => delete draftState.materials[vehicleId]
);
Object.keys(vehicle.personnelIds).forEach(
(personnelId) => delete draftState.personnel[personnelId]
);
});

// Delete related vehicles

relatedVehicles.forEach(
(vehicle) => delete draftState.vehicles[vehicle.id]
);

// Find and delete related patients

Object.values(draftState.patients)
.filter((patients) =>
isInSpecificSimulatedRegion(patients, simulatedRegionId)
)
.forEach((patients) => delete draftState.patients[patients.id]);

// Delete the SimulatedRegion

delete draftState.simulatedRegions[simulatedRegionId];
}

export class AddSimulatedRegionAction implements Action {
@IsValue('[SimulatedRegion] Add simulated region' as const)
readonly type = '[SimulatedRegion] Add simulated region';
Expand Down Expand Up @@ -236,7 +146,25 @@ export namespace SimulatedRegionActionReducers {
{
action: RemoveSimulatedRegionAction,
reducer: (draftState, { simulatedRegionId }) => {
deleteSimulatedRegion(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',
Expand Down
100 changes: 54 additions & 46 deletions shared/src/store/action-reducers/transfer-point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import {
nestedCoordinatesOf,
} from '../../models/utils';
import { changePositionWithId } from '../../models/utils/position/position-helpers-mutable';
import type { ExerciseState } from '../../state';
import type { Mutable } from '../../utils';
import { cloneDeepMutable, UUID, uuidValidationOptions } from '../../utils';
import { IsValue } from '../../utils/validators';
import type { Action, ActionReducer } from '../action-reducer';
Expand All @@ -25,49 +23,6 @@ import { letElementArrive } from './transfer';
import { calculateDistance } from './utils/calculate-distance';
import { getElement } from './utils/get-element';

export function deleteTransferPoint(
draftState: Mutable<ExerciseState>,
transferPointId: UUID
) {
// 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];
}

// TODO check: type "TransferPoint" the T is big, in other files, the second word starts with a small letter

export class AddTransferPointAction implements Action {
Expand Down Expand Up @@ -281,7 +236,60 @@ export namespace TransferPointActionReducers {
{
action: RemoveTransferPointAction,
reducer: (draftState, { transferPointId }) => {
deleteTransferPoint(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',
Expand Down

0 comments on commit 7b91763

Please sign in to comment.