只有周一才重置周榜

This commit is contained in:
zhl 2021-08-11 16:30:17 +08:00
parent 12c78ab264
commit fb82166f64
2 changed files with 13 additions and 6 deletions

View File

@ -93,8 +93,8 @@ class RankController extends BaseController {
@role('anon')
@router('post /api/svr/games/rank/init')
async update(req: any) {
let { gameId, channelId } = req.params
await new RankSchedule().initRankData({ gameId, channelId })
let { gameId, channelId, refresh } = req.params
await new RankSchedule().initRankData({ gameId, channelId, refresh })
return {}
}
}

View File

@ -4,6 +4,7 @@ import logger from '../logger/logger'
import { RankUser } from '../models/RankUser'
import { delRankList, rankKey, updateRank } from '../services/rank.svr'
import { fetchRankCfg, randomUsers } from '../services/jcfw.svr'
import { getThisWeekData } from '../utils/time.util'
// 每天晚上1点更新
const WORLD_RANK_TASK = '1 0 1 * * *'
@ -16,15 +17,18 @@ export default class RankSchedule {
* n个, n个机器人
* n个机器人更新至总榜
*/
async initRankData({ gameId, channelId }) {
async initRankData({ gameId, channelId, refresh }: { gameId: string; channelId: string; refresh?: boolean }) {
let oldList = await RankUser.find({ gameId, channelId, isBot: true, type: 1 })
const { startDay } = getThisWeekData()
const today = new Date().format('yyyy-M-d')
const needRest = today === startDay || refresh
if (oldList.length > 0) {
const removeCount = Math.random2(5, 10) | 0
const maxRemoveCount = needRest ? Math.random2(5, 10) | 0 : Math.random2(2, 6) | 0
let deleteCount = 0
let tmpList = []
for (let i = 0, l = oldList.length; i < l; i++) {
let record = oldList[i]
if (Math.random() > 0.5 && deleteCount <= removeCount) {
if (Math.random() > 0.5 && deleteCount < maxRemoveCount) {
record.type = 0
await record.save()
deleteCount++
@ -50,7 +54,10 @@ export default class RankSchedule {
}
let weekKey = rankKey(gameId, channelId, false)
await delRankList(weekKey)
if (needRest) {
await delRankList(weekKey)
}
let totalKey = rankKey(gameId, channelId, true)
const { min, max, valType } = await fetchRankCfg(gameId, channelId)
for (let record of oldList) {