Skip to content

Commit

Permalink
feat: steam成就 & steam统计
Browse files Browse the repository at this point in the history
  • Loading branch information
XasYer committed Nov 21, 2024
1 parent 06efe48 commit a946ce8
Show file tree
Hide file tree
Showing 12 changed files with 307 additions and 67 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,13 @@ pnpm install --filter=steam-plugin
- [x] steam帮助
- [x] steam设置
- [x] steam搜索
- [ ] steam成就统计
- [x] steam成就
- [x] steam统计
- [ ] 群友上下线通知
- [ ] steam喜加一
- [x] 开启/关闭推送
- [x] steam特惠
- [ ] 推送黑/白群名单
- [x] 推送黑/白群名单

## 联系方式

Expand Down
124 changes: 124 additions & 0 deletions apps/achievement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { App, Render } from '#components'
import { utils, db, api } from '#models'
import { segment } from '#lib'

const app = {
id: 'achievement',
name: '成就统计'
}

export const rule = {
achievements: {
reg: /^#?steam(成就|统计)\s*(\d+|\d+[-:\s]\d+)?$/i,
fnc: async e => {
const regRet = rule.achievements.reg.exec(e.msg)
const textId = regRet[2]?.trim()
if (!textId) {
await e.reply([segment.at(e.user_id), '\n需要带上游戏的appid哦'])
return true
}
const uid = utils.getAtUid(e.at, e.user_id)
const { steamId, appid } = await (async () => {
if (/[-:\s]/.test(textId)) {
const [appid, steamId] = textId.split(/[-:\s]/)
return { steamId, appid }
}
const steamId = await db.UserTableGetBindSteamIdByUserId(uid)
return { steamId, appid: textId }
})()
if (!steamId) {
await e.reply([segment.at(uid), '\n还没有绑定steamId哦, 先绑定steamId吧'])
return true
}
e.reply(['在查了...在查了...'])
// 先获取游戏的成就列表
const achievementsByGame = await api.ISteamUserStats.GetSchemaForGame(appid)
if (!achievementsByGame || !achievementsByGame.availableGameStats) {
await e.reply([segment.at(uid), `\n没有找到${appid}的成就信息`])
return true
}
const achievementsByUser = await api.ISteamUserStats.GetUserStatsForGame(appid, steamId)
if (!achievementsByUser) {
await e.reply([segment.at(uid), `\n没有找到${steamId}${appid}的成就信息`])
return true
}
const nickname = await utils.getUserName(e.self_id, uid, e.group_id)
const data = [
{
title: `${nickname}${achievementsByGame.gameName} ${regRet[1]}统计`,
games: [{
name: achievementsByGame.gameName,
appid
}],
column: 2
}
]
if (regRet[1] === '成就') {
const completeAchievements = []
const unCompleteAchievements = [];
// 处理成就数据
(achievementsByGame.availableGameStats.achievements || []).forEach(all => {
const user = achievementsByUser.achievements.find(i => i.name === all.name)
const info = {
name: all.displayName,
appid: all.hidden ? '已隐藏' : all.description,
header_image: user ? all.icon : all.icongray,
header_image_class: 'square'
}
if (user) {
completeAchievements.push(info)
} else {
unCompleteAchievements.push(info)
}
})
data.push(
{
title: '已完成成就',
desc: `共${completeAchievements.length}个`,
games: completeAchievements,
column: 2
},
{
title: '未完成成就',
desc: `共${unCompleteAchievements.length}个`,
games: unCompleteAchievements,
column: 2
}
)
}
if (regRet[1] === '统计') {
// 处理后的统计数据
const completeStats = (achievementsByUser.stats || []).map(i => {
const item = (achievementsByGame.availableGameStats.stats || []).find(j => j.name === i.name)
if (item) {
return {
name: item.name,
appid: i.value,
desc: item.displayName || '',
noImg: true
}
} else {
return false
}
}).filter(Boolean)
data.push(
{
title: '已完成统计',
desc: `共${completeStats.length}个`,
games: completeStats,
column: 2
}
)
}
const img = await Render.simpleRender('inventory/index', { data })
if (img) {
await e.reply(img)
} else {
await e.reply('制作图片出错辣! 再试一次吧')
}
return true
}
}
}

