Skip to content
Merged

#43 #62

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async function bootstrap(): Promise<void> {
url: 'https://github.com/Space-Game-Engine/Warp-Core/blob/main/docs/install/installation.md',
description: 'GitHub installation instructions',
})
.ad.build();
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup(localDocUrl, app, document);

Expand Down
16 changes: 14 additions & 2 deletions src/user/resources/resources.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,33 @@
public resource(
@Args('id', {type: () => ID}) id: string,
): Promise<HabitatResourceCombined | null> {
return this.resourcesService.getSingleResourceById(id);
return this.resourcesService.getSingleResourceById(
this.habitatModel.id,
id,
);
}

@Query(() => [HabitatResourceCombined], {
description: 'Returns all resource types',
name: 'resource_getAll',
})
public allResources(): Promise<HabitatResourceCombined[]> {
return this.resourcesService.getAllResourcesForHabitat();
return this.resourcesService.getAllResourcesForHabitat(
this.habitatModel.id,
);
}

@ResolveField()
public habitat(
@Parent() resource: HabitatResourceCombined,

Check warning on line 39 in src/user/resources/resources.resolver.ts

View workflow job for this annotation

GitHub Actions / test (20.x)

'resource' is defined but never used
): AuthorizedHabitatModel {
return this.habitatModel;
}

@ResolveField('productionRate', () => Number)
public async productionRate(
@Parent() habitatResource: HabitatResourceCombined,
): Promise<number> {
return this.resourcesService.getProductionRateForResource(habitatResource);
}
}
85 changes: 28 additions & 57 deletions src/user/resources/service/resources.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,66 @@
import {Test, TestingModule} from '@nestjs/testing';
import {when} from 'jest-when';

import {AuthorizedHabitatModel} from '@warp-core/auth';
import {HabitatResourceCombined} from '@warp-core/database/model/habitat-resource.mapped.model';
import {HabitatResourceModel} from '@warp-core/database/model/habitat-resource.model';
import {HabitatModel} from '@warp-core/database/model/habitat.model';
import {ResourceModel} from '@warp-core/database/model/resource.model';
import {HabitatResourceRepository} from '@warp-core/database/repository/habitat-resource.repository';
import {CalculationMechanic} from '@warp-core/user/resources/service/calculate/resource-calculation/calculation-mechanic.interface';
import {ResourcesService} from '@warp-core/user/resources/service/resources.service';

jest.mock('@warp-core/database/repository/habitat-resource.repository');
jest.mock('@warp-core/auth/payload/model/habitat.model');
jest.mock(
'@warp-core/user/resources/service/calculate/resource-calculation/calculation-mechanic.interface',
);

