Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,33 @@ describe('OnlinePlayersService.getAllOnlinePlayers() test suite', () => {
expect(player_ids).toContainEqual(payload2);
});

it('Should be able to filter returning players by status field', async () => {
await playerModel.create(player1);
await playerModel.create(player2);
const payload1 = {
name: player1.name,
_id: _id1,
status: OnlinePlayerStatus.UI,
};
const payload2 = {
name: player2.name,
_id: _id2,
status: OnlinePlayerStatus.BATTLE,
};

jest.spyOn(redisService, 'getValuesByKeyPattern').mockResolvedValue({
[_id1]: JSON.stringify(payload1),
[_id2]: JSON.stringify(payload2),
});

const player_ids = await service.getAllOnlinePlayers({
filter: { status: [OnlinePlayerStatus.UI] },
});

expect(player_ids).toContainEqual(payload1);
expect(player_ids).not.toContainEqual(payload2);
});

it('Should not return players if there are no players', async () => {
jest.spyOn(redisService, 'getValuesByKeyPattern').mockResolvedValue({});

Expand Down
23 changes: 23 additions & 0 deletions src/onlinePlayers/dto/OnlinePlayerSearchQuery.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { OnlinePlayerStatus } from '../enum/OnlinePlayerStatus';
import { IsArray, IsEnum, IsOptional } from 'class-validator';
import { Transform } from 'class-transformer';

export default class OnlinePlayerSearchQueryDto {
/**
* Filter online players
*
* @example ?search=status="UI"
*/
@IsOptional()
@IsArray()
@IsEnum(OnlinePlayerStatus, { each: true })
@Transform(({ value }) => {
if (!value) return undefined;

const matches = value.match(/status="(.*?)"/g);
if (!matches) return [];

return matches.map((m) => m.replace(/status="|"$/g, ''));
})
search?: OnlinePlayerStatus[];
}
10 changes: 7 additions & 3 deletions src/onlinePlayers/onlinePlayers.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Body, Controller, Get, Post } from '@nestjs/common';
import { Body, Controller, Get, Post, Query } from '@nestjs/common';
import { OnlinePlayersService } from './onlinePlayers.service';
import { LoggedUser } from '../common/decorator/param/LoggedUser.decorator';
import { User } from '../auth/user';
import { UniformResponse } from '../common/decorator/response/UniformResponse';
import ApiResponseDescription from '../common/swagger/response/ApiResponseDescription';
import OnlinePlayerDto from './dto/onlinePlayer.dto';
import InformPlayerIsOnlineDto from './dto/InformPlayerIsOnline.dto';
import OnlinePlayerSearchQueryDto from './dto/OnlinePlayerSearchQuery.dto';

@Controller('online-players')
export class OnlinePlayersController {
Expand Down Expand Up @@ -50,7 +51,10 @@ export class OnlinePlayersController {
})
@Get()
@UniformResponse(null, OnlinePlayerDto)
async getAllOnlinePlayers() {
return this.onlinePlayersService.getAllOnlinePlayers();
async getAllOnlinePlayers(@Query() query: OnlinePlayerSearchQueryDto) {
const filter = { status: query.search };
return this.onlinePlayersService.getAllOnlinePlayers({
filter,
});
}
}
17 changes: 14 additions & 3 deletions src/onlinePlayers/onlinePlayers.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,25 @@ export class OnlinePlayersService {
*
* @returns Array of OnlinePlayers or empty array if nothing found
*/
async getAllOnlinePlayers(): Promise<OnlinePlayer[]> {
async getAllOnlinePlayers(options?: {
filter?: { status?: OnlinePlayerStatus[] };
}): Promise<OnlinePlayer[]> {
const players = await this.redisService.getValuesByKeyPattern(
`${this.ONLINE_PLAYERS_KEY}:*`,
);

if (!players) return [];

const onlinePlayersStr = Object.values(players);
return onlinePlayersStr.map((playerStr) => JSON.parse(playerStr));
const onlinePlayers = Object.values(players).map((playerStr) =>
JSON.parse(playerStr),
) as OnlinePlayer[];

if (options?.filter?.status) {
return onlinePlayers.filter((p) =>
options.filter.status.includes(p.status),
);
}

return onlinePlayers;
}
}