Skip to content

Commit

Permalink
fix: Consider fleet effects in travel jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
Clashsoft committed Aug 10, 2024
1 parent b541bdd commit bf06598
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/job/job.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export class JobService extends MongooseRepository<Job> {
if (!fleet || !ships) {
continue;
}
const slowestShipSpeed = this.systemLogicService.getSlowestShipSpeed(ships, empire);
const slowestShipSpeed = this.systemLogicService.getSlowestShipSpeed(fleet, ships, empire);

let linkTimeSum = 0;
for (let i = 1; i < job.path.length; i++) {
Expand Down
12 changes: 5 additions & 7 deletions src/system/system-logic.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export class SystemLogicService {
}

getTravelTime(paths: SystemDocument[], fleet: FleetDocument, ships: ShipDocument[], empire: EmpireDocument): number {
const slowestShipSpeed = this.getSlowestShipSpeed(ships, empire);
const slowestShipSpeed = this.getSlowestShipSpeed(fleet, ships, empire);
let totalTravelTime = 0;
for (let i = 1; i < paths.length; i++) {
const fromSystem = paths[i - 1];
Expand All @@ -283,16 +283,14 @@ export class SystemLogicService {
return linkTime !== undefined ? linkTime / slowestShipSpeed : null;
}

getSlowestShipSpeed(ships: ShipDocument[], empire: EmpireDocument): number {
getSlowestShipSpeed(fleet: FleetDocument, ships: ShipDocument[], empire: EmpireDocument): number {
if (!ships || ships.length === 0) {
throw new NotFoundException('No ships in the fleet.');
}
let slowestSpeed = Infinity;
for (const ship of ships) {
const variableKey = `ships.${ship.type}.speed` as keyof typeof speedVariables;
const speedVariables: Partial<Record<Variable, number>> = {[variableKey]: SHIP_TYPES[ship.type].speed};
calculateVariables(speedVariables, empire);
const calculatedSpeed = speedVariables[variableKey];
const shipTypes = new Set(ships.map(s => s.type));
for (const shipType of shipTypes) {
const calculatedSpeed = calculateVariable(`ships.${shipType}.speed`, empire, fleet);
if (calculatedSpeed !== undefined && calculatedSpeed < slowestSpeed) {
slowestSpeed = calculatedSpeed;
}
Expand Down

0 comments on commit bf06598

Please sign in to comment.