ddstats-server/libs/database/sqlite2mongo/postTasks.js

124 lines
3.9 KiB
JavaScript
Raw Normal View History

2021-10-06 19:52:40 +02:00
import Level from '../../../schemas/Level.js'
import Player from '../../../schemas/Player.js'
import initLog from '../../utils/log.js'
import decodeMsgpack from './decodeMsgpack.js'
2021-10-16 19:11:16 +02:00
import { spread } from '../../utils/multithread.js'
2021-10-11 19:37:37 +02:00
2021-10-06 19:52:40 +02:00
const log = initLog("sqlite2mongo")
2021-10-06 20:36:20 +02:00
/**
* This handles some post-tasks that run after the initial checks.
* @module libs/database/sqlite2mongo/postTasks
*/
2021-10-06 19:52:40 +02:00
export default async function postTasks() {
log("Post tasks...")
log("Loading cached info from DDNet...")
const cache = decodeMsgpack()
2021-10-09 23:52:41 +02:00
log("Adding total amounts of finishes to maps...")
2021-10-16 19:11:16 +02:00
const totalMaps = await Level.find({}).count()
let processedMaps = 0
for (const category in cache.maps) {
2021-10-16 19:11:16 +02:00
for (const map of cache.maps[category]) {
2021-10-11 19:37:37 +02:00
Level.findOneAndUpdate(
{ name: map[0] },
{ totalFinishes: map[2] }
).then(
2021-10-16 19:11:16 +02:00
() => {
++processedMaps
log(`Processed map ${processedMaps}/${totalMaps} -> «${map[0]}» [${category}] with ${map[2]} finishes!`)
}
2021-10-11 19:37:37 +02:00
)
}
}
2021-10-16 19:11:16 +02:00
log("Reseting weekly and monthly points...")
await Player.updateMany({}, { pointsThisWeek: 0, pointsThisMonth: 0 })
log("Done!")
2021-10-09 23:52:41 +02:00
log("Processing points for players...")
2021-10-11 19:37:37 +02:00
let processedPlayerPoints = 0
for (const entry of cache.pointsRanks) {
2021-10-16 19:11:16 +02:00
spread(
'./playerPoints.js',
{
name: entry[0],
points: entry[1]
}
2021-10-11 19:37:37 +02:00
).then(
() => {
++processedPlayerPoints
log(`Processed player ${processedPlayerPoints}/${cache.pointsRanks.length} -> «${entry[0]}» has ${entry[1]} points!`)
}
)
}
2021-10-09 23:52:41 +02:00
log("Processing rank points for players...")
2021-10-11 19:37:37 +02:00
let processedPlayerRankPoints = 0
for (const entry of cache.rankPoints) {
2021-10-16 19:11:16 +02:00
spread(
'./playerRankPoints.js',
{
name: entry[0],
rankPoints: [1]
}
2021-10-11 19:37:37 +02:00
).then(
() => {
++processedPlayerRankPoints
log(`Processed player ${processedPlayerRankPoints}/${cache.rankPoints.length} -> «${entry[0]}» has ${entry[1]} rank points!`)
}
)
}
2021-10-09 23:52:41 +02:00
log("Processing team points for players...")
2021-10-11 19:37:37 +02:00
let processedTeamPoints = 0
for (const entry in cache.teamRankPoints) {
2021-10-16 19:11:16 +02:00
spread(
'./playerTeamPoints.js',
{
name: entry[0],
teamPoints: entry[1]
}
2021-10-11 19:37:37 +02:00
).then(
() => {
++processedTeamPoints
log(`Processed player ${processedTeamPoints}/${cache.teamRankPoints.length} -> «${entry[0]}» has ${entry[1]} team points!`)
}
)
}
2021-10-09 23:52:41 +02:00
log("Processing players' points for the last week...")
2021-10-11 19:37:37 +02:00
let processedPlayerPointsWeek = 0
for (const entry of cache.pointsThisWeek) {
2021-10-16 19:11:16 +02:00
spread(
'./playerWeekPoints.js',
{
name: entry[0],
pointsThisWeek: entry[1]
}
2021-10-11 19:37:37 +02:00
).then(
() => {
++processedPlayerPointsWeek
log(`Processed player ${processedPlayerPointsWeek}/${cache.pointsThisWeek.length} -> «${entry[0]}» got ${entry[1]} points this week!`)
}
)
}
2021-10-09 23:52:41 +02:00
log("Processing players' points for the last month...")
2021-10-11 19:37:37 +02:00
let processedPlayerPointsMonth = 0
for (const entry of cache.pointsThisMonth) {
2021-10-16 19:11:16 +02:00
spread(
'./playerMonthPoints.js',
{
name: entry[0],
pointsThisMonth: entry[1]
}
2021-10-11 19:37:37 +02:00
).then(
() => {
++processedPlayerPointsMonth
log(`Processed player ${processedPlayerPointsMonth}/${cache.pointsThisMonth.length} -> «${entry[0]}» got ${entry[1]} points this month!`)
}
)
}
2021-10-06 19:52:40 +02:00
}