Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Treatment delays change instantly #749

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project does **not** adhere to [Semantic Versioning](https://semver.org

## [Unreleased]

### Changed

- New intervals for the treat patients behavior are now applied instantly when changed by a trainer

## [0.2.0] - 2023-03-10

### Added
Expand Down
97 changes: 36 additions & 61 deletions shared/src/simulation/behaviors/treat-patients.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { Type } from 'class-transformer';
import {
IsInt,
IsOptional,
IsUUID,
Min,
ValidateNested,
} from 'class-validator';
import { IsOptional, IsUUID, ValidateNested } from 'class-validator';
import { getCreate } from '../../models/utils';
import { uuid, UUID, uuidValidationOptions } from '../../utils';
import { IsLiteralUnion, IsValue } from '../../utils/validators';
Expand All @@ -14,6 +8,7 @@ import { ReassignTreatmentsActivityState } from '../activities/reassign-treatmen
import { addActivity, terminateActivity } from '../activities/utils';
import { TreatmentsTimerEvent } from '../events/treatments-timer-event';
import { nextUUID } from '../utils/randomness';
import { TreatPatientsIntervals } from '../utils/treat-patients-intervals';
import {
TreatmentProgress,
treatmentProgressAllowedValues,
Expand All @@ -23,60 +18,6 @@ import type {
SimulationBehaviorState,
} from './simulation-behavior';

export class TreatPatientsIntervals {
/**
* How frequent reassignments should occur when the personnel just arrived and the situation in unclear
*/
@IsInt()
@Min(0)
public readonly unknown: number;

/**
* How frequent reassignments should occur when the patients have been counted
*/
@IsInt()
@Min(0)
public readonly counted: number;

/**
* How frequent reassignments should occur when all patients are triaged
*/
@IsInt()
@Min(0)
public readonly triaged: number;

/**
* How frequent reassignments should occur when there is enough personnel to fulfil each patient's treatment needs
*/
@IsInt()
@Min(0)
public readonly secured: number;

/**
* How long counting each patient should take.
* Counting will be finished after {patient count} times this value.
*/
@IsInt()
@Min(0)
public readonly countingTimePerPatient: number;

constructor(
unknown: number,
counted: number,
triaged: number,
secured: number,
countingTimePerPatient: number
) {
this.unknown = unknown;
this.counted = counted;
this.triaged = triaged;
this.secured = secured;
this.countingTimePerPatient = countingTimePerPatient;
}

static readonly create = getCreate(this);
}

export class TreatPatientsBehaviorState implements SimulationBehaviorState {
@IsValue('treatPatientsBehavior' as const)
readonly type = `treatPatientsBehavior`;
Expand Down Expand Up @@ -186,6 +127,40 @@ export const treatPatientsBehavior: SimulationBehavior<TreatPatientsBehaviorStat
case 'treatmentProgressChangedEvent':
behaviorState.treatmentProgress = event.newProgress;
break;
case 'treatPatientIntervalsChangedEvent':
{
if (
event.newTreatPatientsIntervals[
behaviorState.treatmentProgress
] !==
behaviorState.intervals[
behaviorState.treatmentProgress
] &&
behaviorState.delayActivityId !== null
) {
const id = nextUUID(draftState);
terminateActivity(
draftState,
simulatedRegion,
behaviorState.delayActivityId
);
addActivity(
simulatedRegion,
DelayEventActivityState.create(
id,
TreatmentsTimerEvent.create(),
draftState.currentTime +
behaviorState.intervals[
behaviorState.treatmentProgress
]
)
);
behaviorState.delayActivityId = id;
}
behaviorState.intervals =
event.newTreatPatientsIntervals;
}
break;
default:
// Ignore event
}
Expand Down
3 changes: 3 additions & 0 deletions shared/src/simulation/events/exercise-simulation-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { TickEvent } from './tick';
import { VehicleArrivedEvent } from './vehicle-arrived';
import { TreatmentsTimerEvent } from './treatments-timer-event';
import { TreatmentProgressChangedEvent } from './treatment-progress-changed';
import { TreatPatientIntervalsChangedEvent } from './treat-patient-intervals-changed';

export const simulationEvents = {
MaterialAvailableEvent,
Expand All @@ -17,6 +18,7 @@ export const simulationEvents = {
TreatmentProgressChangedEvent,
TreatmentsTimerEvent,
VehicleArrivedEvent,
TreatPatientIntervalsChangedEvent,
};

export type ExerciseSimulationEvent = InstanceType<
Expand All @@ -36,6 +38,7 @@ export const simulationEventDictionary: ExerciseSimulationEventDictionary = {
treatmentProgressChangedEvent: TreatmentProgressChangedEvent,
treatmentsTimerEvent: TreatmentsTimerEvent,
vehicleArrivedEvent: VehicleArrivedEvent,
treatPatientIntervalsChangedEvent: TreatPatientIntervalsChangedEvent,
};

export const simulationEventTypeOptions: Parameters<typeof Type> = [
Expand Down
24 changes: 24 additions & 0 deletions shared/src/simulation/events/treat-patient-intervals-changed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Type } from 'class-transformer';
import { ValidateNested } from 'class-validator';
import { getCreate } from '../../models/utils';
import { IsValue } from '../../utils/validators';
import { TreatPatientsIntervals } from '../utils/treat-patients-intervals';
import type { SimulationEvent } from './simulation-event';

export class TreatPatientIntervalsChangedEvent implements SimulationEvent {
@IsValue('treatPatientIntervalsChangedEvent')
readonly type = 'treatPatientIntervalsChangedEvent';

@Type(() => TreatPatientsIntervals)
@ValidateNested()
readonly newTreatPatientsIntervals: TreatPatientsIntervals;

/**
* @deprecated Use {@link create} instead
*/
constructor(newTreatPatientsIntervals: TreatPatientsIntervals) {
this.newTreatPatientsIntervals = newTreatPatientsIntervals;
}

static readonly create = getCreate(this);
}
56 changes: 56 additions & 0 deletions shared/src/simulation/utils/treat-patients-intervals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { IsInt, Min } from 'class-validator';
import { getCreate } from '../../models/utils';

export class TreatPatientsIntervals {
/**
* How frequent reassignments should occur when the personnel just arrived and the situation in unclear
*/
@IsInt()
@Min(0)
public readonly unknown: number;

/**
* How frequent reassignments should occur when the patients have been counted
*/
@IsInt()
@Min(0)
public readonly counted: number;

/**
* How frequent reassignments should occur when all patients are triaged
*/
@IsInt()
@Min(0)
public readonly triaged: number;

/**
* How frequent reassignments should occur when there is enough personnel to fulfil each patient's treatment needs
*/
@IsInt()
@Min(0)
public readonly secured: number;

/**
* How long counting each patient should take.
* Counting will be finished after {patient count} times this value.
*/
@IsInt()
@Min(0)
public readonly countingTimePerPatient: number;

constructor(
unknown: number,
counted: number,
triaged: number,
secured: number,
countingTimePerPatient: number
) {
this.unknown = unknown;
this.counted = counted;
this.triaged = triaged;
this.secured = secured;
this.countingTimePerPatient = countingTimePerPatient;
}

static readonly create = getCreate(this);
}
30 changes: 22 additions & 8 deletions shared/src/store/action-reducers/simulation.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { IsNumber, IsOptional, IsUUID, Min } from 'class-validator';
import { cloneDeep } from 'lodash-es';
import type {
TreatPatientsBehaviorState,
UnloadArrivingVehiclesBehaviorState,
} from '../../simulation';
import { TreatPatientIntervalsChangedEvent } from '../../simulation/events/treat-patient-intervals-changed';
import { sendSimulationEvent } from '../../simulation/events/utils';
import type { Mutable } from '../../utils';
import { UUID, uuidValidationOptions } from '../../utils';
import { IsValue } from '../../utils/validators';
Expand Down Expand Up @@ -90,25 +93,36 @@ export namespace SimulationActionReducers {
simulatedRegionId
);
const behaviorStates = simulatedRegion.behaviors;
const treatPatientsBehaviorState = behaviorStates.find(
(behaviorState) => behaviorState.id === behaviorStateId
) as Mutable<TreatPatientsBehaviorState>;
const treatPatientsIntervals = cloneDeep(
(
behaviorStates.find(
(behaviorState) =>
behaviorState.id === behaviorStateId
) as Mutable<TreatPatientsBehaviorState>
).intervals
);
if (unknown !== undefined) {
treatPatientsBehaviorState.intervals.unknown = unknown;
treatPatientsIntervals.unknown = unknown;
}
if (counted !== undefined) {
treatPatientsBehaviorState.intervals.counted = counted;
treatPatientsIntervals.counted = counted;
}
if (triaged !== undefined) {
treatPatientsBehaviorState.intervals.triaged = triaged;
treatPatientsIntervals.triaged = triaged;
}
if (secured !== undefined) {
treatPatientsBehaviorState.intervals.secured = secured;
treatPatientsIntervals.secured = secured;
}
if (countingTimePerPatient !== undefined) {
treatPatientsBehaviorState.intervals.countingTimePerPatient =
treatPatientsIntervals.countingTimePerPatient =
countingTimePerPatient;
}
sendSimulationEvent(
simulatedRegion,
TreatPatientIntervalsChangedEvent.create(
treatPatientsIntervals
)
);
return draftState;
},
rights: 'trainer',
Expand Down