Skip to content

Commit

Permalink
feat(discord): delete substitute request msgs
Browse files Browse the repository at this point in the history
When the substitute request is canceled or resolved delete the discord
announcement.
  • Loading branch information
garrappachc committed Jun 12, 2020
1 parent acc4d1c commit dc20895
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/discord/services/__mocks__/discord.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { Injectable } from '@nestjs/common';

class Message {
delete() { return Promise.resolve(this); }
}

@Injectable()
export class DiscordService {

playersChannel = {
send: () => Promise.resolve(),
send: () => Promise.resolve(new Message()),
};

adminsChannel = {
send: () => Promise.resolve(),
send: () => Promise.resolve(new Message()),
};

getPlayersChannel() {
Expand Down
26 changes: 26 additions & 0 deletions src/games/services/player-substitution.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,14 @@ describe('PlayerSubstitutionService', () => {
});

describe('#cancelSubstitutionRequest()', () => {
let discordMessage: any;
const channel = { send: () => discordMessage };

beforeEach(async () => {
discordMessage = await discordService.getPlayersChannel().send({ });
// @ts-expect-error
discordService.getPlayersChannel = () => channel;

await service.substitutePlayer(mockGame.id, player1.id);
});

Expand Down Expand Up @@ -239,10 +246,23 @@ describe('PlayerSubstitutionService', () => {
await service.cancelSubstitutionRequest(mockGame.id, player1.id);
expect(spy).toHaveBeenCalled();
});

it('should get rid of discord announcement', async () => {
const spy = jest.spyOn(discordMessage, 'delete');
await service.cancelSubstitutionRequest(mockGame.id, player1.id);
expect(spy).toHaveBeenCalled();
});
});

describe('#replacePlayer()', () => {
let discordMessage: any;
const channel = { send: () => discordMessage };

beforeEach(async () => {
discordMessage = await discordService.getPlayersChannel().send({ });
// @ts-expect-error
discordService.getPlayersChannel = () => channel;

await service.substitutePlayer(mockGame.id, player1.id);
});

Expand Down Expand Up @@ -346,5 +366,11 @@ describe('PlayerSubstitutionService', () => {
await service.replacePlayer(mockGame.id, player1.id, player3.id);
expect(spy).toHaveBeenCalledWith(mockGame.gameServer.toString(), 'fake_player_3 is replacing fake_player_1 on soldier.');
});

it('should delete the discord announcement', async () => {
const spy = jest.spyOn(discordMessage, 'delete');
await service.replacePlayer(mockGame.id, player1.id, player3.id);
expect(spy).toHaveBeenCalled();
});
});
});
18 changes: 17 additions & 1 deletion src/games/services/player-substitution.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { QueueService } from '@/queue/services/queue.service';
import { DiscordService } from '@/discord/services/discord.service';
import { substituteRequest } from '@/discord/notifications';
import { Environment } from '@/environment/environment';
import { Message } from 'discord.js';

/**
* A service that handles player substitution logic.
Expand All @@ -21,6 +22,7 @@ import { Environment } from '@/environment/environment';
export class PlayerSubstitutionService {

private logger = new Logger(PlayerSubstitutionService.name);
private discordNotifications = new Map<string, Message>(); // playerId <-> message pairs

constructor(
@Inject(forwardRef(() => GamesService)) private gamesService: GamesService,
Expand Down Expand Up @@ -57,12 +59,13 @@ export class PlayerSubstitutionService {
this.gamesGateway.emitGameUpdated(game);
this.queueGateway.updateSubstituteRequests();

this.discordService.getPlayersChannel().send(substituteRequest({
const message = await this.discordService.getPlayersChannel().send(substituteRequest({
gameNumber: game.number,
gameClass: slot.gameClass,
team: game.teams.get(slot.teamId),
gameUrl: `${this.environment.clientUrl}/game/${game.id}`,
}));
this.discordNotifications.set(playerId, message);

return game;
}
Expand All @@ -89,6 +92,13 @@ export class PlayerSubstitutionService {
await game.save();
this.gamesGateway.emitGameUpdated(game);
this.queueGateway.updateSubstituteRequests();

const message = this.discordNotifications.get(playerId);
if (message) {
await message.delete({ reason: 'substitution request canceled' });
this.discordNotifications.delete(playerId);
}

return game;
}

Expand Down Expand Up @@ -156,6 +166,12 @@ export class PlayerSubstitutionService {
`${replacement.name} is replacing ${replacee.name} on ${replacementSlot.gameClass}.`,
);

const message = this.discordNotifications.get(replaceeId);
if (message) {
await message.delete();
this.discordNotifications.delete(replaceeId);
}

this.logger.verbose(`player ${replacement.name} is replacing ${replacee.name} on ${replacementSlot.gameClass} in game #${game.number}`);

setImmediate(() => this.gameRuntimeService.replacePlayer(game.id, replaceeId, replacementSlot));
Expand Down

0 comments on commit dc20895

Please sign in to comment.