diff --git a/docs/Changelog.md b/docs/Changelog.md index 77e583e..40470e2 100644 --- a/docs/Changelog.md +++ b/docs/Changelog.md @@ -127,3 +127,13 @@ * Fixed a wrong population delta value in the system resource aggregate. * Fixed invalid pop growth when a system with capacity 0 is somehow colonized. + +# v1.3.3 (2024-06-20) + +## Improvements + +* Creating a new game now pauses all other games of the same owner. + +## Documentation + +* Improved the `403 Forbidden` error documentation for updating a running game. diff --git a/src/game-logic/game-logic.service.ts b/src/game-logic/game-logic.service.ts index d4f5da3..334a6c6 100644 --- a/src/game-logic/game-logic.service.ts +++ b/src/game-logic/game-logic.service.ts @@ -13,6 +13,7 @@ import {notFound} from '@mean-stream/nestx'; import {Game} from '../game/game.schema'; import {HOMESYSTEM_BUILDINGS, HOMESYSTEM_DISTRICT_COUNT, HOMESYSTEM_DISTRICTS} from './constants'; import {MemberService} from '../member/member.service'; +import {SYSTEM_UPGRADES} from './system-upgrade'; @Injectable() export class GameLogicService { diff --git a/src/game/game.controller.ts b/src/game/game.controller.ts index bfa15e1..fbb28ae 100644 --- a/src/game/game.controller.ts +++ b/src/game/game.controller.ts @@ -74,8 +74,8 @@ export class GameController { @NotFound() @ApiOperation({description: 'Change a game as owner.'}) @ApiOkResponse({type: Game}) - @ApiConflictResponse({description: 'Game is already running.'}) - @ApiForbiddenResponse({description: 'Attempt to change a game that the current user does not own.'}) + @ApiConflictResponse({description: 'Cannot change a running game (only `speed` is allowed).'}) + @ApiForbiddenResponse({description: 'Only the owner can change the game.'}) @ApiQuery({ name: 'tick', description: 'Advance the game by one period and run all empire and system calculations.', @@ -92,8 +92,8 @@ export class GameController { if (!user._id.equals(existing.owner)) { throw new ForbiddenException('Only the owner can change the game.'); } - if (existing.started && !(Object.keys(dto).length === 1 && dto.speed !== undefined)) { - throw new ConflictException('Cannot change a running game.'); + if (existing.started && !(Object.keys(dto).every(key => key === 'speed'))) { + throw new ConflictException('Cannot change a running game (only `speed` is allowed).'); } const update: UpdateQuery = dto; if (tick) { diff --git a/src/game/game.handler.ts b/src/game/game.handler.ts index 0e390b1..ff3669e 100644 --- a/src/game/game.handler.ts +++ b/src/game/game.handler.ts @@ -2,6 +2,7 @@ import {Injectable} from '@nestjs/common'; import {OnEvent} from '@nestjs/event-emitter'; import {User} from '../user/user.schema'; import {GameService} from './game.service'; +import {Game} from './game.schema'; @Injectable() export class GameHandler { @@ -10,6 +11,14 @@ export class GameHandler { ) { } + /** + * When a game is created, pause all the other games owner by the same user. + */ + @OnEvent('games.*.created') + async onGameCreated(game: Game): Promise { + await this.gameService.updateMany({owner: game.owner}, {speed: 0}); + } + @OnEvent('users.*.deleted') async onUserDeleted(user: User): Promise { await this.gameService.deleteMany({owner: user._id}); diff --git a/src/game/game.schema.ts b/src/game/game.schema.ts index 45c322e..b76d369 100644 --- a/src/game/game.schema.ts +++ b/src/game/game.schema.ts @@ -84,7 +84,7 @@ export class Game extends GlobalSchema { @IsBoolean() started: boolean; - @Prop({default: 0}) + @Prop({default: 0, index: 1}) @ApiProperty({ description: 'The speed of the game, interpreted by clients.', default: 0,