Skip to content

Commit

Permalink
fix: 历史记录统计有误
Browse files Browse the repository at this point in the history
  • Loading branch information
XasYer committed Dec 18, 2024
1 parent f4aacf5 commit 8b6c967
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
7 changes: 4 additions & 3 deletions models/bind/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,19 @@ export async function getBindSteamIdsImg (bid, uid, gid, userBindAll = []) {
const enablePushSteamIdList = enablePush ? await db.PushTableGetAllSteamIdBySteamIdAndGroupId(uid, gid, true) : []
const allSteamIdInfo = {}
try {
const res = await api.ISteamUser.GetPlayerSummaries(userBindAll.map(i => i.steamId)).catch(() => [])
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 i = allSteamIdInfo[item.steamId] || {}
const avatar = Config.other.steamAvatar ? i.avatarfull : await utils.getUserAvatar(bid, uid, gid)
const info = {
steamId: item.steamId,
isBind: item.isBind,
name: allSteamIdInfo[item.steamId].personaname || await utils.getUserName(bid, uid, gid),
name: i.personaname || await utils.getUserName(bid, uid, gid),
avatar: avatar || await utils.getUserAvatar(bid, uid, gid),
index
}
Expand Down
11 changes: 11 additions & 0 deletions models/db/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ const HistoryTable = sequelize.define('history', {

await HistoryTable.sync()

HistoryTable.findAll().then(res => {
if (res.length) {
// 在这之前统计的记录有误差 全部删除重新统计
const time = 1734495420
const data = res[0].dataValues
if (data.start < time && data.end < time) {
HistoryTable.truncate().catch(() => {})
}
}
}).catch(() => {})

/**
* 添加一条记录
* @param {string} userId
Expand Down
25 changes: 14 additions & 11 deletions models/task/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ export function startTimer () {
if (lastPlay) {
lastPlay = JSON.parse(lastPlay)
} else {
lastPlay = { name: '', time: 0, appid: 0, state: 0 }
lastPlay = { name: '', appid: 0, state: 0, playTime: 0, onlineTime: 0 }
}
const state = {
name: player.gameextrainfo,
appid: player.gameid,
time: lastPlay.time,
state: player.personastate
state: player.personastate,
playTime: lastPlay.time || lastPlay.playTime,
onlineTime: lastPlay.time || lastPlay.onlineTime
}
// 如果这一次和上一次的状态不一样
if (lastPlay.appid != player.gameid || lastPlay.state != player.personastate) {
Expand Down Expand Up @@ -68,44 +69,46 @@ export function startTimer () {
state: []
}
}
const time = now - lastPlay.time
if (Config.push.enable && player.gameid && player.gameid != lastPlay.appid) {
state.time = now
const time = now - lastPlay.playTime
state.playTime = now
userList[i.groupId][i.botId].start.push({
name: player.gameextrainfo,
appid: `${nickname}(${player.personaname})`,
desc: lastPlay.time ? `距离上次 ${utils.formatDuration(time)}` : '',
desc: lastPlay.playTime ? `距离上次 ${utils.formatDuration(time)}` : '',
header_image: iconUrl
})
db.StatsTableUpdate(i.userId, i.groupId, i.botId, i.steamId, player.gameid, player.gameextrainfo, 'playTotal', 1).catch(e => logger.error('更新统计数据失败', e))
db.HistoryAdd(i.userId, i.groupId, i.botId, i.steamId, now, null, player.gameid, player.gameextrainfo).catch(e => logger.error('添加历史记录失败', e))
}
if (Config.push.enable && lastPlay.name && lastPlay.name != player.gameextrainfo) {
state.time = now
const time = now - lastPlay.playTime
state.playTime = now
userList[i.groupId][i.botId].end.push({
name: lastPlay.name,
appid: `${nickname}(${player.personaname})`,
desc: `时长: ${utils.formatDuration(time)}`,
header_image: utils.getHeaderImgUrlByAppid(lastPlay.appid)
})
db.StatsTableUpdate(i.userId, i.groupId, i.botId, i.steamId, lastPlay.appid, lastPlay.name, 'playTime', time).catch(e => logger.error('更新统计数据失败', e))
db.HistoryAdd(i.userId, i.groupId, i.botId, i.steamId, lastPlay.time, now, lastPlay.appid, lastPlay.name).catch(e => logger.error('添加历史记录失败', e))
db.HistoryAdd(i.userId, i.groupId, i.botId, i.steamId, lastPlay.playTime, now, lastPlay.appid, lastPlay.name).catch(e => logger.error('添加历史记录失败', e))
}
// 在线状态改变
if (Config.push.stateChange && player.personastate != lastPlay.state) {
const time = now - lastPlay.onlineTime
if ([0, 1].includes(player.personastate)) {
state.time = now
state.onlineTime = now
userList[i.groupId][i.botId].state.push({
name: `${nickname}(${player.personaname})`,
appid: lastPlay.time ? `距离上次 ${utils.formatDuration(time)}` : '',
appid: lastPlay.onlineTime ? `距离上次 ${utils.formatDuration(time)}` : '',
desc: `已${utils.getPersonaState(player.personastate)}`,
header_image: await utils.getUserAvatar(i.botId, i.userId, i.groupId) || (Config.other.steamAvatar ? i.avatarfull : ''),
header_image_class: 'square',
desc_style: `style="background-color: #${getColor(player.personastate)};color: white;width: fit-content;border-radius: 5px; padding: 0 5px;"`
})
if (player.personastate === 0) {
db.StatsTableUpdate(i.userId, i.groupId, i.botId, i.steamId, player.gameid, player.gameextrainfo, 'onlineTime', time).catch(e => logger.error('更新统计数据失败', e))
db.HistoryAdd(i.userId, i.groupId, i.botId, i.steamId, lastPlay.time, now).catch(e => logger.error('添加历史记录失败', e))
db.HistoryAdd(i.userId, i.groupId, i.botId, i.steamId, lastPlay.onlineTime, now).catch(e => logger.error('添加历史记录失败', e))
} else {
db.StatsTableUpdate(i.userId, i.groupId, i.botId, i.steamId, player.gameid, player.gameextrainfo, 'onlineTotal', 1).catch(e => logger.error('更新统计数据失败', e))
db.HistoryAdd(i.userId, i.groupId, i.botId, i.steamId, now).catch(e => logger.error('添加历史记录失败', e))
Expand Down

0 comments on commit 8b6c967

Please sign in to comment.