1
- import { Injectable } from '@nestjs/common' ;
1
+ import { HttpStatus , Injectable } from '@nestjs/common' ;
2
2
import { EmpireService } from '../empire/empire.service' ;
3
3
import { SystemService } from '../system/system.service' ;
4
4
import { EmpireDocument } from '../empire/empire.schema' ;
@@ -8,9 +8,7 @@ import {Variable} from './types';
8
8
import { RESOURCE_NAMES , ResourceName } from './resources' ;
9
9
import { AggregateResult } from './aggregates' ;
10
10
import { Game } from '../game/game.schema' ;
11
- import { HOMESYSTEM_BUILDINGS , HOMESYSTEM_DISTRICT_COUNT , HOMESYSTEM_DISTRICTS } from './constants' ;
12
11
import { MemberService } from '../member/member.service' ;
13
- import { SYSTEM_UPGRADES } from './system-upgrade' ;
14
12
import { JobService } from '../job/job.service' ;
15
13
import { JobDocument } from '../job/job.schema' ;
16
14
import { SystemLogicService } from '../system/system-logic.service' ;
@@ -22,7 +20,7 @@ import {Ship, ShipDocument} from '../ship/ship.schema';
22
20
import { War , WarDocument } from '../war/war.schema' ;
23
21
import { Types } from 'mongoose' ;
24
22
import { BUILDINGS } from './buildings' ;
25
- import { Member } from '../member/member.schema ' ;
23
+ import { JobType } from '../job/job-type.enum ' ;
26
24
27
25
@Injectable ( )
28
26
export class GameLogicService {
@@ -98,7 +96,7 @@ export class GameLogicService {
98
96
await this . jobService . deleteMany ( { game : game . _id , $expr : { $gte : [ '$progress' , '$total' ] } } ) ;
99
97
const jobs = await this . jobService . findAll ( { game : game . _id } , { sort : { priority : 1 , createdAt : 1 } } ) ;
100
98
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 ) ;
102
100
103
101
await this . empireService . saveAll ( empires ) ;
104
102
await this . systemService . saveAll ( systems ) ;
@@ -372,7 +370,7 @@ export class GameLogicService {
372
370
system . population = population + growth ;
373
371
}
374
372
375
- private async updateFleets (
373
+ private updateFleets (
376
374
empires : EmpireDocument [ ] ,
377
375
systems : SystemDocument [ ] ,
378
376
fleets : FleetDocument [ ] ,
@@ -437,6 +435,8 @@ export class GameLogicService {
437
435
if ( bestShip . health < 0 ) {
438
436
bestShip . health = 0 ;
439
437
}
438
+ this . cancelTravelJobs ( ship . fleet , jobs ) ;
439
+ this . cancelTravelJobs ( bestShip . fleet , jobs ) ;
440
440
} else if ( system && system . owner ) { // Unowned systems are never attacked.
441
441
const variables = fleetVariables [ ship . fleet . toString ( ) ] ;
442
442
if ( system . owner . equals ( ship . empire ) ) {
@@ -449,6 +449,7 @@ export class GameLogicService {
449
449
} else {
450
450
// Attack the system
451
451
this . attackSystem ( ship , system , systemDefense [ systemId ] ?? 1 , variables , jobs ) ;
452
+ this . cancelTravelJobs ( ship . fleet , jobs ) ;
452
453
}
453
454
}
454
455
}
@@ -562,6 +563,19 @@ export class GameLogicService {
562
563
ship . health = Math . min ( maxHealth , ship . health + shipyardHeal ) ;
563
564
}
564
565
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
+
565
579
private async deleteDestroyedShipsAndFleets ( fleets : FleetDocument [ ] , ships : ShipDocument [ ] ) {
566
580
const deleteShips = ships . filter ( s => ! s . health ) ;
567
581
await this . shipService . deleteAll ( deleteShips ) ;
0 commit comments