Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
garrappachc committed May 2, 2022
1 parent e45d9e9 commit ea4eadc
Show file tree
Hide file tree
Showing 25 changed files with 139 additions and 90 deletions.
3 changes: 2 additions & 1 deletion src/game-servers/models/game-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { MongooseDocument } from '@/utils/mongoose-document';
import { Expose, Transform } from 'class-transformer';
import { Rcon } from 'rcon-client/lib';
import { NotImplementedError } from '../errors/not-implemented.error';
import { ExposeObjectId } from '@/shared/decorators/expose-object-id';

@Schema({ discriminatorKey: 'provider' })
export class GameServer extends MongooseDocument {
Expand All @@ -28,7 +29,7 @@ export class GameServer extends MongooseDocument {
@Prop({ required: true })
port: string;

@Transform(({ value }) => value.toString())
@ExposeObjectId()
@Prop({ type: Types.ObjectId, ref: 'Game' })
game?: Types.ObjectId; // currently running game

Expand Down
8 changes: 7 additions & 1 deletion src/game-servers/services/game-servers.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ import { mongooseTestingModule } from '@/utils/testing-mongoose-module';
import { MongoMemoryServer } from 'mongodb-memory-server';
import { Events } from '@/events/events';
import { Game, GameDocument, gameSchema } from '@/games/models/game';
import { Connection, Model, Schema as MongooseSchema, Types } from 'mongoose';
import {
Connection,
Model,
Schema as MongooseSchema,
Types,
Document,
} from 'mongoose';
import {
getConnectionToken,
getModelToken,
Expand Down
6 changes: 4 additions & 2 deletions src/game-servers/services/game-servers.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@ export class GameServersService implements OnModuleInit {
`Using gameserver ${gameServer.name} for game #${game.number}`,
);
gameServer = await this.updateGameServer(gameServer.id, {
game: game._id,
game: new Types.ObjectId(game.id),
});
await this.gamesService.update(game.id, {
gameServer: new Types.ObjectId(gameServer.id),
});
await this.gamesService.update(game.id, { gameServer: gameServer._id });
return gameServer;
});
}
Expand Down
5 changes: 3 additions & 2 deletions src/games/models/game-slot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import { SlotStatus } from './slot-status';
import { Tf2ClassName } from '@/shared/models/tf2-class-name';
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Types } from 'mongoose';
import { Exclude, Transform } from 'class-transformer';
import { Exclude } from 'class-transformer';
import { ExposeObjectId } from '@/shared/decorators/expose-object-id';

