Skip to content

Commit 53aac72

Browse files
committed
fix: Cancel travel jobs when fleets enter combat
1 parent 2ba0fd8 commit 53aac72

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/game-logic/game-logic.service.ts

+20-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Injectable} from '@nestjs/common';
1+
import {HttpStatus, Injectable} from '@nestjs/common';
22
import {EmpireService} from '../empire/empire.service';
33
import {SystemService} from '../system/system.service';
44
import {EmpireDocument} from '../empire/empire.schema';
@@ -8,9 +8,7 @@ import {Variable} from './types';
88
import {RESOURCE_NAMES, ResourceName} from './resources';
99
import {AggregateResult} from './aggregates';
1010
import {Game} from '../game/game.schema';
11-
import {HOMESYSTEM_BUILDINGS, HOMESYSTEM_DISTRICT_COUNT, HOMESYSTEM_DISTRICTS} from './constants';
1211
import {MemberService} from '../member/member.service';
13-
import {SYSTEM_UPGRADES} from './system-upgrade';
1412
import {JobService} from '../job/job.service';
1513
import {JobDocument} from '../job/job.schema';
1614
import {SystemLogicService} from '../system/system-logic.service';
@@ -22,7 +20,7 @@ import {Ship, ShipDocument} from '../ship/ship.schema';
2220
import {War, WarDocument} from '../war/war.schema';
2321
import {Types} from 'mongoose';
2422
import {BUILDINGS} from './buildings';
25-
import {Member} from '../member/member.schema';
23+
import {JobType} from '../job/job-type.enum';
2624

2725
@Injectable()
2826
export class GameLogicService {
@@ -98,7 +96,7 @@ export class GameLogicService {
9896
await this.jobService.deleteMany({game: game._id, $expr: {$gte: ['$progress', '$total']}});
9997
const jobs = await this.jobService.findAll({game: game._id}, {sort: {priority: 1, createdAt: 1}});
10098
await this.updateEmpires(empires, systems, jobs, ships);
101-
await this.updateFleets(empires, systems, fleets, ships, wars, jobs);
99+
this.updateFleets(empires, systems, fleets, ships, wars, jobs);
102100

103101
await this.empireService.saveAll(empires);
104102
await this.systemService.saveAll(systems);
@@ -372,7 +370,7 @@ export class GameLogicService {
372370
system.population = population + growth;
373371
}
374372

375-
private async updateFleets(
373+
private updateFleets(
376374
empires: EmpireDocument[],
377375
systems: SystemDocument[],
378376
fleets: FleetDocument[],
@@ -437,6 +435,8 @@ export class GameLogicService {
437435
if (bestShip.health < 0) {
438436
bestShip.health = 0;
439437
}
438+
this.cancelTravelJobs(ship.fleet, jobs);
439+
this.cancelTravelJobs(bestShip.fleet, jobs);
440440
} else if (system && system.owner) { // Unowned systems are never attacked.
441441
const variables = fleetVariables[ship.fleet.toString()];
442442
if (system.owner.equals(ship.empire)) {
@@ -449,6 +449,7 @@ export class GameLogicService {
449449
} else {
450450
// Attack the system
451451
this.attackSystem(ship, system, systemDefense[systemId] ?? 1, variables, jobs);
452+
this.cancelTravelJobs(ship.fleet, jobs);
452453
}
453454
}
454455
}
@@ -562,6 +563,19 @@ export class GameLogicService {
562563
ship.health = Math.min(maxHealth, ship.health + shipyardHeal);
563564
}
564565

566+
private cancelTravelJobs(fleet: Types.ObjectId, jobs: JobDocument[]) {
567+
for (const job of jobs) {
568+
if (job.type === JobType.TRAVEL && fleet.equals(job.fleet) && job.progress < job.total) {
569+
job.progress = job.total;
570+
job.result = {
571+
statusCode: HttpStatus.CONFLICT,
572+
error: 'Conflict',
573+
message: 'Travel cancelled because the fleet entered a battle',
574+
};
575+
}
576+
}
577+
}
578+
565579
private async deleteDestroyedShipsAndFleets(fleets: FleetDocument[], ships: ShipDocument[]) {
566580
const deleteShips = ships.filter(s => !s.health);
567581
await this.shipService.deleteAll(deleteShips);

0 commit comments

Comments
 (0)