From 0bafd5e4a6f91e31fad7e766d7dcd726f7890d99 Mon Sep 17 00:00:00 2001 From: BurnyLlama Date: Mon, 11 Oct 2021 19:37:37 +0200 Subject: [PATCH] Now imports points... --- libs/database/sqlite2mongo/postTasks.js | 115 +++++++++++++----------- 1 file changed, 62 insertions(+), 53 deletions(-) diff --git a/libs/database/sqlite2mongo/postTasks.js b/libs/database/sqlite2mongo/postTasks.js index 45ba008..18e58d3 100644 --- a/libs/database/sqlite2mongo/postTasks.js +++ b/libs/database/sqlite2mongo/postTasks.js @@ -1,8 +1,8 @@ -import Finish from '../../../schemas/Finish.js' 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") /** @@ -19,73 +19,82 @@ export default async function postTasks() { for (const category in cache.maps) { for (const i in cache.maps[category]) { const map = cache.maps[category][i] - const mapFromDB = await Level.findOne({ name: map[0] }) - if (!mapFromDB) continue - - mapFromDB.totalFinishes = map[2] - mapFromDB.save() - - log(`Processed ${category} map ${i + 1}/${cache.maps[category].length} -> «${map[0]}» with ${map[2]} finishes!`) + 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...") - for (const i in cache.pointsRanks) { - const entry = cache.pointsRanks[i] - const playerFromDB = await Player.findOne({ name: entry[0] }) - if (!playerFromDB) continue - - playerFromDB.points = entry[1] - playerFromDB.save() - - log(`Processed player ${i + 1}/${cache.pointsRanks.length} -> «${entry[0]}» has ${entry[1]} points!`) + 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...") - for (const i in cache.rankPoints) { - const entry = cache.rankPoints[i] - const playerFromDB = await Player.findOne({ name: entry[0] }) - if (!playerFromDB) continue - - playerFromDB.rankPoints = entry[1] - playerFromDB.save() - - log(`Processed player ${i + 1}/${cache.pointsRanks.length} -> «${entry[0]}» has ${entry[1]} rank points!`) + 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...") - for (const i in cache.teamRankPoints) { - const entry = cache.teamRankPoints[i] - const playerFromDB = await Player.findOne({ name: entry[0] }) - if (!playerFromDB) continue - - playerFromDB.teamPoints = entry[1] - playerFromDB.save() - - log(`Processed player ${i + 1}/${cache.pointsRanks.length} -> «${entry[0]}» has ${entry[1]} team points!`) + 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...") - for (const i of cache.pointsThisWeek) { - const entry = cache.pointsThisWeek[i] - const playerFromDB = await Player.findOne({ name: entry[0] }) - if (!playerFromDB) continue - - playerFromDB.pointsThisWeek = entry[1] - playerFromDB.save() - - log(`Processed player ${i + 1}/${cache.pointsRanks.length} -> «${entry[0]}» got ${entry[1]} points this 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...") - for (const i of cache.pointsThisMonth) { - const entry = cache.pointsThisMonth[i] - const playerFromDB = await Player.findOne({ name: entry[0] }) - if (!playerFromDB) continue - - playerFromDB.pointsThisMonth = entry[1] - playerFromDB.save() - - log(`Processed player ${i + 1}/${cache.pointsRanks.length} -> «${entry[0]}» got ${entry[1]} points this 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!`) + } + ) } } \ No newline at end of file