Skip to content

Commit

Permalink
perf: 记录最后体力刷新时间以免一直刷新
Browse files Browse the repository at this point in the history
  • Loading branch information
TomyJan committed Nov 15, 2024
1 parent 638f1a8 commit 649d8b6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ git -C ./plugins/Yunzai-Kuro-Plugin/ pull

- [x] 战双签到 指令的时间估计的修复 / 延时方法的修复

- [ ] 体力刷新逻辑优化
- [x] 体力刷新逻辑优化

- [ ] 增加 游戏签到 补签功能

Expand Down
13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import {
appsPath,
pluginVer,
pluginThemeColor,
_DataPath
} from './data/system/pluginConstants.js'
import {
initAutoTask,
checkUpdateTask,
gameEnergyPushTask,
gameEnergyPushTask
} from './model/autoTask.js'

await kuroLogger.info(pluginThemeColor('============(≧∇≦)ノ============'))
await kuroLogger.info(pluginThemeColor(`库洛插件 V${pluginVer} 开始载入~`))

await kuroLogger.info(pluginThemeColor(`-----------载入模块-----------`))
await kuroLogger.info(pluginThemeColor('-----------载入模块-----------'))

const files = fs.readdirSync(appsPath).filter((file) => file.endsWith('.js'))

Expand All @@ -38,13 +39,13 @@ for (let i in files) {
}
apps[name] = ret[i].value[Object.keys(ret[i].value)[0]]
}
await kuroLogger.info(pluginThemeColor(`载入模块完成!`))
await kuroLogger.info(pluginThemeColor('载入模块完成!'))
export { apps }

await kuroLogger.info(pluginThemeColor(`---------载入定时任务---------`))
await kuroLogger.info(pluginThemeColor('---------载入定时任务---------'))
await initAutoTask()

await kuroLogger.info(pluginThemeColor(`载入定时任务完成啦!`))
await kuroLogger.info(pluginThemeColor('载入定时任务完成啦!'))

await kuroLogger.info(pluginThemeColor('插件载入完成, 欢迎使用~'))
await kuroLogger.info(pluginThemeColor('=============================='))
Expand All @@ -53,5 +54,5 @@ await kuroLogger.info(pluginThemeColor('=============================='))
// 延迟5s再开始以防止第三方适配器没连接上
setTimeout(() => {
checkUpdateTask()
gameEnergyPushTask() // TODO: 保存最后完成刷新的时间, 启动判断如果超过一小时再刷新
gameEnergyPushTask(3600)
}, 5000)
44 changes: 43 additions & 1 deletion model/autoTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,32 @@ async function bbsDailyTask() {
return true
}

export async function gameEnergyPushTask() {
export async function gameEnergyPushTask (checkTimeInterval = 0) {
const taskProcessFile = _DataPath + '/system/taskProcess.json'
if (checkTimeInterval) {
const now = new Date().getTime() / 1000
let taskProcess = ''
try {
taskProcess = fs.readFileSync(taskProcessFile, 'utf8')
kuroLogger.debug('读取 taskProcess:', taskProcess)
} catch (err) {
kuroLogger.error('读取 taskProcess.json 时出现错误:', err.message)
taskProcess = '{}'
}
taskProcess = JSON.parse(taskProcess)
let lastGameEnergyPushTime = taskProcess?.lastGameEnergyPushTime || 0
if (now - lastGameEnergyPushTime < checkTimeInterval) {
kuroLogger.info(`游戏体力推送: 上次检查时间 ${new Date(
lastGameEnergyPushTime
)}, 距离上次将查体力不足 ${checkTimeInterval}s, 跳过本次检查`)
return false
} else {
kuroLogger.info(`游戏体力推送: 上次检查时间 ${new Date(
lastGameEnergyPushTime
)}, 距离上次将查体力超过 ${checkTimeInterval}s, 开始本次检查`)
return false
}
}
kuroLogger.info(`游戏体力推送: 开始刷新数据...`)

const gameSignUins = fs
Expand All @@ -168,6 +193,23 @@ export async function gameEnergyPushTask() {
}
}
kuroLogger.info(`游戏体力推送: 数据刷新完成`)
// 读入并更新任务进度文件
let taskProcess = ''
try {
taskProcess = fs.readFileSync(taskProcessFile, 'utf8')
kuroLogger.debug('读取 taskProcess:', taskProcess)
} catch (err) {
kuroLogger.error('读取 taskProcess.json 时出现错误:', err.message)
taskProcess = '{}'
}
taskProcess = JSON.parse(taskProcess)
taskProcess.lastGameEnergyPushTime = new Date().getTime() / 1000
try {
fs.writeFileSync(taskProcessFile, JSON.stringify(taskProcess))
kuroLogger.debug('写入 taskProcess:', taskProcess)
} catch (err) {
kuroLogger.error('写入 taskProcess.json 时出现错误:', err.message)
}
return true
}

Expand Down

0 comments on commit 649d8b6

Please sign in to comment.