Skip to content

Commit

Permalink
#276 Fix clan caching (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyvg authored Sep 9, 2023
1 parent 6951074 commit eb817c4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
5 changes: 5 additions & 0 deletions src/arena/CharacterService.js
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,11 @@ class CharacterService {
* @return {Promise<CharacterService>}
*/
static async getCharacterById(id) {
const cachedChar = arena.characters[id];
if (cachedChar) {
return cachedChar;
}

const charFromDb = await findCharacter({ _id: id });
if (!charFromDb) {
return null;
Expand Down
5 changes: 2 additions & 3 deletions src/arena/ClanService/ClanService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,14 @@ export class ClanService {
if (!clan.hasEmptySlot) {
throw new Error('Клан уже сформирован');
}
const char: CharacterService = arena.characters[charId];
const char = await CharacterService.getCharacterById(charId);

await this.updateClan(clan.id, {
$push: { players: charId },
$pull: { requests: { $in: [charId] } },
});

/** @todo не сохраняется клан у игрока */
arena.characters[char.id] = await char.joinClan(clan);
await char.joinClan(clan);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/arena/PlayersService/PlayersService.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { times } from 'lodash';
import arena from '@/arena';
import type { Clan } from '@/models/clan';
import TestUtils from '@/utils/testUtils';
import CharacterService from '../CharacterService';
Expand All @@ -18,13 +19,15 @@ describe('PlayerService', () => {
const chars = await Promise.all(times(9, () => TestUtils.createCharacter()));
const charIds = chars.map(({ id }) => id);

arena.characters = {};

clans = await Promise.all([
await TestUtils.createClan(charIds[0], { players: [charIds[1], charIds[2]] }),
await TestUtils.createClan(charIds[3], { players: [charIds[4]] }),
await TestUtils.createClan(charIds[5]),
]);

characters = await Promise.all(charIds.map((CharacterService.getCharacterById)));
characters = await Promise.all(charIds.map((id) => CharacterService.getCharacterById(id)));
players = new PlayersService(characters.map(({ id }) => id));
});

Expand Down
21 changes: 13 additions & 8 deletions src/scenes/clan.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
import { Scenes, Markup } from 'telegraf';
import CharacterService from '@/arena/CharacterService';
import { broadcast } from '@/helpers/channelHelper';
import arena from '../arena';
import { ClanService } from '../arena/ClanService';
Expand Down Expand Up @@ -65,7 +66,7 @@ clanScene.action(/^(lvlup|back|remove|leave)$/, async (ctx) => {
await ctx.answerCbQuery('Клан был удалён');
}
if (ctx.match.input === 'leave') {
await ClanService.leaveClan(char.clan.id, char.tgId);
await ClanService.leaveClan(char.clan.id, char.id);
}
} catch (e) {
await ctx.answerCbQuery(e.message);
Expand Down Expand Up @@ -160,27 +161,31 @@ ${list.join('\n')}`,

clanScene.action(/requests_list|(accept|reject)(?=_)/, async (ctx) => {
const [action, charId] = ctx.match.input.split('_') as [string, string];
const clan = await ClanService.getClanById(ctx.session.character.clan.id);
try {
if (action === 'accept') {
await ClanService.acceptRequest(clan.id, charId);
const requester = await CharacterService.getCharacterById(charId);
await ClanService.acceptRequest(ctx.session.character.clan.id, charId);

await broadcast(
`Твоя заявка на вступление в клан *${clan.name}* была одобрена`,
ctx.session.character.tgId,
`Твоя заявка на вступление в клан *${ctx.session.character.clan.name}* была одобрена`,
requester.tgId,
);
}
if (action === 'reject') {
await ClanService.rejectRequest(clan.id, charId);
const requester = await CharacterService.getCharacterById(charId);
await ClanService.rejectRequest(ctx.session.character.clan.id, charId);

await broadcast(
`Твоя заявка на вступление в клан *${clan.name}* была отклонена`,
ctx.session.character.tgId,
`Твоя заявка на вступление в клан *${ctx.session.character.clan.name}* была отклонена`,
requester.tgId,
);
}
await ctx.answerCbQuery();
} catch (e) {
await ctx.answerCbQuery(e.message);
}

const clan = await ClanService.getClanById(ctx.session.character.clan.id);
const isAdmin = clan.owner.tgId === ctx.session.character.tgId;

const list = clan.requests.map((player) => {
Expand Down

0 comments on commit eb817c4

Please sign in to comment.