describe('Resources service', () => {
let resourcesService: ResourcesService;
let habitatResourceRepository: jest.Mocked<HabitatResourceRepository>;
let authorizedHabitatModel: AuthorizedHabitatModel;
let calculationMechanic: jest.Mocked<CalculationMechanic>;

Check warning on line 20 in src/user/resources/service/resources.service.spec.ts

View workflow job for this annotation

GitHub Actions / test (20.x)

'calculationMechanic' is assigned a value but never used

beforeEach(async () => {
jest.clearAllMocks();
const module: TestingModule = await Test.createTestingModule({
providers: [
ResourcesService,
HabitatResourceRepository,
AuthorizedHabitatModel,
{
provide: CalculationMechanic,
useValue: {
getProductionRate: jest.fn(),
},
},
],
}).compile();

resourcesService = module.get<ResourcesService>(ResourcesService);
habitatResourceRepository = module.get(HabitatResourceRepository);
authorizedHabitatModel = module.get(AuthorizedHabitatModel);
calculationMechanic = module.get(CalculationMechanic);
});

describe('getSingleResourceById', () => {
it('should return null when habitat resource was not found', async () => {
const resourceId = 'wood';
authorizedHabitatModel.id = 5;
const habitatId = 5;

when(habitatResourceRepository.findOneBy)
.calledWith(
expect.objectContaining({
habitatId: authorizedHabitatModel.id,
resourceId: resourceId,
habitatId,
resourceId,
}),
)
.mockResolvedValue(null);

expect(
resourcesService.getSingleResourceById(resourceId),
return expect(
resourcesService.getSingleResourceById(habitatId, resourceId),
).resolves.toBeNull();
});

it('should return mapped resource type when resource was found', async () => {
const resourceId = 'wood';
authorizedHabitatModel.id = 5;
const habitatId = 5;

const resourceModel = {
id: resourceId,
Expand All @@ -65,57 +72,21 @@
when(habitatResourceRepository.findOneBy)
.calledWith(
expect.objectContaining({
habitatId: authorizedHabitatModel.id,
resourceId: resourceId,
habitatId,
resourceId,
}),
)
.mockResolvedValue(habitatResourceModel);

expect(
resourcesService.getSingleResourceById(resourceId),
return expect(
resourcesService.getSingleResourceById(habitatId, resourceId),
).resolves.toBeInstanceOf(HabitatResourceCombined);
});
});

describe('getAllResourcesForHabitat', () => {
it('should return array of combined habitat resource models for authorized habitat', async () => {
authorizedHabitatModel.id = 5;

const habitatResourceArray = [
{
id: '1',
resource: {
id: 'wood',
},
},
{
id: '2',
resource: {
id: 'steel',
},
},
{
id: '3',
resource: {
id: 'stone',
},
},
] as HabitatResourceModel[];

when(habitatResourceRepository.findBy)
.calledWith({
habitatId: 5,
})
.mockResolvedValue(habitatResourceArray);

expect(
await resourcesService.getAllResourcesForHabitat(),
).toMatchObject<HabitatResourceCombined>(expect.anything());
});

it('should return array of combined habitat resource models for provided habitat', async () => {
const habitatModel = new HabitatModel();
habitatModel.id = 10;
it('should return array of combined habitat resource models for habitat', async () => {
const habitatId = 5;

const habitatResourceArray = [
{
Expand All @@ -140,13 +111,13 @@

when(habitatResourceRepository.findBy)
.calledWith({
habitatId: 10,
habitatId,
})
.mockResolvedValue(habitatResourceArray);

expect(
await resourcesService.getAllResourcesForHabitat(habitatModel),
).toMatchObject<HabitatResourceCombined>(expect.anything());
return expect(
resourcesService.getAllResourcesForHabitat(habitatId),
).resolves.toMatchObject<HabitatResourceCombined>(expect.anything());
});
});
});
54 changes: 34 additions & 20 deletions src/user/resources/service/resources.service.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
import {Injectable} from '@nestjs/common';

import {AuthorizedHabitatModel} from '@warp-core/auth';
import {HabitatResourceCombined} from '@warp-core/database/model/habitat-resource.mapped.model';
import {HabitatResourceModel} from '@warp-core/database/model/habitat-resource.model';
import {HabitatModel} from '@warp-core/database/model/habitat.model';
import {HabitatResourceRepository} from '@warp-core/database/repository/habitat-resource.repository';
import {CalculationMechanic} from '@warp-core/user/resources/service/calculate/resource-calculation/calculation-mechanic.interface';

@Injectable()
export class ResourcesService {
constructor(
private readonly habitatResourceRepository: HabitatResourceRepository,
private readonly habitatModel: AuthorizedHabitatModel,
private readonly calculationMechanic: CalculationMechanic,
) {}

public async getProductionRateForResource(
habitatResource: HabitatResourceCombined,
): Promise<number> {
const resource = await this.getHabitatResource(
habitatResource.habitatId,
habitatResource.id,
);

if (!resource) {
return 0;
}

return this.calculationMechanic.getProductionRate(resource);
}

public async getSingleResourceById(
id: string,
habitatId: number,
resourceId: string,
): Promise<HabitatResourceCombined | null> {
const habitatResource = await this.habitatResourceRepository.findOneBy({
habitatId: this.habitatModel.id,
resourceId: id,
});
const habitatResource = await this.getHabitatResource(
habitatId,
resourceId,
);

if (!habitatResource) {
return null;
Expand All @@ -29,19 +44,8 @@ export class ResourcesService {
}

public async getAllResourcesForHabitat(
habitatModelOrId: HabitatModel | number | null = null,
habitatId: number,
): Promise<HabitatResourceCombined[]> {
let habitatId: number;
if (habitatModelOrId) {
if (habitatModelOrId instanceof HabitatModel) {
habitatId = habitatModelOrId.id;
} else {
habitatId = habitatModelOrId;
}
} else {
habitatId = this.habitatModel.id;
}

const habitatResources = await this.habitatResourceRepository.findBy({
habitatId,
});
Expand All @@ -68,4 +72,14 @@ export class ResourcesService {

return mappedResource;
}

private getHabitatResource(
habitatId: number,
resourceId: string,
): Promise<HabitatResourceModel | null> {
return this.habitatResourceRepository.findOneBy({
habitatId,
resourceId,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('Habitat Creation when onStart config contains buildings and resources'
.query({
root: 'resource_getAll',
fields: {
fields: ['id', 'currentAmount'],
fields: ['id', 'currentAmount', 'productionRate'],
},
})
.send()
Expand All @@ -77,14 +77,29 @@ describe('Habitat Creation when onStart config contains buildings and resources'
resourceId: 'wood',
value: 10,
});
expect(resourcesFromResponse).toHaveResourceWithCustomProperty({
resourceId: 'wood',
property: 'productionRate',
value: 1,
});
expect(resourcesFromResponse).toHaveResourceWithValue({
resourceId: 'stone_granite',
value: 20,
});
expect(resourcesFromResponse).toHaveResourceWithCustomProperty({
resourceId: 'stone_granite',
property: 'productionRate',
value: 0,
});
expect(resourcesFromResponse).toHaveResourceWithValue({
resourceId: 'coal',
value: 10,
});
expect(resourcesFromResponse).toHaveResourceWithCustomProperty({
resourceId: 'coal',
property: 'productionRate',
value: 0,
});
});

it('should have pre-build buildings on building zones', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,25 @@ describe('Habitat Creation when onStart config contains buildings', () => {
.query({
root: 'resource_getAll',
fields: {
fields: ['id', 'currentAmount'],
fields: ['id', 'currentAmount', 'productionRate'],
},
})
.send()
.expect(HttpStatus.OK);

response.body.data.resource_getAll.forEach(
const resourcesFromResponse = response.body.data.resource_getAll;

resourcesFromResponse.forEach(
(singleResource: Partial<HabitatResourceCombined>) => {
expect(singleResource.currentAmount).toEqual(0);
},
);

expect(resourcesFromResponse).toHaveResourceWithCustomProperty({
resourceId: 'wood',
property: 'productionRate',
value: 1,
});
});

it('should have pre-build buildings on building zones', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe('Habitat Creation when onStart config contains resources', () => {
.query({
root: 'resource_getAll',
fields: {
fields: ['id', 'currentAmount'],
fields: ['id', 'currentAmount', 'productionRate'],
},
})
.send()
Expand All @@ -102,13 +102,28 @@ describe('Habitat Creation when onStart config contains resources', () => {
resourceId: 'wood',
value: 100,
});
expect(resourcesFromResponse).toHaveResourceWithCustomProperty({
resourceId: 'wood',
property: 'productionRate',
value: 0,
});
expect(resourcesFromResponse).toHaveResourceWithValue({
resourceId: 'stone_granite',
value: 200,
});
expect(resourcesFromResponse).toHaveResourceWithCustomProperty({
resourceId: 'stone_granite',
property: 'productionRate',
value: 0,
});
expect(resourcesFromResponse).toHaveResourceWithValue({
resourceId: 'coal',
value: 50,
});
expect(resourcesFromResponse).toHaveResourceWithCustomProperty({
resourceId: 'coal',
property: 'productionRate',
value: 0,
});
});
});
Loading
Loading