export const achievementApp = new App(app).create()
51 changes: 36 additions & 15 deletions apps/bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ export const rule = {
if (!userBindAll.length) {
await e.reply('要和SteamID或好友码一起发送哦')
} else {
const pushSteamIds = await db.PushTableGetAllSteamIdBySteamIdAndGroupId(uid, e.group_id)
await e.reply(`全部steamId(${Config.push.enable ? '✧:是否推送 ' : ''}√:是否绑定):\n${userBindAll.map(item => {
const isBind = item.isBind ? '√' : ''
const isPush = (Config.push.enable && pushSteamIds.includes(item.steamId)) ? '✧' : ''
return `${item.steamId} ${isPush} ${isBind} `
}).join('\n')}`)
await e.reply(await getBindSteamIdsText(uid, e.group_id, userBindAll))
}
return true
}
Expand All @@ -45,13 +40,8 @@ export const rule = {
await db.PushTableAddData(uid, steamId, e.self_id, e.group_id)
}
}
const userBindAll = await db.UserTableGetDataByUserId(uid)
const pushSteamIds = await db.PushTableGetAllSteamIdBySteamIdAndGroupId(uid, e.group_id)
await e.reply(`已添加steamId: ${steamId}\n全部steamId(${Config.push.enable ? '✧:是否推送 ' : ''}√:是否绑定):\n${userBindAll.map(item => {
const isBind = item.isBind ? '√' : ''
const isPush = (Config.push.enable && pushSteamIds.includes(item.steamId)) ? '✧' : ''
return `${item.steamId} ${isPush} ${isBind} `
}).join('\n')}`)
const text = await getBindSteamIdsText(uid, e.group_id)
await e.reply(`已添加steamId: ${steamId}\n${text}`)
return true
}
},
Expand All @@ -71,8 +61,8 @@ export const rule = {
if (bindInfo) {
if (bindInfo.userId == uid) {
await db.UserTableDelSteamIdByUserId(uid, steamId)
const userBindAll = await db.UserTableGetDataByUserId(uid)
await e.reply(`已删除steamId: ${steamId}\n已绑定:\n${userBindAll.map(item => `${item.steamId} ${item.isBind ? '√' : ''}`).join('\n')}`)
const text = await getBindSteamIdsText(uid, e.group_id)
await e.reply(`已添加steamId: ${steamId}\n${text}`)
} else {
await e.reply('只能解绑自己绑定的steamId哦')
}
Expand All @@ -85,3 +75,34 @@ export const rule = {
}

export const bind = new App(app, rule).create()

/**
* 获得已绑定的steamId的文本
* @param {string} uid
* @param {string} gid
* @param {UserColumns[]?} userBindAll
* @returns
*/
async function getBindSteamIdsText (uid, gid, userBindAll = []) {
if (!userBindAll?.length) {
userBindAll = await db.UserTableGetDataByUserId(uid)
}
const pushEnable = (() => {
if (!Config.push.enable) {
return false
}
if (Config.push.whiteGroupList.length && !Config.push.whiteGroupList.some(i => i == gid)) {
return false
}
if (Config.push.blackGroupList.length && Config.push.blackGroupList.some(i => i == gid)) {
return false
}
return true
})()
const pushSteamIds = await db.PushTableGetAllSteamIdBySteamIdAndGroupId(uid, gid)
return `全部steamId(${pushEnable ? '✧:是否推送 ' : ''}√:是否绑定):\n${userBindAll.map(item => {
const isBind = item.isBind ? '√' : ''
const isPush = (pushEnable && pushSteamIds.includes(item.steamId)) ? '✧' : ''
return `${item.steamId} ${isPush} ${isBind} `
}).join('\n')}`
}
15 changes: 7 additions & 8 deletions apps/discounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,24 @@ export const rule = {
for (const item of items) {
const key = {
title: item.title,
type: 'wishlist',
column: 2,
games: []
}
for (const i of res[item.key].items) {
key.games.push({
appid: i.id,
name: i.name,
date_added: i.discount_expiration ? moment.unix(i.discount_expiration).format('YYYY-MM-DD HH:mm:ss') : '',
desc: i.discount_expiration ? moment.unix(i.discount_expiration).format('YYYY-MM-DD HH:mm:ss') : '',
header_image: i.header_image,
header_image_type: i.header_image.match(/store_item_assets\/steam\/(.+?)\//)?.[1] || 'apps',
price_overview: i.discounted
price: i.discounted
? {
initial_formatted: ${i.original_price / 100}`,
discount_percent: i.discount_percent,
final_formatted: ${i.final_price / 100}`
original: ${i.original_price / 100}`,
discount: i.discount_percent,
current: ${i.final_price / 100}`
}
: {
initial_formatted: i.original_price ? ${i.original_price / 100}` : '',
discount_percent: 0
original: i.original_price ? ${i.original_price / 100}` : ''
}
})
}
Expand Down
27 changes: 13 additions & 14 deletions apps/inventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export const rule = {
return true
}
e.reply([segment.at(uid), '\n在查了...在查了...'])
const nickname = await utils.getUserName(e.self_id, e.user_id, e.group_id)
const nickname = await utils.getUserName(e.self_id, uid, e.group_id)
const screenshotOptions = {
title: '',
games: [],
type: 'inventory',
column: 3,
desc: ''
}
if (e.msg.includes('近')) {
Expand All @@ -46,23 +46,22 @@ export const rule = {
let errorCount = 0
for (const i in wishlist) {
wishlist[i].price_overview = {
discount_percent: 0,
initial_formatted: '获取失败'
discount: 0,
original: '获取失败'
}
wishlist[i].date_added = moment.unix(wishlist[i].date_added).format('YYYY-MM-DD HH:mm:ss')
wishlist[i].desc = moment.unix(wishlist[i].date_added).format('YYYY-MM-DD HH:mm:ss')
try {
if (errorCount < 3) {
const info = await api.appdetails(wishlist[i].appid)
wishlist[i].name = info.name
wishlist[i].price_overview = info.name
wishlist[i].price = info.name
? {
discount_percent: 0,
initial_formatted: '即将推出',
...info.price_overview
discount: info.price_overview?.discount_percent || 0,
original: info.price_overview?.initial_formatted || info.price_overview?.final_formatted || '即将推出',
current: info.price_overview?.final_formatted || ''
}
: {
discount_percent: 0,
initial_formatted: '获取失败'
original: '获取失败'
}
}
} catch {
Expand All @@ -71,7 +70,7 @@ export const rule = {
}
screenshotOptions.title = `${nickname} 愿望单共有 ${wishlist.length} 个游戏`
screenshotOptions.games = _.orderBy(wishlist, 'date_added', 'desc')
screenshotOptions.type = 'wishlist'
screenshotOptions.column = 2
} else {
const games = await api.IPlayerService.GetOwnedGames(steamId)
if (!games.length) {
Expand All @@ -81,11 +80,11 @@ export const rule = {
screenshotOptions.games = _.orderBy(games, 'playtime_forever', 'desc')
screenshotOptions.title = `${nickname} 库存共有 ${games.length} 个游戏`
}
if (screenshotOptions.type !== 'wishlist') {
if (screenshotOptions.column === 3) {
let playtimeForever = 0
let playtime2weeks = 0
screenshotOptions.games.map(i => {
i.time_info = `${getTime(i.playtime_forever)} ${i.playtime_2weeks ? `/ ${getTime(i.playtime_2weeks)}` : ''}`
i.desc = `${getTime(i.playtime_forever)} ${i.playtime_2weeks ? `/ ${getTime(i.playtime_2weeks)}` : ''}`
playtimeForever += i.playtime_forever
i.playtime_2weeks && (playtime2weeks += i.playtime_2weeks)
return i
Expand Down
8 changes: 4 additions & 4 deletions apps/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ export const rule = {
return {
appid,
name,
price_overview: {
discount_percent: 0,
initial_formatted: price
price: {
discount: 0,
original: price
}
}
}).filter(Boolean)
Expand All @@ -39,7 +39,7 @@ export const rule = {
const screenshotOptions = {
title: `${name} 搜索结果`,
games,
type: 'wishlist'
column: 2
}
const img = await Render.simpleRender('inventory/index', { data: [screenshotOptions] })
if (img) {
Expand Down
2 changes: 1 addition & 1 deletion apps/setting.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const rule = {
return true
}
const type = regRet[1] === '添加' ? 'add' : 'del'
const id = regRet[3] || String(e.group_id)
const id = regRet[3]?.trim() || String(e.group_id)
if (!id) {
await e.reply('请输入群号或在指定群中使用~')
return true
Expand Down
2 changes: 1 addition & 1 deletion components/Render.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const Render = {
saveId: path.split('/').pop(),
imgType: 'jpeg',
pageGotoParams: {
waitUntil: 'networkidle2'
waitUntil: 'networkidle0' // +0.5s
},
...params
}
Expand Down
Loading

0 comments on commit a946ce8

Please sign in to comment.