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

100 lines
3.4 KiB
JavaScript

import Level from '../../../schemas/Level.js'
import Player from '../../../schemas/Player.js'
import initLog from '../../utills/log.js'
import decodeMsgpack from './decodeMsgpack.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...")
for (const category in cache.maps) {
for (const i in cache.maps[category]) {
const map = cache.maps[category][i]
Level.findOneAndUpdate(
{ name: map[0] },
{ totalFinishes: map[2] }
).then(
() => log(`Processed ${category} map ${i + 1}/${cache.maps[category].length} -> «${map[0]}» with ${map[2]} finishes!`)
)
}
}
log("Processing points for players...")
let processedPlayerPoints = 0
for (const entry of cache.pointsRanks) {
Player.findOneAndUpdate(
{ name: entry[0] },
{ points: entry[1] }
).then(
() => {
++processedPlayerPoints
log(`Processed player ${processedPlayerPoints}/${cache.pointsRanks.length} -> «${entry[0]}» has ${entry[1]} points!`)
}
)
}
log("Processing rank points for players...")
let processedPlayerRankPoints = 0
for (const entry of cache.rankPoints) {
Player.findOneAndUpdate(
{ 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) {
Player.findOneAndUpdate(
{ 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) {
Player.findOneAndUpdate(
{ 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) {
Player.findOneAndUpdate(
{ name: entry[0] },
{ pointsThisWeek: entry[1] }
).then(
() => {
++processedPlayerPointsMonth
log(`Processed player ${processedPlayerPointsMonth}/${cache.pointsThisMonth.length} -> «${entry[0]}» got ${entry[1]} points this month!`)
}
)
}
}