Skip to content

Commit

Permalink
Merge branch 'refs/heads/fix/v1'
Browse files Browse the repository at this point in the history
  • Loading branch information
Clashsoft committed Jun 20, 2024
2 parents 54f43a2 + 512f6cd commit 2aba669
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
10 changes: 10 additions & 0 deletions docs/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
1 change: 1 addition & 0 deletions src/game-logic/game-logic.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions src/game/game.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand All @@ -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<Game> = dto;
if (tick) {
Expand Down
9 changes: 9 additions & 0 deletions src/game/game.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<void> {
await this.gameService.updateMany({owner: game.owner}, {speed: 0});
}

@OnEvent('users.*.deleted')
async onUserDeleted(user: User): Promise<void> {
await this.gameService.deleteMany({owner: user._id});
Expand Down
2 changes: 1 addition & 1 deletion src/game/game.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 2aba669

Please sign in to comment.