Skip to content

Commit

Permalink
perf: #steam绑定 渲染成图片
Browse files Browse the repository at this point in the history
  • Loading branch information
XasYer committed Dec 15, 2024
1 parent a811c85 commit 65f5def
Show file tree
Hide file tree
Showing 11 changed files with 376 additions and 62 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pnpm install --filter=steam-plugin
- [x] 开启/关闭推送
- [x] steam特惠
- [x] 推送黑/白群名单
- [ ] steam绑定渲染成图片
- [x] steam绑定渲染成图片
- [x] steam评论
- [ ] steam游戏详情
- [x] steam成就统计
Expand Down
61 changes: 12 additions & 49 deletions apps/bind.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { utils, db } from '#models'
import { App, Config } from '#components'
import { utils, db, bind } from '#models'
import { App } from '#components'

const app = {
id: 'bind',
name: '绑定Steam'
}

export const rule = {
getBindImg: {
reg: /^#steam$/i,
fnc: async e => await e.reply(await bind.getBindSteamIdsImg(e.self_id, e.user_id, e.group_id))
},
bind: {
reg: /^#?steam(?:[切更]换)?(?:绑定|bind)\s*(\d+)?$/i,
fnc: async e => {
Expand All @@ -15,11 +19,7 @@ export const rule = {
const textId = rule.bind.reg.exec(e.msg)[1]
const userBindAll = await db.UserTableGetDataByUserId(uid)
if (!textId) {
if (!userBindAll.length) {
await e.reply('要和SteamID或好友码一起发送哦')
} else {
await e.reply(await getBindSteamIdsText(e.self_id, uid, e.group_id, userBindAll))
}
await e.reply(await bind.getBindSteamIdsImg(e.self_id, uid, e.group_id, userBindAll))
return true
}
const index = Number(textId) <= userBindAll.length ? Number(textId) - 1 : -1
Expand All @@ -41,8 +41,8 @@ export const rule = {
await db.PushTableAddData(uid, steamId, e.self_id, e.group_id)
}
}
const text = await getBindSteamIdsText(e.self_id, uid, e.group_id)
await e.reply(`已添加steamId: ${steamId}\n${text}`)
const text = await bind.getBindSteamIdsImg(e.self_id, uid, e.group_id)
await e.reply(text)
return true
}
},
Expand All @@ -66,8 +66,8 @@ export const rule = {
if (bindInfo.userId == uid || isForce) {
const id = isForce ? bindInfo.userId : uid
await db.UserTableDelSteamIdByUserId(id, steamId)
const text = await getBindSteamIdsText(e.self_id, id, e.group_id)
await e.reply(`${id}\n已删除steamId: ${steamId}\n${text}`)
const text = await bind.getBindSteamIdsImg(e.self_id, id, e.group_id)
await e.reply(text)
} else {
await e.reply('只能解绑自己绑定的steamId哦')
}
Expand All @@ -79,41 +79,4 @@ 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 (bid, uid, gid, userBindAll = []) {
if (!userBindAll?.length) {
userBindAll = await db.UserTableGetDataByUserId(uid)
}
const pushEnable = (() => {
if (!Config.push.enable) {
return false
}
if (Config.push.whiteBotList.length && !Config.push.whiteBotList.some(i => i == bid)) {
return false
}
if (Config.push.blackBotList.length && Config.push.blackBotList.some(i => i == bid)) {
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, true)
return `全部steamId(${pushEnable ? '✧:是否推送 ' : ''}√:是否绑定):\n${userBindAll.map((item, index) => {
const isBind = item.isBind ? '√' : ''
const isPush = (pushEnable && pushSteamIds.includes(item.steamId)) ? '✧' : ''
return `${index + 1}: ${item.steamId} ${isPush} ${isBind} `
}).join('\n')}`
}
export const bindApp = new App(app, rule).create()
2 changes: 1 addition & 1 deletion apps/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export const rule = {
}
},
now: {
reg: /^#?(全部)?群友(在玩什么呢?|状态)[??]?$/i,
reg: /^#?(steam)?(全部)?群友(在玩什么呢?|状态)[??]?$/i,
fnc: async e => {
if (!e.group_id) {
return false
Expand Down
4 changes: 2 additions & 2 deletions components/Render.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const Render = {
const len = minLength === 1 ? 1.4 : minLength
data.style = `<style>\n#container,.games{\nwidth: ${len * size}px;\n}\n</style>`
}
return await puppeteer.screenshot(path, data)
return await puppeteer.screenshot(`${Version.pluginName}/${path}`, data)
},
async simpleRender (path, params) {
path = path.replace(/.html$/, '')
Expand All @@ -52,7 +52,7 @@ const Render = {
},
...params
}
return await puppeteer.screenshot(path, data)
return await puppeteer.screenshot(`${Version.pluginName}/${path}`, data)
}
}

Expand Down
92 changes: 91 additions & 1 deletion models/bind/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,93 @@
export function bindSteamId (e) {
import { db, utils, api } from '#models'
import { Config, Render } from '#components'

/**
* 获得已绑定的steamId的文本
* @param {string} uid
* @param {string} gid
* @param {UserColumns[]?} userBindAll
* @returns
*/
export async function getBindSteamIdsImg (bid, uid, gid, userBindAll = []) {
if (!userBindAll?.length) {
userBindAll = await db.UserTableGetDataByUserId(uid)
}
if (!userBindAll.length) {
return '没有绑定任何steamId, 请使用#steam绑定steamId或好友码 进行绑定'
}
const enablePush = (() => {
if (!Config.push.enable) {
return false
}
if (Config.push.whiteBotList.length && !Config.push.whiteBotList.some(i => i == bid)) {
return false
}
if (Config.push.blackBotList.length && Config.push.blackBotList.some(i => i == bid)) {
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 data = []
const pushSteamId = []
const unPushSteamId = []
const enablePushSteamIdList = enablePush ? await db.PushTableGetAllSteamIdBySteamIdAndGroupId(uid, gid, true) : []
const allSteamIdInfo = {}
try {
const res = await api.ISteamUser.GetPlayerSummaries(userBindAll.map(i => i.steamId))
res.forEach(i => {
allSteamIdInfo[i.steamid] = i
})
} catch { }
let index = 1
for (const item of userBindAll) {
const avatar = Config.other.steamAvatar ? allSteamIdInfo[item.steamId].avatarfull : await utils.getUserAvatar(bid, uid, gid)
const info = {
steamId: item.steamId,
isBind: item.isBind,
name: allSteamIdInfo[item.steamId].personaname,
avatar: avatar ?? await utils.getUserAvatar(bid, uid, gid),
index
}
if (enablePushSteamIdList.includes(item.steamId)) {
pushSteamId.push(info)
} else {
unPushSteamId.push(info)
}
index++
}
if (enablePush) {
if (pushSteamId.length) {
data.push({
title: '已开启推送',
list: pushSteamId
})
}
if (unPushSteamId.length) {
data.push({
title: '未开启推送',
list: unPushSteamId
})
}
} else {
data.push({
title: '所有steamId',
list: unPushSteamId
})
}
const img = await Render.render('user/index', {
data,
enablePush,
random: Math.floor(Math.random() * 5) + 1
})
if (img) {
return img
} else {
return '制作图片失败,重试一下'
}
}
4 changes: 2 additions & 2 deletions models/db/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,9 @@ export async function StatsTableDelete (steamId, transaction) {
transaction
})
}
hasTransaction && await transaction.commit()
!hasTransaction && await transaction.commit()
} catch (error) {
hasTransaction && await transaction.rollback()
!hasTransaction && await transaction.rollback()
throw error
}
return true
Expand Down
16 changes: 13 additions & 3 deletions models/help/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export const helpList = [
{
group: '需要绑定steamid或好友码后才能使用',
list: [
{
icon: 66,
title: '#steam',
desc: '查看已绑定的steamId'
},
{
icon: 9,
title: '#steam绑定',
Expand All @@ -23,8 +28,13 @@ export const helpList = [
},
{
icon: 34,
title: '#steam(开启|关闭)推送',
desc: '开启或关闭steam个人状态推送'
title: '#steam开启推送',
desc: '开启steam个人状态推送'
},
{
icon: 67,
title: '#steam关闭推送',
desc: '关闭steam个人状态推送'
}
]
},
Expand Down Expand Up @@ -78,7 +88,7 @@ export const helpList = [
},
{
icon: 50,
title: '#群友在玩什么',
title: '#steam群友状态',
desc: '看看群聊绑定了steamId的成员状态'
},
{
Expand Down
4 changes: 1 addition & 3 deletions models/help/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const helpTheme = {
getThemeCfg () {
const resPath = `{{pluResPath}}help/theme/${lodash.random(1, 5)}.png`
return {
main: resPath,
bg: resPath,
style
}
Expand All @@ -18,8 +17,7 @@ const helpTheme = {
const theme = helpTheme.getThemeCfg()
const themeStyle = theme.style || {}
const ret = [`
body{background-image:url(${theme.bg});width:${width}px;}
.container{background-image:url(${theme.main});width:${width}px;}
.container{background-image:url(${theme.bg});width:${width}px;}
.help-table .td,.help-table .th{width:${100 / colCount}%}
`]
const defFnc = (...args) => {
Expand Down
Binary file added resources/user/check.webp
Binary file not shown.
Loading

0 comments on commit 65f5def

Please sign in to comment.