@Schema()
export class GameSlot {
@Exclude({ toPlainOnly: true })
_id?: Types.ObjectId;

@Transform(({ value }) => value.toString(), { toPlainOnly: true })
@ExposeObjectId()
@Prop({ required: true, type: Types.ObjectId, ref: 'Player', index: true })
player!: Types.ObjectId;

Expand Down
7 changes: 5 additions & 2 deletions src/games/models/game.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ExposeObjectId } from '@/shared/decorators/expose-object-id';
import { MongooseDocument } from '@/utils/mongoose-document';
import { Prop, raw, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Exclude, Expose, Transform, Type } from 'class-transformer';
Expand All @@ -9,7 +10,9 @@ import { SlotStatus } from './slot-status';
@Schema()
export class Game extends MongooseDocument {
@Expose()
@Transform(({ value, obj }) => value ?? obj._id.toString())
@Transform(({ value, obj }) => {
return value ?? obj._id.toString();
})
id: string;

@Prop({ default: () => new Date() })
Expand Down Expand Up @@ -60,7 +63,7 @@ export class Game extends MongooseDocument {
@Prop()
error?: string;

@Transform(({ value }) => value.toString(), { toPlainOnly: true })
@ExposeObjectId()
@Prop({ ref: 'GameServer' })
gameServer?: Types.ObjectId;

Expand Down
12 changes: 7 additions & 5 deletions src/games/services/game-event-handler.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { PlayersService } from '@/players/services/players.service';
import { GameRuntimeService } from './game-runtime.service';
import { mongooseTestingModule } from '@/utils/testing-mongoose-module';
import { Game, GameDocument, gameSchema } from '../models/game';
import { ObjectId } from 'mongodb';
import { Player, PlayerDocument, playerSchema } from '@/players/models/player';
import { MongoMemoryServer } from 'mongodb-memory-server';
import { serverCleanupDelay } from '@configs/game-servers';
Expand All @@ -19,7 +18,7 @@ import {
MongooseModule,
} from '@nestjs/mongoose';
import { Tf2ClassName } from '@/shared/models/tf2-class-name';
import { Error } from 'mongoose';
import { Error, Types } from 'mongoose';

jest.mock('@/players/services/players.service');
jest.mock('@nestjs/config');
Expand Down Expand Up @@ -131,10 +130,10 @@ describe('GameEventHandlerService', () => {
});

describe('#onMatchEnded()', () => {
let gameServerId: ObjectId;
let gameServerId: Types.ObjectId;

beforeEach(async () => {
gameServerId = new ObjectId();
gameServerId = new Types.ObjectId();
const game = await gameModel.findById(mockGame.id);
game.gameServer = gameServerId;
game.state = GameState.started;
Expand Down Expand Up @@ -250,7 +249,10 @@ describe('GameEventHandlerService', () => {
describe('when a wrong gameId is captured', () => {
it('should throw an error', async () => {
await expect(
service.onDemoUploaded(new ObjectId().toString(), 'FAKE_DEMO_URL'),
service.onDemoUploaded(
new Types.ObjectId().toString(),
'FAKE_DEMO_URL',
),
).rejects.toThrow(Error.DocumentNotFoundError);
});
});
Expand Down
4 changes: 3 additions & 1 deletion src/games/services/game-event-handler.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ export class GameEventHandlerService implements OnModuleDestroy {
},
{
new: true, // return updated document
arrayFilters: [{ 'element.player': { $eq: player._id } }],
arrayFilters: [
{ 'element.player': { $eq: new Types.ObjectId(player.id) } },
],
},
)
.orFail()
Expand Down
4 changes: 2 additions & 2 deletions src/games/services/game-launcher.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
MongooseModule,
} from '@nestjs/mongoose';
import { Game, GameDocument, gameSchema } from '../models/game';
import { Model, Types, Error, Connection } from 'mongoose';
import { Model, Types, Error as MongooseError, Connection } from 'mongoose';
import { staticGameServerProviderName } from '@/game-servers/providers/static-game-server/static-game-server-provider-name';

jest.mock('@/game-servers/services/game-servers.service');
Expand Down Expand Up @@ -103,7 +103,7 @@ describe('GameLauncherService', () => {
it('should throw', async () => {
await expect(
service.launch(new Types.ObjectId().toString()),
).rejects.toThrow(Error.DocumentNotFoundError);
).rejects.toThrow(MongooseError.DocumentNotFoundError);
});
});

Expand Down
25 changes: 12 additions & 13 deletions src/games/services/game-runtime.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { ServerConfiguratorService } from './server-configurator.service';
import { PlayersService } from '@/players/services/players.service';
import { say } from '../utils/rcon-commands';
import { Tf2Team } from '../models/tf2-team';
import { ObjectId } from 'mongodb';
import { MongoMemoryServer } from 'mongodb-memory-server';
import { mongooseTestingModule } from '@/utils/testing-mongoose-module';
import { Player, PlayerDocument, playerSchema } from '@/players/models/player';
Expand All @@ -16,7 +15,7 @@ import { Events } from '@/events/events';
import { SlotStatus } from '../models/slot-status';
import { Tf2ClassName } from '@/shared/models/tf2-class-name';
import { getConnectionToken, MongooseModule } from '@nestjs/mongoose';
import { Connection, Error, Types } from 'mongoose';
import { Connection, Error as MongooseError, Types } from 'mongoose';
import { GameServerNotAssignedError } from '../errors/game-server-not-assigned.error';
import { Rcon } from 'rcon-client/lib';
import { staticGameServerProviderName } from '@/game-servers/providers/static-game-server/static-game-server-provider-name';
Expand Down Expand Up @@ -74,7 +73,7 @@ describe('GameRuntimeService', () => {

beforeEach(async () => {
mockGameServer = {
id: new ObjectId().toString(),
id: new Types.ObjectId().toString(),
name: 'FAKE_GAME_SERVER',
address: 'FAKE_ADDRESS',
port: '1234',
Expand All @@ -98,7 +97,7 @@ describe('GameRuntimeService', () => {
// @ts-expect-error
mockGame = await gamesService._createOne(mockPlayers);

mockGame.gameServer = new ObjectId(mockGameServer.id);
mockGame.gameServer = new Types.ObjectId(mockGameServer.id);
await mockGame.save();

serverConfiguratorService.configureServer.mockResolvedValue({
Expand Down Expand Up @@ -137,8 +136,8 @@ describe('GameRuntimeService', () => {
describe('when the given game does not exist', () => {
it('should throw an error', async () => {
await expect(
service.reconfigure(new ObjectId().toString()),
).rejects.toThrow(Error.DocumentNotFoundError);
service.reconfigure(new Types.ObjectId().toString()),
).rejects.toThrow(MongooseError.DocumentNotFoundError);
});
});

Expand Down Expand Up @@ -210,8 +209,8 @@ describe('GameRuntimeService', () => {
describe('when the given game does not exist', () => {
it('should reject', async () => {
await expect(
service.forceEnd(new ObjectId().toString()),
).rejects.toThrow(Error.DocumentNotFoundError);
service.forceEnd(new Types.ObjectId().toString()),
).rejects.toThrow(MongooseError.DocumentNotFoundError);
});
});

Expand Down Expand Up @@ -256,7 +255,7 @@ describe('GameRuntimeService', () => {
'FAKE_REPLACEE_ID',
null,
),
).rejects.toThrow(Error.DocumentNotFoundError);
).rejects.toThrow(MongooseError.DocumentNotFoundError);
});
});

Expand Down Expand Up @@ -288,7 +287,7 @@ describe('GameRuntimeService', () => {

it('should close the RCON connection', async () => {
await service.replacePlayer(mockGame.id, mockPlayers[0].id, {
player: new ObjectId(),
player: new Types.ObjectId(),
team: Tf2Team.red,
gameClass: Tf2ClassName.soldier,
});
Expand All @@ -299,7 +298,7 @@ describe('GameRuntimeService', () => {
it('should close the RCON connection', async () => {
const spy = jest.spyOn(rcon, 'end');
await service.replacePlayer(mockGame.id, mockPlayers[0].id, {
player: new ObjectId(),
player: new Types.ObjectId(),
team: Tf2Team.red,
gameClass: Tf2ClassName.soldier,
});
Expand All @@ -318,14 +317,14 @@ describe('GameRuntimeService', () => {
describe('when the given game server does not exist', () => {
beforeEach(() => {
gameServersService.getById.mockRejectedValue(
new Error.DocumentNotFoundError(''),
new MongooseError.DocumentNotFoundError(''),
);
});

it('should throw an error', async () => {
await expect(
service.sayChat(mockGameServer.id, 'some message'),
).rejects.toThrow(Error.DocumentNotFoundError);
).rejects.toThrow(MongooseError.DocumentNotFoundError);
});
});

Expand Down
7 changes: 3 additions & 4 deletions src/games/services/games.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { PlayerSkillService } from '@/players/services/player-skill.service';
import { Player, PlayerDocument, playerSchema } from '@/players/models/player';
import { mongooseTestingModule } from '@/utils/testing-mongoose-module';
import { Game, GameDocument, gameSchema } from '../models/game';
import { ObjectId } from 'mongodb';
import { QueueSlot } from '@/queue/queue-slot';
import { GameLauncherService } from './game-launcher.service';
import { QueueConfigService } from '@/queue/services/queue-config.service';
Expand All @@ -20,7 +19,7 @@ import { GameServersService } from '@/game-servers/services/game-servers.service
import { GameServer } from '@/game-servers/models/game-server';
import { PlayerNotInThisGameError } from '../errors/player-not-in-this-game.error';
import { GameInWrongStateError } from '../errors/game-in-wrong-state.error';
import { Connection, Model } from 'mongoose';
import { Connection, Model, Types } from 'mongoose';
import {
getConnectionToken,
getModelToken,
Expand Down Expand Up @@ -539,7 +538,7 @@ describe('GamesService', () => {
map: 'cp_badlands',
});

const gameServer = new ObjectId();
const gameServer = new Types.ObjectId();
await gameModel.create({
number: 2,
slots: [],
Expand Down Expand Up @@ -654,7 +653,7 @@ describe('GamesService', () => {
let gameServer: GameServer;

beforeEach(async () => {
game.gameServer = new ObjectId();
game.gameServer = new Types.ObjectId();
await game.save();

gameServer = {
Expand Down
21 changes: 11 additions & 10 deletions src/games/services/player-substitution.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { DiscordService } from '@/plugins/discord/services/discord.service';
import { Environment } from '@/environment/environment';
import { MongoMemoryServer } from 'mongodb-memory-server';
import { Player, PlayerDocument, playerSchema } from '@/players/models/player';
import { ObjectId } from 'mongodb';
import { mongooseTestingModule } from '@/utils/testing-mongoose-module';
import { Game, GameDocument, gameSchema } from '../models/game';
import { Events } from '@/events/events';
Expand All @@ -20,7 +19,7 @@ import {
getModelToken,
MongooseModule,
} from '@nestjs/mongoose';
import { Connection, Error, Model } from 'mongoose';
import { Connection, Error, Model, Types } from 'mongoose';
import { PlayerNotInThisGameError } from '../errors/player-not-in-this-game.error';
import { WrongGameSlotStatusError } from '../errors/wrong-game-slot-status.error';
import { GameInWrongStateError } from '../errors/game-in-wrong-state.error';
Expand Down Expand Up @@ -102,11 +101,10 @@ describe('PlayerSubstitutionService', () => {
// @ts-expect-error
mockGame = await gamesService._createOne([player1, player2]);

mockGame.gameServer = new ObjectId();
mockGame.gameServer = new Types.ObjectId();
await mockGame.save();

playerBansService.getPlayerActiveBans = () => Promise.resolve([]);

gameRuntimeService.sayChat.mockResolvedValue(null);
gameRuntimeService.replacePlayer.mockResolvedValue(null);
});
Expand All @@ -127,15 +125,18 @@ describe('PlayerSubstitutionService', () => {
describe('when the given game does not exist', () => {
it('should throw an error', async () => {
await expect(
service.substitutePlayer(new ObjectId().toString(), player1.id),
service.substitutePlayer(new Types.ObjectId().toString(), player1.id),
).rejects.toThrow(Error.DocumentNotFoundError);
});
});

describe('when the target player does not exist', () => {
it('should throw an error', async () => {
await expect(
service.substitutePlayer(mockGame.id, new ObjectId().toString()),
service.substitutePlayer(
mockGame.id,
new Types.ObjectId().toString(),
),
).rejects.toThrow(PlayerNotInThisGameError);
});
});
Expand Down Expand Up @@ -236,7 +237,7 @@ describe('PlayerSubstitutionService', () => {
it('should throw an error', async () => {
await expect(
service.cancelSubstitutionRequest(
new ObjectId().toString(),
new Types.ObjectId().toString(),
player1.id,
),
).rejects.toThrow(Error.DocumentNotFoundError);
Expand All @@ -248,7 +249,7 @@ describe('PlayerSubstitutionService', () => {
await expect(
service.cancelSubstitutionRequest(
mockGame.id,
new ObjectId().toString(),
new Types.ObjectId().toString(),
),
).rejects.toThrow(PlayerNotInThisGameError);
});
Expand Down Expand Up @@ -434,7 +435,7 @@ describe('PlayerSubstitutionService', () => {

describe('when the given player is involved in another game', () => {
beforeEach(async () => {
player3.activeGame = new ObjectId();
player3.activeGame = new Types.ObjectId();
await player3.save();
});

Expand Down Expand Up @@ -471,7 +472,7 @@ describe('PlayerSubstitutionService', () => {
service.replacePlayer(
mockGame.id,
player1.id,
new ObjectId().toString(),
new Types.ObjectId().toString(),
),
).rejects.toThrow(Error.DocumentNotFoundError);
});
Expand Down
Loading

0 comments on commit ea4eadc

Please sign in to comment.