From 11c430276cac8073a82fd07a2335e5c8466bcb83 Mon Sep 17 00:00:00 2001 From: BurnyLlama Date: Sat, 9 Oct 2021 20:12:06 +0200 Subject: [PATCH] Now adds total amount of finishes per map... --- libs/database/sqlite2mongo.js | 6 ++--- libs/database/sqlite2mongo/postTasks.js | 29 +++++++++++++------------ schemas/Level.js | 3 ++- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/libs/database/sqlite2mongo.js b/libs/database/sqlite2mongo.js index 01d6208..8ed9da9 100644 --- a/libs/database/sqlite2mongo.js +++ b/libs/database/sqlite2mongo.js @@ -12,9 +12,9 @@ const log = initLog("sqlite2mongo") async function sqlite2mongo() { log("Checking for additions to 'ddnet.sqlite'...") - await checkForMaps() - await checkForPlayers() - await checkForFinishes() + // await checkForMaps() + // await checkForPlayers() + // await checkForFinishes() await postTasks() } diff --git a/libs/database/sqlite2mongo/postTasks.js b/libs/database/sqlite2mongo/postTasks.js index 7375fb2..840bbc2 100644 --- a/libs/database/sqlite2mongo/postTasks.js +++ b/libs/database/sqlite2mongo/postTasks.js @@ -2,8 +2,7 @@ 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 { sqlite } from '../init.js' - +import decodeMsgpack from './decodeMsgpack.js' const log = initLog("sqlite2mongo") /** @@ -12,19 +11,21 @@ const log = initLog("sqlite2mongo") */ export default async function postTasks() { log("Post tasks...") - - log("Calculating player points...") - const playerAmount = await Player.find({}).count() - const playerAgg = Player.aggregate([]) - let processedPlayers = 0 - for await (const player of playerAgg) { - const points = (await sqlite.get(`SELECT sum(Points) FROM maps JOIN (SELECT DISTINCT Map FROM race WHERE Name = "${player.name}") AS r ON maps.Map = r.Map`))['sum(Points)'] - player.points = points + log("Loading cached info from DDNet...") + const cache = decodeMsgpack() - Player.updateOne({ name: player.name }, { rankPoints: points }, () => log(`Task ${processedPlayers}/${playerAmount} >> Player «${player.name}» has ${points} points!`)) - ++processedPlayers + 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] + const mapFromDB = await Level.findOne({ name: map[0] }) + if (!mapFromDB) continue + + mapFromDB.totalFinishes = map[2] + mapFromDB.save() + + log(`Processed ${category} map ${i}/${cache.maps[category].length} -> «${map[0]}» with ${map[2]} finishes!`) + } } - - log("Done calculating points!") } \ No newline at end of file diff --git a/schemas/Level.js b/schemas/Level.js index 6f8442e..ba7765c 100644 --- a/schemas/Level.js +++ b/schemas/Level.js @@ -6,7 +6,8 @@ const Level = new mongoose.Schema({ release: Date, category: String, rating: Number, - awardPoints: Number + awardPoints: Number, + totalFinishes: Number }) /**