Skip to content

Commit

Permalink
trainrun: isolate symmetry check (#410)
Browse files Browse the repository at this point in the history
add checkAndAdjustSymmetry() in trainrunsection.helper.ts
  • Loading branch information
Yohh authored Mar 7, 2025
1 parent f265e62 commit 95aab7e
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 96 deletions.
130 changes: 62 additions & 68 deletions src/app/services/data/trainrun-section-times.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {Injectable} from "@angular/core";
import {MathUtils} from "../../utils/math";
import {LeftAndRightElement, TrainrunsectionHelper,} from "../util/trainrunsection.helper";
import {
LeftAndRightElement,
TrainrunsectionHelper,
} from "../util/trainrunsection.helper";
import {
LeftAndRightLockStructure,
LeftAndRightTimeStructure,
Expand Down Expand Up @@ -144,22 +147,21 @@ export class TrainrunSectionTimesService {
this.roundAllTimes();
this.removeOffsetAndBackTransformTimeStructure();

if (this.selectedTrainrunSection.getTrainrun().getIsSymmetric()) {
this.timeStructure.leftArrivalTime = TrainrunsectionHelper.getSymmetricTime(
this.timeStructure.leftDepartureTime,
);
}
TrainrunsectionHelper.checkAndAdjustSymmetry(
this.selectedTrainrunSection,
this.timeStructure.leftArrivalTime,
this.timeStructure.leftDepartureTime,
);
if (!this.lockStructure.rightLock) {
this.timeStructure.rightArrivalTime =
this.timeStructure.leftDepartureTime +
(this.timeStructure.travelTime % 60);
this.timeStructure.rightArrivalTime %= 60;
if (this.selectedTrainrunSection.getTrainrun().getIsSymmetric()) {
this.timeStructure.rightDepartureTime =
TrainrunsectionHelper.getSymmetricTime(
this.timeStructure.rightArrivalTime,
);
}
TrainrunsectionHelper.checkAndAdjustSymmetry(
this.selectedTrainrunSection,
this.timeStructure.rightDepartureTime,
this.timeStructure.rightArrivalTime,
);
} else if (
!this.lockStructure.travelTimeLock &&
this.lockStructure.rightLock
Expand Down Expand Up @@ -202,25 +204,22 @@ export class TrainrunSectionTimesService {
this.showWarningTwoLocks = false;
this.roundAllTimes();
this.removeOffsetAndBackTransformTimeStructure();

if (this.selectedTrainrunSection.getTrainrun().getIsSymmetric()) {
this.timeStructure.leftDepartureTime =
TrainrunsectionHelper.getSymmetricTime(
this.timeStructure.leftArrivalTime,
);
}
TrainrunsectionHelper.checkAndAdjustSymmetry(
this.selectedTrainrunSection,
this.timeStructure.leftDepartureTime,
this.timeStructure.leftArrivalTime,
);
if (!this.lockStructure.rightLock) {
this.timeStructure.rightDepartureTime =
this.timeStructure.leftArrivalTime -
(this.timeStructure.travelTime % 60);
this.timeStructure.rightDepartureTime +=
this.timeStructure.rightDepartureTime < 0 ? 60 : 0;
if (this.selectedTrainrunSection.getTrainrun().getIsSymmetric()) {
this.timeStructure.rightArrivalTime =
TrainrunsectionHelper.getSymmetricTime(
this.timeStructure.rightDepartureTime,
);
}
TrainrunsectionHelper.checkAndAdjustSymmetry(
this.selectedTrainrunSection,
this.timeStructure.rightArrivalTime,
this.timeStructure.rightDepartureTime,
);
} else if (
!this.lockStructure.travelTimeLock &&
this.lockStructure.rightLock
Expand Down Expand Up @@ -263,25 +262,23 @@ export class TrainrunSectionTimesService {
this.showWarningTwoLocks = false;
this.roundAllTimes();
this.removeOffsetAndBackTransformTimeStructure();
if (this.selectedTrainrunSection.getTrainrun().getIsSymmetric()) {
this.timeStructure.rightDepartureTime =
TrainrunsectionHelper.getSymmetricTime(
this.timeStructure.rightArrivalTime,
);
}
TrainrunsectionHelper.checkAndAdjustSymmetry(
this.selectedTrainrunSection,
this.timeStructure.rightDepartureTime,
this.timeStructure.rightArrivalTime,
);

if (!this.lockStructure.leftLock) {
this.timeStructure.leftDepartureTime =
this.timeStructure.rightArrivalTime -
(this.timeStructure.travelTime % 60);
this.timeStructure.leftDepartureTime +=
this.timeStructure.leftDepartureTime < 0 ? 60 : 0;
if (this.selectedTrainrunSection.getTrainrun().getIsSymmetric()) {
this.timeStructure.leftArrivalTime =
TrainrunsectionHelper.getSymmetricTime(
this.timeStructure.leftDepartureTime,
);
}
TrainrunsectionHelper.checkAndAdjustSymmetry(
this.selectedTrainrunSection,
this.timeStructure.leftArrivalTime,
this.timeStructure.leftDepartureTime,
);
} else if (
!this.lockStructure.travelTimeLock &&
this.lockStructure.leftLock
Expand Down Expand Up @@ -324,24 +321,22 @@ export class TrainrunSectionTimesService {
this.showWarningTwoLocks = false;
this.roundAllTimes();
this.removeOffsetAndBackTransformTimeStructure();
TrainrunsectionHelper.checkAndAdjustSymmetry(
this.selectedTrainrunSection,
this.timeStructure.rightArrivalTime,
this.timeStructure.rightDepartureTime,
);

if (this.selectedTrainrunSection.getTrainrun().getIsSymmetric()) {
this.timeStructure.rightArrivalTime =
TrainrunsectionHelper.getSymmetricTime(
this.timeStructure.rightDepartureTime,
);
}
if (!this.lockStructure.leftLock) {
this.timeStructure.leftArrivalTime =
this.timeStructure.rightDepartureTime +
(this.timeStructure.travelTime % 60);
this.timeStructure.leftArrivalTime %= 60;
if (this.selectedTrainrunSection.getTrainrun().getIsSymmetric()) {
this.timeStructure.leftDepartureTime =
TrainrunsectionHelper.getSymmetricTime(
this.timeStructure.leftArrivalTime,
);
}
TrainrunsectionHelper.checkAndAdjustSymmetry(
this.selectedTrainrunSection,
this.timeStructure.leftDepartureTime,
this.timeStructure.leftArrivalTime,
);
} else if (
!this.lockStructure.travelTimeLock &&
this.lockStructure.leftLock
Expand Down Expand Up @@ -390,24 +385,22 @@ export class TrainrunSectionTimesService {
this.timeStructure.rightArrivalTime +=
this.timeStructure.rightArrivalTime < 0 ? 60 : 0;
this.timeStructure.rightArrivalTime %= 60;
if (this.selectedTrainrunSection.getTrainrun().getIsSymmetric()) {
this.timeStructure.rightDepartureTime =
TrainrunsectionHelper.getSymmetricTime(
this.timeStructure.rightArrivalTime,
);
}
TrainrunsectionHelper.checkAndAdjustSymmetry(
this.selectedTrainrunSection,
this.timeStructure.rightDepartureTime,
this.timeStructure.rightArrivalTime,
);
} else if (!this.lockStructure.leftLock) {
this.timeStructure.leftArrivalTime =
this.timeStructure.rightDepartureTime + this.timeStructure.travelTime;
this.timeStructure.leftArrivalTime +=
this.timeStructure.leftArrivalTime < 0 ? 60 : 0;
this.timeStructure.leftArrivalTime %= 60;
if (this.selectedTrainrunSection.getTrainrun().getIsSymmetric()) {
this.timeStructure.leftDepartureTime =
TrainrunsectionHelper.getSymmetricTime(
this.timeStructure.leftArrivalTime,
);
}
TrainrunsectionHelper.checkAndAdjustSymmetry(
this.selectedTrainrunSection,
this.timeStructure.leftDepartureTime,
this.timeStructure.leftArrivalTime,
);
} else {
this.showWarningTwoLocks = true;
}
Expand Down Expand Up @@ -441,8 +434,9 @@ export class TrainrunSectionTimesService {
}

updateTrainrunSectionTimeLock() {
const leftRight =
this.trainrunSectionHelper.getLeftRightSections(this.selectedTrainrunSection);
const leftRight = this.trainrunSectionHelper.getLeftRightSections(
this.selectedTrainrunSection,
);

this.trainrunSectionService.updateTrainrunSectionTimeLock(
leftRight.leftSection.getId(),
Expand All @@ -455,7 +449,7 @@ export class TrainrunSectionTimesService {
leftRight.leftSection,
),
this.lockStructure.travelTimeLock,
true
true,
);

this.trainrunSectionService.updateTrainrunSectionTimeLock(
Expand All @@ -469,7 +463,7 @@ export class TrainrunSectionTimesService {
leftRight.rightSection,
),
undefined,
true
true,
);
}

Expand Down Expand Up @@ -513,7 +507,7 @@ export class TrainrunSectionTimesService {
this.initialLeftAndRightElement === LeftAndRightElement.LeftDeparture ||
this.initialLeftAndRightElement === LeftAndRightElement.RightArrival ||
this.initialLeftAndRightElement ===
LeftAndRightElement.LeftRightTrainrunName
LeftAndRightElement.LeftRightTrainrunName
) {
this.timeStructure.leftDepartureTime =
(this.timeStructure.leftDepartureTime + this.offset) % 60;
Expand Down Expand Up @@ -543,7 +537,7 @@ export class TrainrunSectionTimesService {
this.initialLeftAndRightElement === LeftAndRightElement.LeftDeparture ||
this.initialLeftAndRightElement === LeftAndRightElement.RightArrival ||
this.initialLeftAndRightElement ===
LeftAndRightElement.LeftRightTrainrunName
LeftAndRightElement.LeftRightTrainrunName
) {
this.timeStructure.leftDepartureTime =
(maxMinutes + this.timeStructure.leftDepartureTime - this.offset) % 60;
Expand Down Expand Up @@ -622,7 +616,7 @@ export class TrainrunSectionTimesService {
this.timeStructure,
),
this.selectedTrainrunSection,
this.filterService.getTimeDisplayPrecision()
this.filterService.getTimeDisplayPrecision(),
);
}
}
77 changes: 49 additions & 28 deletions src/app/services/util/trainrunsection.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ export enum LeftAndRightElement {
}

export class TrainrunsectionHelper {
constructor(private trainrunService: TrainrunService) {
}
constructor(private trainrunService: TrainrunService) {}

static getSymmetricTime(time: number) {
return time === 0 ? 0 : 60 - time;
Expand All @@ -46,11 +45,13 @@ export class TrainrunsectionHelper {
travelTimeFactor: number,
trsTravelTime: number,
isRightNodeNonStopTransit: boolean,
precision = TrainrunSectionService.TIME_PRECISION
precision = TrainrunSectionService.TIME_PRECISION,
): number {
if (isRightNodeNonStopTransit) {
return Math.max(MathUtils.round(trsTravelTime * travelTimeFactor, precision),
1.0 / Math.pow(10, precision));
return Math.max(
MathUtils.round(trsTravelTime * travelTimeFactor, precision),
1.0 / Math.pow(10, precision),
);
} else {
return Math.max(
MathUtils.round(totalTravelTime - summedTravelTime, precision),
Expand All @@ -61,21 +62,21 @@ export class TrainrunsectionHelper {

static getRightArrivalTime(
timeStructure: LeftAndRightTimeStructure,
precision = TrainrunSectionService.TIME_PRECISION
precision = TrainrunSectionService.TIME_PRECISION,
): number {
return MathUtils.round(
(timeStructure.leftDepartureTime + (timeStructure.travelTime % 60)) % 60,
precision
precision,
);
}

static getRightDepartureTime(
timeStructure: LeftAndRightTimeStructure,
precision = TrainrunSectionService.TIME_PRECISION
precision = TrainrunSectionService.TIME_PRECISION,
): number {
return MathUtils.round(
this.getSymmetricTime(timeStructure.rightArrivalTime),
precision
precision,
);
}

Expand Down Expand Up @@ -113,27 +114,29 @@ export class TrainrunsectionHelper {
const lastLeftNode = startForwardBackwardNode.startForwardNode;
const lastRightNode = startForwardBackwardNode.startBackwardNode;

const towardsSource =
this.trainrunService.getLastNonStopTrainrunSection(
trainrunSection.getSourceNode(),
trainrunSection);
const towradsTarget =
this.trainrunService.getLastNonStopTrainrunSection(
trainrunSection.getTargetNode(),
trainrunSection);
const towardsSource = this.trainrunService.getLastNonStopTrainrunSection(
trainrunSection.getSourceNode(),
trainrunSection,
);
const towradsTarget = this.trainrunService.getLastNonStopTrainrunSection(
trainrunSection.getTargetNode(),
trainrunSection,
);

let leftSection = towradsTarget;
let rightSection = towardsSource;
if (towardsSource.getSourceNodeId() === lastLeftNode.getId() ||
towardsSource.getTargetNodeId() === lastLeftNode.getId()) {
if (
towardsSource.getSourceNodeId() === lastLeftNode.getId() ||
towardsSource.getTargetNodeId() === lastLeftNode.getId()
) {
leftSection = towardsSource;
rightSection = towradsTarget;
}
return {
leftSection: leftSection,
rightSection: rightSection,
lastLeftNode: lastLeftNode,
lastRightNode: lastRightNode
lastRightNode: lastRightNode,
};
}

Expand Down Expand Up @@ -172,12 +175,20 @@ export class TrainrunsectionHelper {
const lastLeftNode = this.getLeftNode(trainrunSection, orderedNodes);
const lastRightNode = this.getRightNode(trainrunSection, orderedNodes);

const towardsSource = this.trainrunService.getLastNonStopTrainrunSection(trainrunSection.getSourceNode(), trainrunSection);
const towradsTarget = this.trainrunService.getLastNonStopTrainrunSection(trainrunSection.getTargetNode(), trainrunSection);
const towardsSource = this.trainrunService.getLastNonStopTrainrunSection(
trainrunSection.getSourceNode(),
trainrunSection,
);
const towradsTarget = this.trainrunService.getLastNonStopTrainrunSection(
trainrunSection.getTargetNode(),
trainrunSection,
);
let leftSection = towradsTarget;
let rightSection = towardsSource;
if (towardsSource.getSourceNodeId() === lastLeftNode.getId() ||
towardsSource.getTargetNodeId() === lastLeftNode.getId()) {
if (
towardsSource.getSourceNodeId() === lastLeftNode.getId() ||
towardsSource.getTargetNodeId() === lastLeftNode.getId()
) {
leftSection = towardsSource;
rightSection = towradsTarget;
}
Expand All @@ -186,15 +197,15 @@ export class TrainrunsectionHelper {
leftLock:
leftSection.getSourceNodeId() === lastLeftNode.getId()
? leftSection.getSourceArrivalLock() ||
leftSection.getSourceDepartureLock()
leftSection.getSourceDepartureLock()
: leftSection.getTargetArrivalLock() ||
leftSection.getTargetDepartureLock(),
leftSection.getTargetDepartureLock(),
rightLock:
rightSection.getSourceNodeId() === lastRightNode.getId()
? rightSection.getSourceArrivalLock() ||
rightSection.getSourceDepartureLock()
rightSection.getSourceDepartureLock()
: rightSection.getTargetArrivalLock() ||
rightSection.getTargetDepartureLock(),
rightSection.getTargetDepartureLock(),
travelTimeLock: trainrunSection.getTravelTimeLock(),
};
}
Expand Down Expand Up @@ -362,4 +373,14 @@ export class TrainrunsectionHelper {
}
return rightNode;
}

static checkAndAdjustSymmetry(
selectedTrainrunSection: TrainrunSection,
firstTime: number,
secondTime: number,
) {
if (selectedTrainrunSection.getTrainrun().getIsSymmetric()) {
firstTime = TrainrunsectionHelper.getSymmetricTime(secondTime);
}
}
}

0 comments on commit 95aab7e

Please sign in to comment.