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

100 lines
3.4 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 '../../utills/log.js'
import decodeMsgpack from './decodeMsgpack.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...")
for (const category in cache.maps) {
for (const i in cache.maps[category]) {
const map = cache.maps[category][i]
2021-10-11 19:37:37 +02:00
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!`)
)
}
}
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) {
Player.findOneAndUpdate(
{ name: entry[0] },
{ points: entry[1] }
).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) {
Player.findOneAndUpdate(
{ name: entry[0] },
{ rankPoints: entry[1] }
).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) {
Player.findOneAndUpdate(
{ name: entry[0] },
{ teamPoints: entry[1] }
).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) {
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!`)
}
)
}
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) {
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!`)
}
)
}
2021-10-06 19:52:40 +02:00
}