mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-16 23:40:32 +00:00
Add logic to fire the SHUTTLE_WILL_LEAVE_STOP event
This commit is contained in:
@@ -398,13 +398,24 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt
|
|||||||
shuttle: IShuttle,
|
shuttle: IShuttle,
|
||||||
travelTimeTimestamp = Date.now(),
|
travelTimeTimestamp = Date.now(),
|
||||||
) {
|
) {
|
||||||
|
const isAtStop = await this.checkIfShuttleIsAtStop(shuttle.id);
|
||||||
const arrivedStop = await this.getArrivedStopIfExists(shuttle);
|
const arrivedStop = await this.getArrivedStopIfExists(shuttle);
|
||||||
|
|
||||||
|
// Will not fire *any* events if the same stop
|
||||||
|
const lastStop = await this.getShuttleLastStopArrival(shuttle.id);
|
||||||
|
if (lastStop?.stopId === arrivedStop?.id) return;
|
||||||
|
|
||||||
|
if (isAtStop) {
|
||||||
|
if (lastStop) {
|
||||||
|
this.emit(ShuttleRepositoryEvent.SHUTTLE_WILL_LEAVE_STOP, {
|
||||||
|
stopArrivalThatShuttleIsLeaving: lastStop,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
await this.markShuttleAsNotAtStop(shuttle.id);
|
||||||
|
}
|
||||||
|
|
||||||
if (arrivedStop) {
|
if (arrivedStop) {
|
||||||
// stop if same stop
|
// stop if same stop
|
||||||
const lastStop = await this.getShuttleLastStopArrival(shuttle.id);
|
|
||||||
if (lastStop?.stopId === arrivedStop.id) return;
|
|
||||||
|
|
||||||
const shuttleArrival = {
|
const shuttleArrival = {
|
||||||
stopId: arrivedStop.id,
|
stopId: arrivedStop.id,
|
||||||
timestamp: new Date(travelTimeTimestamp),
|
timestamp: new Date(travelTimeTimestamp),
|
||||||
|
|||||||
@@ -3,13 +3,13 @@ import { ShuttleGetterSetterRepository } from "./ShuttleGetterSetterRepository";
|
|||||||
import { IOrderedStop, IRoute, IShuttle, IStop, shuttleHasArrivedAtStop } from "../../entities/ShuttleRepositoryEntities";
|
import { IOrderedStop, IRoute, IShuttle, IStop, shuttleHasArrivedAtStop } from "../../entities/ShuttleRepositoryEntities";
|
||||||
import { IEntityWithId } from "../../entities/SharedEntities";
|
import { IEntityWithId } from "../../entities/SharedEntities";
|
||||||
import {
|
import {
|
||||||
ShuttleRepositoryEvent,
|
ShuttleRepositoryEvent,
|
||||||
ShuttleRepositoryEventListener,
|
ShuttleRepositoryEventListener,
|
||||||
ShuttleRepositoryEventName,
|
ShuttleRepositoryEventName,
|
||||||
ShuttleRepositoryEventPayloads,
|
ShuttleRepositoryEventPayloads,
|
||||||
ShuttleStopArrival,
|
ShuttleStopArrival,
|
||||||
ShuttleTravelTimeDataIdentifier,
|
ShuttleTravelTimeDataIdentifier,
|
||||||
ShuttleTravelTimeDateFilterArguments,
|
ShuttleTravelTimeDateFilterArguments,
|
||||||
} from "./ShuttleGetterRepository";
|
} from "./ShuttleGetterRepository";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -74,6 +74,7 @@ export class UnoptimizedInMemoryShuttleRepository
|
|||||||
private orderedStops: IOrderedStop[] = [];
|
private orderedStops: IOrderedStop[] = [];
|
||||||
private shuttleLastStopArrivals: Map<string, ShuttleStopArrival> = new Map();
|
private shuttleLastStopArrivals: Map<string, ShuttleStopArrival> = new Map();
|
||||||
private travelTimeData: Map<string, Array<{ timestamp: number; seconds: number }>> = new Map();
|
private travelTimeData: Map<string, Array<{ timestamp: number; seconds: number }>> = new Map();
|
||||||
|
private shuttlesAtStop: Set<string> = new Set();
|
||||||
|
|
||||||
public async getStops(): Promise<IStop[]> {
|
public async getStops(): Promise<IStop[]> {
|
||||||
return this.stops;
|
return this.stops;
|
||||||
@@ -174,13 +175,24 @@ export class UnoptimizedInMemoryShuttleRepository
|
|||||||
shuttle: IShuttle,
|
shuttle: IShuttle,
|
||||||
travelTimeTimestamp = Date.now(),
|
travelTimeTimestamp = Date.now(),
|
||||||
) {
|
) {
|
||||||
|
const isAtStop = await this.checkIfShuttleIsAtStop(shuttle.id);
|
||||||
const arrivedStop = await this.getArrivedStopIfExists(shuttle);
|
const arrivedStop = await this.getArrivedStopIfExists(shuttle);
|
||||||
|
|
||||||
if (arrivedStop != undefined) {
|
// Will not fire *any* events if the same stop
|
||||||
// stop if same stop
|
const lastStop = await this.getShuttleLastStopArrival(shuttle.id);
|
||||||
const lastStop = await this.getShuttleLastStopArrival(shuttle.id);
|
if (lastStop?.stopId === arrivedStop?.id) return;
|
||||||
if (lastStop?.stopId === arrivedStop.id) return;
|
|
||||||
|
|
||||||
|
if (isAtStop) {
|
||||||
|
if (lastStop) {
|
||||||
|
this.emit(ShuttleRepositoryEvent.SHUTTLE_WILL_LEAVE_STOP, {
|
||||||
|
stopArrivalThatShuttleIsLeaving: lastStop,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
await this.markShuttleAsNotAtStop(shuttle.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (arrivedStop) {
|
||||||
|
// stop if same stop
|
||||||
const shuttleArrival = {
|
const shuttleArrival = {
|
||||||
stopId: arrivedStop.id,
|
stopId: arrivedStop.id,
|
||||||
timestamp: new Date(travelTimeTimestamp),
|
timestamp: new Date(travelTimeTimestamp),
|
||||||
@@ -190,10 +202,23 @@ export class UnoptimizedInMemoryShuttleRepository
|
|||||||
lastStopArrival: lastStop,
|
lastStopArrival: lastStop,
|
||||||
willArriveAt: shuttleArrival,
|
willArriveAt: shuttleArrival,
|
||||||
});
|
});
|
||||||
|
await this.markShuttleAsAtStop(shuttleArrival.shuttleId);
|
||||||
await this.updateShuttleLastStopArrival(shuttleArrival);
|
await this.updateShuttleLastStopArrival(shuttleArrival);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async markShuttleAsAtStop(shuttleId: string) {
|
||||||
|
this.shuttlesAtStop.add(shuttleId);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async markShuttleAsNotAtStop(shuttleId: string) {
|
||||||
|
this.shuttlesAtStop.delete(shuttleId);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async checkIfShuttleIsAtStop(shuttleId: string) {
|
||||||
|
return this.shuttlesAtStop.has(shuttleId);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private async updateShuttleLastStopArrival(lastStopArrival: ShuttleStopArrival) {
|
private async updateShuttleLastStopArrival(lastStopArrival: ShuttleStopArrival) {
|
||||||
this.shuttleLastStopArrivals.set(lastStopArrival.shuttleId, lastStopArrival);
|
this.shuttleLastStopArrivals.set(lastStopArrival.shuttleId, lastStopArrival);
|
||||||
@@ -267,6 +292,7 @@ export class UnoptimizedInMemoryShuttleRepository
|
|||||||
const shuttle = await this.removeEntityByIdIfExists(shuttleId, this.shuttles);
|
const shuttle = await this.removeEntityByIdIfExists(shuttleId, this.shuttles);
|
||||||
if (shuttle != null) {
|
if (shuttle != null) {
|
||||||
this.emit(ShuttleRepositoryEvent.SHUTTLE_REMOVED, shuttle);
|
this.emit(ShuttleRepositoryEvent.SHUTTLE_REMOVED, shuttle);
|
||||||
|
this.shuttlesAtStop.delete(shuttleId);
|
||||||
await this.removeShuttleLastStopIfExists(shuttleId);
|
await this.removeShuttleLastStopIfExists(shuttleId);
|
||||||
}
|
}
|
||||||
return shuttle;
|
return shuttle;
|
||||||
@@ -289,6 +315,7 @@ export class UnoptimizedInMemoryShuttleRepository
|
|||||||
|
|
||||||
public async clearShuttleData(): Promise<void> {
|
public async clearShuttleData(): Promise<void> {
|
||||||
this.shuttles = [];
|
this.shuttles = [];
|
||||||
|
this.shuttlesAtStop.clear();
|
||||||
await this.clearShuttleLastStopData();
|
await this.clearShuttleLastStopData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user