Skip to content

Commit

Permalink
new: !test items x
Browse files Browse the repository at this point in the history
  • Loading branch information
Geczy committed Sep 28, 2023
1 parent c109a52 commit bdaa7e1
Showing 1 changed file with 69 additions and 5 deletions.
74 changes: 69 additions & 5 deletions packages/dota/src/twitch/commands/test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import DOTA_ITEM_IDS from 'dotaconstants/build/item_ids.json' assert { type: 'json' }
import DOTA_ITEMS from 'dotaconstants/build/items.json' assert { type: 'json' }
import { t } from 'i18next'

import { gsiHandlers } from '../../dota/lib/consts.js'
import { getAccountsFromMatch } from '../../dota/lib/getAccountsFromMatch.js'
import { steamSocket } from '../../steam/ws.js'
import { DelayedGames } from '../../types.js'
import { logger } from '../../utils/logger.js'
import { chatClient } from '../chatClient.js'
import commandHandler, { MessageType } from '../lib/CommandHandler.js'
Expand All @@ -23,6 +26,67 @@ commandHandler.registerCommand('test', {
return
}

if (args[0] === 'items') {
const [, matchId, steam32Id] = args
let itemList: string[] | false | undefined = false

steamSocket.emit('getUserSteamServer', steam32Id, async (err: any, steamServerId: string) => {
console.log({ steamServerId })
if (!steamServerId) {
chatClient.say(channel, t('gameNotFound', { lng: message.channel.client.locale }))
return
}

logger.info('test command', {
command: 'TEST',
steam32Id: steam32Id || client.steam32Id,
steamServerId,
})

// Wrap the steamSocket.emit in a Promise
const getDelayedDataPromise = new Promise<DelayedGames>((resolve, reject) => {
steamSocket.emit(
'getRealTimeStats',
{
match_id: matchId,
forceRefetchAll: true,
steam_server_id: steamServerId,
token: client.token,
},
(err: any, cards: any) => {
if (err) {
reject(err)
} else {
resolve(cards)
}
},
)
})

const playerIdx = 1
const delayedData = await getDelayedDataPromise
const teamIndex = (playerIdx ?? 0) > 4 ? 1 : 0
const teamPlayerIdx = (playerIdx ?? 0) % 5
const itemIds = delayedData.teams[teamIndex]?.players[teamPlayerIdx]?.items

itemList =
Array.isArray(itemIds) &&
itemIds.length > 0 &&
itemIds
.map((itemId) => {
const id = itemId as unknown as keyof typeof DOTA_ITEM_IDS
const itemShortname = DOTA_ITEM_IDS[id] as keyof typeof DOTA_ITEMS
const item = DOTA_ITEMS[itemShortname]
const itemName: string | boolean = item && 'dname' in item && item.dname

return itemName || itemShortname
})
.filter(Boolean)

console.log(itemList)
})
}

if (args[0] === 'cards') {
const { accountIds } = await getAccountsFromMatch({
gsi: client.gsi,
Expand Down Expand Up @@ -51,22 +115,22 @@ commandHandler.registerCommand('test', {
steamSocket.emit(
'getUserSteamServer',
steam32Id || client.steam32Id,
(err: any, steamserverid: string) => {
console.log({ steamserverid })
if (!steamserverid) {
(err: any, steamServerId: string) => {
console.log({ steamServerId })
if (!steamServerId) {
chatClient.say(channel, t('gameNotFound', { lng: message.channel.client.locale }))
return
}

logger.info('test command', {
command: 'TEST',
steam32Id: steam32Id || client.steam32Id,
steamserverid,
steamServerId,
})

logger.info(
`https://api.steampowered.com/IDOTA2MatchStats_570/GetRealtimeStats/v1/?key=${process.env
.STEAM_WEB_API!}&server_steam_id=${steamserverid}`,
.STEAM_WEB_API!}&server_steam_id=${steamServerId}`,
)
},
)
Expand Down

0 comments on commit bdaa7e1

Please sign in to comment.