ddstats-server/libs/database/sqlite2mongo/postTasks.js
2021-10-30 19:19:58 +02:00

125 lines
3.9 KiB
JavaScript

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