From f2e71afa4cad9071dfb6b1a8a297ae9e083f75fc Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 3 Jun 2024 23:16:53 -0500 Subject: [PATCH] fix: facet of current hero --- .github/workflows/deploy.yml | 2 +- .../dota/src/dota/lib/getAccountsFromMatch.ts | 19 ++++++++++++++++--- packages/dota/src/dota/lib/heroes.ts | 16 +++++++++++----- packages/dota/src/twitch/commands/facet.ts | 12 ++++++++---- .../dota/src/twitch/lib/findGSIByAccountId.ts | 2 +- .../dota/src/twitch/lib/getPlayerFromArgs.ts | 11 ++++++++--- 6 files changed, 45 insertions(+), 17 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index b687eae4..67e8fbd5 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -28,7 +28,7 @@ jobs: # Notify Coolify echo "Notifying Coolify for $env_name environment: $UUID" - # curl -s -X GET -H "Authorization: Bearer ${{ secrets.COOLIFY_API_KEY }}" "https://${{ secrets.COOLIFY_HOST }}/api/v1/deploy?uuid=${UUID}&force=false" + curl -s -X GET -H "Authorization: Bearer ${{ secrets.COOLIFY_API_KEY }}" "https://${{ secrets.COOLIFY_HOST }}/api/v1/deploy?uuid=${UUID}&force=false" } # UUID mappings for prod and dev diff --git a/packages/dota/src/dota/lib/getAccountsFromMatch.ts b/packages/dota/src/dota/lib/getAccountsFromMatch.ts index 77dea782..a28ff27e 100644 --- a/packages/dota/src/dota/lib/getAccountsFromMatch.ts +++ b/packages/dota/src/dota/lib/getAccountsFromMatch.ts @@ -51,11 +51,24 @@ export async function getAccountsFromMatch({ ] : ([] as { heroid: number; accountid: number; playerid: number }[]) - return { - matchPlayers, - accountIds: matchPlayers.map((player) => player.accountid), + if (matchPlayers.length) { + return { + matchPlayers, + accountIds: matchPlayers.map((player) => player.accountid), + } } } finally { await mongo.close() } + + return { + matchPlayers: [ + { + heroid: gsi?.hero?.id, + accountid: Number(gsi?.player?.accountid), + playerid: 0, + }, + ], + accountIds: [gsi?.player?.accountid], + } } diff --git a/packages/dota/src/dota/lib/heroes.ts b/packages/dota/src/dota/lib/heroes.ts index e50cf34c..646fe28e 100644 --- a/packages/dota/src/dota/lib/heroes.ts +++ b/packages/dota/src/dota/lib/heroes.ts @@ -666,17 +666,23 @@ export function getHeroNameOrColor(id: number, index?: number) { return name ?? 'Unknown' } -export function getHeroById(id: number) { +export function getHeroById(id?: number) { if (!id) return null - return Object.values(heroes).find((h) => h.id === id) + for (const [key, hero] of Object.entries(heroes)) { + if (hero.id === id) { + return { ...hero, key } + } + } + + return null } export function getHeroByName(name: string, heroIdsInMatch: number[]) { if (!name) return null // only keep a-z in name - name = name + const localName = name .replace(/[^a-z]/gi, '') .toLowerCase() .trim() @@ -690,7 +696,7 @@ export function getHeroByName(name: string, heroIdsInMatch: number[]) { alias .replace(/[^a-z]/gi, '') .toLowerCase() - .trim() === name, + .trim() === localName, ) if (hasAlias) return true @@ -705,7 +711,7 @@ export function getHeroByName(name: string, heroIdsInMatch: number[]) { .toLowerCase() .trim() - return inName.includes(name) + return inName.includes(localName) }) } diff --git a/packages/dota/src/twitch/commands/facet.ts b/packages/dota/src/twitch/commands/facet.ts index c18b94e7..6a8427fe 100644 --- a/packages/dota/src/twitch/commands/facet.ts +++ b/packages/dota/src/twitch/commands/facet.ts @@ -1,7 +1,7 @@ import DOTA_HERO_ABILITIES from 'dotaconstants/build/hero_abilities.json' assert { type: 'json' } import { t } from 'i18next' import { gsiHandlers } from '../../dota/lib/consts.js' -import { getHeroNameOrColor } from '../../dota/lib/heroes.js' +import { getHeroById, getHeroNameOrColor } from '../../dota/lib/heroes.js' import { DBSettings } from '../../settings.js' import { chatClient } from '../chatClient.js' import commandHandler from '../lib/CommandHandler.js' @@ -26,20 +26,24 @@ commandHandler.registerCommand('facet', { try { const { hero, playerIdx } = await findAccountFromCmd(client.gsi, args, client.locale, command) - if (typeof hero?.id !== 'number') { + const heroData = getHeroById(hero?.id) + + if (typeof hero?.id !== 'number' || !heroData) { chatClient.say(channel, t('gameNotFound', { lng: message.channel.client.locale })) return } const facet = - DOTA_HERO_ABILITIES?.[hero.name as keyof typeof DOTA_HERO_ABILITIES]?.facets[hero.facet - 1] + DOTA_HERO_ABILITIES?.[heroData.key as keyof typeof DOTA_HERO_ABILITIES]?.facets[ + hero.facet - 1 + ] if (!facet) { chatClient.say( channel, t('facetNotFound', { lng: message.channel.client.locale, - heroName: getHeroNameOrColor(hero?.id ?? 0, playerIdx), + heroName: heroData?.localized_name ?? 'Unknown', }), ) return diff --git a/packages/dota/src/twitch/lib/findGSIByAccountId.ts b/packages/dota/src/twitch/lib/findGSIByAccountId.ts index e2ee04f5..496bdc6d 100644 --- a/packages/dota/src/twitch/lib/findGSIByAccountId.ts +++ b/packages/dota/src/twitch/lib/findGSIByAccountId.ts @@ -60,7 +60,7 @@ export async function findAccountFromCmd( playerIdx, accountIdFromArgs, player: { accountid: accountIdFromArgs }, - hero: { id: data?.player?.heroid }, + hero: { id: data?.player?.heroid, facet: data?.player?.facet }, } } diff --git a/packages/dota/src/twitch/lib/getPlayerFromArgs.ts b/packages/dota/src/twitch/lib/getPlayerFromArgs.ts index 1391d95f..3c9d6522 100644 --- a/packages/dota/src/twitch/lib/getPlayerFromArgs.ts +++ b/packages/dota/src/twitch/lib/getPlayerFromArgs.ts @@ -17,7 +17,7 @@ export async function getPlayerFromArgs({ command: string }) { const { matchPlayers: players } = await getAccountsFromMatch({ gsi: packet }) - const heroIdsInMatch = players.map((player) => player.heroid) + const heroIdsInMatch = players.map((player) => player.heroid || 0) const heroList = heroIdsInMatch .map((heroId) => getHeroById(heroId)) .map((hero) => hero?.localized_name) @@ -49,7 +49,12 @@ export async function getPlayerFromArgs({ } else { // hero name input or alias const hero = getHeroByName(args.join('').toLowerCase().trim(), heroIdsInMatch) - playerIdx = hero ? players.findIndex((player) => player.heroid === hero.id) : -1 + + if (packet?.hero?.id === hero?.id) { + playerIdx = players.findIndex((player) => player.heroid === hero?.id) + } else { + playerIdx = hero ? players.findIndex((player) => player.heroid === hero.id) : -1 + } } if (playerIdx < 0 || playerIdx > 9) { @@ -62,5 +67,5 @@ export async function getPlayerFromArgs({ ) } - return { playerIdx, player: players[playerIdx] } + return { playerIdx, player: { ...packet?.player, ...packet?.hero, ...players[playerIdx] } } }