Skip to content

Commit

Permalink
perf: 优化愿望单请求
Browse files Browse the repository at this point in the history
  • Loading branch information
XasYer committed Nov 27, 2024
1 parent 4c9ff00 commit 498b811
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 60 deletions.
42 changes: 21 additions & 21 deletions apps/inventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,30 @@ export const rule = {
wishlist.length = Config.other.hiddenLength
}
// 愿望单没有给name, 尝试获取一下, 顺便也可以获取一下价格 获取失败超过3次就不再获取了
let errorCount = 0
// 2024年11月27日 已更新 有个api可以获取多个appid
const appidsInfo = await api.IStoreBrowseService.GetItems(wishlist.map(i => i.appid))
for (const i in wishlist) {
wishlist[i].price_overview = {
discount: 0,
original: '获取失败'
}
wishlist[i].desc = moment.unix(wishlist[i].date_added).format('YYYY-MM-DD HH:mm:ss')
try {
if (errorCount < 3) {
const info = await api.store.appdetails(wishlist[i].appid)
wishlist[i].name = info.name
wishlist[i].price = info.name
? {
discount: info.price_overview?.discount_percent || 0,
original: info.price_overview?.initial_formatted || info.price_overview?.final_formatted || '即将推出',
current: info.price_overview?.final_formatted || ''
}
: {
original: '获取失败'
}
const appid = wishlist[i].appid
const info = appidsInfo[appid]
if (!info) {
wishlist[i].price = {
discount: 0,
original: '获取失败'
}
} catch {
errorCount++
continue
}
wishlist[i].desc = moment.unix(wishlist[i].date_added).format('YYYY-MM-DD HH:mm:ss')
wishlist[i].name = info.name
wishlist[i].price = info.is_free
? {
discount: 0,
original: '免费'
}
: {
discount: info.best_purchase_option?.discount_pct || 0,
original: info.best_purchase_option?.formatted_original_price || info.best_purchase_option?.formatted_final_price || '即将推出',
current: info.best_purchase_option?.formatted_final_price || ''
}
}
screenshotOptions.title = `${nickname} 愿望单共有 ${wishlist.length} 个游戏`
screenshotOptions.games = _.orderBy(wishlist, 'date_added', 'desc')
Expand Down
69 changes: 40 additions & 29 deletions models/api/IStoreBrowseService.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,61 @@ import { utils } from '#models'
import { logger } from '#lib'

/**
* 也是获取游戏信息?
* @param {string} appid
* 获取多个appid的游戏信息
* @param {string[]} appids
* @returns {Promise<{
* item_type: number,
* id: number,
* success: boolean,
* visible: boolean,
* name: string,
* store_url_path: string,
* appid: number,
* type: number,
* categories: {
* supported_player_categoryids: number[],
* feature_categoryids: number[],
* controller_categoryids: number[],
* }
* best_purchase_option: {
* packageid: number,
* purchase_option_name: string,
* final_price_in_cents: string,
* formatted_final_price: string,
* user_can_purchase_as_gift: boolean,
* hide_discount_pct_for_compliance: boolean,
* included_game_count: number,
* [appid: string] : {
* item_type: number,
* id: number,
* success: boolean,
* visible: boolean,
* name: string,
* store_url_path: string,
* appid: number,
* type: number,
* is_free?: boolean,
* categories: {
* supported_player_categoryids: number[],
* feature_categoryids: number[],
* controller_categoryids: number[],
* }
* best_purchase_option?: {
* packageid: number,
* purchase_option_name: string,
* final_price_in_cents: string,
* original_price_in_cents: string,
* formatted_final_price: string,
* formatted_original_price: string,
* discount_pct: number,
* active_discounts: {
* discount_amount: string,
* discount_description: string,
* discount_end_date: number
* },
* user_can_purchase_as_gift: boolean,
* hide_discount_pct_for_compliance: boolean,
* included_game_count: number,
* }
* }
* }>}
*/
export async function GetItems (appid) {
export async function GetItems (appids) {
const data = {
ids: [{ appid }],
ids: appids.map(appid => ({ appid })),
context: {
language: 'schinese',
country_code: 'CN'
}
}
const start = Date.now()
logger.info(`开始获取${appid}游戏信息`)
logger.info(`开始获取${appids.length}个游戏信息`)
return utils.request.get('IStoreBrowseService/GetItems/v1', {
params: {
input_json: JSON.stringify(data)
}
}).then(res => {
const data = res.data.response.store_items?.[0] || {}
logger.info(`获取${appid}游戏信息成功: ${data.name},耗时${Date.now() - start}ms`)
return data
const data = res.data.response.store_items || []
logger.info(`获取${appids.length}游戏信息成功,耗时${Date.now() - start}ms`)
return data.reduce((acc, cur) => (acc[cur.appid] = cur) && acc, {})
})
}
5 changes: 3 additions & 2 deletions models/api/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * as store from './store.js'
export * as ISteamUser from './ISteamUser.js'
export * as IPlayerService from './IPlayerService.js'
export * as IWishlistService from './IWishlistService.js'
export * as ISteamUserStats from './ISteamUserStats.js'
export * as store from './store.js'
export * as IWishlistService from './IWishlistService.js'
export * as IStoreBrowseService from './IStoreBrowseService.js'
7 changes: 1 addition & 6 deletions models/task/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,8 @@ export function startTimer () {
}
}
} else {
// TODO: 临时方案
let send = false
// 如果有gameid就是开始玩
if (Config.push.enable && player.gameid && player.gameid != lastPlay.appid) {
send = true
state.time = now
msg.push(`${nickname}(${player.personaname}) 正在玩 ${player.gameextrainfo}`)
// 看看上次有没有在玩别的游戏
Expand All @@ -124,12 +121,10 @@ export function startTimer () {
// 记录这一次的状态
// 如果有上次记录就是结束游玩
} else if (Config.push.enable && lastPlay.name && lastPlay.name != player.gameextrainfo) {
send = true
state.time = now
msg.push(`${nickname}(${player.personaname}) 已结束游玩 ${lastPlay.name} 时长 ${utils.formatDuration(now - lastPlay.time)}`)
} else if (Config.push.stateChange && player.personastate != lastPlay.state) {
if ([0, 1].includes(player.personastate)) {
send = true
state.time = now
msg.shift()
msg.push(`${nickname}(${player.personaname}) 已${utils.getPersonaState(player.personastate)}`)
Expand All @@ -143,7 +138,7 @@ export function startTimer () {
continue
}
try {
if (send) {
if (state.time === now) {
await utils.sendGroupMsg(i.botId, i.groupId, msg)
}
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion resources/inventory/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@

.size-large-info {
width: 150px;
margin-right: 10px;
margin-right: 5px;
}

.size-small-info {
Expand Down
2 changes: 1 addition & 1 deletion resources/inventory/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ <h4 class="header-desc">
<img src="{{i.header_image}}" class="{{i.header_image_class || 'rectangle'}}">
{{else if !i.noImg}}
<img
src="https://steamcdn-a.akamaihd.net/steam/{{i.header_image_type || 'apps'}}/{{i.appid}}/header.jpg">
src="https://steamcdn-a.akamaihd.net/steam/{{i.header_image_type || 'apps'}}/{{i.appid}}/header.jpg" class="{{i.header_image_class || 'rectangle'}}">
{{/if}}
</div>
<!-- TODO: 待重构 屎一样 -->
Expand Down

0 comments on commit 498b811

Please sign in to comment.