Skip to content

Commit 080897f

Browse files
committed
fix: Make sure resources are properly defined for new empires
1 parent bcfd2d2 commit 080897f

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

src/empire/empire-logic.service.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import {Empire, EmpireDocument} from './empire.schema';
33
import {TECH_CATEGORIES, TECHNOLOGIES} from '../game-logic/technologies';
44
import {notFound} from '@mean-stream/nestx';
55
import {ShipType, Technology, TechnologyTag, Variable} from '../game-logic/types';
6-
import {calculateVariable, calculateVariables, flatten, getVariables} from '../game-logic/variables';
6+
import {
7+
calculateVariable,
8+
calculateVariables,
9+
EmpireEffectSources,
10+
flatten,
11+
getVariables,
12+
} from '../game-logic/variables';
713
import {RESOURCE_NAMES, ResourceName} from '../game-logic/resources';
814
import {AggregateResult} from '../game-logic/aggregates';
915
import {EMPIRE_VARIABLES} from '../game-logic/empire-variables';
@@ -56,12 +62,9 @@ export class EmpireLogicService {
5662
empire.markModified('resources');
5763
}
5864

59-
getInitialResources(empire: EmpireTemplate): Record<ResourceName, number> {
65+
getInitialResources(empire: EmpireEffectSources): Record<ResourceName, number> {
6066
const resourceVariables: Record<Variable, number> = getVariables('resources');
61-
calculateVariables(resourceVariables, {
62-
traits: empire.traits,
63-
technologies: [],
64-
});
67+
calculateVariables(resourceVariables, empire);
6568
const resources: any = {};
6669
for (const resource of RESOURCE_NAMES) {
6770
resources[resource] = resourceVariables[`resources.${resource}.starting`];

src/empire/empire.controller.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import {Body, Controller, ForbiddenException, Get, Param, ParseBoolPipe, Patch, Post, Query} from '@nestjs/common';
22
import {
33
ApiCreatedResponse,
4-
ApiForbiddenResponse, ApiNotFoundResponse,
4+
ApiForbiddenResponse,
5+
ApiNotFoundResponse,
56
ApiOkResponse,
67
ApiOperation,
78
ApiQuery,
@@ -17,8 +18,8 @@ import {Empire} from './empire.schema';
1718
import {EmpireService} from './empire.service';
1819
import {notFound, NotFound, ObjectIdPipe} from '@mean-stream/nestx';
1920
import {Types} from 'mongoose';
20-
import {MemberService} from '../member/member.service';
2121
import {GameService} from '../game/game.service';
22+
import {EmpireLogicService} from './empire-logic.service';
2223

2324
@Controller('games/:game/empires')
2425
@ApiTags('Game Empires')
@@ -28,8 +29,8 @@ import {GameService} from '../game/game.service';
2829
export class EmpireController {
2930
constructor(
3031
private readonly gameService: GameService,
31-
private readonly memberService: MemberService,
3232
private readonly empireService: EmpireService,
33+
private readonly empireLogicService: EmpireLogicService,
3334
) {
3435
}
3536

@@ -50,6 +51,11 @@ export class EmpireController {
5051
...dto,
5152
game,
5253
user: currentUser._id,
54+
// Although dto may specify the resources, there might be some missing. Ensure it is a complete record.
55+
resources: {
56+
...this.empireLogicService.getInitialResources(dto),
57+
...dto.resources,
58+
},
5359
});
5460
}
5561

src/empire/empire.service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class EmpireService extends MongooseRepository<Empire> {
7070
game: member.game,
7171
user: member.user,
7272
technologies: [],
73-
resources: this.empireLogicService.getInitialResources(member.empire!),
73+
resources: this.empireLogicService.getInitialResources({traits: member.empire!.traits, technologies: []}),
7474
homeSystem: undefined, // set in a later stage of game initialization
7575
});
7676
}));

0 commit comments

Comments
 (0)